/*
Page image slideshow, with click-to-select controls.
Uses the Revealing Module Pattern.
*/
try {
	var test = COMO.ns;
}
catch (e) {
	COMO = {};
}

COMO.PageImages = (function($) {
	var ns = 'COMO.PageImages';
	var description = 'Page image slideshow and click-to-select-image controls.';
	var slideshowEngine;
	var captionSlideshowEngine;

	var gallerySlideShowEngine;
	var galleryCaptionSlideshowEngine;

	function createSlideshow() {
		// create slideshow and set up image array
		slideshowEngine = new imageslideshowEngine();
		slideshowEngine.images = $('#pageimages img').get();

		// set up control click event handlers
		$('#pageimages .controls li a').each(function(i) {
			$(this).click(function(event) {
				selectImage(i);
				event.preventDefault();
			});
		});

		// start the engine
		slideshowEngine.init(5000, 500, imageSwitched);
	}

	//gallery methods
	function createGallerySlideshow() {
		$("#content #galleryimages img:first-child").show();
		$("#content #galleryimages h2").animate({ bottom: '-33px', opacity: 0 }, 500);
		timeoutID = window.setTimeout(startSlideShow, 5000);
		function startSlideShow() {
			// create slideshow and set up image array
			gallerySlideShowEngine = new imageslideshowEngine();
			gallerySlideShowEngine.images = $('#galleryimages img').get();

			// set up control click event handlers
			$('#galleryimages .controls li a').each(function(i) {
				$(this).click(function(event) {
					gallerySelectImage(i);
					event.preventDefault();
				});
			});

			// start the engine
			gallerySlideShowEngine.init(5000, 500, imageSwitched);

			//engine for image captions
			galleryCaptionSlideshowEngine = new imageslideshowEngine();
			galleryCaptionSlideshowEngine.images = $('#galleryimages h2').get();
			galleryCaptionSlideshowEngine.init(5000, 500);

			//control buttons
			$("#gallery_previous").click(function() {
				galleryNavigate("previous");
			});

			$("#gallery_pause").click(function() {
				gallerySlideShowEngine.pauseEngine();
				galleryCaptionSlideshowEngine.pauseEngine();
			});

			$("#gallery_next").click(function() {
				galleryNavigate("next");
			});

		}
	}

	function galleryNavigate(direction) {
		var currentIndex = 0;
		var topindex = 0;
		//stop the engine so the image order does not change.
		gallerySlideShowEngine.pauseEngine();
		galleryCaptionSlideshowEngine.pauseEngine();

		//find current index
		topindex = $('#galleryimages img').size();
		currentIndex = gallerySlideShowEngine.currentIndex;
		if (direction == "previous") {

			//go back one
			if (currentIndex > 0) {
				gallerySlideShowEngine.jumpToPair(currentIndex - 1, true);
				imageSwitched({
					to: currentIndex - 1
				});
				galleryCaptionSlideshowEngine.jumpToPair(currentIndex - 1, true);
				imageSwitched({
					to: currentIndex - 1
				});
			}
			else {
				//otherwise jump to topindex which sould be the last img.
				gallerySlideShowEngine.jumpToPair(topindex, true);
				imageSwitched({
					to: topindex
				});
				galleryCaptionSlideshowEngine.jumpToPair(topindex, true);
				imageSwitched({
					to: topindex
				});
			}
		}
		else {
			//go forward one
			if (currentIndex != topindex) {
				gallerySlideShowEngine.jumpToPair(currentIndex + 1, true);
				imageSwitched({
					to: currentIndex + 1
				});
				galleryCaptionSlideshowEngine.jumpToPair(currentIndex + 1, true);
				imageSwitched({
					to: currentIndex + 1
				});
			}
			else {
				//otherwise you are at last img so jump to beginning again.
				gallerySlideShowEngine.jumpToPair(0, true);
				imageSwitched({
					to: 0
				});

				galleryCaptionSlideshowEngine.jumpToPair(0, true);
				imageSwitched({
					to: 0
				});
			}
		}
		//start up again
		gallerySlideShowEngine.unpauseEngine();
		galleryCaptionSlideshowEngine.unpauseEngine();
	}

	function showGallery() {
		var cancelhideGallery = "";
		$("#galleryimages .imagehover").hover(
        function() {
        	if (cancelhideGallery != "") {
        		window.clearTimeout(cancelhideGallery);
        	}
        	cancelhideGallery = window.setTimeout(function galleryVisible() {
        		$("#galleryimages .imageoverlay").show("slide", { direction: "down" }, 1000);
        		$("#gallerycontrols").show("slide", { direction: "down" }, 1000);
        		// $("#galleryimages .caption-container").show("slide", { direction: "down" }, 1000);

        		$("#content #galleryimages h2").animate({ bottom: '33px', opacity: 1 }, 1000);
        	}, 1000);
        	$(".imagehover").animate({ opacity: 0 }, 3000).hide();
        }, function() {
        });


		$("#gallerycontrols").hover(function() {
		}, function() {
			if (cancelhideGallery != "") {
				window.clearTimeout(cancelhideGallery);
			}
			cancelhideGallery = window.setTimeout(function galleryHide() {
				$("#content #galleryimages h2").animate({ bottom: '-33px', opacity: 0 }, 500);

				$("#galleryimages .imageoverlay").hide("slide", { direction: "down" }, 1000);
				$("#gallerycontrols").hide("slide", { direction: "down" }, 1000);
			}, 1000);
			$(".imagehover").show().animate({ opacity: 0.1 }, 1000);

		});
	}
	//gallery methods
	function gallerySelectImage(i) {
		gallerySlideShowEngine.jumpToPair(i, true);
		imageSwitched({
			to: i
		});
	}

	function selectImage(i) {
		slideshowEngine.jumpToPair(i, true);
		imageSwitched({
			to: i
		});
	}

	function imageSwitched(indexes) {
		// deactivate all controls items
		$('#pageimages .controls li.active').removeClass('active');
		// activate the indexed control item
		$('#pageimages .controls li').eq(indexes.to).addClass('active');
		// change caption
		$('#pageimages .caption').text($('#pageimages img').eq(indexes.to).attr('alt'));
		$('#xgalleryimages h2.active-caption').removeClass("active-caption");
		$('#xgalleryimages h2').eq(indexes.to).addClass("active-caption");
	}

	function pause() {
		if (slideshowEngine) {
			slideshowEngine.pauseEngine();
		}
	}
	function unpause() {
		if (slideshowEngine) {
			slideshowEngine.unpauseEngine();
		}
	}

	return {
		ns: ns,
		description: description,
		createSlideshow: createSlideshow,
		selectImage: selectImage,
		imageSwitched: imageSwitched,
		pause: pause,
		unpause: unpause,
		createGallerySlideshow: createGallerySlideshow,
		showGallery: showGallery
	};

} (jQuery));


jQuery(document).ready(COMO.PageImages.createSlideshow);
jQuery(document).ready(COMO.PageImages.createGallerySlideshow);
jQuery(document).ready(COMO.PageImages.showGallery);
