var curHome = 0;
var curImage = 1;
var shotTimer = '';
var skipToNextFeature = true;
var nextImageDelay = 5000;
var startFirstFeatureDelay = 5000;
var movingToNextFeature = false;


/* extend jquery to handle preloading */
(function($) {
	var imgList = [];
	$.extend({
		preload: function(imgArr, option) {
			var setting = $.extend({
				init: function(loaded, total) {},
				loaded: function(img, loaded, total) {},
				loaded_all: function(loaded, total) {}
			}, option);
			var total = imgArr.length;
			var loaded = 0;
			
			setting.init(0, total);
			for(var i in imgArr) {
				imgList.push($("<img />")
					.attr("src", imgArr[i])
					.load(function() {
						loaded++;
						setting.loaded(this, loaded, total);
						if(loaded == total) {
							setting.loaded_all(loaded, total);
						}
					})
				);
			}
			
		}
	});
})(jQuery);

$(document).ready(function() {
	//shotTimer = setTimeout("rotateScreenShots()",nextImageDelay);
	
	//set click handler for feature list
	$("ul.homeList li").click(function() {
		
		clearTimeout(shotTimer);
		
		//get the ID for this feature
		var i = $(this).attr("id").replace("f","")*1;
		
		//if its not the currently selected feature...
		if (i != curHome)
		{
			//$("#iphone #screen #loading").css({"opacity":0});
			
			//stop auto featuring
			if (!movingToNextFeature) skipToNextFeature = false;
			else movingToNextFeature = false;
			
			//update the caption
			var featureId = $(this).attr("rel").replace("fid-","");
			var titleText = $(this).html();
			$("#homeIntro .iPadded").html("<span class='orange'><strong>"+titleText+"</strong></span> " + shotCaptions[i] + " <a onclick='skipToNextFeature=false;' href='/features/"+featureId+"/readFeature' class='colorme orange lined'>read more</a>");
			
			colorMeUp();
			
			//remove the hover state from the checkbox
			$("ul.homeList li#f"+curHome).removeClass("selected");
			//add the hover state to the new checkbox
			$("ul.homeList li#f"+i).addClass("selected");
			//update the current selection
			curHome = i;
			
			var shotStr = "";
			//fade out the currently showing screenshots
			$("#iphone #screen #loading").hide();
			$("#iphone #screen #screenPadding").stop().animate({ opacity: 0 }, 400, function() {
				//fade in the loading animation
				$("#iphone #screen #loading").fadeIn("normal",function() {

					//preload next images
					//loop through count and prep image path array
					var imgs = [];
					var addCounter = 0;
					for (iCounter = 0; iCounter < scrShotCount[i]; iCounter++)
					{
						//imgs[iCounter] = "http://scanbizcards.com/images/shot"+i+"-"+i+".jpg";
						addCounter = iCounter + 1;
						imgs[iCounter] = "http://www.scanbizcards.com/images/casestudiesGalleries/large/"+scrShots[i][addCounter];
					}
					
					//preload them
					$.preload(imgs, {
						init: function(loaded, total) {
							//no init functions required
						},
						loaded: function(img, loaded, total) {
							//var percent = Math.round((loaded*1 / total*1)*200)-200;
							//$("#loadBar").css({"left":percent+"px"});
						},
						loaded_all: function(loaded, total) {
							$("#iphone #screen #loading").fadeOut("normal",function() {
								for (iCounter = 1; iCounter <= scrShotCount[i]; iCounter++)
								{
									if (iCounter == 1) var addHidden = " ";
									else var addHidden = " style='display:none;' ";
									shotStr += "<img"+addHidden+"src='/images/casestudiesGalleries/large/"+scrShots[i][iCounter]+"' alt='' />";
								}									  
								$("#iphone #screen #screenPadding #screenShots").html(shotStr);
								$("#iphone #screen #screenPadding").stop().animate({	opacity: 1 }, 400);
								
								//start shotTimer
								curImage = 1;
								shotTimer = setTimeout("rotateScreenShots()",nextImageDelay);
							});		
						}
					});
				});
				
			});
		}
		else shotTimer = setTimeout("rotateScreenShots()",nextImageDelay);
	});
	//load the first set of images
	/*var shotStr = '';
	for (iCounter = 1; iCounter <= scrShotCount[1]; iCounter++)
	{
		if (iCounter == 1) var addHidden = " ";
		else var addHidden = " style='display:none;' ";
		shotStr += "<img"+addHidden+"src='/images/casestudiesGalleries/large/"+scrShots[1][iCounter]+"' alt='' />";
	}
	$("#iphone #screen #screenPadding #screenShots").html(shotStr);
	
	//start the rotation
	shotTimer = setTimeout("rotateScreenShots()",nextImageDelay);*/
	
	//start timer to switch to feature1
	shotTimer = setTimeout("startItUp()",startFirstFeatureDelay);
});

function startItUp()
{
	movingToNextFeature=true;
	$("ul.homeList li:first").click();
	//skipToNextFeature=true;
	
}

function rotateScreenShots()
{
	clearTimeout(shotTimer);
	var skipMovement = false;
	var maxShots = scrShotCount[curHome];
	var nextShot = curImage + 1;
	
	if (nextShot > maxShots) 
	{
		if (skipToNextFeature) 
		{
			var maxFeatures = $(".homeList li").length;
			skipMovement = true;
			var nextFeature = curHome + 1;
			
			if (nextFeature > maxFeatures) nextFeature = 1;
			nextFeature = nextFeature-1;
			
			movingToNextFeature = true;
			$("ul.homeList li:eq("+nextFeature+")").click();
		}
		else nextShot = 1;
	}
	if (!skipMovement)
	{
		findCurImage = curImage - 1;
		findNextShot = nextShot - 1;
		$("#iphone #screen #screenPadding #screenShots img:eq("+findCurImage+")").animate({ left: "-268px" }, 1000);
		$("#iphone #screen #screenPadding #screenShots img:eq("+findNextShot+")").css({"top":0,"left":"268px","display":"block"}).animate({ "left": "0px" }, 1000);
		curImage = nextShot;
		shotTimer = setTimeout("rotateScreenShots()",nextImageDelay);
	}
}


