var IE = document.all?true:false;
var CurrentSubMenu = null;
var CurrentMenuId = -1;
var MouseX = 0;
var MouseY = 0;

if (!IE)
{
    document.captureEvents(Event.MOUSEMOVE);
}

document.onmousemove = getMouseXY;

var tempX = 0;
var tempY = 0;

function getMouseXY(e) 
{
    if (IE) 
    { // grab the x-y pos.s if browser is IE
        tempX = event.clientX + document.documentElement.scrollLeft;
        tempY = event.clientY + document.documentElement.scrollTop;
    }
    else 
    {  // grab the x-y pos.s if browser is NS
        tempX = e.pageX;
        tempY = e.pageY;
    }  
    if (tempX < 0) tempX = 0;
    if (tempY < 0) tempY = 0;  

    MouseX = tempX;
    MouseY = tempY;
    
    if (CurrentSubMenu) CheckMousePositionForNavigation();
}

var NavBuilt = false;

function BuildNavigation()
{
    TopHeader = document.getElementById("divTopHeader");
    for (x=0;x<NavigationArr.length;x++)
    {
        navDiv = document.createElement("div");
        navDiv.id = "navDiv_" + x;
        navDiv.className = "Navigation_SubContainer";
        navDiv.style.display = "none";
        navDiv.index = x;
        tbl = document.createElement("table");
        tbl.cellSpacing = 0;
        tbl.cellPadding = 0;
        tbody = document.createElement("tbody");
        
        for (y=0;y<NavigationArr[x].length;y++)
        {
            tr = document.createElement("tr");
            
            tdLeft = document.createElement("td");
            tdLeft.className = "SubNav_Left";
            if (y==0) tdLeft.className = "SubNav_TopLeft";
            if (y==NavigationArr[x].length-1) tdLeft.className = "SubNav_BottomLeft";
            tdLeft.innerHTML = "<img src='images/spacer.gif' width='7' height='1' />";
            tr.appendChild(tdLeft);
            
            td = document.createElement("td");
            td.className = "SubNav";
            if (y==0) td.className = "SubNav_Top";
            if (y==NavigationArr[x].length-1) td.className = "SubNav_Bottom";         
            link = document.createElement("a");
            link.href = NavigationArr[x][y][1];
            link.innerHTML = NavigationArr[x][y][0];
            td.appendChild(link);
            tr.appendChild(td);

            tdRight = document.createElement("td");
            tdRight.className = "SubNav_Right";
            if (y==0) tdRight.className = "SubNav_TopRight";
            if (y==NavigationArr[x].length-1) tdRight.className = "SubNav_BottomRight";
            tdRight.innerHTML = "<img src='images/spacer.gif' width='7' height='1' />";
            tr.appendChild(tdRight);
            
            tbody.appendChild(tr);            
        }
        
        tbl.appendChild(tbody);
        navDiv.appendChild(tbl);
        document.body.appendChild(navDiv);
    }
    NavBuilt = true;
}

function NavSubMenu()
{
    this.Index = -1;
    this.Top = 0;
    this.Left = 0;
    this.Right = 0;
    this.Bottom = 0;
    this.HasSubMenu = false;
    this.MenuImage = null;
    this.NavDiv = null;
    this.Show = _ShowSubNav;
    this.Hide = _HideSubNav;
}

function _ShowSubNav(Index)
{
    this.MenuImage.src = this.MenuImage.src.replace(".png","_Over.png");
    this.Index = Index;
    if (this.NavDiv)
    {
	this.Top = this.Top - 20;
        this.Left = this.Left + 7;
        this.Top = this.Top + 24;
        this.NavDiv.style.top = this.Top + "px";
        this.NavDiv.style.left = this.Left + "px";
        this.NavDiv.style.display = "";
        this.Right = this.Left + this.NavDiv.clientWidth;
        this.Bottom = this.Top + this.NavDiv.clientHeight;
        this.HasSubMenu = true;
        if (this.Right >= document.documentElement.clientWidth)
        {
            this.Left = this.Left - (this.Right - document.documentElement.clientWidth + 4);
            this.NavDiv.style.left = this.Left + "px";
        }
    }
    else
    {
        this.MenuImage.onmouseout = SwapNavImage;
        this.HasSubMenu = false;
    }
    return false;
}

function _HideSubNav()
{
    this.MenuImage.src = this.MenuImage.src.replace("_Over.png",".png");
    if (this.NavDiv)
    {
        this.NavDiv.style.display = "none";
    }
    CurrentSubMenu = null;
    CurrentMenuId = -1;
}

function SwapNavImage()
{
    if (CurrentSubMenu) CurrentSubMenu.Hide();
}

function CheckMousePositionForNavigation()
{
    if (CurrentSubMenu.HasSubMenu)
    {
        if (CurrentSubMenu.Index != CurrentMenuId)
        {
            if ((MouseX <= CurrentSubMenu.Left) || (MouseX >= CurrentSubMenu.Right) || (MouseY <= CurrentSubMenu.Top) || (MouseY >= CurrentSubMenu.Bottom))
            {
                imgPos = findPos(CurrentSubMenu.MenuImage);
                imgRight = imgPos[0] + CurrentSubMenu.MenuImage.clientWidth;
                imgBottom = CurrentSubMenu.Top + 4;
                if ((MouseX <= imgPos[0] - 1) || (MouseX >= imgRight + 1) || (MouseY <= imgPos[1] - 1) || (MouseY >= imgBottom))
                {
                    CurrentSubMenu.Hide();
                }
            }
        }
    }
}

function Navigation_Over(navLink,subIndex)
{
    CurrentMenuId = subIndex;
    if (!NavBuilt) BuildNavigation();
    
    if (CurrentSubMenu)
    {
        if (CurrentSubMenu.Index != subIndex) CurrentSubMenu.Hide();
    }
    
    CurrentSubMenu = new NavSubMenu();
    CurrentSubMenu.Index = subIndex;
    CurrentSubMenu.MenuImage = navLink.firstChild;
    
    if (NavigationArr[subIndex].length > 0)
    {        
        CurrentSubMenu.NavDiv = document.getElementById("navDiv_" + subIndex);        
        pos = findPos(navLink);
        CurrentSubMenu.Top = pos[1];
        CurrentSubMenu.Left = pos[0];
    }
    CurrentSubMenu.Show();
    return true;
}

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
		}
	}
	return [curleft,curtop];
}