var gallery;

jQuery(document).ready(function($) {
    _writeContact();
	_initGallery();
});


function _writeContact() {
	//add contact link via JS to thwart bots
	var pre = "&nbsp; &bull; &nbsp;";
	var d = "thew@ber";
	var d2 = "gsmavis";
	var d3 = "uals";
	var lin = "ref"
	var lin2 = "ma";
	var ht = pre + '<a h'+lin+'="'+lin2+'il'+'to'+':mat'+d+d2+d3+'.'+'co'+'m"'+'>'+'mat'+d+d2+d3+'.'+'co'+'m</a>';
	$('p.contact').append(ht);
}


function _initGallery() {
	
	var onMouseOutOpacity = .75;
	$('ul.thumbs li').opacityrollover({
		mouseOutOpacity:   onMouseOutOpacity,
		mouseOverOpacity:  1,
		fadeSpeed:         'fast',
		exemptionSelector: undefined//'.selected'
	});
	
	var divCaption = '';
	
	
	gallery = $('div#sidebar').galleriffic({
        delay:                     10000, // in milliseconds
        numThumbs:                 27, // The number of thumbnails to show page
        preloadAhead:              1, // Set to -1 to preload all images
        imageContainerSel:         '#main_image', // The CSS selector for the element within which the main slideshow image should be rendered
        controlsContainerSel:      '', // The CSS selector for the element within which the slideshow controls should be rendered
        loadingContainerSel:       '', // The CSS selector for the element within which should be shown when an image is loading
        renderSSControls:          true, // Specifies whether the slideshow's Play and Pause links should be rendered
        renderNavControls:         true, // Specifies whether the slideshow's Next and Previous links should be rendered
        playLinkText:              'Play',
        pauseLinkText:             'Pause',
        prevLinkText:              '&lsaquo; Previous',
        nextLinkText:              'Next &rsaquo;',
        nextPageLinkText:          'Next &rsaquo;',
        prevPageLinkText:          '&lsaquo; Prev',
        enableHistory:             false, // Specifies whether the url's hash and the browser's history cache should update when the current slideshow image changes
        enableKeyboardNavigation:  false, // Specifies whether keyboard navigation is enabled
        autoStart:                 true, // Specifies whether the slideshow should be playing or paused when the page first loads
        syncTransitions:           false, // Specifies whether the out and in transitions occur simultaneously or distinctly
        defaultTransitionDuration: 1000, // If using the default transitions, specifies the duration of the transitions
        onSlideChange:            	function(prevIndex, nextIndex) {
										var children = this.find('ul.thumbs').children();
										children.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
										children.eq(nextIndex).fadeTo('fast', 1);
										
										var div = children.eq(nextIndex).find('div');
										if (div[0]) {
											divCaption = div.html();
										} else {
											divCaption = undefined;
										}
										
									},
        onTransitionOut:           undefined, // accepts a delegate like such: function(slide, caption, isSync, callback) { ... }
        onTransitionIn:            function(slide, caption, isSync) {
					slide.fadeTo(this.getDefaultTransitionDuration(isSync)*.75, 1.0);
					if (divCaption) slide.append('<div>'+divCaption+'</div>');
					//alert(slide.append('<h1>hey</h1>'));
  		  },//undefined, // accepts a delegate like such: function(slide, caption, isSync) { ... }
        onPageTransitionOut:       undefined, // accepts a delegate like such: function(callback) { ... }
        onPageTransitionIn:        undefined, // accepts a delegate like such: function() { ... }
        onImageAdded:              undefined, // accepts a delegate like such: function(imageData, $li) { ... }
        onImageRemoved:            undefined  // accepts a delegate like such: function(imageData, $li) { ... }
    });

	/*function(prevIndex, nextIndex) {
									var children = this.find('ul.thumbs').children();
									if (nextIndex%3 == 0) {
										children.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
										children.eq(prevIndex-1).fadeTo('fast', onMouseOutOpacity).end()
										children.eq(prevIndex-2).fadeTo('fast', onMouseOutOpacity).end()
										children.eq(nextIndex).fadeTo('fast', 1.0);
										children.eq(nextIndex+1).fadeTo('fast', 1.0);
										children.eq(nextIndex+2).fadeTo('fast', 1.0);
									}											
								},*/

	// setup hover actions on next/prev buttons
	var _pauseFunc = function() { gallery.pause() };
	var _playFunc = function() { gallery.play() };
	$('a#next').hover(_pauseFunc, _playFunc);
	$('a#prev').hover(_pauseFunc, _playFunc);
	$('ul.thumbs li').hover(_pauseFunc, _playFunc);
}

