/* functionality for the what's on slider on the home page */
jQuery(document).ready(function() {

	/* image fade from black and white to color */
		
		var fadeSpeed = 1000;
		
		jQuery('img.bw').hover(			
			function() {
				jQuery(this).stop().animate({"opacity": "0"}, fadeSpeed);
			},
			function() {
				jQuery(this).stop().animate({"opacity": "1"}, fadeSpeed);
			}
		);
		
		jQuery('#info-slider ul li').hover(
			function() {
				activeImage = $(this).attr('id').replace('info-slide-', '');
				jQuery('#image-slide-' + activeImage + ' img.bw').stop().animate({"opacity": "0"}, fadeSpeed);
			},
			function() {
				activeImage = $(this).attr('id').replace('info-slide-', '');
				jQuery('#image-slide-' + activeImage + ' img.bw').stop().animate({"opacity": "1"}, fadeSpeed);
			}
		);
		
	/* next/previous buttons */
	
		var imageSlideSpeed = 300;
		var numberOfSlides = jQuery('#image-slider-mask ul li').length;
		var imageWidth = 528;
		var imageWidthTotal = parseInt(imageWidth) * (parseInt(numberOfSlides) - 1);
		var infoWidth = 224;
		var infoWidthTotal = parseInt(infoWidth) * (parseInt(numberOfSlides) - 1);
		var currentSlide = 1;
	
		jQuery('#image-slider-next').click(function () {
			// go to next slide
			if (currentSlide != numberOfSlides) {
				currentSlide = currentSlide + 1;
				// slide image
				jQuery('#image-slider-mask').animate({
					left: '-=' + imageWidth
				}, imageSlideSpeed, function() {
				
				});
				// slide info
				jQuery('#info-slider ul').animate({
					left: '-=' + infoWidth
				}, imageSlideSpeed, function() {
				
				});
			}
			// go back to the very start
			else {
				currentSlide = 1;
				// slide image
				jQuery('#image-slider-mask').animate({
					left: '0'
				}, imageSlideSpeed, function() {
				
				});
				// slide info
				jQuery('#info-slider ul').animate({
					left: '0'
				}, imageSlideSpeed, function() {
				
				});				
			}
			return false;
		});
		
		jQuery('#image-slider-prev').click(function () {
			// go to previous slide
			if (currentSlide != 1) {
				currentSlide = currentSlide - 1;
				// slide image
				jQuery('#image-slider-mask').animate({
					left: '+=' + imageWidth
				}, imageSlideSpeed, function() {
				
				});
				// slide info
				jQuery('#info-slider ul').animate({
					left: '+=' + infoWidth
				}, imageSlideSpeed, function() {
				
				});
			}
			// go to the very end
			else {
				currentSlide = numberOfSlides;
				// slide image
				jQuery('#image-slider-mask').animate({
					left: '-' + imageWidthTotal
				}, imageSlideSpeed, function() {
				
				});
				// slide info
				jQuery('#info-slider ul').animate({
					left: '-' + infoWidthTotal
				}, imageSlideSpeed, function() {
				
				});					
			}
			return false;
		});
		
	/* auto play slideshow */
	
		var timeBetweenSlides = 7000;
		//var slideshowTimer = setInterval(showNextSlide,timeBetweenSlides);
		//initiateTimer();
		
		function initiateTimer() {
			slideshowTimer = setInterval(showNextSlide,timeBetweenSlides);
		}

		function clearTimer() {
			clearInterval(slideshowTimer);
		}
		
		function showNextSlide() {
			jQuery('#image-slider-next').click();
		}
		
		jQuery('#image-slider-mask ul li a, #image-slider-prev, #image-slider-next, #info-slider ul li').hover(
			function () {
				clearTimer();
			},
			function () {
				initiateTimer();
			}
		);

		/* stop the animation when we leave the tab/page (as this causes weird queuing issue in Chrome when tab is left in the background */
		jQuery('body').mouseenter(function() {
			initiateTimer();
		});
		jQuery('body').mouseleave(function() {
			clearTimer();
		});		
});
