var initialLoadDone = false;

$(window).load(function(){
	equalHeights();
});

$(document).ready(function(){
	var is_major = parseInt(navigator.appVersion);
	var is_minor = parseFloat(navigator.appVersion);
	var agt=navigator.userAgent.toLowerCase();
	var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );

	begin();
	
	$("#popClose").click(function(){$("#popOverlay").fadeOut();});
	
	equalHeights();
	
	//Search Results
	$(".result").click(function(){
		var bHeight = ($(window).height() / 2) - 150; 
		var bWidth =  ($(window).width() / 2) - 150;
		$("#popHead").html($(this).attr("title"));
		$("#popOverlay").css({top: bHeight, left: bWidth, height: '400px'}); //Center the overlay
		var theId = $(this).attr("rel");
		//AJAX Request that stuff
		
		$.get("/index.php/products/json_product/"+theId+"/",function(data){
				var jsonData = eval("("+removeNL(data)+")");
				if (jsonData.imageURL == ""){
					$("#teir3bg").html("Image Coming Soon.");
				}else{
					$("#teir3bg").html("<div style=\"padding-top:10px;\">"+jsonData.imageURL+"</div>");
				}
				
				if (jsonData.ingredients == ""){
					var ingredCSS = "opacity: 0.33; cursor: default;";
				}else{
					var ingredCSS = "opacity: 1; cursor: pointer;";
				}
				
				if (jsonData.about == ""){
					var aboutCSS = "opacity: 0.33; cursor: default;";
				}else{
					var aboutCSS = "opacity: 1; cursor: pointer;";
				}
				
				if (jsonData.nutritionalImage == ""){
					var nutritCSS = "opacity: 0.33; cursor: default;";
				}else{
					var nutritCSS = "opacity: 1; cursor: pointer;";
				}
				
				//productURL
				if (jsonData.productURL == ""){
					var urlCSS = "opacity: 0.33; cursor: default;";
				}else{
					var urlCSS = "opacity: 1; cursor: pointer;";
				}
				var resultHTML = "<table id=\"productMenu\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"240\" align=\"center\"><tbody><tr><td colspan=\"2\" align=\"center\"><div id=\"teir3bg\" style=\"color:#000; padding:5px; overflow:auto; text-align:left;\" align=\"center\"><div style=\"padding-top: 10px;\">"+jsonData.imageURL+"</div></div><div id=\"spacer\" style=\"height: 15px;\">&nbsp;</div></td></tr><tr><td><a href=\"javascript:void(0);\" 				onclick=\"$('#teir3bg').html('"+jsonData.ingredients+"')\"><img style=\""+ingredCSS+"\" id=\"ingredients\" src=\"/media/images/hain/product_menu_01.png\" alt=\""+jsonData.productName+"\" border=\"0\" height=\"24\" width=\"119\"></a></td><td><a href=\"javascript:void(0);\" onclick=\"doNutritImage('"+jsonData.nutritionalImage+"')\"><img style=\""+nutritCSS+"\" id=\"nutritional\" src=\"/media/images/hain/product_menu_02.png\" alt=\" \" border=\"0\" height=\"24\" width=\"121\"></a></td></tr><tr><td><a href=\"javascript:void(0);\" onclick=\"$('#teir3bg').html('"+jsonData.about+"')\"><img style=\""+aboutCSS+"\" id=\"about\" src=\"/media/images/hain/product_menu_03.png\" alt=\""+jsonData.about+"\" border=\"0\" height=\"23\" width=\"119\"></a></td><td><a href=\""+jsonData.productURL+"\" target=\"_BLANK\"><img style=\""+urlCSS+"\" id=\"website\" src=\"/media/images/hain/product_menu_04.png\" alt=\""+jsonData.productURL+"\" border=\"0\" height=\"23\" width=\"121\"></a></td></tr></tbody></table>";
				$("#popBody").html(resultHTML);
			});				
		$("#popOverlay").fadeIn();
	});

	
	//Navigation

	$("#navigation").hover(function(){
		if ($(this).css("height") == "53px"){
			$("#navImage").attr("src", "/media/images/hain/navigation_open.png");
			if (is_ie6){
					$("#navigation").css({height: "367px"});
					$("#navImage").css({height: "367px"});
			}else{
				$(this).animate({height: "367px"}, function(){
					$(this).css({height: "367px"});
				});
			}
		}
	}, function(){closeNav();});

	$("#navigation").hover(function(){
		if ($(this).css("height") == "53px"){
			$("#navImage").attr("src", "/media/images/hain/navigation_open.png");
			if (is_ie6){
					$("#navigation").css({height: "367px"});
					$("#navImage").css({height: "367px"});
			}else{
				$(this).animate({height: "367px"}, function(){
					$(this).css({height: "367px"});
				});
			}
		}
	}, function(){closeNav();});

		
	//ingredients popup
	$("#ingredients").click(function(){
		var cursorStatus = $(this).css("cursor");
		if (cursorStatus == "pointer"){
			var bHeight = ($(window).height() / 2) - 150; 
			var bWidth =  ($(window).width() / 2) - 150;
			if ($("#productImage").attr("alt") == undefined)
				var productTitle = "";
			else
				var productTitle = $("#productImage").attr("alt");
			$("#popHead").html(productTitle+" Ingredients");				
			$("#popBody").html($("#ingredients").attr("alt"));
			
			$("#popOverlay").css({top: bHeight, left: bWidth, height: '300px'}); //Center the overlay
			$("#popOverlay").fadeIn(function(){
				var theHeight = 40;
				theHeight = theHeight + parseInt($("#popHead").height()) + parseInt($("#popBody").height());
				if (theHeight < 300){
					var withPX = theHeight+"px";
					$("#popOverlay").animate({height: withPX});
				} 
			});
		}
	});

	//about popup
	$("#about").click(function(){
		var cursorStatus = $(this).css("cursor");
		if (cursorStatus == "pointer"){
			var bHeight = ($(window).height() / 2) - 150; 
			var bWidth =  ($(window).width() / 2) - 150;
			if ($("#productImage").attr("alt") == undefined)
				var productTitle = "";
			else
				var productTitle = $("#productImage").attr("alt");
			$("#popHead").html("About "+productTitle);				
			$("#popBody").html($("#about").attr("alt"));
			$("#popOverlay").css({top: bHeight, left: bWidth, height: '300px'}); //Center the overlay
			$("#popOverlay").fadeIn(function(){
				var theHeight = 40;
				theHeight = theHeight + parseInt($("#popHead").height()) + parseInt($("#popBody").height());
				if (theHeight < 300){
					var withPX = theHeight+"px";
					$("#popOverlay").animate({height: withPX});
				} 
			});
		}
	});
	
	//nutritional popup
	$("#nutritional").click(function(){
		var cursorStatus = $(this).css("cursor");
		if (cursorStatus == "pointer"){
			var bHeight = ($(window).height() / 2) - 250; 
			var bWidth =  ($(window).width() / 2) - 150;
			if ($("#productImage").attr("alt") == undefined)
				var productTitle = "";
			else
				var productTitle = $("#productImage").attr("alt");
			$("#popHead").html("Nutritional Facts: " + productTitle);				
			$("#popBody").html("<div align='center'><img src='"+$("#nutritional").attr("alt")+"' align='middle' height='350' alt=' ' border='0' /></div>");
			$("#popOverlay").css({top: bHeight, left: bWidth, height: '500px'}); //Center the overlay
			$("#popOverlay").fadeIn();
		}
	});
	
	//website
	$("#website").click(function(){
		var cursorStatus = $(this).css("cursor");
		if (cursorStatus == "pointer"){
			if ($(this).attr("alt") != ""){window.open($(this).attr("alt"),"","")}
		}
	});
	
	
});

function equalHeights(){
	$(".home-left #news .content, .home-left #twitter_feed .content").equalHeight();
	$(".home-left,  .home-right").equalHeight();
}

function closeNav(){
	if ($("#navigation").css("height") ==  "367px"){
		$("#navigation").animate({height: "53px"}, function(){

			$("#navImage").attr("src", "/media/images/hain/navigation.png");

			$("#navigation").css({height: "53px"});
			if (is_ie6)
				$("#navImage").css({height: "53px"});
		});
	}
}

var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var agt=navigator.userAgent.toLowerCase();
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );

function doNutritImage(imgUrl){
	$("#teir3bg").html("<img src=\""+imgUrl+"\" alt=\" \" width=\"210\" border=\"0\"/>");
}

//Sort by:
function sortBy(sortBy){
	$(".sortBy").css("fontWeight", "normal");
	$("#"+sortBy).css("fontWeight", "bolder");
	if (sortBy == "btn_brand")
		var qUrl = "/index.php/products/json_brand_listing/";
	else if (sortBy == "btn_prodType")
		var qUrl = "/index.php/products/json_category_listing/";
	else if (sortBy == "btn_health")
		var qUrl = "/index.php/products/health_benefits_listing/";
	
	$("#teir2, #teir3").hide();
	$("#teir1").load(qUrl, function(){
		$('#teir1 a').tooltip({showURL: false});
		
	});
}

function teir1click(theId){
		$("#teir3").hide();
		$("#teir1 a").css("fontWeight", "normal");
		$("#teir1 a[rel='"+theId+"']").css("fontWeight", "bolder");

		if ($("#btn_brand").css("fontWeight") == "bolder" || $("#btn_brand").css("fontWeight") == "bold" || $("#btn_brand").css("fontWeight") == "700"){
			var qUrl = "/index.php/products/json_brand/"+theId+"/";
		}else{

			var qUrl = "/index.php/products/json_category/"+theId+"/";
		}
		$("#teir2").load(qUrl, function(data){
			$('#teir2').show('blind', {direction: 'horizontal'}, 1000);
			
			if(typeof(segment_4) !== "undefined" && segment_4 != "" && initialLoadDone === false){
				setTimeout(function() {
					initialLoadDone = true;
					teir2click(segment_4);
				}, 1000)
			}else{
				initialLoadDone = true;
			}
		});
}

function teir2click(theId){
	$("#teir2 a").css("fontWeight", "normal");
	$("#teir2 a[rel='"+theId+"']").css("fontWeight", "bolder");
		$.get("/index.php/products/json_product/"+theId+"/",function(data){
		var jsonData = eval("("+removeNL(data)+")");
		
		if (jsonData.imageURL == ""){
			$("#teir3bg").html("Image Coming Soon.");
		}else{
			$("#teir3bg").html("<div style=\"padding-top:10px;\">"+jsonData.imageURL+"</div>");
		}
		
		if (jsonData.ingredients == ""){
			$("#ingredients").fadeTo("fast", 0.33);
			$("#nutritional").css("cursor", "default");
		}else{
			$("#ingredients").fadeTo("fast", 1.00);
			$("#ingredients").css("cursor", "pointer");
			$("#ingredients").attr("alt", jsonData.ingredients);
		}
		
		if (jsonData.about == ""){
			$("#about").fadeTo("fast", 0.33);
			$("#nutritional").css("cursor", "default");
		}else{
			$("#about").fadeTo("fast", 1.00);
			$("#about").css("cursor", "pointer");
			$("#about").attr("alt", jsonData.about);
		}
		
		if (jsonData.nutritionalImage == ""){
			$("#nutritional").fadeTo("fast", 0.33);
			$("#nutritional").css("cursor", "default");
		}else{
			$("#nutritional").fadeTo("fast", 1.00);
			$("#nutritional").css("cursor", "pointer");
			$("#nutritional").attr("alt", jsonData.nutritionalImage);
		}
		
		//productURL
		if (jsonData.productURL == ""){
			$("#website").fadeTo("fast", 0.33);
			$("#website").css("cursor", "default");
		}else{
			$("#website").fadeTo("fast", 1.00);
			$("#website").css("cursor", "pointer");
			$("#website").attr("alt", jsonData.productURL);
		}
		$("#productCopy").html(jsonData.brand+"<br />"+jsonData.productName);
		$("a [rel='website']").attr("href", jsonData.productURL);
		$('#teir3').show('blind', {direction: 'horizontal'}, 1000);
	});
}

function qmark(theId){
	//Question Mark Icon
		var bHeight = ($(window).height() / 2) - 150; 
		var bWidth =  ($(window).width() / 2) - 150;
		$("#popHead").html($("#"+theId).attr("title"));
		$("#popBody").html($("#"+theId).attr("alt"));
		$("#popOverlay").css({top: bHeight, left: bWidth}); //Center the overlay
		$("#popOverlay").fadeIn(function(){
			var theHeight = 40;
			theHeight = theHeight + parseInt($("#popHead").height()) + parseInt($("#popBody").height());
			if (theHeight < 300){
				var withPX = theHeight+"px";
				$("#popOverlay").animate({height: withPX});
			}
		});
}

function category(theId){
	//Category Name click
		var bHeight = ($(window).height() / 2) - 150; 
		var bWidth =  ($(window).width() / 2) - 150;
		$("#popHead").html($("#"+theId).attr("rel"));
		$("#popBody").html($("#"+theId).attr("title"));
		$("#popOverlay").css({top: bHeight, left: bWidth}); //Center the overlay
		$("#popOverlay").fadeIn(function(){
			var theHeight = 40;
			theHeight = theHeight + parseInt($("#popHead").height()) + parseInt($("#popBody").height());
			if (theHeight < 400){
				var withPX = theHeight+"px";
				$("#popOverlay").animate({height: withPX});
			}else{
				theHeight = 400;
				var withPX = theHeight+"px";
				$("#popOverlay").animate({height: withPX});
			}
		});
}

function removeNL(s){ 
	return s.replace(/[\n\r\t]/g, ''); 
}


function begin(){
	//Detect if we need to display something by default
	var curURL = window.location.href;
	var curURLArray = curURL.split("/");
	
	/*
	if (curURLArray[6] == "health_benefit"){
		//sortBy('btn_health'); //This is loaded by default
		if (curURLArray[7] != "" && curURLArray[7] != undefined){
			setTimeout("teir1click("+curURLArray[7]+")", 1500);
		}
	}else if (curURLArray[6] == "brand"){
		sortBy('btn_brand');
		if (curURLArray[7] != "" && curURLArray[7] != undefined){
			setTimeout("teir1click("+curURLArray[7]+")", 1500);
		}
	}else if (curURLArray[6] == "product_type"){
		sortBy('btn_prodType');
		if (curURLArray[7] != "" && curURLArray[7] != undefined){
			setTimeout("teir1click("+curURLArray[7]+")", 1500);
		}
	}
	*/
	
	if (curURLArray[5] == "" || curURLArray[5] == undefined)
		setTimeout("startOpenNav()", 2800);
		
	if(typeof(segment_2) !== "undefined" && segment_2 == "brand"){
		sortBy('btn_brand');
	}
	
	if(typeof(segment_3) !== "undefined" && segment_3 != "" && initialLoadDone === false){
		setTimeout(function() {
			teir1click(segment_3);
		}, 1000)
	}
	
	$('#productMid').show('blind', {direction: 'vertical'}, 1000, function(){$('#teir1').fadeIn();});			
}

function startOpenNav(){
	if ($("#navigation").css("height") == "53px"){

		$("#navImage").attr("src", "/media/images/hain/navigation_open.png");

		$("#navigation").animate({height: "367px"}, function(){
			$("#navigation").css({height: "367px"});
			if (is_ie6)
				$("#navImage").css({height: "367px"});

			setTimeout("closeNav()", 3000);
		});
	}
}



