// DHTMLapi.js custom API for cross-platform
// object positioning by Danny Goodman (http://www.dannyg.com).
// Release 2.0. Supports NN4, IE, and W3C DOMs.

// Global variables
var isCSS, isW3C, isIE4, isNN4;

// initialize upon load to let all browsers establish content objects
function initDHTMLAPI()
{
    if (document.images)
    {
        isCSS = (document.body && document.body.style) ? true : false;
        isW3C = (isCSS && document.getElementById) ? true : false;
        isIE4 = (isCSS && document.all) ? true : false;
        isNN4 = (document.layers) ? true : false;
        isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
    }
}

// set event handler to initialize API
//window.onload = initDHTMLAPI;

// Seek nested NN4 layer from string name
function seekLayer(doc, name)
{
    var theObj;
    for (var i = 0; i < doc.layers.length; i++)
    {
        if (doc.layers[i].name == name)
        {
            theObj = doc.layers[i];
            break;
        }
        // dive into nested layers if necessary
        if (doc.layers[i].document.layers.length > 0)
        {
            theObj = seekLayer(doc.layers[i].document, name);
        }
    }
    return theObj;
}

// Convert object name string or object reference
// into a valid element object reference
function getRawObject(obj)
{
    var theObj;
    if (typeof obj == "string")
    {
        if (isW3C)
        {
            theObj = document.getElementById(obj);
        }
        else if (isIE4)
        {
            theObj = document.all(obj);
        }
        else if (isNN4)
        {
            theObj = seekLayer(document, obj);
        }
    }
    else
    {
        // pass through object reference
        theObj = obj;
    }
    return theObj;
}

// Convert object name string or object reference
// into a valid style (or NN4 layer) reference
function getObject(obj)
{
    var theObj = getRawObject(obj);
    if (theObj && isCSS)
    {
        theObj = theObj.style;
    }
    return theObj;
}

// Position an object at a specific pixel coordinate
function shiftTo(obj, x, y)
{
    var theObj = getObject(obj);
    if (theObj)
    {
        if (isCSS)
        {
            // equalize incorrect numeric value type
            var units = (typeof theObj.left == "string") ? "px" : 0;
            theObj.left = x + units;
            theObj.top = y + units;
        }
        else if (isNN4)
        {
            theObj.moveTo(x,y)
        }
    }
}

// Move an object by x and/or y pixels
function shiftBy(obj, deltaX, deltaY)
{
    var theObj = getObject(obj);
    if (theObj)
    {
        if (isCSS)
        {
            // equalize incorrect numeric value type
            var units = (typeof theObj.left == "string") ? "px" : 0;
            theObj.left = getObjectLeft(obj) + deltaX + units;
            theObj.top = getObjectTop(obj) + deltaY + units;
        }
        else if (isNN4)
        {
            theObj.moveBy(deltaX, deltaY);
        }
    }
}

// Set the z-order of an object
function setZIndex(obj, zOrder)
{
    var theObj = getObject(obj);
    if (theObj)
    {
        theObj.zIndex = zOrder;
    }
}

// Toggle the z-index of an object
function toggleZIndex(obj)
{
    var theObj = getObject(obj);
    if (theObj)
    {
        theObj.zIndex = - theObj.zIndex;
    }
}

// Set the background color of an object
function setBGColor(obj, color)
{
    var theObj = getObject(obj);
    if (theObj)
    {
        if (isNN4)
        {
            theObj.bgColor = color;
        }
        else if (isCSS)
        {
            theObj.backgroundColor = color;
        }
    }
}

// Set the display of an object to block
function appear(obj)
{
    var theObj = getObject(obj);
    if (theObj)
    {
        theObj.display = "block";
    }
}

// Set the display of an object to inline
function conjure(obj)
{
    var theObj = getObject(obj);
    if (theObj)
    {
        theObj.display = "inline";
    }
}

// Set the display of an object to none
function vanish(obj)
{
    var theObj = getObject(obj);
    if (theObj)
    {
        theObj.display = "none";
    }
}

// Set the display of a series of objects to none
function vanishAll()
{
    var month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    for(var i = 0; i < 12; i++)
    {
		vanish(month[i]);
    }
}

// Set the visibility of an object to visible
function show(obj)
{
    var theObj = getObject(obj);
    if (theObj)
    {
        theObj.visibility = "visible";
    }
}

// Set the visibility of an object to hidden
function hide(obj)
{
    var theObj = getObject(obj);
    if (theObj)
    {
        theObj.visibility = "hidden";
    }
}

// Toggle the visibility of an object between hidden and visible
function toggleVisibility(elemID)
{
    var theObj = getObject(obj);
    if (theObj)
    {
        theObj.visibility = (theObj.visibility == "hidden") ? "visible" : "hidden";
    }
}

// Retrieve the x coordinate of a positionable object
function getObjectLeft(obj)
{
    var elem = getRawObject(obj);
    var result = 0;
    if (document.defaultView)
    {
        var style = document.defaultView;
        var cssDecl = style.getComputedStyle(elem, "");
        result = cssDecl.getPropertyValue("left");
    }
    else if (elem.currentStyle)
    {
        result = elem.currentStyle.left;
    }
    else if (elem.style)
    {
        result = elem.style.left;
    }
    else if (isNN4)
    {
        result = elem.left;
    }
    return parseInt(result);
}

// Retrieve the y coordinate of a positionable object
function getObjectTop(obj)
{
    var elem = getRawObject(obj);
    var result = 0;
    if (document.defaultView)
    {
        var style = document.defaultView;
        var cssDecl = style.getComputedStyle(elem, "");
        result = cssDecl.getPropertyValue("top");
    }
    else if (elem.currentStyle)
    {
        result = elem.currentStyle.top;
    }
    else if (elem.style)
    {
        result = elem.style.top;
    }
    else if (isNN4)
    {
        result = elem.top;
    }
    return parseInt(result);
}

// Retrieve the rendered width of an element
function getObjectWidth(obj)
{
    var elem = getRawObject(obj);
    var result = 0;
    if (elem.offsetWidth)
    {
        result = elem.offsetWidth;
    }
    else if (elem.clip && elem.clip.width)
    {
        result = elem.clip.width;
    }
    else if (elem.style && elem.style.pixelWidth)
    {
        result = elem.style.pixelWidth;
    }
    return parseInt(result);
}

// Retrieve the rendered height of an element
function getObjectHeight(obj)
{
    var elem = getRawObject(obj);
	var result = 0;
    if (elem.offsetHeight)
    {
        result = elem.offsetHeight;
    }
    else if (elem.clip && elem.clip.height)
    {
        result = elem.clip.height;
    }
    else if (elem.style && elem.style.pixelHeight)
    {
        result = elem.style.pixelHeight;
    }
    return parseInt(result);
}

// Retrieve the left edge of a scrolled window
function findScrollLeft()
{
    if (window.pageXOffset != null)
    {
        return window.pageXOffset;
    }
    if (document.documentElement.scrollLeft != null)
    {
        return document.documentElement.scrollLeft;
    }
	if (document.body.scrollLeft != null)
    {
        return document.body.scrollLeft;
    }
    return (null);
}


// Retrieve the top edge of a scrolled window
function findScrollTop()
{
    if (window.pageYOffset != null)
    {
        return window.pageYOffset;
    }
    if (document.documentElement.scrollTop != null)
    {
        return document.documentElement.scrollTop;
    }
	if (document.body.scrollTop != null)
    {
        return document.body.scrollTop;
    }
	return (null);
}

// Retrieve the mouse pointer x coordinate
function mouseX(evt)
{
    if (evt.pageX)
    {
        return evt.pageX;
    }
    else if (evt.clientX)
    {
        return evt.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    }
    else return null;
}

// Retrieve the mouse pointer y coordinate
function mouseY(evt)
{
    if (evt.pageY)
    {
        return evt.pageY;
    }
    else if (evt.clientY)
    {
        return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    }
    else return null;
}

// Position menu at 26px from the left edge and at the top edge of a scrolled window
function positionMenu()
{
	var menuX = findScrollLeft()+ 26;
	var menuY = findScrollTop();
	shiftTo('menu', menuX, menuY);
}

function positionMenu2()
{
	var menuX = findScrollLeft()+ 26;
	var menuY = findScrollTop();
    alert(menuX + ", " + menuY);
	shiftTo('menu', menuX, menuY);
}


// Display menu at the left edge and at the top edge of a scrolled window
function displayMenu()
{
	positionMenu();
    show('menu');
}


// Position a subMenu at the top right coordinates of the calling anchor
function SNextToC(subMenu, caller)
{
    var theObjS = getObject(subMenu);
    var theObjC = getObject(caller);
    if (theObjS && theObjC)
    {
        if (isCSS)
        {
            // equalize incorrect numeric value type
            var units = (typeof theObjC.left == "string") ? "px" : 0;
            theObjS.left = getObjectLeft(caller) + getObjectWidth(caller) + units;
            theObjS.top = getObjectTop(caller) + units;
        }
        else if (isNN4)
        {
            theObjS.moveTo(getObjectLeft(caller) + getObjectWidth(caller), getObjectTop(caller));
        }
    }
}


// Position a subMenu at the bottom of the calling anchor 40px from the left
function SBelowC(subMenu, caller)
{
    var theObjS = getObject(subMenu);
    var theObjC = getObject(caller);
    if (theObjS && theObjC)
    {
        if (isCSS)
        {
            // equalize incorrect numeric value type
            var units = (typeof theObjC.left == "string") ? "px" : 0;
            if (theObjS)
            {
                theObjS.left = getObjectLeft(caller) + 40 + units;
                theObjS.top = getObjectTop(caller) + getObjectHeight(caller) + units;
            }
        }
        else if (isNN4)
        {
            theObjS.moveTo(getObjectLeft(caller) + 40, getObjectTop(caller) + getObjectHeight(caller));
        }
    }
}

function SBelowC2(subMenu, caller)
{
    var theObjS = getObject(subMenu);
    var theObjC = getObject(caller);
    if (theObjS && theObjC)
    {
        if (isCSS)
        {
            // equalize incorrect numeric value type
            var units = (typeof theObjC.left == "string") ? "px" : 0;
            if (theObjS)
            {
                theObjS.left = getObjectLeft(caller) + 40 + units;
                theObjS.top = getObjectTop(caller) + getObjectHeight(caller) + units;
                alert("getObjectLeft(caller) = " + getObjectLeft(caller) + ", getObjectTop(caller) = " + getObjectTop(caller) + ", getObjectHeight(caller) = " + getObjectHeight(caller));
                alert("theObjS.left = " + theObjS.left + ", theObjS.top = " + theObjS.top);
            }
        }
        else if (isNN4)
        {
            theObjS.moveTo(getObjectLeft(caller) + 40, getObjectTop(caller) + getObjectHeight(caller));
        }
    }
}


// Display subMenu at the rightt edge and at the top edge of the calling anchor
function displaySubMenu(subMenu, caller)
{
	SBelowC(subMenu, caller);
    show(subMenu);
}

function displaySubMenu2(subMenu, caller)
{
	SBelowC2(subMenu, caller);
    show(subMenu);
}

// Extend menubar to the bottom of page
function setMenuBarHeight()
{
    var elem = getRawObject('body');
	var result = 0;
	var theObj = getObject('menuBar');
    if (elem.offsetHeight)
    {
        result = elem.offsetHeight;
    }
	else if (elem.clip && elem.clip.height)
    {
        result = elem.clip.height;
    }
	else if (elem.style && elem.style.pixelHeight)
    {
        result = elem.style.pixelHeight;
    }
	if (theObj)
    {
        theObj.height = parseInt(result);
    }
}

//Cycle menuBar text colors to attract attention to it

var currColor = 0;
var colors, intervalID;
function init()
{
    colors = ["#007777", "#228888", "#449988", "#66AA99", "#88BBAA", "#AACCBB", "#CCDDBB", "#EEEECC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#FFFFCC", "#EEEECC", "#CCDDBB", "#AACCBB", "#88BBAA", "#66AA99", "#449988", "#228888", "#007777"];
}

function cycleColors()
{
    currColor = (++currColor % 60);
    document.getElementById("menuBar").style.color = colors[currColor];
    intervalID = setTimeout("cycleColors()", 100);
}

function initialize()
{
    initDHTMLAPI();
    setMenuBarHeight();
    init();
    cycleColors();
}
