var timeout = null;
var subMenuOn = false;
var subMenus = new Array(3);
subMenus[0] = [ new menuItem('Team Settings', 'teaminfo.aspx'), new menuItem('Rename Team', 'teaminfo.aspx'), new menuItem('Join League', 'leaguejoin.aspx'), new menuItem('Manage League', 'manageleague.aspx'), new menuItem('Delete Team', 'teamdrop.aspx') ];
subMenus[1] = [ new menuItem('Weekly Standings (Detailed)', 'standings.aspx?report=weekly&detail=detailed'), new menuItem('Weekly Standings (Basic)', 'standings.aspx?report=weekly&detail=basic'), new menuItem('Season Standings (Detailed)', 'standings.aspx?report=season&detail=detailed'), new menuItem('Season Standings (Basic)', 'standings.aspx?report=season&detail=basic') ];
subMenus[2] = [ new menuItem('E-mail Us', 'contact.aspx'), new menuItem('Frequently Asked Questions', 'faq.asp'), new menuItem('Take the Tour', 'tour.aspx') ];

function hoverMenu(menu, on)
{
	if (on)
	{
	    switch (menu.id)
	    {
	        case 'menuLeauges':
	            showSubMenu(menu, subMenus[0]);	            
	            break;
	            
            case 'menuStandings':
	            showSubMenu(menu, subMenus[1]);
	            break;
	            
            case 'menuContact':
	            showSubMenu(menu, subMenus[2]);
	            break;
	            
	        default:
	            if (subMenuOn)
	                hideSubMenu();
	            break;
	    }
	    
		menu.className = 'Hover';
	}
	else
	{
	    if (menu.id != '' && subMenuOn)
	        timeoutBegin();
	    
    	menu.className = '';
    }
}

function showSubMenu(parentMenu, menuItems)
{
    timeoutEnd();

    var parentPos = getPosition(parentMenu);
    var subMenu = document.getElementById('subMenu');
    subMenu.innerHTML = '';
    
    for (var i = 0; i < menuItems.length; i++)
    {
        var itemLink = document.createElement('a');
        itemLink.setAttribute('href', menuItems[i].url);
        itemLink.innerHTML = menuItems[i].text;
        itemLink.onmouseover = function() { timeoutEnd() };
        itemLink.onmouseout = function() { timeoutBegin() };
        
        subMenu.appendChild(itemLink);
    }

    subMenu.style.top = parentPos.y + parentMenu.clientHeight + 2 + 'px';
    subMenu.style.left = parentPos.x - 2 + 'px';
    subMenu.style.visibility = 'visible';
    
    subMenuOn = true;
}

function hideSubMenu()
{
    timeoutEnd();
    subMenuOn = false;

    document.getElementById('subMenu').style.visibility = 'hidden';
}

function timeoutBegin()
{
    timeout = setTimeout(hideSubMenu, 200);
}

function timeoutEnd()
{
    if (timeout != null)
        clearTimeout(timeout);
}

function menuItem(text, url)
{
    this.text = text;
    this.url = url;
}

function popOn(e, text)
{
	var divGamePopup = document.getElementById("divGamePopup");
	
	document.getElementById("divGamePopupText").innerHTML = text;

	divGamePopup.style.left = e.offsetLeft + e.offsetParent.offsetLeft;
	divGamePopup.style.top = e.offsetParent.offsetTop - divGamePopup.clientHeight;
	divGamePopup.style.visibility = "visible";
}

function popOff()
{
	document.getElementById("divGamePopup").style.visibility = "hidden";
}

function getPosition(element)
{
	var parentElement = element;
	var x = 0;
	var y = 0;
	
	while (parentElement != null)
	{
		x += parentElement.offsetLeft;
		y += parentElement.offsetTop;
		parentElement = parentElement.offsetParent;
	}
	
	this.x = x;
	this.y = y;

	return this;
}

function showBalloon(element, text)
{
	var divBalloon = document.getElementById('divBalloon');
	var divBalloonText = document.getElementById('divBalloonText');
	var element = document.getElementById(element);
	var pos = getPosition(element);
	
	divBalloon.style.visibility = 'hidden';

	if (element != null)
	{
		divBalloonText.innerHTML = text;
		divBalloon.style.left = pos.x - (divBalloon.clientWidth - element.clientWidth) / 2;
		divBalloon.style.top = pos.y - divBalloon.clientHeight;
		divBalloon.style.visibility = 'visible';
	}
}

function hideBalloon()
{
	document.getElementById('divBalloon').style.visibility = 'hidden';
}

function expandCollapseMessage(id, show)
{
	message = document.getElementById(id);
	
	if (message != null)
		if (show)
			message.style.display = "inline";
		else
			message.style.display = "none";
}
