﻿var menuHtml = [];
var menuItemCount = 0;
var prevChildNum;
var prevMenuLevel = 0;
var currentChildMenuId;

writeHeaderHtml();

function writeHeaderHtml() {
    writeHeaderStartHtml();

    addMenuItem(1, 'Home', 'index.html');

    addMenuItem(1, 'About Us', 'we_are.html');
        addMenuItem(2, 'Who We Are', 'we_are.html');
        addMenuItem(2, 'Meet our staff', 'we_are.html');
        addMenuItem(2, 'Contact us by phone/mail/e-mail', 'Contact.html');
        addMenuItem(2, 'Contact the &quot;webservant&quot;', 'tech_contact.html');
        addMenuItem(2, 'Map &amp; driving directions', 'find_us.html');
        addMenuItem(2, 'Our history', 'history.html');
        addMenuItem(2, 'What to expect in worship', 'worship.html');
        addMenuItem(2, 'Annual Reports', 'AnnRpt.html');
        addMenuItem(2, 'Watch our video!', 'video.html');

    addMenuItem(1, 'Spiritual Growth');
        addMenuItem(2, 'Children &amp; Youth', 'kids_youth.html');
        addMenuItem(2, 'Adult Education', 'growth.html');

    addMenuItem(1, 'Ministry/Mission');
        addMenuItem(2, 'A Season of Discerning...', 'Discerning.html');
        addMenuItem(2, 'Our Equipping Ministry', 'Equipping.html');
        addMenuItem(2, 'Community Partners', 'CommunityPartners.html');
        addMenuItem(2, 'Habitat For Humanity', 'habitat2009.html');
        addMenuItem(2, 'SHED: Safe Homes for the Elderly & Disabled', 'SHED.html');
        addMenuItem(2, 'Service Projects', 'service.html');
        addMenuItem(2, 'The "Service Station" - Current Opportunities to Help', 'ServiceStation.html');
        addMenuItem(2, 'Peacemaking', 'peacepage.html');
        addMenuItem(2, 'Covenant Network', 'covenant.html');
        addMenuItem(2, 'Interfaith Exploration', 'interfaith.html');
        addMenuItem(2, 'Remember in your prayers...', 'pray4.html');
        addMenuItem(2, 'Peace Prayer Schedule', 'PeacePrayer.html');

    addMenuItem(1, 'Groups', 'events.html');
        addMenuItem(2, 'Commissions and Leadership Groups', 'we_are.html');
        addMenuItem(2, 'Deacons', 'deacons.html');
        addMenuItem(2, 'Liturgists', 'liturgist.html');
        //addMenuItem(2, 'FEAST', 'FEAST.html');
        addMenuItem(2, 'Annual Reports', 'AnnRpt.html');
        addMenuItem(2, 'Other Groups and Special Events', 'events.html');
        addMenuItem(2, 'B&amp;Bs: Bonding and Bridging Together', 'http://www.trinityucity.org/bandb');

    addMenuItem(1, 'News', 'news/archive.html');
        addMenuItem(2, 'Trinity News', 'news/archive.html');
        addMenuItem(2, 'Bulletins', 'news/bulletins.html');
        addMenuItem(2, 'Calendar of Events', 'calendar.html');

    addMenuItem(1, 'Sermons', 'sermons/archive.html');

    addMenuItem(1, 'Music', 'music.html');

    addMenuItem(1, 'Pictures', 'PicPages/PicsIndex.html');

    addMenuItem(1, 'Info/Links');
        addMenuItem(2, 'Event Planning Guide', 'eventplan.html');
        addMenuItem(2, 'Wedding Fee Schedule: Members', 'WeddingsForMembers.html');
        addMenuItem(2, 'Wedding Fee Schedule: Non-Members', 'WeddingsForNonMembers.html');
        addMenuItem(2, 'Baptism at Trinity', 'baptism.html');
        addMenuItem(2, 'Liturgist Resources', 'liturgist.html');
        addMenuItem(2, 'Presbyterian Church (USA)', 'http://www.pcusa.org');
        addMenuItem(2, 'Giddings-Lovejoy Presbytery', 'http://www.glpby.org');
        addMenuItem(2, 'Metropolitan Congregations United', 'http://www.mcustl.org/');
        addMenuItem(2, 'Links to other interesting sites', 'links.html');

    addMenuItem(1, 'Search', 'sitemap.html');

    addMenuItem(1, '<img src="http://www.trinityucity.org/images/rss.jpg" />', 'syndication.html', 'RSS');

    checkMenuLevel(1);

    menuHtml.push('</div></div></div>'); // HeadLinks, header, logoAndHeader

    document.write(menuHtml.join(''));

    AdjustRelativePaths('#logoAndHeader a', 'href');
}

function writeHeaderStartHtml() {
    menuHtml.push('<link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.trinityucity.org/rss.xml" />');
    menuHtml.push('<link rel="search" type="application/opensearchdescription+xml" href="/OpenSearch.xml" title="Trinity - University City">');
    menuHtml.push('<div id="logoAndHeader">');
    writeLogoHtml();
    menuHtml.push('<div id="header" onmouseout="hideChildMenu()">');
    menuHtml.push('<div class="HeadLinks">');
}

function writeLogoHtml() {
    menuHtml.push('<div id="logos" align="center" style="margin: 2px">');
    menuHtml.push('<a href="http://www.trinityucity.org"><img src="http://www.trinityucity.org/images/TrinityLogo.jpg" alt="Trinity Presbyterian Church: Celebrating the Diversity of All" /></a>');
    menuHtml.push('<a style="margin-left: 4em" href="http://www.pcusa.org"><img src="http://www.trinityucity.org/images/PCUSA_trans.gif" alt="Trinity is a member of the Presbyterian Church (USA)" /><a>');
    menuHtml.push('</div>');
}

function addMenuItem(level, text, href, alt) {
    checkMenuLevel(level);

    menuItemCount++;

    menuHtml.push('<div');
    addMenuAttribute('class', 'HeadLink' + level);
    addMenuAttribute('onmouseover', 'showChildMenu(' + menuItemCount + ', this)');
    menuHtml.push('><a');
    addMenuAttribute('href', href || '#');

    if (alt) {
        addMenuAttribute('alt', alt);
    }

    menuHtml.push('>');
    menuHtml.push(text);

    menuHtml.push('</a>');
}

function addMenuAttribute(attName, attValue) {
    menuHtml.push(buildAttributeHtml(attName, attValue));
}

function checkMenuLevel(level) {
    if (prevMenuLevel) {
        if (level < prevMenuLevel) {
            menuHtml.push('</div></div>');
        }
        else {
            if (level > prevMenuLevel) {
                menuHtml.push('</div><div');
                addMenuAttribute('id', getSubMenuId());
                addMenuAttribute('class', 'HeadSubLinks');
                addMenuAttribute('onmouseover', 'showChildMenu(' + menuItemCount + ')');
                menuHtml.push('>');
            }
            else {
                menuHtml.push('</div>');
            }
        }
    }

    prevMenuLevel = level;
}

function getSubMenuId(menuNum) {
    menuNum = menuNum || menuItemCount;
    return 'subMenu' + menuNum;
}

function showChildMenu(menuNum, parentDiv) {
    var childMenu = $('#' + getSubMenuId(menuNum)).get(0);

    if (!childMenu) {
        hideChildMenu();
        return;
    }

    if (currentChildMenuId && currentChildMenuId !== childMenu.id) {
        hideChildMenu();
    }

    var style = childMenu.style;

    if (parentDiv) {
        var newLeft = new Number(parentDiv.offsetLeft);
        style.left = newLeft + 'px';
        style.top = parentDiv.offsetTop + parentDiv.offsetHeight + 'px';
        style.position = 'absolute';
    }

    style.display = 'block';
    currentChildMenuId = childMenu.id;
}

function hideChildMenu() {
    if (currentChildMenuId) {
        $('#' + currentChildMenuId).get(0).style.display = 'none';
    }
}

function ApplyYouAreHereStyle() {
    var url = document.location.href.toLowerCase();
    var menuCaption = '';
    url = url.substring(DetermineUrlRoot().length);

    if (url.substring(0, 5) == 'news/') {
        menuCaption = 'News';
    }
    else if (url.substring(0, 8) == 'sermons/') {
        menuCaption = 'Sermons';
    }
    else if (url.substring(0, 9) == 'picpages/') {
        menuCaption = 'Pictures';
    }
    else {
        switch (url) {
            case '':
            case 'index.html':
                menuCaption = 'Home';
                break;

            case 'we_are.html':
            case 'equipping.html':
            case 'find_us.html':
            case 'history.html':
            case 'worship.html':
            case 'video.html':
            case 'contact.html':
            case 'tech_contact.html':
                menuCaption = 'About Us';
                break;

            case 'kids_youth.html':
            case 'funandgames.html':
            case 'seniorhigh.html':
            case 'growth.html':
                menuCaption = 'Spiritual Growth';
                break;

            case 'music.html':
                menuCaption = 'Music';
                break;

            case 'service.html':
            case 'peacepage.html':
            case 'covenant.html':
            case 'mcu.html':
                menuCaption = 'Ministry/Mission';
                break;

            case 'groups.html':
            case 'events.html':
            case 'eventplan.html':
            case 'deacons.html':
            case 'liturgist.html':
            case 'feast.html':
                menuCaption = 'Groups';
                break;

            case 'links.html':
                menuCaption = 'Info/Link';
                break;

            case 'sitemap.html':
                menuCaption = 'Search';
                break;
        }
    }

    if (menuCaption) {
        $('.HeadLink1 :contains("' + menuCaption + '")').addClass("HeadLinkCurrent");
    }
}