// JavaScript for index.html
	var display_time = 5000;		//time an image will display in milliseconds
	var effect_time = 1500;			//time it takes for an image to transition in milliseconds
	var images;
	var interval;
	var timeout;
	var index = 0;
	var items;
	
	function changeLoc(index){
		var locations = $("span.location");
		//alert(images[index].src)
		try{
			var src = images[index].src.replace(/%20/g, " ");
			//if( src.indexOf( 'China') != -1) alert(src);
			for(x = 0; x < locations.length; x++){
				if( src.indexOf( jQuery(locations[x]).text() ) != -1){
					$("span.location:eq(" + x + ")").css('color','#FFFFFF');
					$("span.location:lt(" + x + ")[style]").css('color','#464646');
					$("span.location:gt(" + x + ")[style]").css('color','#464646');
					break;
				}
			}	
		}catch(e){}
	}
	
	function swap(firstTime){
		try
		{
			
		changeLoc(index);
		
		var img = $("#imageRotator div:eq(" + index + ") img");
		if(!img[0].complete)
		{
			showProgress();
			//img[0].onload = function() {
			//	play(false);
			//};
			
			//attempt to load the image
			var image = new Image();
			image.src = img[0].src;
			if(!firstTime)
			{
				clearInterval(interval);
				clearTimeout(timeout);
				timeout = setTimeout("play(true);",display_time);
			}
			return false;
		}
		
		hideProgress();
		$("#imageRotator div:eq(" + index + ")").fadeIn(effect_time);
		
		if(index == items ){
			changeLoc(index);
			$("#imageRotator div:gt(0)").fadeOut(effect_time);
			index = 0;
		}
		index++;
		
		return true;
		} catch(e) {return false;} 
	}
	
	function pause(){
		$("#enter").css('visibility','visible');
		clearInterval(interval);
		$("#imageRotator div").css('display','none');
	}
	
	function play(first){
		
		clearInterval(interval);
		clearTimeout(timeout);
		if(swap(first))
		{
			$("#enter").css('visibility','hidden');
			interval = setInterval("swap(false);",display_time);
		} else {
			timeout = setTimeout("play(true);",1000);
		}
		//alert('play');
	}
	
	function changeSet(folder){
		var div = document.getElementById(folder);
		index = $("#imageRotator div").index(div);
		pause();
		play(true);
	}
	
	function showProgress()
	{
		$("#progress").css('visibility','visible');
	}
	
	function hideProgress()
	{
		$("#progress").css('visibility','hidden');			
	}
	
	$(document).ready(function(){
		/*$.ajaxSetup({
			async: false
		});*/
		showProgress();
		clearInterval(interval);
		$("#imageRotatorCont").load("/getLandingPageImages.php",null, function (responseText, textStatus, XMLHttpRequest) {
																				
			images = $("#imageRotator div img");
			items = images.length - 1;
			
			$("#imageRotator").hover(
				function(){pause();},
				function(){play(true);}					
			 );
			//console.log('called');
			clearInterval(interval);
			//setTimeout("play(true);",1000);
			play(true);
		});
   	});	
