/*
 * Supports TabGroup.ascx
 */ 
$(function() {
	if(typeof(ttgv2) == 'undefined' || !ttgv2){PositionTabGroupMenus();}
	else{PositionTabGroupMenus2();}
});

function PositionTabGroupMenus()
{
	var tabGroupHost = $("div.topTabGroupHost");
	tabGroupHost.find("div.spiderMenuItemRow").hover(function () {
		$(this).addClass("spiderMenuItemRowOver")
		$(this).removeClass("spiderMenuItemRow");
		},
		function () {
		$(this).addClass("spiderMenuItemRow");
		$(this).removeClass("spiderMenuItemRowOver");
		});
	var tabGroupHostChildDivs = tabGroupHost.children("div");
	tabGroupHostChildDivs.each(function () {
		var $this = $(this);
		var spiderMenuDiv = $this.children("div.spiderMenuDiv");
		//The width needs to be set so IE6 doesn't put everything into a single column. (IE6 doesn't expand a DIV to include
		//floated elements)
		var width = 0;
		//Department only drop downs do not have a spiderMenuPersonGroupTable, so use the spiderMenuDeptTable.
		var $dataParent = spiderMenuDiv.children("div.menuData,#menuData").children(".spiderMenuPersonGroupTable");
		if($dataParent.length == 0)
		{
			$dataParent = spiderMenuDiv.children("div.menuData,#menuData").children(".spiderMenuDeptTable:first");
			//If there still are no items, check the child dept table
			if($dataParent.length == 0)
			{
				$dataParent = spiderMenuDiv.children("div.menuData,#menuData").children(".spiderMenuChildDeptTable:first");
			}
			$dataParent.children(".spiderMenuColumn").each(function () {
				width += $(this).width() + 1;
			});
		}
		else
		{
			//Calc width of each table, using the largest width.
			var largestWidth = 0;
			$dataParent.each(function(){
				var teamTableWidth = 0;
				var $parent = $(this);
				var $teamTableColumn = $parent.children("div.spiderMenuColumn");
				$teamTableColumn.each(function(){
					//Add 1 for the border between each column.
					teamTableWidth += $(this).width() + 1;
				});
				if(largestWidth < teamTableWidth)
				{
					largestWidth = teamTableWidth;
				}
			});
			width = largestWidth;
		}
		if (width > 0)
		{
			//Add 24 for the margins either side
			spiderMenuDiv.children("div.menuData,#menuData").width(width + 24);
		}
		
		//Determine the left offset, if the spiderMenuDiv would be wider than the active page width, right justify.
		//This must be done after setting the width of div.spiderMenuDiv so that IE6 doesn't think the width is still small.
		var leftOffset = $this.offset().left;
		if (spiderMenuDiv.width() + $this.offset().left > tabGroupHost.offset().left + activePageWidth)
		{
			//Subtract 5 for the borders
			leftOffset = tabGroupHost.offset().left + activePageWidth - spiderMenuDiv.width() - 5;
		}
		//If offset puts the left edge less than the top tab group main div left offset, left justify.
		if (leftOffset < tabGroupHost.offset().left && requireLeftJustify)
		{
			leftOffset = tabGroupHost.offset().left;
		}
		
		//Size the Iframe to match the spiderMenuDiv, add 4 to width for borders, add 4 to height for borders
		$this.children("iframe.spiderMenuIframe")
			.width(spiderMenuDiv.width() + 4)
			.height(spiderMenuDiv.height() + 4);
		
		//Position the spiderMenuDiv and Iframe just below the tab, add borders (if exists) to height.
		$this.children("iframe.spiderMenuIframe,div.spiderMenuDiv")
			.css("top", $this.offset().top + $this.height())
			.css("left", leftOffset);
		});
	tabGroupHostChildDivs.mouseenter(function () {
		var $this = $(this);
		$this.addClass($this.attr("css"));
		$this.children("iframe.spiderMenuIframe,div.spiderMenuDiv")
			.css("visibility", "visible")
			.css("visibility", "visible");
		});
	tabGroupHostChildDivs.mouseleave(function () {
		var $this = $(this);
		$this.removeClass($this.attr("css"));
		$this.children("iframe.spiderMenuIframe,div.spiderMenuDiv")
			.css("visibility", "hidden")
			.css("visibility", "hidden");
		});
}
function PositionTabGroupMenus2()
{
	var tabGroupHost = $("#" + tabGroup.Options.hostElementId);
	tabGroupHost.find("div." + tabGroup.Options.linkCss).hover(function () {
		$(this).addClass(tabGroup.Options.linkCssHover)
		$(this).removeClass(tabGroup.Options.linkCss);
		},
		function () {
		$(this).addClass(tabGroup.Options.linkCss);
		$(this).removeClass(tabGroup.Options.linkCssHover);
		});
	var tabGroupHostChildDivs = tabGroupHost.children("div");
	tabGroupHostChildDivs.each(function () {
		var $this = $(this);
		//Position the drop down arrow
		var $dropDownArrow = $this.children("img." + tabGroup.Options.arrowImageCss);
		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 spiderMenuDiv = $this.children("div." + tabGroup.Options.primaryDivCss);
		//The width needs to be set so IE6 doesn't put everything into a single column. (IE6 doesn't expand a DIV to include
		//floated elements)
		var width = 0;
		//Start columns at 1, this gives 2 extra pixels based on number of columns (ex: if there is 1 column, there are 2 pixels added to width - 2 cols, 3 pixels)
		var columns = 1;
		//Department only drop downs do not have a spiderMenuPersonGroupTable, so use the spiderMenuDeptTable.
		var $teamParent = spiderMenuDiv.children("div.menuData,#menuData").children("div." + tabGroup.Options.containerCss);
		if($teamParent.length > 0)
		{
			var $teamTables = $teamParent.children("div." + tabGroup.Options.teamContainerCss);
			var teamTableCount = $teamTables.length;
			//Add width for padding on the entire table + border between each table.
			width = 10 + ((teamTableCount - 1) * 15);
			//Add width of each column + border of each column in each team container
			var largestStackedWidth = 0;
			var numStacked = 0;
			$teamTables.each(function(){
				var teamTableWidth = 0;
				var teamHeaderWidth = 0;
				var $this = $(this);
				var $header = $this.children("div." + tabGroup.Options.headerCss);
				teamHeaderWidth = $header + 4;
				var $teamTable = $this.children("div." + tabGroup.Options.teamTableCss);
				$teamTable.children("div." + tabGroup.Options.columnCss).each(function () {
					teamTableWidth += $(this).width() + 11;
				});
				if(teamHeaderWidth > teamTableWidth)
				{
					teamTableWidth = teamHeaderWidth;
				}
				//Explicitly set the header width for IE6
				$header.width(teamTableWidth - 4);
				$teamTable.width(teamTableWidth);
				if($this.hasClass(tabGroup.Options.teamContainerCss + "Stacked"))
				{
					numStacked++;
					if(largestStackedWidth < teamTableWidth)
					{
						largestStackedWidth = teamTableWidth;
					}
				}
				else
				{
					width += teamTableWidth;
				}
			});
			width += largestStackedWidth;
			if(numStacked > 0)
			{
				width -= ((numStacked - 1) * 15 + 10);
				var $teamTablesStacked = $teamParent.children("div." + tabGroup.Options.teamContainerCss + "Stacked");
				$teamTablesStacked.each(function(){
					var $this = $(this);
					var $teamTable = $this.children("div." + tabGroup.Options.teamTableCss);
					if($teamTable.width() < largestStackedWidth)
					{
						$this.css("margin-right", (largestStackedWidth - $teamTable.width()) / 2);
					}
				});
			}
			$teamParent.width(width);
		}
		var deptWidth = 0;
		var $deptParent = spiderMenuDiv.children("div.menuData,#menuData").children("div." + tabGroup.Options.departmentContainerCss);
		$deptParent.find("div." + tabGroup.Options.columnCss).each(function () {
			deptWidth += $(this).width() + 11;
		});
		$deptParent.width(deptWidth);
		if(deptWidth > width)
		{
			width = deptWidth;
		}
		if (width > 0)
		{
			//Add 4 for the border
			spiderMenuDiv.children("div.menuData,#menuData").width(width + 4);
		}
		
		//Determine the left offset, if the spiderMenuDiv would be wider than the active page width, right justify.
		//This must be done after setting the width of div.spiderMenuDiv so that IE6 doesn't think the width is still small.
		var leftOffset = $this.offset().left;
		if (spiderMenuDiv.width() + $this.offset().left > tabGroupHost.offset().left + activePageWidth)
		{
			//Subtract 5 for the borders
			leftOffset = tabGroupHost.offset().left + activePageWidth - spiderMenuDiv.width() - 5;
		}
		//If offset puts the left edge less than the top tab group main div left offset, left justify.
		if (leftOffset < tabGroupHost.offset().left && requireLeftJustify)
		{
			leftOffset = tabGroupHost.offset().left;
		}
		
		//Size the Iframe to match the spiderMenuDiv, add 4 to width for borders, add 4 to height for borders
		$this.children("iframe." + tabGroup.Options.iframeCss)
			.width(spiderMenuDiv.width() + 4)
			.height(spiderMenuDiv.height() + 4);
		
		//Position the spiderMenuDiv and Iframe just below the tab, add borders (if exists) to height.
		$this.children("iframe." + tabGroup.Options.iframeCss + ",div." + tabGroup.Options.primaryDivCss)
			.css("top", $this.offset().top + $this.height())
			.css("left", leftOffset);
		});
	tabGroupHostChildDivs.mouseenter(function () {
		var $this = $(this);
		$this.addClass($this.attr("css"));
		$this.children("iframe." + tabGroup.Options.iframeCss + ",div." + tabGroup.Options.primaryDivCss + ",img." + tabGroup.Options.arrowImageCss)
			.css("visibility", "visible")
			.css("visibility", "visible");
		});
	tabGroupHostChildDivs.mouseleave(function () {
		var $this = $(this);
		$this.removeClass($this.attr("css"));
		$this.children("iframe." + tabGroup.Options.iframeCss + ",div." + tabGroup.Options.primaryDivCss + ",img." + tabGroup.Options.arrowImageCss)
			.css("visibility", "hidden")
			.css("visibility", "hidden");
		});
}

function HandleLeagueTab(e, url)
{	
	var element = e.target || e.srcElement;
	var elementClass = element.className;
	if(elementClass.indexOf("TabMenuItem") >= 0)
	{
		window.location = url;
	}
}

/*
Builds the top tab group from TopTabGroupData.js 
*/
function BuildNormalizedTopTabGroup ()
{
    //check for existence of the tab data and the menu div before processing.
	if ( typeof(tabGroupDivID) == 'undefined' ||
	 	 typeof(ttgMenus) == 'undefined')
	{
		window.setTimeout(BuildNormalizedTopTabGroup, 100);
		return;
	}

	// Get each of the required elements.
	var ttgDivMenu = document.getElementById(tabGroupDivID);
	
	if ( typeof(ttgDivMenu) == 'undefined' )
	{
		window.setTimeout(BuildNormalizedTopTabGroup, 100);
		return;
	}
	
	var i = 0;
	for(i = 0; i < ttgMenus.length; i++)
	{
		$("#" + tabGroupDivID).children("div.Item" + i.toString()).html(ttgMenus[i]);
	}
	
	PositionTabGroupMenus();
}
/*
Builds the top tab group from TopTabGroupDataV2.js 
*/
function BuildNormalizedTopTabGroupV2 ()
{
    //check for existence of the tab data and the menu div before processing.
	if ( typeof(tabGroupDivID) == 'undefined' ||
	 	 typeof(ttgMenusV2) == 'undefined')
	{
		window.setTimeout(BuildNormalizedTopTabGroupV2, 100);
		return;
	}

	// Get each of the required elements.
	var ttgDivMenu = document.getElementById(tabGroupDivID);
	
	if ( typeof(ttgDivMenu) == 'undefined' )
	{
		window.setTimeout(BuildNormalizedTopTabGroupV2, 100);
		return;
	}
	
	var i = 0;
	for(i = 0; i < ttgMenusV2.length; i++)
	{
		$("#" + tabGroupDivID).children("div.ItemV2" + i.toString()).html(ttgMenusV2[i]);
	}
	
	PositionTabGroupMenus2();
}

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;
}