/* EQware.js -- JavaScript extensions for EQware web pages.
 * Copyright C2006 by EQware Engineering, Inc.
 */


/*** GLOBALS ******************************************/

var Geometry = {};
    //Used to establish portable window geometry acquisition.


/*** FUNCTIONS ****************************************/

/* initExpandCollapseList() -- For the current document, initializes all 
 * needed Id's and other tags to make an expand/collapse list work correctly.
 * Assumes that each expandable/collapsable list entry include an "onclick" 
 * call in the form onclick="expandCollapse(document, 'first', 0);", 
 * where the second parameter is valid, unique indentifier.
 */
function initExpandCollapseList()
{
    var anchors = document.getElementsByTagName("a");
    var divs = document.getElementsByTagName("div");
    var imgs = document.getElementsByTagName("img");

    for (var i = 0; i < anchors.length; i++)
    {
        var divId = anchors[i].onclick.toString();
        divId = divId.substring(divId.indexOf(", ") + 3, divId.indexOf(", 0") - 1);

        anchors[i].id = "header-" + divId;
        anchors[i].href = "#header-" + divId;

        divs[i].id = divId;

        imgs[i].id = "icon-" + divId;
        imgs[i].src = "Images/CollapseIcon.gif";
    }
}   /* initExpandCollapseList*/


/* expandCollapse -- For the specified document, expand or collapse the expandable 
 * list item identified by id, by unhiding or hiding the associated div.  If
 * force > 0, expand the item; if force < 0, collapse it.  If force == 0, toggle
 * the state of expansion/collapse.
 */
function expandCollapse(document, id, force)
{
    var iconId = "icon-" + id;
 
    if (document.getElementById) 
    {
        if (force > 0 
        ||  (force == 0 && document.getElementById(id).style.display == "none"))
        {
            document.getElementById(id).style.display = "block";
            document.getElementById(iconId).src = "Images/ExpandIcon.gif";
        } 
        else if (force < 0
        ||       (force == 0 && document.getElementById(id).style.display == "block"))
        {
            document.getElementById(id).style.display = "none";
            document.getElementById(iconId).src = "Images/CollapseIcon.gif";
        }
    } 
}   /* expandCollapse */


/* expandAll() -- Expands all expand/collapse list items in the specified frame.
 */
function expandAll(frame)
{
    var divs = frame.document.getElementsByTagName("div");

    for (var i = 0; i < divs.length; i++)
    {
        if (divs[i].id)
        {
            expandCollapse(frame.document, divs[i].id, 1);
        }
    }
}   /* expandAll */


/* collapseAll() -- Collapses all expand/collapse list items in the specified frame.
 */
function collapseAll(frame)
{
    var divs = frame.document.getElementsByTagName("div");

    for (var i = 0; i < divs.length; i++)
    {
        if (divs[i].id)
        {
            expandCollapse(frame.document, divs[i].id, -1);
        }
    }
}   /* contractAll */


/* adjustIFrameSize -- Dynamically adjusts the size of an iframe object.  Note
 * that this implementation is very specific to the EQware Projects.html page.
 *
 * Based on adjustIFrameSize() from a web page by Keith G and Martin Honnen,
 * dated Aug 4th, 2005 16:52.
 */
function adjustIFrameSize(iframeID, followingID) 
{
    var iframe = document.getElementById(iframeID);
    var following = document.getElementById(followingID);

    if (iframe)
    {
        var winHeight = Geometry.getViewportHeight();
        var iframeTop = getTop(iframe);
        var followingHeight = 0;

        if (following)
        {
            var followingHeight = following.offsetHeight;
        }

        /* The value 32 matches the iframe margin-bottom, plus enough
         * extra to prevent scrolling.
         */
        iframe.height = (winHeight - iframeTop - followingHeight - 32) + "px";
    }
}   /* adjustIFrameSize */


/* doRollovers -- Finds all <img> tags in the document with a "rollover"
 *  attribute, and performs the appropriate rollover, if any.
 *
 * Based on initRollovers() from "JavaScript: The Definitive Guide, 5th Edition" by David Flanagan. 
 */
function doRollovers()
{
    var images = document.getElementsByTagName("img");

    for (var i = 0; i < images.length; i++)
    {
        var imageURL = images[i];
        var rolloverURL = imageURL.getAttribute("rollover");
        if (rolloverURL) doRollover(imageURL, rolloverURL);
    }
}   /* doRollovers */


/* doRollover -- Handles rollover for the specified image.
 *
 * Based on addRollover() from "JavaScript: The Definitive Guide, 5th Edition" by David Flanagan. 
 */
function doRollover(imgURL, rolloverURL)
{
    /* If the specified imageURL isn"t an image, return silently.
     */
    if (imgURL.tagName.toLowerCase() != "img") return;

    /* Remember the original URL of the image, for onmouseout.
     */
    var baseURL = imgURL.src;

    /* Preload the rollover image into cache.
     */
    (new Image()).src = rolloverURL;

    /* Perform the rollover operation.
     */
    imgURL.onmouseover = function() { imgURL.src = rolloverURL; }
    imgURL.onmouseout  = function() { imgURL.src = baseURL; }

}   /* doRollover */


/* getTop() -- Determine the top of the specified element by summing
 * the offsets from each parent in turn.
 *
 * Based on getX() from "JavaScript: The Definitive Guide, 5th Edition" by David Flanagan. 
 */
function getTop(element)
{
    var top = 0;

    while (element)
    {
        top += element.offsetTop;
        element = element.offsetParent;
    }
    return top;

}   /* getTop */


/* addListeners() -- Add event listeners.
 */
function addListeners()
{
    /* Set event handlers to use initRollovers.
     */
    if (window.addEventListener)
    {
        window.addEventListener("load", doRollovers, false);

    }
    else
    {
        window.attachEvent("onload", doRollovers);
    }
}   /* addListeners */


/* initGeometry() -- Initialize the global Geometry object,
 * which provides a browser-independent method of getting window geometry.
 */
function initGeometry()
{
    /* Establish a standard method of getting window geometry.
     */
    if (window.screenTop)
    {
        Geometry.getWindowTop
            = function() { return window.screenTop; };
    }
    else
    {
        Geometry.getWindowTop
            = function() { return window.screenY; };
    }
    
    if (Geometry.innerWidth)
    {
        Geometry.getViewportHeight 
            = function() { return window.innerHeight; };
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    {
        Geometry.getViewportHeight 
            = function() { return document.documentElement.clientHeight; };
    }
    else
    {
        Geometry.getViewportHeight
            = function() { return document.body.clientHeight; };
    }
}   /* initGeometry */


/*** BODY ************************************************/

addListeners();
initGeometry();



