// JavaScript Document
//initalize the movie


//get the imageIndex
var imageIndex = -1;
var slideTimer = null;

//image params
var imgNext = null;
var imgBack = null;
var divThmb = null;
var playBtn = null;
var caption = null;


$(document).ready(
				  
	function()
	{
		//get the image queries
		imgNext = $("div.tourNextImage img.picture");	
		imgBack = $("div.tourBackImage img.picture");
		divThmb = $("div.tourFrame div.tourThumbnail");
		caption = $("div.tourFrame div.tourCaption");
		playBtn = $("#playBtn a");
		
		
		//verify params
		if (IsTourReady())
		{
			//start timer
			initTimer();
			
		}
		
		
		//init playBtn toggle
		playBtn.click(			
		 	function()
			{
				if (playBtn.find("img").attr("alt") == "Play")
				{
					initTimer();
					return;
				}
				else
				{
					stopTimer();	
				}
				
			}		 
		);
		
		
	}


);


function toggleThumbs()
{
	stopTimer();
	
	if (divThmb != null)
	{
		if (divThmb.css("visibility") != "hidden")
		{
			divThmb.css("visibility","hidden");
			return;
		}
		else
		{
			divThmb.css("visibility","visible");
		}
		
		
	}
}





function stopTimer()
{
	if (slideTimer != null)
	{
		//stop button and slides
		playBtn.find("img")
			.attr("src","../images/Style2008/PlayerButtons_Play.jpg")
			.attr("alt","Play");
		
		clearInterval(slideTimer);
	}
}

function initTimer()
{
	//start the next squence	
	nextImage();
	
	
	playBtn.find("img")
			.attr("src","../images/Style2008/PlayerButtons_Pause.jpg")
			.attr("alt","Pause");;
	
			
	//start the interval
	slideTimer = setInterval ( "nextSlide()", 7500 );
}


function nextSlide()
{
	nextImage();
	
}

function showCaption(imageIndex)
{
	if (IsTourReady())
	{
		//hide the caption
		caption.fadeOut(500, function()	
			{
				//swap the text
				caption.html(pictureArray[imageIndex].caption);
			
				//show the caption
				caption.fadeIn(1250);						   
							
			}
						
		);
		
		
		
		
	}
	
	
}



function itemImage(index)
{
	
	
	if (IsTourReady())
	{
		
		
		if (index > -1 	&& index < pictureArray.length)
		{
			imgBack.css("display","block");
			imgBack.attr("src",imgNext.get(0).src);
			
			
			
			imageIndex = index;
			
			loadImage(pictureArray[imageIndex].large,imgNext,false,showImage);
		}
		
	}
	
	
	
}

function nextImage()
{
	if (IsTourReady())
	{	
		//1) copy the next image source to the back
		swapImage()
		
		//2) next image index
		nextIndex();
		
		//3) load the new image
		loadImage(pictureArray[imageIndex].large,imgNext,true,showImage);
	}
	
	
	
}


function backImage()
{
	if (IsTourReady())
	{	
		//1) copy the next image source to the back
		swapImage()
		
		
		
		//2) next image index
		backIndex();
		
		//3) load the new image		
		loadImage(pictureArray[imageIndex].large,imgNext,true,showImage);
	}
	
}

function swapImage()
{
	if (IsTourReady())
	{
		imgBack.css("display","block");
		imgBack.attr("src",imgNext.get(0).src);
		imgBack.attr("width",imgNext.get(0).width);
		imgBack.attr("height",imgNext.get(0).height);
		imgBack.css("margin-top",imgNext.css("margin-top"));
	}
	
	
}


function nextIndex()
{
	if (imageIndex < pictureArray.length-1)
	{
		imageIndex += 1;
		return;
	}
	else
	{
		imageIndex = 0;
	}
}


function backIndex()
{
	if (imageIndex > 0 && imageIndex < pictureArray.length)
	{
		imageIndex += -1;
		return;
	}
	else
	{
		imageIndex = pictureArray.length -1;
	}
}










function loadImage(imgUrl, imgQuery, isTransition, delegate)
{
	if (imgUrl != null)
	{
		var source = new Image();
		
		$(source).bind('load readystatechange',function()
		{
			delegate(imgQuery,source,isTransition);
			showCaption(imageIndex);
		});
		//source.onload = function()
//		{
//			delegate(imgQuery,source,isTransition);
//			showCaption(imageIndex);
//		}
		
		
		
		$(source).attr("src",imgUrl);		
		
	}	
}

function showImage(imgQuery, source, isTransition)
{	
	
	if (imgQuery != null && source != null)
	{
		 
		 imgQuery.css("margin-top",null);
		 
		 var qWidth = source.width;
		 var qHeight = source.height;
		 
		 //adjust min width
		 if (source.width < 580)
		 {
			 qWidth = 580;
			 qHeight =  Math.round((580 * source.height)/source.width);
		 }
		 
		 //adjust min height
		 if (qHeight < 460)
		 {
			 
			 qWidth = Math.round((460 * qWidth)/qHeight);
			 qHeight = 460;
		 }
		
		
		if (isTransition)
		{
			imgQuery.css("display","none");
		}
		
		
		//set marging
		if (source.height < 460)
		{	
			//calculate margin offset
			var offSet = (460-parseInt(source.height))/2;
			imgQuery.css("margin-top",(offSet + "px"));
			//alert(offSet);
		}
				
		//imgQuery.attr("width",qWidth);
		//imgQuery.attr("height",qHeight);
		imgQuery.attr("src",source.src);
		
		var rand = Math.round(Math.random() * 10);
		
		
		
		
		if (isTransition)
		{
			var Trans = null;
			
			//choose fade
			if (rand <1)
			{
				imgQuery.show("drop",{direction: "down"},1500);
				
				Trans = "drop";
				
			}
			
			if (rand ==2)
			{
				imgQuery.show("drop",{direction: "up"},1500);
				
				Trans = "drop";
				
			}
			
					
			
			if (rand == 3)
			{
				imgQuery.show("blind", { direction: "vertical" }, 1500);
				
				Trans = "blind";
				
			}
			
			if (rand == 4)
			{
				imgQuery.show("blind", { direction: "horizontal" }, 1500);
				
				Trans = "blind";
				
			}
			
			if (rand == 5)
			{
				imgQuery.show("slide", { direction: "left" }, 1500);
				
				Trans = "slide";
				
			}
			
			if (rand == 6)
			{
				imgQuery.show("slide", { direction: "right" }, 1500);
				
				Trans = "slide";
				
			}
			
			
			if (rand == 7)
			{
				
				imgQuery.fadeIn("slow");
				
				Trans = "fadeIn";
			}
			
			
			if (rand == 8)
			{
				var sWidth = source.width * 2;
				var sHeight = source.height * 2;
				
				//imgQuery.show("scale", { percent: 100, from: {width:sWidth, height:sHeight} }, 1500);
				
				imgQuery.fadeIn("slow");
				
				Trans = "fadeIn";
				
			}
			
			
			if (rand > 8)
			{
				imgQuery.show("fold", {size:200}, 1500);
				
				Trans = "fold";
				
			}
			
			
			//caption.append(" - " + Trans);
			
		}
		
		

		
	}
	
}

function IsTourReady()
{
	if (caption != null && caption.length == 1 && playBtn != null && playBtn.length == 1 && divThmb != null && divThmb.length == 1 && imgNext != null && imgNext.length == 1 && imgBack != null && imgBack.length == 1 && pictureArray != null && pictureArray.length > 0)
	{
		return true;	
	}
	
	return false
}

var dimQuery = null

function toggleDim()
{
	if (IsTourReady())
	{
		if (dimQuery == null)
		{
			dimQuery = $("div.tourModal");
		}
		
		if(dimQuery != null)
		{
			if (dimQuery.css("visibility") != "hidden")
			{
				dimQuery.css("visibility","hidden");
				caption.css("color","#000000");
				return;
			}
			else
			{
				dimQuery.css("visibility","visible");
				caption.css("color","#ffffff");
			}
		}
		
		
		
		
	}
	
	
	
}
