var activeProduct, newProduct, numproducts;

function setActiveProduct(productboxpopId, newProduct) {

	productboxpop = $(productboxpopId);		
	productboxpop.css("background","none");

	// Set active proudct and kick off slidey
	activeProduct = newProduct;
	if (productboxpop.attr("class").indexOf("initial") != -1) {
		productboxpop.removeClass("initial");

		//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);
	}
	
	productboxpop.children("div").children("h3").css("background","none");

	productboxpop.addClass("productboxpop-active");
		
	 if(productboxpop.parent("div").attr("class").indexOf("four") != -1){
		numproducts = 4;
	} else if(productboxpop.parent("div").attr("class").indexOf("three") != -1){
		numproducts = 3;	
	} else if(productboxpop.parent("div").attr("class").indexOf("two") != -1){
		numproducts = 2;
	} else {
		numproducts = 5;
	}  

	var uniqueID = "";
	if(productboxpop.parent("div").attr("id")){
		uniqueID = "_" + productboxpop.parent("div").attr("id");
	}  
	
    productboxpop.children("div").css("background","url(assets/images/bg_productboxpop" + numproducts + uniqueID + "_active_" + newProduct + ".png) no-repeat left top");

	productboxpop.find(".autobtn").css("margin-bottom","6px");
	productboxpop.find(".autobtn-wide").css("margin-bottom","6px");
	
	// Increase font size and line heights
	
	productboxpop.find(".lh-small").addClass("lh-normal");
	productboxpop.find(".lh-small").removeClass("lh-small");

	productboxpop.find(".fs-larger").addClass("fs-largest");
	productboxpop.find(".fs-larger").removeClass("fs-larger");

	productboxpop.find(".fs-large").addClass("fs-larger");
	productboxpop.find(".fs-large").removeClass("fs-large");

	productboxpop.find(".fs-small").addClass("fs-normal");
	productboxpop.find(".fs-small").removeClass("fs-small");

	return true;
}

function prodGrow(defaultParam, thisProduct) {
	
	if(thisProduct){
	} else {
		thisProduct = $(this);
	}
	
	// id is in the form "productboxpop_[product]"
	newProduct = thisProduct.children(".container").attr("id").substring(14);
	
	// Only kick off billboard slidey if selecting a non-active product;
	if(newProduct == activeProduct)
		return true;
	
	var productboxpop, productcontent;
	
	if(thisProduct.parent("div").attr("class").indexOf("four") != -1){
		numproducts = 4;
	} else if(thisProduct.parent("div").attr("class").indexOf("three") != -1){
		numproducts = 3;	
	} else if(thisProduct.parent("div").attr("class").indexOf("two") != -1){
		numproducts = 2;
	} else {
		numproducts = 5;
	}  

	if ($("#sign_in_box").attr("class") == "on") {
		$("#sign_in_box .hdr").click();
		$("#sign_in_box .hdr").children("img").attr("src", "assets/images/btn-sign-in-to-turbotax.png");
	}
	
	
	// Reset active productboxpop display and functionality
	if(activeProduct) {
		productboxpop = $("#productboxpop_" + activeProduct).parent("div");

		var uniqueID = "";
		if(productboxpop.parent("div").attr("id")){
			uniqueID = "_" + productboxpop.parent("div").attr("id");
		}  

		productboxpop.removeClass("productboxpop-active");
        productboxpop.css("background","url(assets/images/bg_productboxpop" + numproducts + "_btmcap.gif) no-repeat left bottom");
		productboxpop.children("div").css("background","none");
		productboxpop.find("h3").css("background","url(assets/images/bg_productboxpop" + numproducts + "_topcap_" + activeProduct + ".gif) no-repeat left top");
		
		productboxpop.find(".autobtn").css("margin-bottom","5px");
		productboxpop.find(".autobtn-wide").css("margin-bottom","5px");

		productboxpop.find(".fs-normal").addClass("fs-small");
		productboxpop.find(".fs-normal").removeClass("fs-normal");

		productboxpop.find(".fs-larger").addClass("fs-large");
		productboxpop.find(".fs-larger").removeClass("fs-larger");
		
		productboxpop.find(".fs-largest").addClass("fs-larger");
		productboxpop.find(".fs-largest").removeClass("fs-largest");
		
		productboxpop.find(".lh-normal").addClass("lh-small");
		productboxpop.find(".lh-normal").removeClass("lh-normal");
	}
	
	productboxpop = thisProduct;
	
	setActiveProduct(productboxpop, newProduct);

};

function prodShrink() {

};

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);
		}
	}
}

/*-------------------------- Image Preload -----------------------------*/

jQuery.preloadImages = function() {
	for(var i = 0; i<arguments.length; i++)	{
		jQuery("<img>").attr("src", arguments[i]);
	}
}


/*-------------------------- Flippy Functionality ----------------------------*/

jQuery.fn.flipIt = function(body_height) {
	var container = jQuery(this);

	container.find("li").addClass("flip");

	container.find("li").not(".active").children(".body").css("height","0").append("<p class='smallest lh-small' style='padding:0;'>&nbsp;</p>");
	
	container.find("li.active").children(".body").css("height",body_height);

	container.find(".title").hoverIntent({
   sensitivity: 25, 
   interval: 100, 
   over: function() {
		var target = $(this).parent();
		if (target.attr("class").indexOf("active") == -1) {
			target.children(".body").animate({height:body_height}, {
				step: function(now, fx) {
					target.parent().find(".active").children(".body").height(body_height - (now)).css("overflow","hidden");
				},
				duration: 300,
				complete: function() {
					target.parent().find(".active").removeClass("active");
					target.addClass("active");
				}
			});
		}	
	 }, 
   timeout: 100, 
   out: function() {} 
	});
	
}

/*-------------------------- Slidey Functionality ----------------------------*/

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(".content").animate({left:cnt}, {
		duration: 600,
		easing: "easeInOutQuint",		
		complete: function() {
			var left = $(this).css("left");
			var distance = left.split("px");
			if (distance[0] > -500) $(this).css("left",0);
		}
	});		
}
