/*
 * Copyright (c) 2008 Impact Computing Corporation
 */

//----------------------------------------------------------------------------
// Page Initialization
//----------------------------------------------------------------------------

function initializePage()
{
    var currentPageId = getCurrentPageId();
    var currentSubpageId = getCurrentSubpageId();
    var currentSubsectionId = getCurrentSubsectionId();

    updateMainButtons(currentPageId);
    setPageContent(currentSubpageId, currentSubsectionId);
}

function initializeMainPage(tallPageId, shortPageId)
{
    var height = document.body.clientHeight;
    var activePageId = (height > 650 ? tallPageId : shortPageId);
    document.getElementById(activePageId).style.display = 'inline';
    addMainButtons(activePageId + "Buttons");

    var currentPageId = getCurrentPageId();
    updateMainButtons(currentPageId);
}

//----------------------------------------------------------------------------
// HTML Generation Functions
//----------------------------------------------------------------------------

function beginMainPageContent(pageName, hasMenu)
{
    var pageId = textToId(pageName);
    var pageTitle = pageName.toUpperCase();
    document.writeln('<div id="' + pageId + '" class="mainPage">');
    document.writeln('<table cellpadding="0" cellspacing="0" style="width:100%;"><tr><td>');
    document.writeln('<div class="pageHeader">' + pageTitle + '</div>');
    if (hasMenu)
    {
        document.writeln('<div class="pageContent"><table><tr>');
    }
    else
    {
        document.writeln('<div class="contentAreaNoMenu">');
    }
}

function endMainPageContent(pageName, defaultPageImage, hasMenu)
{
    if (hasMenu)
    {
        // NOTE: These next two tags (div and td) were opened in addMenu()
        document.writeln('</div></td>');
        document.writeln('</tr></table></div>');
    }
    else
    {
        document.writeln('</div>');
    }
    document.writeln('</td>');
    document.writeln('<td class="pageImage">');
    document.writeln('<img id="PageImage" class="pageImage" src="images/' + defaultPageImage + '">');
    document.writeln('</td>');
    document.writeln('</tr></table></div>');
}

function addMenu(pageName, menuItems, parentMenuItem, noTopLink)
{
    var pageId = textToId(pageName);
    var menuId = pageId + "Menu";
    var menuClassName = (parentMenuItem ? "submenu" : "menu");
    var linkClassName = menuClassName + "Link";
    var parentMenuItemId = (parentMenuItem && (! noTopLink) ? textToId(parentMenuItem) : null);
    if (! parentMenuItem)
    {
        document.writeln('<td class="menuBox">');
    }
    document.writeln('<table id="' + menuId + '" class="' + menuClassName + '">');
    var n = menuItems.length;
    for (var i = 0; i < n; i++)
    {
        if (typeof(menuItems[i]) == 'string')
        {
            addMenuItem(pageId, menuItems[i], menuClassName, linkClassName, parentMenuItemId);
        }
        else
        {
            // submenu
            addMenuItem(pageId, menuItems[i][0], menuClassName, linkClassName, parentMenuItemId, noTopLink);
            document.writeln('<tr><td>');
            // NOTE WELL: Recursive call
            addMenu(pageId, menuItems[i].slice(1), menuItems[i][0], noTopLink);
            document.writeln('</td></tr>');
        }
    }
    document.writeln('</table>');
    if (! parentMenuItem)
    {
        document.writeln('</td>');
        // NOTE: These next two tags are closed out in endMainPageContent()
        document.writeln('<td><div class="contentAreaWithMenu">');
    }
}

function addMenuItem(pageId, linkInfo, menuClassName, linkClassName, parentLinkId, noLink)
{
    var linkText = null;
    var linkId = null;
    var barIndex = linkInfo.indexOf('|');
    if (barIndex >= 0)
    {
        linkText = linkInfo.substring(0, barIndex);
        linkId = linkInfo.substring(barIndex + 1);
    }
    else
    {
        linkText = linkInfo;
        linkId = textToId(linkInfo);
    }
    var linkAttributes = '';
    if (! noLink)
    {
        linkAttributes =
            'onmouseover="' + menuClassName + 'LinkMouseover(this);" ' +
            'onmouseout="' + menuClassName + 'LinkMouseout(this);" ' +
            'onclick="setPageContent(\'';
        if (parentLinkId)
        {
            // need to track parent for submenu item
            linkAttributes += parentLinkId + '\', \'';
        }
        linkAttributes += linkId + '\');"';
    }
    document.writeln('<tr id="' + linkId + 'MenuItem"><td>');
    document.writeln('<li class="' + linkClassName + '" ' +
                     linkAttributes + '>' + linkText + '</li>');
    document.writeln('</td></tr>');
}

function addLink(linkText, subpageId, subsectionId)
{
    var onclick = 'onclick="setPageContent(\'' + subpageId;
    if (subsectionId)
    {
        onclick += '\', \'' + subsectionId;
    }
    onclick += '\');" ';
    document.writeln('<span class="link" ' +
                       'onmouseover="this.className=\'linkMouseover\';" ' +
                       'onmouseout="this.className=\'link\';" ' +
                       onclick + '>' + linkText + 
                     '</span>');
}

function addMainButtons(parentId)
{
    var html = '<table cellspacing="0" style="width:100%;">' +
                   '<tr><td style="height:5px;" /></tr>' +
                   '<tr id="mainButtons" style="text-align:center;vertical-align:middle;width:100%;height:41px;">' +
                   '<td />';
    var buttonList = ["Home", "About Us", "Services", "News", "Careers", "Contact Us"];
    var n = buttonList.length;
    for (var i = 0; i < n; i++)
    {
        html += mainButtonHTML(buttonList[i]);
    }
    html += '</tr></table>';
    if (parentId)
    {
        var parentElement = document.getElementById(parentId);
        parentElement.innerHTML = html;
    }
    else
    {
        document.writeln(html);
    }
}

// TBD: to add tooltips and/or submenus, see http://www.texsoft.it/index.php?c=software&m=sw.js.htmltooltip&l=it
function mainButtonHTML(buttonText)
{
    var id = textToId(buttonText);
    return '<td id="' + id + 'Button" class="mainButtonUnselected" ' +
                'style="width:109px;height:41px;" ' +
                'onmouseover="mainButtonMouseover(this);" ' +
                'onmouseout="mainButtonMouseout(this);" ' +
                'onclick="mainButtonClicked(this);" >' +
                buttonText +
           '</td><td />';
}

//----------------------------------------------------------------------------
// Page Update Functions
//----------------------------------------------------------------------------

function textToId(text)
{
    return text.replace(/ /g, "").replace(/\?/g, "");
}

var _currentSubpageId = null;

function setPageContent(subpageId, subsectionId)
{
    if (! subpageId)
    {
        // no subpage specified: if page has an overview section, use that;
        //                       otherwise, no op (i.e., stick with default
        //                       subpage for this page)
        var overviewSubpageId = getCurrentPageId() + "Overview";
        if (document.getElementById(overviewSubpageId))
        {
            subpageId = overviewSubpageId;
        }
        else
        {
            return;
        }
    }

    setPageImage(subpageId);

    if (subpageId != _currentSubpageId)     // don't reload same subpage redundantly
    {
        var subpages = getElementsByClassName("subPage", document.body, "span");
        if (subpages)
        {
            for (var i = 0; i < subpages.length; i++)
            {
                subpages[i].style.display = (subpages[i].id == subpageId ? "inline" : "none");
            }
        }
        _currentSubpageId = subpageId;
    }

    var sectionId = subpageId + (subsectionId ? ("." + subsectionId) : "");
    location.hash = "_" + sectionId;
}

function setPageImage(sectionId)
{
    document.getElementById("PageImage").src = "./images/" + sectionId + ".png";
}

function goToHomePage()
{
    var homeButton = document.getElementById(pageIdToButtonId("Home"));
    mainButtonClicked(homeButton);
}

//----------------------------------------------------------------------------
// Page ID, Subpage ID and Subsection ID Functions
//----------------------------------------------------------------------------

function buttonIdToPageId(buttonId)
{
    return buttonId.replace(/Button/,"");
}

function pageIdToButtonId(pageId)
{
    return pageId + "Button";
}

// NOTE: we use hash string rather than query string to allow for bookmarking of subpages
//       without browser doing a page reload (setting query string forces a page reload)

function getCurrentPageId()
{
    return document.body.id;
}

function getCurrentSubpageId()
{
    var currentSubpageId = null;      // default is no subpage
    var hashString = location.hash.substring(1, location.hash.length);
    if (hashString)
    {
        var endPos = hashString.indexOf(".");
        if (endPos < 0)
        {
            endPos = hashString.length;
        }
        currentSubpageId = hashString.substring(1, endPos);
    }
    return currentSubpageId;
}

function getCurrentSubsectionId()
{
    var currentSubsectionId = null;      // default is no subpage
    var hashString = location.hash.substring(1, location.hash.length);
    if (hashString)
    {
        var dotPos = hashString.indexOf(".");
        if (dotPos >= 0)
        {
            var startPos = dotPos + 1;
            currentSubsectionId = hashString.substring(startPos);
        }
    }
    return currentSubsectionId;
}

//----------------------------------------------------------------------------
// Main Button Functions
//----------------------------------------------------------------------------

function updateMainButtons(currentPageId)
{
    var currentButtonId = currentPageId + "Button";
    var mainButtonRow = document.getElementById("mainButtons");
    var selectedButtons = getElementsByClassName("mainButtonSelected", mainButtonRow, "td");
    if (selectedButtons.length > 0)
    {
        // necessarily only 1 button selected
        if (selectedButtons[0].id == currentButtonId)
        {
            return;     // no op (clicked on already selected button)
        }
        selectedButtons[0].className = "mainButtonUnselected";
    }
    var mouseOverButtons = getElementsByClassName("mainButtonMouseover", mainButtonRow, "td");
    if (mouseOverButtons.length > 0)
    {
        // necessarily only 1 button in mouseover state
        if (mouseOverButtons[0].id == currentButtonId)
        {
            mouseOverButtons[0].className = "mainButtonSelected";
            return;     // done
        }
    }
    var unselectedButtons = getElementsByClassName("mainButtonUnselected", mainButtonRow, "td");
    var n = unselectedButtons.length;
    for (var i = 0; i < n; i++)
    {
        if (unselectedButtons[i].id == currentButtonId)
        {
            unselectedButtons[i].className = "mainButtonSelected";
            break;      // done
        }
    }
}

function mainButtonMouseover(button)
{
    if (button.className == 'mainButtonUnselected')
    {
        button.className = 'mainButtonMouseover';
    }
}

function mainButtonMouseout(button)
{
    if (button.className == 'mainButtonMouseover')
    {
        button.className = 'mainButtonUnselected';
    }
}

function mainButtonClicked(button)
{
    if (button.className == 'mainButtonSelected')
    {
        // button already selected: re-clicking it is no-op
        return;
    }
    var pageName = buttonIdToPageId(button.id);
    if (pageName == "Home")
    {
        pageName = "index";
    }
    location.href = "./" + pageName + ".html";
}

//----------------------------------------------------------------------------
// Menu Link Functions
//----------------------------------------------------------------------------

function menuLinkMouseover(link) { link.className = 'menuLinkMouseover'; }
function menuLinkMouseout(link) { link.className = 'menuLink'; }
function submenuLinkMouseover(link) { link.className = 'submenuLinkMouseover'; }
function submenuLinkMouseout(link) { link.className = 'submenuLink'; }

//----------------------------------------------------------------------------
// Mail To Functions
//----------------------------------------------------------------------------

function mailToCareers(anchorTag)
{
    mailToAtImpact(anchorTag, "careers");
}

function mailToInfo(anchorTag)
{
    mailToAtImpact(anchorTag, "info");
}

function mailToAtImpact(anchorTag, who)
{
    anchorTag.href="mailto:" + who + "@impact-computing.com";
}

function genericMailTo(anchorTag)
{
    anchorTag.href="mailto:";
}

