MallPage.jQuery(function() {
	PositionNavigationMenu();
});
function PositionNavigationMenu()
{
	var $navMenu = MallPage.jQuery("#headerNavMenu");
	if(!$navMenu.length) return;
	var $navMenuItems = $navMenu.children("li.MenuItem"),
		maxwidth = parseInt($navMenu.attr("maxwidth"), 10),
		leftjustify = $navMenu.attr("leftjustify").toLowerCase() === "true",
		navMenuOffset = $navMenu.offset();
	$navMenuItems.hover(
		function(){
			var $this = MallPage.jQuery(this);
			$this.addClass("Hover");
		},
		function(){
			var $this = MallPage.jQuery(this);
			$this.removeClass("Hover");
		}
	);
	$navMenuItems.each(function(){
		var $this = MallPage.jQuery(this),
			$dropDownArrow = $this.children("img.dropdownArrow"),
			$dropDown = $this.children("div.dropdown"),
			$sections = $dropDown.children("div.section"),
			menuWidth = 0,
			dropDownOffset = $this.offset();
		if($dropDownArrow.length > 0)
		{
			$dropDownArrow.css("top", dropDownOffset.top + $this.height() + 2).css("left", dropDownOffset.left + ($this.width()/2) - ($dropDownArrow.width()/2));
		}
		$sections.each(function(){
			var sectionWidth = 0, entryHeight = 0, titleWidth = 0, alternateTeam = false,
				$section = MallPage.jQuery(this),
				$entries = $section.children("div.entry");
			$entries.each(function(){
				var entryWidth = 0, h,
					$entry = MallPage.jQuery(this),
					$columns = $entry.children("ul.column");
				if($entry.hasClass("altteams"))
				{
					var $altDivs = $entry.children("div.alt");
					$altDivs.each(function(){
						var altDivWidth = 0,
							$altDiv = MallPage.jQuery(this),
							$columns = $altDiv.children("ul.column");
						titleWidth = $altDiv.children("div.title").width();
						alternateTeam = true;
						$columns.each(function(){
							var columnWidth = 0, innerWidth = 0,
								$column = MallPage.jQuery(this),
								$items = $column.children("li");
							$items.each(function(){
								var $item = MallPage.jQuery(this),
									itemOuterWidth = $item.outerWidth(),
									itemWidth = $item.width();
								if(innerWidth < itemWidth) innerWidth = itemWidth;
								if(columnWidth < itemOuterWidth) columnWidth = itemOuterWidth;
							});
							$items.width(innerWidth);
							altDivWidth += columnWidth + 1;
						});
						if(entryWidth < altDivWidth) entryWidth = altDivWidth + 1;
					});
					$altDivs.width(entryWidth);
				}
				else
				{
					$columns.each(function(){
						var columnWidth = 0, innerWidth = 0,
							$column = MallPage.jQuery(this),
							$items = $column.children("li");
						$items.each(function(){
							var $item = MallPage.jQuery(this),
								itemOuterWidth = $item.outerWidth(),
								itemWidth = $item.width();
							if(innerWidth < itemWidth) innerWidth = itemWidth;
							if(columnWidth < itemOuterWidth) columnWidth = itemOuterWidth;
						});
						$items.width(innerWidth);
						entryWidth += columnWidth + 1;
					});
					titleWidth = $entry.children("div.title").width();
				}
				sectionWidth += entryWidth + 1;
				$entry.width(entryWidth);
				h = $entry.height();
				if(entryHeight < h) entryHeight = h;
			});
			$entries.height(entryHeight);
			if(menuWidth < sectionWidth) menuWidth = sectionWidth + 1;
			if(titleWidth > sectionWidth)
			{
				sectionWidth = titleWidth + 1;
				if(alternateTeam)
				{
					$section.children("div.altteams").width(sectionWidth);
					$section.children("div.altteams").children("div.alt").width(sectionWidth);
				}
			}
			$section.width(sectionWidth);
		});
		$dropDown.width(menuWidth);
		
		var dropDownWidth = $dropDown.outerWidth(),
			dropDownHeight = $dropDown.outerHeight(),
			dropDownLeftOffset = dropDownOffset.left,
			dropDownTopOffset = dropDownOffset.top + $this.height();
		dropDownLeftOffset = dropDownLeftOffset + ($this.width() / 2) - (dropDownWidth / 2);
		if(dropDownLeftOffset < navMenuOffset.left)
		{
			dropDownLeftOffset = navMenuOffset.left;
		}
		if (dropDownLeftOffset + dropDownWidth > navMenuOffset.left + maxwidth)
		{
			dropDownLeftOffset = navMenuOffset.left + maxwidth - dropDownWidth;
		}
		if (leftjustify && dropDownLeftOffset < navMenuOffset.left)
		{
			dropDownLeftOffset = navMenuOffset.left;
		}
		
		var $iframes = $this.children("iframe");
		if($iframes.length)
		{
			$iframes.width(dropDownWidth).height(dropDownHeight).css("top", dropDownTopOffset).css("left", dropDownLeftOffset);
		}
		$dropDown.css("top", dropDownTopOffset).css("left", dropDownLeftOffset);
	});
		
	$navMenuItems.mouseenter(function(){
		var $this = MallPage.jQuery(this);
		$this.children("iframe,img.dropdownArrow,div.dropdown").css("visibility", "visible");
	});
	$navMenuItems.mouseleave(function(){
		var $this = MallPage.jQuery(this);
		$this.children("iframe,img.dropdownArrow,div.dropdown").css("visibility", "hidden");
	});
}

function BuildNavigationMenuFromJS ()
{
	//check for existence of the tab data and the menu div before processing.
	if (typeof(ttgMenusV2) == 'undefined' || typeof(ttgMenusV2.Menus) == 'undefined')
	{
		window.setTimeout(BuildNavigationMenuFromJS, 100);
		return;
	}
	
	// Get each of the required elements.
	var ttgDivMenu = document.getElementById("headerNavMenu");
	
	if (typeof(ttgDivMenu) == 'undefined')
	{
		window.setTimeout(BuildNavigationMenuFromJS, 100);
		return;
	}
	
	MallPage.jQuery("#headerNavMenu").html(ttgMenusV2.Menus);
	
	PositionNavigationMenu();
}

function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent)
	{
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent)
		{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	var retval = new Array();
	retval[0] = curleft;
	retval[1] = curtop;
	return retval;
}

var PositionHoverHyperlinkGroups = function(selector){
	var host = MallPage.jQuery(selector);
	host.each(function () {
		var $this = MallPage.jQuery(this);
		//Position the drop down arrow
		var $dropDownArrow = $this.children("img.Arrow");
		if($dropDownArrow.length > 0)
		{
			//Add 2 to top for the border of the menu.
			$dropDownArrow.css("top", $this.offset().top + $this.height() + 2).css("left", $this.offset().left + ($this.width()/2) - ($dropDownArrow.width()/2));
		}
		var $listContainer = $this.children("div");
		var $list = $listContainer.children("ul");
		$listContainer.width($list.width());
		
		//Size the Iframe to match the list, add 4 to width for borders, add 4 to height for borders
		$this.children("iframe")
			.width($list.width() + 4)
			.height($listContainer.height() + 4);
		
		var leftOffset = $this.offset().left + ($this.width() / 2) - ($listContainer.width() / 2);
		//Position the linkList and Iframe just below the tab, add borders (if exists) to height.
		$this.children("iframe,div")
			.css("top", $this.offset().top + $this.height())
			.css("left", leftOffset);
		});
	host.mouseenter(function () {
		var $this = MallPage.jQuery(this);
		$this.children("iframe,div,img.Arrow")
			.css("visibility", "visible")
			.css("visibility", "visible");
		});
	host.mouseleave(function () {
		var $this = MallPage.jQuery(this);
		$this.children("iframe,div,img.Arrow")
			.css("visibility", "hidden")
			.css("visibility", "hidden");
		});

};
