//  common.js
//  APSLOT javascript goes here so the client only downloads once.
//  Copyright (c) 2003 by Automated Publishing Solutions, Inc <info@apsolut.net>.  All rights reserved.
//_________________________________________________________________________________________________________________________________

/* This is clipped snapshot of the list DOM that forms the menu.

    <UL id=NodeMenu>
        00  <LI id=zz>
            00  <A ...>
                00  <IMG ...>
                01 #text " Home"        Text of the <A> tag
            01  #text ""                Text of the <LI> tag
            .
            .
            .
        03  <LI>
            00  <A ...>
                00  <IMG ...>
                01  #text " Our People"
            01  #text ""
            02  <UL>
                00  <LI>
                    00  <A ...>
                        00  <IMG ...>
                        01  #text " Wayne Brown"
                    01  #text ""
            .
            .
            .
*/

//- Global Variables -----------------------------------------------------------
var TheGlowPulser;

//- Utility Functions ------------------------------------------------------------------------------------------------------------

function Print(msg) {
    // add some text to <span id=Messages>
    Messages.innerHTML += msg;
}

function Println(msg) {
    // add some text to <span id=Messages>
    Messages.innerHTML += "<p>" + msg;
}

function ShowNode (msg, e)
{
    var nn, ht;

    try { nn = e.nodeName; }
    catch (x) { nn = ""; }

    try { ht = e.outerHTML; }
    catch (x) { ht = ""; }

    alert(msg + ": " + nn + " [" + ht + "]");
}

function SafeGetNodeName (e)
{
    // Wont bomb if 'e' is null
    return e == null ? "" : e.nodeName;
}

function GetUrlParam(param)
{
    var begin = location.href.search;
    if (begin == -1)
        return null;

    var s = location.href.substring(begin + 1);
    begin = s.indexOf(param + "=");
    if (begin == -1)
        return null;

    begin += param.length + 1;
    var end = s.indexOf(";", begin);
    if (end == -1)
        end = s.length;

    return s.substring(begin, end);
}

function FindStyle(selectorText) {
    selectorText = selectorText.toLowerCase();

    var ste = document.styleSheets.length;
    var sti;
    for (sti = 0; sti != ste; ++sti) {
        var rules = document.styleSheets[sti].rules;
        var rue = rules.length;
        var rui;

        for (rui = 0; rui != rue; ++rui) {
            var ru = rules[rui];
            var s = ru.selectorText.toLowerCase();
            if (selectorText == s)
                return ru.style;
        }
    }
}

//- Encapsulte how to play the Swish sound on mouse overs ----------------------

function PlaySwish() {
    sound.src = "/main/res/swish.wav";
}

//- GlowPulser -----------------------------------------------------------------
// Slow increase and decrease the glow filter strength.

function GlowPulser_Pulse() {
    return;

    this.strength += this.dir;
    if (this.strength == this.strengthMax) this.dir = -1;
    if (this.strength == this.strengthMin) this.dir =  1;

    var s = this.style.filter;

    // cheese parsing only supports single digit glow strength.
    this.style.filter = s.substring(0, s.length - 2) + this.strength + ")";
}

function GlowPulser(style) {
    this.strengthMax = 9;  // can only use single digits 0..9!
    this.strengthMin = 1;
    this.strength = 0;
    this.dir = 1;
    this.style = style;

    this.Pulse = GlowPulser_Pulse;
    window.setInterval("TheGlowPulser.Pulse()", 100);
}

function InitFrame() {
    TheGlowPulser = new GlowPulser( FindStyle(".hoverglow a:hover") );
}

//- Menu LI Navigation Functions -------------------------------------------------------------------------------------------------
function GetAFromLi (eli)
{
    var e = eli.childNodes[0];
    return SafeGetNodeName(e) == "A" ? e : null;
}

function GetImgFromLi (eli)
{
    // Encapsulate relationship between LI and child IMG element here.
    var e = eli.childNodes[0];
    if (e) {
        e = e.childNodes[0];
        return SafeGetNodeName(e) == "IMG" ? e : null;
    }
}

function GetUlFromLi (eli)
{
    // Encapsulate relationship between LI and child UL element here.
    var e = eli.childNodes[2];
    return SafeGetNodeName(e) == "UL" ? e : null;
}

function GetChildLi (eli)
{
    // Encapsulate relationship between LI and any child LIs here
    var e = GetUlFromLi(eli);
    if (e) {
        e = e.firstChild;
        return SafeGetNodeName(e) == "LI" ? e : null;
    }
    return null;
}

function FindParentLi (e)
{
    if (SafeGetNodeName(e) == "LI")
        return e;

    if (e) {
        e = e.parentElement;

        if (SafeGetNodeName(e) == "LI")
            return e;
    }

    if (e) {
        e = e.parentElement;

        if (SafeGetNodeName(e) == "LI")
            return e;
    }

    return null;
}

//- Menu Actions -----------------------------------------------------------------------------------------------------------------
function SetLi (eli, state)
{
    if (SafeGetNodeName(eli) != "LI")
        return;

    var eul  = GetUlFromLi(eli);
    var eimg = GetImgFromLi(eli);

    if (eul == null || eimg == null)
        return;

    eul.style.display = (state == "expand" ? "" : "none");
    eimg.src = (state == "expand" ? "/main/res/minus.gif" : "/main/res/plus.gif");
}

function ToggleNode (eli)
{
    // Capture the current status of this node before collapsing them all
    var eul = GetUlFromLi(eli);
    var doExpand = eul != null && eul.currentStyle.display == "none"

    // Collapse all the menus items on the same level as this one
    for (e = eli.parentElement.firstChild; e; e = e.nextSibling)
        SetLi(e, "collapse");

    // IF there is not a UL element then this is not a colapsable list so just ignore it.
    if (eul == null)
        return;

    // Collapse all the menus items below this one
    //for (e = eul.firstChild; e; e = e.nextSibling)
    //    SetLi(e, "collapse");

    // Toggle the clapsed mode of my sublist
    if (doExpand)
        SetLi(eli, "expand");
}

function NodeMenu_onclick ()
{
    // If you click on a node that has an HREF then append the node information and navigate to it.
    // If you click on a node that doen't have a link then just expand or collapse the node.
    var eli = FindParentLi(event.srcElement);
    var ea = GetAFromLi(eli)
    var s = ea.href;
    s = s.substring(s.length - 1);

    if (s == null || s == "" || s == "-")
        ToggleNode(eli);
    else
        location.href = ea.href + "?node=" + eli.id;

    return false;
}

function InitNodes (nd, eli)
{
    while (eli)
    {
        if (nd.nodeId == nd.searchId)
            nd.foundNode = eli;

        eli.id = nd.nodeId++;

        var s = GetAFromLi(eli).pathname.toLowerCase();
        // Println(nd.searchName + " " + s);

        if (nd.foundNode == null && GetAFromLi(eli).pathname.toLowerCase() == nd.searchName)
            nd.foundNode = eli;

        SetLi(eli, "collapse");

        InitNodes(nd, GetChildLi(eli));

        eli = eli.nextSibling;
    }
}

function NodeData()
{
    // Constructor

    // there is a bug in the pathname property of the <A> tag.  It does not return the leading right slash.  It is easier to
    // remove it from the location string here, once, rather than on every <A> tag later.
    this.searchName = location.pathname.toLowerCase().substring(1);
    this.searchId = parseInt(GetUrlParam("node"));
    this.foundNode = null;
    this.nodeId = 0;
}

function InitMenu ()
{
    var x = GetUrlParam("noinit");
    if (x)
        return;

    var nd = new NodeData();

    InitNodes(nd, NodeMenu.firstChild);

    var eli = (nd.foundNode == null ? NodeMenu.firstChild : nd.foundNode);

    while (eli) {
        SetLi(eli, "expand");
        eli = FindParentLi(eli.parentElement);
    }
}
