/*
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;

	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);


		//function delayedAlert()
		//{
		timeoutID = window.setTimeout(startSlideShow, 5000);
		//}

		function startSlideShow()
		{
			// create slideshow and set up image array
			slideshowEngine = new imageslideshowEngine();
			slideshowEngine.images = $('#galleryimages img').get();




			// create slideshow and set up image array
			// slideshowEngine = new imageslideshowEngine();
			// slideshowEngine.images = $('#galleryimages img').get();

			// set up control click event handlers
			$('#galleryimages .controls li a').each(function(i)
			{
				$(this).click(function(event)
				{
					selectImage(i);
					event.preventDefault();
				});
			});

			// start the engine
			slideshowEngine.init(5000, 500, imageSwitched);


			//engine for image captions
			captionSlideshowEngine = new imageslideshowEngine();
			captionSlideshowEngine.images = $('#galleryimages h2').get();
			captionSlideshowEngine.init(5000, 500);

			//control buttons
			$("#gallery_previous").click(function()
			{

				galleryNavigate("previous");
			});

			$("#gallery_pause").click(function()
			{
				slideshowEngine.pauseEngine();
				captionSlideshowEngine.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.
		slideshowEngine.pauseEngine();
		captionSlideshowEngine.pauseEngine();

		//find current index
		topindex = $('#galleryimages img').size();

		currentIndex = slideshowEngine.currentIndex;

		if (direction == "previous")
		{

			//go back one
			if (currentIndex > 0)
			{
				slideshowEngine.jumpToPair(currentIndex - 1, true);
				imageSwitched({
					to: currentIndex - 1
				});

				captionSlideshowEngine.jumpToPair(currentIndex - 1, true);
				imageSwitched({
					to: currentIndex - 1
				});
			}
			else
			{
				//otherwise jump to topindex which sould be the last img.
				slideshowEngine.jumpToPair(topindex, true);
				imageSwitched({
					to: topindex
				});

				captionSlideshowEngine.jumpToPair(topindex, true);
				imageSwitched({
					to: topindex
				});
			}
		}
		else
		{
			//go forward one
			if (currentIndex != topindex)
			{
				slideshowEngine.jumpToPair(currentIndex + 1, true);
				imageSwitched({
					to: currentIndex + 1
				});

				captionSlideshowEngine.jumpToPair(currentIndex + 1, true);
				imageSwitched({
					to: currentIndex + 1
				});
			}
			else
			{
				//otherwise you are at last img so jump to beginning again.
				slideshowEngine.jumpToPair(0, true);
				imageSwitched({
					to: 0
				});

				captionSlideshowEngine.jumpToPair(0, true);
				imageSwitched({
					to: 0
				});
			}
		}

		//start up again
		slideshowEngine.unpauseEngine();
		captionSlideshowEngine.unpauseEngine();
	}

	function showGallery()
	{
		/*var cancelhideGallery = window.setTimeout(function hideGallery() {
		$("#galleryimages .imageoverlay").show("slide", { direction: "down" }, 1000);
		//$("#galleryimages h2").show(2000);
		$("#gallerycontrols").show(2000);

        }, 4000);*/

		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 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);


