var activeSlide, newSlide, numproducts;

function setActiveSlide(boxpopId, newSlide) {

	var boxpop
	boxpop = $(boxpopId);		

	// Set active proudct and kick off slidey
	activeSlide = newSlide;
	if (boxpop.attr("class").indexOf("initial") != -1) {
		boxpop.removeClass("initial");

		//find position of the product within the billboard and adjust (no slide on initial load)
		var container = $(".billboard");
		$.each(container.find(".slide"), function(i, n){
			$(n).attr("rel",i);
		});
		var width = container.width();
		var z = $("#bb_" + newSlide).attr("rel");
		var cnt = - (width*z);
		if (isNaN(cnt)) cnt = 0;
		if(cnt != 0)
			$(".billboard .content").css("left",cnt);

	} else {
		$(".billboard").slideTo("#bb_" + newSlide);
	}
	
	//boxpop.children("div").children("h3").css("background","none");
	//boxpop.addClass("boxpop-active");
	//boxpop.children("div").css("margin-top","-5px").css("height","205px");
	//boxpop.children("div").children("h3").css("color","red");
	boxpop.children("div").addClass("boxpop_active");

	var uniqueID = "";
	if(boxpop.parent("div").attr("id")){
		uniqueID = "_" + boxpop.parent("div").attr("id");
	}

	return true;
}

function boxGrow(defaultParam, thisProduct) {

	if(thisProduct){
	//do nothing
	} else {
		thisProduct = $(this);
	}

	// id is in the form "boxpop_[product]"
	newSlide = thisProduct.children(".container").attr("id").substring(7);
	
	// Only kick off billboard slidey if selecting a non-active product;
	if(newSlide == activeSlide)
		return true;

	var boxpop, productcontent;
	
	// Reset active boxpop display and functionality
	if(activeSlide) {
		boxpop = $("#boxpop_" + activeSlide).parent("div");

		var uniqueID = "";
		if(boxpop.parent("div").attr("id")){
			uniqueID = "_" + boxpop.parent("div").attr("id");
		}

		//alert(boxpop.children("div").attr("class"));
		boxpop.children("div").removeClass("boxpop_active");
		//boxpop.children("div").css("margin-top","0").css("height","200px");
		//boxpop.children("div").children("h3").css("color","white");
		//boxpop.find(".fs-smaller").addClass("fs-smallest");
		//boxpop.find(".fs-smaller").removeClass("fs-smaller");

	}
	
	boxpop = thisProduct;
	setActiveSlide(boxpop, newSlide);
};

function boxShrink() {
};

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]);
	}
}

/*-------------------------- Slidey Functionality ----------------------------*/

jQuery.fn.slideTo = function(target_id, settings) {
	settings = jQuery.extend({
     easeTime: 400
  }, settings);
	var container = jQuery(this);
	$.each( container.find(".slide"), 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(".sliding_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);
		}
	});		
}
