$(document).ready(function() {
	$("#menuContainer").css("display", "block"); //Display the menu after DOM is ready
	var userAgentSelector = navigator.userAgent.toLowerCase();
	var isIE6 = userAgentSelector.indexOf("msie 6") > -1;

	//iPhone Mega Drop Down Fix
	var dropMenuTrigger = "false";
	if((navigator.platform == "iPhone")||(navigator.platform == "iPad")) 
	{ 					
		$("li.topLevel a.menuLink.hasSubLevels").click(function (e) 
		{ 
			var hasSubMenu = $(this).find("div.subMenu").html();	
			if ((dropMenuTrigger=="false"))
				{
					e.preventDefault();
									
					var homePageLeft = $("li.homeTabSelector").offset(); //If this is null homeSelected should be there
					var homePageLeftPosition = homePageLeft.left;
						
					var leftOffset = $(this).offset();
					var leftPostition = leftOffset.left - homePageLeftPosition;
						
					$(this).parents('li').find("div.subMenu").clearQueue().delay(500).css("left", -leftPostition).slideDown(300, function(){
						dropMenuTrigger = "true";	
					});
				}
		});
			
			
		$("div.container").click(function(){
			$("div.subMenu").css("display", "none");
			dropMenuTrigger = "false";	
		});
			
			
		//Expand hidden menu items
		$("li.moreMenuItems").click(function() {							
			$(this).find("div.subMenu").delay(500).slideDown(300);			
		});	
		     
   }
	
	
	//Main Menu Items Navigation	
	$("li.topLevel").mouseenter(function() 
	{
	
		//If IE6
		if(isIE6==true)
			{
				$(this).addClass("listHoverFix");
			}
				
		var hasSubMenu = $(this).find("div.subMenu").html();
		if (hasSubMenu != null)	
			{
				$(this).addClass("thisMenuHighlighted");
				var homePageLeft = $("li.homeTabSelector").offset(); //If this is null homeSelected should be there
				var homePageLeftPosition = homePageLeft.left;
				
				var leftOffset = $(this).offset();
				var leftPostition = leftOffset.left - homePageLeftPosition;
				
				
				var dropMenuHeight = $(this).find("div.subMenu").height();
				var dropMenuTopPosition = $("#menu").offset();
				var dropDownRange = dropMenuTopPosition.top + dropMenuHeight +50;
				var videoPosition = "";

					//Hiding Objects, Dropdown when mega menu is activated
					hidingDropDown("object", dropDownRange, "hide");
					hidingDropDown("embed", dropDownRange, "hide");
					if(isIE6==true)
						{
							hidingDropDown("select", dropDownRange, "hide");
						}
					
				$("div.subMenu").removeClass("thisMenuHighlighted");				
				$(this).find("div.subMenu").addClass("thisMenuHighlighted").clearQueue().delay(500).css("left", -leftPostition).slideDown(300);

			}
		
	});
	
	$("li.topLevel").mouseleave(function() 
	{
		//If IE6
		if(isIE6==true)
			{
				$(this).removeClass("listHoverFix");
			}
	
		var hasSubMenu = $(this).find("div.subMenu").html();
		if (hasSubMenu != null)
			{
				//Display Hidden Objects and Dropdowns
				$("#contentPlaceHolder").find("object").css("visibility", "visible");
				$("#contentPlaceHolder").find("embed").css("visibility", "visible");
				if(isIE6==true)
					{
						$("#contentPlaceHolder").find("select").css("visibility", "visible");
					}
			
				$("div.subMenu").removeClass("thisMenuHighlighted");
				$(this).removeClass("thisMenuHighlighted");
				
				$(this).find("div.subMenu").removeClass("thisMenuHighlighted").clearQueue().delay(500).hide(1);
				
			}
	});	
	
	
		//Hide Over flow menu items
		var newWidth = "" * 1;
		$("ul.main").last().append("<li class='moreMenuItems' style='width: 67px; display:none;'><a class='hasSubLevels' href='#'>More</a><div class='subMenu'><div class='column'><ul class='moreMenuItemsUl'></ul></div></div></li>");
		
		//Overflow items identifing and moving such links to seperate container
		$("li.topLevel").each(function(index) 
		{	
			var width = $(this).width() * 1;
			newWidth = newWidth + width;
			if (newWidth > 852) 
			{
				$(this).find("div.arrowLayer").remove();
				$(this).find("div.subMenu").remove();
				var getLinkHtml = $(this).html();
			
				$(this).remove();
				
				$("li.moreMenuItems").css("display", "");
				$("li.moreMenuItems").find("ul.moreMenuItemsUl").append("<li>"+getLinkHtml+"</li>");
			}
			
		});

		//Expand hidden menu items
		$("li.moreMenuItems").mouseenter(function() {
		
			//If IE6
			if(isIE6==true)
			{
				$(this).addClass("listHoverFix");
			}
			
			$(this).find("div.subMenu").clearQueue().delay(500).slideDown(300);			
		});	
		
		$("li.moreMenuItems").mouseleave(function() {
		
			//If IE6
			if(isIE6==true)
			{
				$(this).removeClass("listHoverFix");
			}
			
		$(this).find("div.subMenu").clearQueue().delay(500).hide(1);
		});
		
		
		//Submenu mouse enter / mouse leave
		$("div.subMenu").mouseenter(function() {
			$(this).parent().addClass("thisMenuHighlighted");
			$(this).addClass("thisMenuHighlighted");
			$(this).css("display", "block");

			//Object, Drop down hide function
			var dropMenuHeight = $(this).height();
			var dropMenuTopPosition = $("#menu").offset();
			var dropDownRange = dropMenuTopPosition.top + dropMenuHeight +50;
				
			hidingDropDown("object", dropDownRange, "hide");
			hidingDropDown("embed", dropDownRange, "hide");
			if(isIE6==true)
				{
					hidingDropDown("select", dropDownRange, "hide");
				}			
		});	
		
		$("div.subMenu").mouseleave(function() {
		if($(this).parent().hasClass("thisMenuHighlighted")!=true)
			{
			$(this).delay(500).hide(1);
			}
			
		if($(this).parent().hasClass("moreMenuItems")==true) {
			$(this).parent().removeClass("thisMenuHighlighted");
			}
			
		//Display Hidden Objects and Dropdowns
		$("#contentPlaceHolder").find("object").css("visibility", "visible");
		$("#contentPlaceHolder").find("embed").css("visibility", "visible");
		if(isIE6==true)
			{
				$("#contentPlaceHolder").find("select").css("visibility", "visible");
			}

		});
		
	
		//Append the Leaf Node Menu Items to the Container
		if($("#pageMenuContainer").html() != null) 
			{
				$("<ul class='pageMenuHiddenSecondRow'></ul>").insertAfter("#pageMenuHidden");
			
				var leafNodeHtml = $(this).find("#pageMenu").html();
				$("#pageMenuContainer").remove();
				$("#pageMenuHidden").html("").append(leafNodeHtml).css("display", "block");
				$(".pageMenuHiddenSecondRow").html(""); //Clearing the second row - Depatureboard fix
				
				//Leaf Node
				var newLeafNodeWidth = "" * 1;
				var elems = $("#pageMenuHidden li").nextAll();
				$("#pageMenuHidden li").each(function(index) 
				{						
					var leafNodeWidth = $(this).width() * 1;
					newLeafNodeWidth = newLeafNodeWidth + leafNodeWidth;
					if (newLeafNodeWidth > 950) 
					{			
						var getLinkHtml = $(this).html();
						var selectState = $(this).attr("class");
						$(this).remove();					
						$("ul.pageMenuHiddenSecondRow").append("<li class='"+selectState+"'>"+getLinkHtml+"</li>");
						
							if (index==elems.length) 
							{
								$(".pageMenuHiddenSecondRow").css("display", "block");
							}
					}					
				});
				
								
			}

			
			//IE Home Highlight
			$("li.homeTab").mouseenter(function() {		
				if(isIE6==true)
				{
					$(this).addClass("listHoverFix");
				}					
			});
			
			$("li.homeTab").mouseleave(function() {		
				if(isIE6==true)
				{
					$(this).removeClass("listHoverFix");
				}				
			});
			
			
		//Function For Hide and show object, dropdown
		function hidingDropDown(objectType, str, hideShow) 
			{
				var objectPosition = "";
				if(hideShow=="hide")
					{
						$("#contentPlaceHolder").find(objectType).each(function (i) 
							{
								objectPosition = $(this).offset();
								if(objectPosition.top < str) 
									{
										$(this).css("visibility", "hidden");
									}
								else if (objectPosition.top > str) 
									{
										$(this).css("visibility", "visible");
									}
							});
					}
						
				/*
				//For Developer Referance
				else if(hideShow=="show")
					{
						$("#contentPlaceHolder").find(objectType).css("display", "block");
					}
				*/
			}
			
			
			//Home Menu Arrow hover bug fix
			$("li.homeTab").find(".arrowLayer").mouseover(function() {	
				if(isIE6==false) {
				$("li.homeTab").addClass("homeTabArrow");	
				}
			});
			
			$("li.homeTab").find(".arrowLayer").mouseleave(function() {		
				if(isIE6==false) {
				$("li.homeTab").removeClass("homeTabArrow");
				}
			});
			
});


