var timeoutHideMenu;
var timeoutShowMenu;
var anchorMemory;
var currentTab; // the tab that the mouse is hovering over.
var tabTable;
var activeTab; //  the selected tab
var className;
//hardcoded values that help w/the dynamic menu positioning.
var fixedTop = 81;
var fixedLeft = 3;
var navigation_numTabs = 0;

//var newPageObject;

/*****************/
/* Generate Tabs */
/*****************/
function Navigation_initTabs()
{
    /*if(currentPage.displayName == g_mapperPage)
    {
        // the layout for the mapping page is a little bit different:
        fixedTop = 67;
        fixedLeft = 1;
    }*/
    var table = document.createElement("TABLE");
    table.id = "header";
    table.cellPadding = 0;
    table.cellSpacing = 0;
    table.border = 0;
    table.style.width = "100%";
    var row = table.insertRow(0);
    var lastGroup = "";
    var cellPos = 0;
    var cnt = 0;
    for(var i=0; i < gMenuObject.length; i++)
    {
        var webPage = gMenuObject[i];
        if(lastGroup != webPage.groupName)
        {
            if(cnt != 0)
            {
                var cellSpacer = row.insertCell(cellPos);
                cellSpacer.className = "tabSpacer";
                cellSpacer.innerHTML = "&nbsp;";
                ++cellPos;
            }
            var cell = row.insertCell(cellPos);
            //cell.style.margin = "2px";
            //cell.style.padding = "0px";
            ++cellPos;
            ++cnt;
            var anchor = document.createElement("A");
            if(app.browser().isIE())
                anchor.style.width = "100%";
            anchor.innerHTML = webPage.groupName;
            anchor.href = "#";
            anchor.tabIndex = i+1;
            //anchor.title = "Click or press the 'Enter' key to display the navigation menu"
            cell.appendChild(anchor);
            cell.title = webPage.groupName; //stores the anchor's innerHTML for easy accessibility.
            cell.id = cnt;                  //stores the tab's number
            cell.className = "tab" + cnt;
            //if(g_selectedTabIndex == cnt)
            if(currentPage.groupName == webPage.groupName)
            {
                activeTab = cell;
                app.getPageTitleObject().className = "titleTab" + cnt;
                currentTabIndex = cnt;
            }
            //events:
            anchor.onblur       = hideFocus;
            anchor.onfocus      = showFocus;
            anchor.onclick      = tabEventHandler;
            //anchor.onkeydown    = tabEventHandler;
            anchor.onmouseover  = setShowPopupTimeout;//tabEventHandler;
            anchor.onmouseout   = setPopupTimeout;
        }
        lastGroup = webPage.groupName;
    } //end for loop
    navigation_numTabs = cnt;
    document.getElementById("topMenuContainer").appendChild(table);
    activeTab.className = activeTab.className + "Active";
    tabTable = table;
}

function showFocus(e)
{
    //var anchor = app.browser().getSourceElement(e);
    //anchor.style.textDecoration = "underline";
}

function hideFocus(e)
{
    var anchor = app.browser().getSourceElement(e);
    anchor.style.textDecoration = "none";
}

function showFocus1(e)
{
    var td = app.browser().getParentElement(app.browser().getSourceElement(e));
    td.style.backgroundColor = "#EEEEEE";
}

function hideFocus1(e)
{
    var td = app.browser().getParentElement(app.browser().getSourceElement(e));
    td.style.backgroundColor = "";
}

function tabEventHandler(e)
{
    clearShowPopupTimeout();
    clearPopupTimeout();
    var anchor = app.browser().getSourceElement(e);
    currentTab = app.browser().getParentElement(anchor);

   //get positioning:
    var leftPos = getOffsetLeft();
    var topPos = getOffsetTop();

    var event = app.browser().getE(e);
    modifyStyles();
    var tableId = "popup_" + leftPos + "_" + topPos;
    if(document.getElementById(tableId) != null)
        hideTables(true);
    else
        generateSubmenu(leftPos, topPos);
}

function tabEventHandlerDelay(e)
{
    var anchor = anchorMemory;
    anchorMemory = null
    clearShowPopupTimeout();
    clearPopupTimeout();
    currentTab = app.browser().getParentElement(anchor);

   //get positioning:
    var leftPos = getOffsetLeft();
    var topPos = getOffsetTop();
    modifyStyles();
    var tableId = "popup_" + leftPos + "_" + topPos;
    if(document.getElementById(tableId) != null)
        hideTables(true);
    else
        generateSubmenu(leftPos, topPos);
}


function hideTables(resetData)
{
    /* This function removes the popup menu div (and underlying iframe)
       from the document.
    */
    var mightBeMore = true;
    while(mightBeMore)
    {
        var children;
        if(app.browser().isFirefox())
            children = app.contentContainerObject().childNodes;
        else if(app.browser().isIE())
            children = app.contentContainerObject().children;
        for(var i=0; i < children.length; i++)
        {
            if(children[i].id != null)
            {
                if(children[i].id.indexOf("popup_") != -1)
                {
                    app.contentContainerObject().removeChild(children[i]);
                    break;
                }
            }
            //if the loop gets here, then nothing let to remove.
            if(i+1 == children.length)
            {
                mightBeMore = false;
            }
        }
    }
    clearShowPopupTimeout();
    clearPopupTimeout();
    //in addition to hiding the tables, reset the help context menu and page title.
    if(resetData)
    {
        restoreDataAndStyles();
    }
}

function restoreDataAndStyles()
{
    help_restoreHelpData();
    app.restorePageTitle();
    restoreTabStyles();
    app.getPageTitleObject().className = "titleTab" + activeTab.id;
    document.getElementById("upDiv_" + g_searchControlName).className = "scrollBar" + activeTab.id;
    document.getElementById("downDiv_" + g_searchControlName).className = "scrollBar" + activeTab.id;
    document.getElementById("upDiv_" + g_helpControlName).className = "scrollBar" + activeTab.id;
    document.getElementById("downDiv_" + g_helpControlName).className = "scrollBar" + activeTab.id;
    currentTab = activeTab;
}

function modifyStyles()
{
    //set styles:
    app.getPageTitleObject().className = "titleTab" + currentTab.id;
    document.getElementById("upDiv_" + g_searchControlName).className = "scrollBar" + currentTab.id;
    document.getElementById("downDiv_" + g_searchControlName).className = "scrollBar" + currentTab.id;
    document.getElementById("upDiv_" + g_helpControlName).className = "scrollBar" + currentTab.id;
    document.getElementById("downDiv_" + g_helpControlName).className = "scrollBar" + currentTab.id;
}

function getOffsetLeft() { return currentTab.offsetLeft + fixedLeft; }

function getOffsetTop() {
    return fixedTop; 
}

function generateSubmenu(x, y)
{
    clearTabStyles();
    var tableId = "popup_" + x + "_" + y;
    className = "popupTab" + currentTab.id;
    //alert(tableId);
    document.getElementById(tableId);
    hideTables(false);
    var menu = document.createElement("div");
    menu.className = className;
    menu.id = tableId;
    //table.cellPadding = 0;
    //table.cellSpacing = 0;
    menu.border = 0;
    //menu.noWrap = true;
    menu.style.position = "absolute";
    menu.style.left = x;
    menu.style.top = y;
    menu.style.zIndex = 200;
    //menu.style.overflow = "hidden";
    var cnt = 0;
    for(var i=0; i < gMenuObject.length; i++)
    {
        var menuItem = gMenuObject[i];
        if(menuItem.groupName == currentTab.title)//currentTab.innerHTML)
        {
            //var div = document.createElement("div");
            //div.noWrap = true;
            var anchor = document.createElement("a");
            anchor.href = "#";
            anchor.innerHTML = menuItem.displayName;
            anchor.onkeyup    = submenuHoverOn;
            anchor.onmouseover = submenuHoverOn;
            anchor.onmouseout = submenuHoverOff;
            anchor.onclick = doClick;
            anchor.style.textDecoration = "none";
            anchor.style.color = "#000066";
            anchor.tabIndex = i+1;
            menu.appendChild(anchor);
            menu.appendChild(document.createElement("br"));
            ++cnt;
        }
    }

    if(app.browser().isIE())
        menu.style.width = currentTab.clientWidth + 2;
    else
        menu.style.width = currentTab.clientWidth - 3;
    document.getElementById("contentSection").appendChild(menu);

    // a hack so that menus don't disappear if they are rendered over
    // a dropdown menu or a PDF document.
    var iframe = document.createElement("iframe");
    iframe.id = tableId + "_frame";
    if(app.browser().isIE())
        iframe.style.height = menu.clientHeight+1;
    else
        iframe.style.height = menu.clientHeight-3;
    iframe.style.width = menu.style.width;
    //alert(menu.clientHeight);

    iframe.style.left = x;
    iframe.style.top = y;
    iframe.style.zIndex = 199;
    iframe.style.position = "absolute";
    iframe.style.overflow = "hidden";
    document.getElementById("contentSection").appendChild(iframe);

}

function doClick(e)
{
    var sourceElement = getBrowser().getSourceElement(e);
    app.loadIframe(sourceElement.innerHTML);
    hideTables(true);
    restoreTabStyles();
}

function clearTabStyles()
{
    //alert("restoreTabStyles");
    if(tabTable != null)
    {
        var row = tabTable.getElementsByTagName("tr")[0];
        var cells = app.browser().getChildren(row);
        for(i=0; i < cells.length; i++)
        {
            if(cells[i].className.indexOf("Active") != -1)
            {
                var cName = cells[i].className.substring(0,cells[i].className.indexOf("Active"));
                //alert(cName);
                cells[i].className = cName;
            }
        }
    }
}

function restoreTabStyles()
{
    clearTabStyles();
    if(activeTab != null)
        activeTab.className = activeTab.className + "Active";
}

function submenuHoverOn(e)
{
    var setPageData = false;
    var sourceElement = app.browser().getSourceElement(e);
    help_displayHelpData(e);
    app.setPageTitle(e);
    clearPopupTimeout(e);
}

function submenuHoverOff(e)
{
    setPopupTimeout(e);
}


function clearPopupTimeout(e) {
    window.clearTimeout(timeoutHideMenu);
}
function setPopupTimeout(e)
{
    clearShowPopupTimeout(e);
    timeoutHideMenu = setTimeout("hideTables(true)",1000);
}

function clearShowPopupTimeout(e) {
    window.clearTimeout(timeoutShowMenu);
}
function setShowPopupTimeout(e)
{
    clearPopupTimeout();
    anchorMemory = app.browser().getSourceElement(e);
    timeoutShowMenu = setTimeout("tabEventHandlerDelay()",500);

}

