
var pagerCurrent = -1;
var pagerTimeoutId = -1;
var pagerData = [
	{
		"title" : "SITORSQUAT",
		"link"  : "/work/view/sitorsquat",
		"image" : "/static/img/index_sitorsquat.jpg",
		"description" : "We came up with SitOrSquat back in the summer of 2008. It started as a blog and since then has turned into multiple mobile apps, a web app and is sponsored by Charmin."
	},
	{
		"title" : "LOOKPHAT",
		"link"  : "/work/view/lookphat",
		"image" : "/static/img/index_lookphat.jpg",
		"description" : "Launched as an iPhone app in the Summer of 2009. We came up with this concept after discussing the ways we could help people pick out clothing as they try stuff on in the changing rooms. Little did we know, it would soon turn out to be used for much more than just a shopper’s helper."
	},
	{
		"title" : "MANUALMOB",
		"link"  : "/work/view/manualmob",
		"image" : "/static/img/index_manualmob.jpg",
		"description" : "ManualMob came to us as we were using our new digital camera and wanted to figure out how a function worked. Since we weren’t home and didn’t have the manual on us, we thought... this should be an app!"
	},
	{
		"title" : "JUST THE MAP",
		"link"  : "/work/view/justthemap",
		"image" : "/static/img/index_justthemap.jpg",
		"description" : "Made for numerous mobile devices and countless maps around the world. We’ve either licensed, designed, or received maps which we’ve built mobile applications over."
	}
	
];

function writeHomePagerImages() {
	
	for (var i = 0; i < pagerData.length;i++) {
		var data = pagerData[i];
		document.write("<img style=\"display: none;\" id=\"homePagerImage" + i + "\" src=\"" + data.image + "\">");
	}
}

function nextPager() {
	var nextIndex = pagerCurrent + 1;
	if (nextIndex >= pagerData.length) {
		nextIndex = 0;
	}
	setPagerInternal(nextIndex);
}

function showPagerImage() {
	$("#homePagerImage" + pagerCurrent).css("display", "inline");
	$("#homePagerImage" + pagerCurrent).fadeIn("slow");
}

function setPagerInternal(index) {
	if (pagerCurrent == index)
		return;

	pagerCurrent = index;
	for (var i = 0; i < pagerData.length; i++) {
		$("#pagerLink" + i).attr("src", "/static/img/index_image_" + (i + 1) + ((i == index) ? "_on" : "") + ".jpg");
	}
	
	var page = pagerData[index];
	var foundCurrentPage = false;
	for (var i = 0; i < pagerData.length; i++) {
		var imageElement = $("#homePagerImage" + i); 
		var displayVal = imageElement.css("display");
		if (displayVal != "none") {
			imageElement.fadeOut("slow", function() {
				showPagerImage();
			});
			foundCurrentPage = true;
			break;
		}
	}
	
	if (!foundCurrentPage) 
		showPagerImage();
	/*	
	$("#homePagerImage").fadeOut("slow", function() {
		$("#homePagerImage").attr("src", page.image);
		$("#homePagerImage").fadeIn("slow");
	});
	*/
	$("#homePagerLink").attr("href", page.link);
	$("#homeContentViewNowButton").attr("href", page.link);
	$("#homePagerTitle").text(page.title);
	$("#homePagerDescription").text(page.description);
}

function setPager(index) {
	if (pagerTimeoutId != -1) {
		clearTimeout(pagerTimeoutId);
		pagerTimeoutId = -1;
	}
	setPagerInternal(index);
}

$(document).ready(function() {
	for (var i = 0; i < pagerData.lenth;i++) {
		jQuery.preLoadImages(pagerData[i].image);
	}
	
	setPager(0);
	
	pagerTimeoutId = setInterval("nextPager();", 4500);
});
