var activeProduct, newProduct, numproducts;

function resetSlidey() {
	if ($(".scroll").html() != null) {
		if ($(".scroll").scrollLeft() > 0) {
			$(".scroll").scrollLeft(0);
		}
	}
	if ($(".scroll .wrapper").html() != null) {
		if ($(".scroll .wrapper").scrollLeft() > 0) {
			$(".scroll .wrapper").scrollLeft(0);
		}
	}
}

/*--------------------------- Product Pop ----------------------------*/

function prodPop(defaultParam, thisProduct) {

	if(thisProduct){
	} else {
		thisProduct = $(this);
	}
		
	// id is in the form "productboxpop_[product]"
	newProduct = thisProduct.children(".container").attr("id").substring(5);

	// Only kick off billboard slidey if selecting a non-active product;
	if(newProduct == activeProduct)
		return true;
	
	var productboxpop, productcontent;
	
	numproducts = thisProduct.parent("div").hasClass("four") && 4 || thisProduct.parent("div").hasClass("three") && 3 || 	thisProduct.parent("div").hasClass("two") && 2 || 5;

	if ($("#sign_in_box .hdr_open").css("display") == "none") {
		$("#sign_in_box .hdr_close").click();
	}
	
	// Reset active productboxpop display and functionality
	if(activeProduct) {
		productboxpop = $("#prod_" + activeProduct).parent("div");
		
		productboxpop.removeClass("prodActive");
		productboxpop.children("div.shadow").hide();
		productboxpop.find(".autobtn").css("margin-bottom","5px");
		productboxpop.find(".autobtn-wide").css("margin-bottom","5px");
		productboxpop.find(".fs-smaller").addClass("fs-smallest").removeClass("fs-smaller");
		productboxpop.find(".fs-small").addClass("fs-smaller").removeClass("fs-small");
		productboxpop.find(".fs-normal").addClass("fs-small").removeClass("fs-normal");
		productboxpop.find(".fs-larger").addClass("fs-large").removeClass("fs-larger");
		productboxpop.find(".fs-largest").addClass("fs-larger").removeClass("fs-largest");
		productboxpop.find(".lh-normal").addClass("lh-small").removeClass("lh-normal");
		productboxpop.find(".lh-large").addClass("lh-normal").removeClass("lh-large");
	}
	
	productboxpop = thisProduct;	
	prodActivate(productboxpop, newProduct);
	
}
function prodUnPop() {
	
};

function prodActivate(productboxpopId, newProduct) {

	productboxpop = $(productboxpopId);
	activeProduct = newProduct;

	productboxpop.addClass("prodActive");	

	if (productboxpop.hasClass("initial")) {
		productboxpop.removeClass("initial");
		
		// Assign dynamic height to shadows
		$(".productbox .shadow .ml, .productbox .shadow .mr").height($(productboxpop).height()-7);
		$(".productbox .shadow .tc, .productbox .shadow .bc").width($(productboxpop).width()-18);
		
		// find position of the product within the billboard and adjust 
		// (no slide on initial load)
		var container = $(".billboard");
		$.each( container.find(".section"), function(i, n){
			$(n).attr("rel",i);
		});
		var width = container.width();
		var z = $("#bb_" + newProduct).attr("rel");
		var cnt = - (width*z);
		if (isNaN(cnt)) cnt = 0;
		if(cnt != 0) $(".billboard .content").css("left",cnt);
	} else {
		$(".billboard").slideTo("#bb_" + newProduct);
	}
	
	numproducts = productboxpop.parent("div").hasClass("four") && 4 || productboxpop.parent("div").hasClass("three") && 3 || 	productboxpop.parent("div").hasClass("two") && 2 || 5;

	productboxpop.children("div.shadow").show();
	productboxpop.find(".autobtn").css("margin-bottom","6px");
	productboxpop.find(".autobtn-wide").css("margin-bottom","6px");
	productboxpop.find(".lh-normal").addClass("lh-large").removeClass("lh-normal");	
	productboxpop.find(".lh-small").addClass("lh-normal").removeClass("lh-small");
	productboxpop.find(".fs-larger").addClass("fs-largest").removeClass("fs-larger");
	productboxpop.find(".fs-large").addClass("fs-larger").removeClass("fs-large");
	productboxpop.find(".fs-small").addClass("fs-normal").removeClass("fs-small");	
	productboxpop.find(".fs-smaller").addClass("fs-small").removeClass("fs-smaller");
	productboxpop.find(".fs-smallest").addClass("fs-smaller").removeClass("fs-smallest");

	return true;
}

/*-------------------------- Image Preload -----------------------------*/

jQuery.preloadImages = function() {
	var prefix = "";
	//only use akamai for http
	if(location.protocol == 'http:')
		prefix = "http://images.turbotax.intuit.com";
	
	for(var i = 0; i<arguments.length; i++)	{
		jQuery("<img>").attr("src", prefix + arguments[i]);
	}
}

/*-------------------------- Flippy ----------------------------*/

jQuery.fn.setFlippy = function(body_height) {
	var container = jQuery(this);
	container.find("li").addClass("flip");
	container.find("li").not(".active").children(".body").css("height", "0px").css("overflow","hidden").hide();
	container.find("li.active").children(".body").css("height",body_height);
	container.find(".title").hoverIntent({
   sensitivity: 25, 
   interval: 100, 
   over: function() {
		var target = $(this).parent();
		var target_parent = target.parent();
		if (!target.hasClass("active")) {
			if (target_parent.find(".active_old").html() != null) {
				target_parent.find(".active").children(".body").stop();
				target_parent.find(".active_old").children(".body").height(0).hide();
				target_parent.find(".active").children(".body").height(body_height);
				target_parent.find(".active_old").removeClass("active_old");				
			}
			target_parent.find(".active").addClass("active_old").removeClass("active");
			target.addClass("active");	
			target.children(".body").animate({height:body_height}, {
				step: function(now, fx) {
					target_parent.find(".active_old").children(".body").height(body_height - (now));
				},
				duration: 300,
				complete: function() {
					target_parent.find(".active_old").children(".body").hide();
					target_parent.find(".active_old").removeClass("active_old");
				}
			});
		}		
	 }, 
   timeout: 100, 
   out: function() {} 
	});	
}


/*-------------------------- Slidey ----------------------------*/

jQuery.fn.slideTo = function(target_id, settings) {
	settings = jQuery.extend({
     easeTime: 400
  }, settings);
	var container = jQuery(this);
	$.each( container.find(".section"), function(i, n){
		$(n).attr("rel",i);
	});
	var width = container.width();
	var z = $(target_id).attr("rel");
	var cnt = - (width*z);
	if (isNaN(cnt)) cnt = 0;

	container.find(".itab_content").animate({left:cnt}, {
		duration: 600,
		easing: "easeInOutQuint",
		complete: function() {
			var left = $(this).css("left");
			var distance = left.split("px");
			if (distance[0] > 0) $(this).css("left",0);
		}
	});		
}

/*--------------------------- Poppy ----------------------------*/

/*--------------------------- Page Sizes ----------------------------*/

function getPageSize(){
	var xScroll = (window.innerHeight && window.scrollMaxY) && window.innerWidth + window.scrollMaxX || (document.body.scrollHeight > document.body.offsetHeight) && document.body.scrollWidth || document.body.offsetWidth;
	var yScroll = (window.innerHeight && window.scrollMaxY) && window.innerHeight + window.scrollMaxY || (document.body.scrollHeight > document.body.offsetHeight) && document.body.scrollHeight || document.body.offsetHeight;

	var windowWidth = self.innerHeight && (document.documentElement.clientWidth ? document.documentElement.clientWidth : self.innerWidth) || (document.documentElement && document.documentElement.clientHeight) && document.documentElement.clientWidth || document.body && document.body.clientWidth;
	var windowHeight = self.innerHeight && self.innerHeight || (document.documentElement && document.documentElement.clientHeight) && document.documentElement.clientHeight || document.body && document.body.clientHeight;

	// for small pages with total height less then height of the viewport
	var pageHeight = yScroll < windowHeight ? windowHeight : yScroll;
	
	// for small pages with total width less then width of the viewport
	var pageWidth = xScroll > windowWidth ? xScroll : windowWidth;

	var arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
}

function getPageScroll(){
	var xScroll = self.pageYOffset && self.pageXOffset || (document.documentElement && document.documentElement.scrollTop) && document.documentElement.scrollLeft || document.body.scrollLeft;
	var yScroll = self.pageYOffset && self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) && document.documentElement.scrollTop || document.body.scrollTop;
	var arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
}