$(document).ready(function(){

	animationDuration = 500;


	/* Init slideshow
	-------------------------------------------------------------- */

	var slideshow = $('#slideshow');
	var slideshowControls = $('#slideshow-nav');
	var slideshowControlLinks = $('a', slideshowControls);
	var thumbnails = $('#thumbnails');
	var thumbnailsInterval;
	
	slideshow.cycle({
		autostop: 1,
		nowrap: 1,
		timeout: 0,
		next: $('a.next', slideshowControls),
		prev: $('a.previous', slideshowControls),
		prevNextClick: function(isNext, zeroBasedSlideIndex, slideElement) {
			$('.thumbnail', thumbnails).not(':eq('+zeroBasedSlideIndex+')').fadeTo(animationDuration, 0.2);
        	$('.thumbnail:eq('+zeroBasedSlideIndex+')', thumbnails).fadeTo(animationDuration, 1);
		},
		speed: animationDuration,
		pager:  '#thumbnails',
		pagerAnchorBuilder: function(idx, slide) {
            return '#thumbnails li:eq(' + (idx) + ') a';
        },
        pagerClick: function(zeroBasedSlideIndex, slideElement) {
        	$('.thumbnail', thumbnails).not(':eq('+zeroBasedSlideIndex+')').fadeTo(animationDuration, 0.2);
        	$('.thumbnail:eq('+zeroBasedSlideIndex+')', thumbnails).fadeTo(animationDuration, 1);
        },
        after: function(currSlideElement, nextSlideElement, options, forwardFlag) {
            
            var thumbnailCtr = thumbnails.parent();
            
        	thumbnails.scrollTo(
        		$('li:eq(1)')
        	);
            
            var len = thumbnails.find('li').length;
            
            thumbnails.find('li').each(function (i) {
            
                if ($(this).is('.activeSlide')) {

                    var w = $(this).outerWidth();
                    
                    var diff = (thumbnailCtr.scrollLeft() / w) - i;
                    
                    var go = null;
                    if (diff <= -len + 5) {
                        go = len - 6;
                    } else if (diff <= -6) {
                        go = i;
                    } else if (diff > len - 5) {
                        go = 0;
                    } else if (diff > 0) {
                        go = i - 5;
                    }

                    if (go !== null) {
                        thumbnailCtr.animate({
                            scrollLeft: go * w
                        }, animationDuration);
                    }
                }
                
            });
        	
        }

	});


	/* Fade slideshow nav on hover
	-------------------------------------------------------------- */
	
	slideshowControlLinks.hover(
		function(){ $(this).stop().fadeTo(animationDuration, 0.2); },
		function(){ $(this).stop().fadeTo(animationDuration, 1); }
	);
	
	
	/* Init hover scroll
	-------------------------------------------------------------- */
	
	thumbnails.hoverscroll({
		vertical: false,	// Display the list vertically or horizontally
		width:    822,		// Width of the list container
		height:   74,		// Height of the list container
		arrows:   false,		// Display direction indicator arrows or not
		arrowsOpacity: 0.7,	// Max possible opacity of the arrows
		debug: false		// Debug output in the firebug console
	});
	
	
	/* Thumbnails fade
	-------------------------------------------------------------- */
	
	$('.thumbnail', thumbnails).hover(
		function(){ 
			if (!$(this).hasClass('activeSlide')) {
				$(this).stop().fadeTo(animationDuration, 1); 
			}
		},
		function(){ 
			if (!$(this).hasClass('activeSlide')) {
				$(this).stop().fadeTo(animationDuration, 0.2); 
			}
		}
	);
	
});
