/*
 * Supports TabGroup.ascx
 */ 
$(function() {
	PositionTabGroupMenus();
});

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;
		//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 $dataParent = spiderMenuDiv.children("div.menuData,#menuData").children(".spiderMenuPersonGroupTable:first");
		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();
			columns++;
		});
		if (width > 0)
		{
			//Add 24 for the margins either side
			spiderMenuDiv.children("div.menuData,#menuData").width(width + columns + 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 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();
}

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;
}