/***
 *** lib.js
 ***
 *** Copyright (C) 2005 Christopher Owens.
 ***
 *** Some simple javascript to control rollovers.
 ***
 ***/

var ELEMENT_NODE = 1; // Not defined in IE.

var elRolloverElem = null; // Should be null unless we've set a timeout.
var nTimeoutId;

var aMenuImg = new Array();
var strMenuImgId = "menuimg";

/*
 * Change occurrences of strFrom to strTo in the class attribute of elem.
 */
function changeStyleClass (elem, strFrom, strTo)
{
    var reFrom = new RegExp(strFrom, "g");
    var strClassAttrib = elem.className;
    strClassAttrib = strClassAttrib.replace(reFrom, strTo);
    elem.className = strClassAttrib;
}

/*
 * Looks for a child of elem that's an element with class
 * "rollover".
 *
 * Substitutes strTo for the first occurrence of strFrom
 * in the class text of both elem and that child.
 *
 * For example, if the class of elem starts off as "wrapper-show", and
 * the class of the child starts off as "rollover show", then
 * showOrHide (elem, "show", "hide") sets the class of elem to
 * "wrapper-hide", and the class of the child to "rollover hide".
 */
function showOrHide (elem, strFrom, strTo)
{
    var children = elem.childNodes;
    var reFrom = new RegExp(strFrom);

    changeStyleClass (elem, strFrom, strTo)

    /*
     * Look for a child of elem which is an element and has class
     * "rollover".
     */
    for (var i = 0; i < children.length; i++)
    {
        var elChild = children[i];
        if ((elChild.nodeType === ELEMENT_NODE)
            && (elChild.className !== null)
            && (elChild.className.match(/rollover/)))
        {
            /*
             * Change strFrom to strTo in the class.
             */
            changeStyleClass (elChild, strFrom, strTo)
        }
    } 
}

function hideAfterTimeout ()
{
    showOrHide (elRolloverElem, "show", "hide");
}

function showRollover (elem)
{
    /*
     * If we were waiting for a timeout (i.e. elRolloverElem is set),
     * then hide the window we were waiting to hide before we go on.
     * (Note we clear the timeout first, so the timeout can't go off
     * while we're testing elRolloverElem and give nasty races).
     */
    clearTimeout (nTimeoutId);
    if (elRolloverElem)
    {
        showOrHide (elRolloverElem, "show", "hide");
        elRolloverElem = null;
    }

    showOrHide (elem, "hide", "show");
}

/*
 * Don't do the hide straight away, wait a short while first (in case
 * the mouse goes back over the element).
 */
function hideRollover (elem)
{
    elRolloverElem = elem;
    nTimeoutId = setTimeout("hideAfterTimeout()", 100);
}


function preloadMenuImg (aImg)
{
    if (document.images)
    {
        for (var i = 0; i < aImg.length; i++)
        {
            aMenuImg[aImg[i]] = new Image(100, 100);
            aMenuImg[aImg[i]].src = "images/" + aImg[i];
        }
    }

    aMenuImg['hide'] = new Image(100, 100);
    aMenuImg['hide'].src = "images/spacer.gif";
}

function showMenuImg (strImgName)
{
    if (document.images)
    {
        document.images[strMenuImgId].src = aMenuImg[strImgName].src;
    }
}

function hideMenuImg ()
{
    showMenuImg ("hide");
}
