﻿// JavaScript Document
$(document).ready(function(){
	
	//What buttons do we want?
	setupButton("products");
	setupButton("nutrition");
	setupButton("feeding");
	setupButton("development");
	
	//Setup the mouseout on all mainnav Items
	$(".main_nav > li > a").mouseover(function(){
		$(".subnav").hide();										
	});
});

function setupButton(area) {
	// On the Menu Buttons with sub navs - create hover
	$(".main_nav_" + area).hover(
	  function (e) {
		  
			//Show the specificed sub nav
			$("#subnav-" + area).show()
			
	  }, 
	  function (e) {
			
			//Get the position on this element
			posArray = findPos(this);
			
			//Get the Vertical Position of the mouse relative to the current menu block
			vertical = (e.pageY - posArray[1])
			
			//If the position of the mouse is more than 35 pixels south that must mean that the user
			//is leaving the block and heading to the submenu. Is it isn't over 35. That means the user
			//is leaving to another top menu item and we should turn off the subnav.
			if( vertical < 36 )
			{
				//setTimeout(function(){ 
					//if(!$("#subnav-" + area).hasClass("over"))
						$(".subnav").hide();
				//},100);
			}
		}
	);
	
	//On mouse over for the submenu
	$("#subnav-" + area).hover(
	  function () {
			//Nothing to do
	  },
	  function (e) {
			
			//Get the position on this element
			posArray = findPos(this);
			
			//Get the Vertical Position of the mouse relative to the current menu block
			vertical = (e.pageY - posArray[1])
			
			//If the user moves the mouse north and it goes back to the top menu (mouse pos of -1px) element pertaining to 
			//the subnav. Don't do anything. but if they mouse out going south or left or right. Turn it off!
			if( vertical > 0 )
			{
			//setTimeout(function(){
			//	if(!$(".main_nav_" + area).hasClass("over"))				
					$(".subnav").hide();
			//},100); 
			}
	  } 
	);

}

//Find the relative position to the current element, not the root or parent. To the element itself.
function findPos(obj){
	var posX = obj.offsetLeft; 
	var posY = obj.offsetTop;
	// Loop through all the parents until you reach the body tag.
	while(obj.offsetParent){
		//Keep looping till the break
		if(obj==document.getElementsByTagName('body')[0]){break}
		//Grab the values from the parrent.
		else{
			posX=posX+obj.offsetParent.offsetLeft;
			posY=posY+obj.offsetParent.offsetTop;
			obj=obj.offsetParent;
		}
	}
	//Set them in an array and return.
	var posArray=[posX,posY]
	return posArray;
}


// Coming Soon!
//Include cookie jQuery plugin
document.write('<script type="text/javascript" src="/js/jquery.cookies.js"></script>');

//OnLoad Coming Soon banner
/*
$(document).ready(function(){
	//If the coming soon image doesn't have a cookie set. Show the image.
	if($.cookie('hideComingSoon') != "true" || document.getElementById("mid_row")) 
	{
		
		//Create the coming soon div. and stick it in the top of the html right after the body tag.
		$("#allcontain").prepend("<div id='coming-soon'></div>");
		
		if(window.location.pathname.match(/fr/))
		{
			var so = new SWFObject("/flash/fr/comming-soon.swf", "growth", "920", "80", "7", "#FFFFFF");
		}
		else
		{
			var so = new SWFObject("/flash/en/comming-soon.swf", "growth", "920", "80", "7", "#FFFFFF");
		}
		so.addParam("quality", "high");
		so.addParam("wmode", "transparent");
		so.write("coming-soon");
		
		$("#coming-soon").prepend("<a href=''></a>");
		
		//Create a cookie to hide the banner if clicked on.
		$("#coming-soon a").click( function(){
				//Hide the banner.
				$("#coming-soon").hide();
				//Make a domain level cookie.
				$.cookie('hideComingSoon', "true", { expires: 7 , path: '/'}) 
				return false;
		});
	}
});
*/

//Search Box functionality
$(document).ready(function(){
	if(window.location.pathname.match(/fr/) || window.location.pathname.match(/_f.pl/))
	{
		$("#searchfield").val("recherche");
	} 
	else
	{
		$("#searchfield").val("search");
	}
	$("#searchfield").click( function(){ $("#searchfield").val(""); });

});

//Add hidden form element
$(document).ready(function(){
	$("#frm_newsletter").append("<input type='hidden' name='badges' value='true' />");
	$("input[type='radio']").attr("style","border:0px solid white;");
	$("input[type='checkbox']").attr("style","border:0px solid white;");
	$("input[type='submit']").attr("style","background-color:#999999; color:white;");
});