$(document).ready(function(){
	if (1 < $(".TnImage").length) {
		var Idx = 1;
		var IntervalKey;
		var ChangeImage = function(){
			//If the image still animating, stop it and start the new one
			$("#MainImage").ImageStop(true,true);
			$("#MainImage").ImageSwitch({Type: "FadeIn",NewImage: $(".TnImage").eq(Idx).attr("src")});		
			//Set the next image will be display
			Idx++;
			if(Idx>$(".TnImage").length-1){
				Idx = 0;
			}
			//Start preload the next image
			$.ImagePreload($(".TnImage").eq(Idx).attr("src"));			
		};
		//When a thumbnail's clicked	
		
		var StartSlideShow = function(){
			IntervalKey = setInterval(ChangeImage,5000);
			$("#SlideShow").text("Stop Slide show");
			$("#SlideShow").unbind("click");
			$("#SlideShow").bind("click",StopSlideShow);					
		};
		
		var StopSlideShow = function(){
			clearInterval(IntervalKey);
			$("#SlideShow").text("Start Slide show");
			$("#SlideShow").unbind("click");
			$("#SlideShow").bind("click",StartSlideShow);					
		};
		
		$("#SlideShow").bind("click",StartSlideShow).trigger("click");
	}
});
