var ft = 1500;
var snipeMoveTime = 300;
var curState = "default";
var snipePos = 0;
var snipeTimer;
var bgX = [];
var aX = [];
var animTime = 50000;
var bgoffset = 0;
var bgw = 2048;
var bgt = 0;

bgX["default"] = 3072;
bgX["consumers"] = 1024;
bgX["businesses"] = -1024;
bgX["financial-institutions"] = -3072;

aX["default"] = 0;
aX["consumers"] = -1000;
aX["businesses"] = -2000;
aX["financial-institutions"] = -3000;

if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1)
{
     var chromeCss = document.createElement("link");

     chromeCss.rel = "stylesheet";
     chromeCss.href = "css/chromeStyles.css";

     document.getElementsByTagName("head")[0].appendChild(chromeCss);
}

$.fn.colorBullets = function(bulletClass,textClass) {
	
	$(this).addClass(bulletClass);
	$(this).find("li").wrapInner("<span class=" + textClass + " />");
	
}

function setupAds() {
	
	// backgrounds
	
	// get bgw
	bgw = $("#bg img").width();
	
	// get bgt
	bgt = $("#bg img").length;
	
	// append all images to end of train
	$("#bg img").each(function(i,e){
		$(e).clone().appendTo("#bg div");
	});
	
	// get individual width of bg
	var w = $("#bg img:first").width() * $("#bg img").length;
	
	// center div
	$("#bg div").css({
		width		: w
	});
	
	// check for half width offset (even number of items)
	if (bgt % 2 == 0) bgoffset = bgw/2;
	
	// adjust margin of first img according to snipe pos (0)
	$("#bg div").css({marginLeft: -1 * (((snipePos + 1) * bgw) - bgoffset)});	
	
	// fade in adjusted bg
	$("#bg div").css({opacity:1});
	
	
	// heroes
	
	// append all imags to end of train
	$("#home-hero a").each(function(i,e){
		$(e).clone().appendTo("#home-hero");
	});

	w = $("#home-hero a").width() * $("#home-hero a").length;
	
	// center div
	$("#home-hero").css({
		width		: w
	});
	
	
	
}

function moveBackground(pos) {
	
	if (snipePos != pos) {
		
		var realPos = 0;
		
		if (pos < snipePos) {
			realPos = pos;
			pos += bgt;
		} else {
			realPos = pos;
		}
		
		// animate
		$("#bg div").animate({marginLeft: -1 * (((pos + 1) * bgw) - bgoffset)},ft,function(){
			
			// reset to position 0 after animating as if going to position 4
			// creates an infinite train look
			$("#bg div").css({marginLeft: -1 * (((realPos + 1) * bgw) - bgoffset)});
			snipePos = realPos;
			
		});

		
	}
	
}

function moveHero(pos) {
	
	if (snipePos != pos) {
		
		var realPos = 0;
		
		if (pos < snipePos) {
			realPos = pos;
			pos += bgt;
		} else {
			realPos = pos;
		}
		
		// animate
		$("#home-hero").animate({marginLeft:-(pos * 1000)},ft,function() {
			$("#home-hero").css({marginLeft:-(realPos * 1000)});
		});
		
	}
	
}

function deactivateSnipe(pos) {
	
	// lower snipe and hide background
	$("div.snipe:eq(" + pos + ")").animate({
		marginTop: 0,
		paddingTop: 0,
		paddingBottom: 0
	},snipeMoveTime/2,function() {
		$(this).removeClass("selected");
	});

	
}

function activateSnipe(pos) {
	
	// raise snipe and add background
	$("div.snipe:eq(" + pos + ")").addClass("selected").animate({
		marginTop: -40,
		paddingTop: 25,
		paddingBottom: 13
	},snipeMoveTime);
		
	// move background to correct position
	moveBackground(pos);
	
	// move hero to correct position
	moveHero(pos);

	// adjust boxes' opacities for background color
	if (!($.browser.msie)) {
		o = 0.5;
		switch(pos) {
		
			case 1:
			o = 0.25;
			break;
		
			case 2:
			o = 0.75;
			break;		
		
			default: 
			o = 0.5;
			break;
		
		}
		$("#boxes img").animate({ opacity: o },ft/2);
	}
	
	// set new pos
	snipePos = pos;
	
}

$.fn.touchCycle = function() {

	if (this != undefined) {
		
		// get snipe widths
		var w = Math.floor(1000 / ($(this).length - 1));
		w -= parseInt($("div.snipe:eq(1)").css("border-right-width"));
		var last = $(this).length - 1;

		$(this).each(function(i,e) {
			
			// set index class
			$(this).addClass("index" + i);
	
			$(e).click(function(ev){
				
				// stop prop
				ev.stopPropagation();
				
				// stop cycling
				clearInterval(snipeTimer);
		
				// get id
				var pos = $(this).index();
				
				if (pos != snipePos) {
				
					// switch
					deactivateSnipe(snipePos);
					activateSnipe(pos);
					
				}
		
			});
				
		});
		
		snipeTimer = setInterval('nextSnipe()',7000);
		
	}
	
}

function nextSnipe() {
	
	// hide current
	if (snipePos > 0) deactivateSnipe(snipePos);
	
	// increment snipePos to new position
	var pos = snipePos+1;

	// over the index?
	if (pos >= $("div.snipe").length) pos = 0;

	// get the next snipe
	activateSnipe(pos);
	
}

function animateBoxes() {
	
	// reset left
	$("#boxes").css("left","0px");
	
	// animate left
	$("#boxes").animate({ left: -($("#boxes img:eq(0)").width()) + "px" },animTime,"linear",function() {
		animateBoxes();
	});
	
}

// document ready
// this function runs at initial page load completion and sets all persistent elements
$(window).bind("load",function(){
	
	if (pageLabel == "home") {
		animateBoxes();
		setupAds();
	}
	
	$("div.snipe").touchCycle();	
	$("#interior-right ul").colorBullets("colored","uncolored");
	$("#search-text").bind('focus',function() {
		if ($(this).val() == "SEARCH") $(this).val("");
	});
	$("#search-text").bind('blur',function() {
		if ($(this).val() == "") $(this).val("SEARCH");
	});
	
	$("#nav ul a.first").each(function(i,e) {
		
		var h = Math.floor(($(e).parent().parent().parent().width() - 160) / 2);
		$(e).css({backgroundPosition: h + "px 0px"});
		
	});
	
	
});
