$(document).ready(function() {
	//Super high-tech French detection
	var theURL = document.location.toString();
	var isFrench = theURL.search("yvesveggie.ca\/fr"); //looks for:  yvesveggie.ca/fr/ in the url
	var isAnimating = false;
	
	if (isFrench != -1){
		isFrench = true;
		//console.info("FRAAAAANCH");
	}else{
		isFrench = false;
		//console.info("Engrish");
	}

        if ($(window).height() < 700){
            $("[href='#enlargedImage']").hide();
        }

	$(".kwickLinks").click(function(){
		if (!isAnimating){
			isAnimating = true;
			var whichBlade = $(this).attr("rel");
			var theBladeId = "blade"+whichBlade;
			$(".featProductLayer").hide();
			$(".blade").animate({width: '19px', backgroundColor: '#B5A392'});
			$("#"+theBladeId).animate({width: '590px', backgroundColor: '#FFF'}, function(){
				//Now fade in the content
				$("#"+theBladeId+" .featProductLayer").fadeIn(function(){
					isAnimating = false;
				});
			});
			$(".kwickLinks").css("fontWeight", "normal");
			$(".kwickLinks[rel='"+whichBlade+"']").css("fontWeight", "bolder");
		}
	});
	
	$(".sideBlade").click(function(){
		if (!isAnimating){
			var whichBlade = $(this).attr("rel");
			var theBladeId = "blade"+whichBlade;
			$(".featProductLayer").hide();
			$(".blade").animate({width: '19px', backgroundColor: '#B5A392'});
			$("#"+theBladeId).animate({width: '590px', backgroundColor: '#FFF'}, function(){
				//Now fade in the content
				$("#"+theBladeId+" .featProductLayer").fadeIn(function(){
					isAnimating = false;
				});
			});
			$(".kwickLinks").css("fontWeight", "normal");
			$(".kwickLinks[rel='"+whichBlade+"']").css("fontWeight", "bolder");
		}
	});
	
	$(".blade").hover(function(){
		if ($(this).css("width") == "19px")
			$(this).css("background", "#DBD6D1");
	}, function(){
		if ($(this).css("width") == "19px")
			$(this).css("background", "#B5A392");
	});

	//Modal Settings
	$("a.nyroModal").nyroModal({width: '650', height: '500', minWidth: '400', minHeight: '300', resizeable: 'false', autoSizable: 'false'});

	//Product page hover
	$(".productHover").tooltip({track: true, extraClass: "realnice", delay: 0, showURL: false, fade: 250 });
	
	//Compare
	$(".compareLink").hover(function(){
		var theId = $(this).attr("rel");
		var theContent = $("#"+theId).html();
		$("#showComparison").html(theContent);
		$("#showComparison *").fadeIn();
		$("#showComparison").corner();
	}, function(){
		var theId = $(this).attr("rel");
	});
	
	$(".askFindHover").hover(function(){
		var newSrc = $(this).attr("rel");
		$("#askYves").attr("src", newSrc);
	}, function(){
		$("#askYves").attr("src", "/media/images/interface/ask_find_yves.gif");
	});
	
	$("#productLayer").corner("round 8px").parent().css('padding', '2px').corner("round 10px");
	
	$("#soyFacts").corner();
	$("#showComparison").corner();
	
	$("#popClose").click(function(){
	    $("#popOverlay").fadeOut();
	    $("#jquery-overlay").fadeOut().remove();
    });
    
	$(".productMenu").click(function(){
		//The data is stored in hidden divs at the bottom of the page
		var theId = "#"+$(this).attr("rel")+"_data";
		var theHTML = $(theId).html();
		popOverlay("", theHTML);
	});
    
    //Accordian Type Stuff
    $(".accordion dd").hide();
    $(".accordion dt").click(function(){
		$(this).next("dd").slideToggle("slow").siblings("dd:visible").slideUp("slow");
		$(this).toggleClass("active");
		$(this).siblings("dd").removeClass("active");
	});
	//Check to see if the URL is anchored somewhere
    var urlString = document.location.toString();
	//Is there an anchor?
    if (urlString.match('#')) {
		var theAnchor = urlString.split('#')[1];
		$("[name='"+theAnchor+"']").parent().next("dd").slideToggle("slow").siblings("dd:visible").slideUp("slow");
		$("[name='"+theAnchor+"']").parent().toggleClass("active");
		$("[name='"+theAnchor+"']").parent().siblings("dd").removeClass("active");
	}

	//Checkboxes on product page
	$("#productTable a").each(function(){
		var theId = this.id;
		if ($("#"+theId).css("zIndex") == "69"){
			if (isFrench)
				var newSrc = $("#"+theId+" img").attr("src").replace("/images/ProductMenu_fr/images", "/images/ProductMenu_fr/checked");
			else
				var newSrc = $("#"+theId+" img").attr("src").replace("/media/images/product_page", "/media/images/product_page/checked");
			$("#"+theId+" img").attr("src", newSrc);
		}
	});
		
	$('.search_form').click(
		function(event){
			$('#overlay').fadeIn("slow"),
			$('#search_box').fadeIn("slow");
		}
	);
	$('#close_search').click(
		function(event){
			$('#overlay').fadeOut("slow"),
			$('#search_box').fadeOut("slow");
		}
	);
	
	$('#search_benefits_inner a').tooltip();
	
	$('.recipe_login').click(
		function(event){
			$('#login_overlay').fadeIn("slow"),
			$('#login_popup').fadeIn("slow");
		}
	);
	$('#close_login').click(
		function(event){
			$('#login_overlay').fadeOut("slow"),
			$('#login_popup').fadeOut("slow");
		}
	);
	
});

//PopOverlay
function popOverlay(title, HTMLContent, popWidth, popHeight){
	if (popWidth == "" || popWidth == undefined){
		popWidth = 670;
	}
	if (popHeight == "" || popHeight == undefined){
		popHeight = 400;
	}
	var halfHeight = popHeight / 2;
	var halfWidth = popWidth / 2;
	var totalWidth = $(window).height();
	var totalHeight = $(window).width();
	var bHeight = ($(window).height() / 2) - halfHeight; 
	var bWidth =  ($(window).width() / 2) - halfWidth;
	
	$("#popHead").html(title);
	$("#popBody").html(HTMLContent);
	
	// Style overlay and show it
	$('body').append('<div id="jquery-overlay"></div>');
	$('#jquery-overlay').css({
		backgroundColor:"#000",
		opacity:	"0.8",
		width:		"100%",
		height:		totalHeight
	}).fadeIn();
	$("#popOverlay").css({top: bHeight, left: bWidth, height: '400px'}); //Center the overlay
	$("#popOverlay").fadeIn(function(){
		var theHeight = 40; //40px spacer/header/borders etc
		var maxHeight = $(window).height() - 50; // make sure we don't butt up against the browser frame
		theHeight = theHeight + parseInt($("#popHead").height()) + parseInt($("#popBody").height());
		if (theHeight > popHeight){
			if (theHeight > maxHeight)
				var withPX = maxHeight+"px";
			else
				var withPX = theHeight+"px";
			$("#popOverlay").animate({height: withPX});
		}
	});

}

