<!--//

function hoverLeftNavOff(linkElement)
{
    linkElement.style.borderBottomColor = '';
    linkElement.style.color = '';
}

function hoverLeftNavOn(linkElement)
{
    linkElement.style.borderBottomColor = '#003F5F';
    linkElement.style.color = '#003F5F';
}

function hoverSubMenuItemOff(linkElement)
{
    linkElement.style.backgroundColor = '';
    linkElement.style.color = '';
}

function hoverSubMenuItemOn(linkElement)
{
    linkElement.style.backgroundColor = '#eeeeee';
    linkElement.style.color = '#000000';
}

function navLocal(link)
{
    document.location.href = link;
}

var gExWindowName = "exWindow";
var gExWindow = null;
function navExternal(link)
{
    var options = 'toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1,width=768,height=576';
    gExWindow = window.open(link, gExWindowName, options);
}

function getSourceElement(e)
{
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;

    return targ;
}

function showMenu(e, menuID)
{
    if (window.currentMenu)
        hideMenu();

    var targ = getSourceElement(e);

    // Calculate offsetX & offsetY if necessary...
    if (!e.offsetX) {
        var element = targ;
        var CalculatedTotalOffsetLeft = CalculatedTotalOffsetTop = 0;
        while (element.offsetParent) {
            CalculatedTotalOffsetLeft += element.offsetLeft;
            CalculatedTotalOffsetTop += element.offsetTop;
            element = element.offsetParent;
        }
        e.offsetX = e.pageX - CalculatedTotalOffsetLeft;
        e.offsetY = e.pageY - CalculatedTotalOffsetTop;

    } else {
        e.pageX = e.clientX;
        e.pageY = e.clientY;
    }

    if (!targ.menuElement)
        targ.menuElement = document.getElementById(menuID);

    var menuBox = targ.menuElement;

    menuBox.style.top = (e.pageY - e.offsetY) + 'px';
    menuBox.style.left = ((e.pageX - e.offsetX) + 10) + 'px';
    menuBox.style.display = 'block';

    window.currentMenu = targ;

    startScrollDown(menuBox);
}

function handleMenu(e)
{
    if (window.currentMenu) {
        var eID = "";
        var srcElem = getSourceElement(e);
        if (srcElem.id == '' && srcElem.parentNode) {
            if (srcElem.parentNode)
                eID = srcElem.parentNode.id;
            else
                hideMenu();

        } else {
            eID = srcElem.id;
        }

        if (eID != window.currentMenu.id) {
            if (eID != window.currentMenu.menuElement.id) {
                hideMenu();
            }
        }
    }
}

function hideMenu()
{
    //startScrollUp(window.currentMenu.menuElement);
    window.currentMenu.menuElement.style.display = 'none';
    window.currentMenu = null;
}

/*
 * Collapsable Element Scrolling
 ***********************************************/

var isScrollingUp = false;
var isScrollingDown = false;

// TODO: this function needs work.
function canScroll(element)
{
    if (!element.style.height || !element.scrollHeight)
        return false;

    return true;
}

function startScrollDown(collapsableElement)
{
    if (isScrollingDown)
        window.stopScrollDown();

    isScrollingDown = true;

    var scrollElement       = collapsableElement;
    var scrollHeight        = 0;
    var scrollStop          = 0;

    if (scrollElement.originalHeight) {
        scrollStop = scrollElement.originalHeight;

    } else {
        scrollStop = (scrollElement.scrollHeight); // Just to offset the slightly exaggerated default.
        scrollElement.originalHeight = scrollStop;
    }

    window.incrementScroll = function()
    {
        scrollHeight += Math.floor((scrollStop - scrollHeight) / 4);

        if (scrollHeight >= (scrollStop - 5)) {
            window.stopScrollDown();
            return;
        }

        scrollElement.style.height = scrollHeight + 'px';
        window.scrollDownTimeoutID = setTimeout("incrementScroll()", 1);
    }

    window.stopScrollDown = function()
    {
        if (window.scrollDownTimeoutID)
            clearTimeout(window.scrollDownTimeoutID);

        collapsableElement.style.height = (scrollStop) + 'px';
        isScrollingDown = false;
    }

    incrementScroll();
    return true;
}

function startScrollUp(collapsableElement)
{
    if (isScrollingUp)
        window.stopScrollUp();

    isScrollingUp = true;

    var scrollElement   = collapsableElement;
    var scrollHeight    = 0;

    if (scrollElement.originalHeight) {
        scrollHeight = scrollElement.originalHeight;

    } else {
        scrollHeight = (scrollElement.scrollHeight - 5); // Just to offset the slightly exaggerated default.
        scrollElement.originalHeight = scrollHeight;
    }

    window.decrementScroll = function()
    {
        scrollHeight -= Math.floor((scrollHeight + 5) / 2);

        if (scrollHeight <= 1) {
            window.stopScrollUp();
            return;
        }

        scrollElement.style.height = scrollHeight + 'px';
        window.scrollUpTimoutID = setTimeout("decrementScroll()", 1);
    }

    window.stopScrollUp = function()
    {
        if (window.scrollUpTimeoutID)
            clearTimeout(window.scrollUpTimeoutID);

        scrollElement.style.height = '';
        scrollElement.style.display = 'none';
        isScrollingUp = false;
    }

    decrementScroll();
    return true;
}

/**************************
 * Calendar.
 */

//var calendarFadeInObject;
//var calendarFadeOutObject;

function callCalendar(e, elemName)
{
    if (!e.clientX)
        var e = window.event;

    var elem = document.getElementById(elemName);

    CalTextElement = elem;
    CalDisplayElement = document.getElementById('calendarHere');
    /*
    if (!calendarFadeInObject) {
        calendarFadeInObject = new Fade(calendarElement, 0, 100);
        calendarFadeOutObject = new Fade(calendarElement, 100, 0);
    }
    */

    SelectedDay = new Date();
    if (isValidDate(elem.value)) {
        SelectedDay = new Date(elem.value);
    }
    
    var pY = e.clientY;
    if (document.documentElement.scrollTop) {
        pY += document.documentElement.scrollTop;

    } else if (document.body.scrollTop) {
        pY += document.body.scrollTop;

    } else if (window.pageYOffset) {
        pY += window.pageYOffset;
    }

    showCalendar(e.clientX, pY);
}

function isValidDate(dateString)
{
    var test = new Date(dateString);
    if (test != "Invalid Date" && test != "NaN") {
        return true;
    }
    return false;
}


/**************************
 * Fade logic.
 */

function setOpacity(fElement, hundredScaleValue)
{
    var s = (document.all) ? 'filter' : 'MozOpacity';
    fElement.style[s] = (document.all) ? 'alpha(opacity=' + hundredScaleValue + ')' : (hundredScaleValue/100);
}

function Fade(element, start, end)
{
    this.element = element;
    this.start = start;
    this.end = end;
    this.opacity = this.start;
    this.timeoutID = -1;

    this.reset = function() {
        this.opacity = this.start;
    }

    return this;
}

function findFade(fadeObject, fadeArray)
{
    for (var i=0; i<fadeArray.length; i++) {
        if (fadeArray[i].element.id == fadeObject.element.id)
            return i;
    }
    return -1;
}

var fadeInElements = new Array();
var fadeOutElements = new Array();

function startFadeIn(fadeObject)
{
    window.incrementOpacity = function(arrayIndex)
    {
        var f = fadeInElements[arrayIndex];
        f.opacity += ((f.end - f.opacity) / 5);

        if (f.opacity >= (f.end - 1)) {
            window.stopFadeIn(f);
            return;
        }

        setOpacity(f.element, f.opacity);
        f.timoutID = setTimeout("incrementOpacity(" + arrayIndex + ")", 1);
    }

    window.stopFadeIn = function(fadeObject)
    {
        clearTimeout(fadeObject.timeoutID);
        setOpacity(fadeObject.element, fadeObject.end);
    }

    fadeObject.reset();
    var fIndex = findFade(fadeObject, fadeInElements);
    if (fIndex < 0) {
        fIndex = fadeInElements.push(fadeObject);
        fIndex--;
    }

    incrementOpacity(fIndex);

    return true;
}

function startFadeOut(fadeObject)
{
    window.decrementOpacity = function(arrayIndex)
    {
        var f = fadeOutElements[arrayIndex];
        f.opacity -= ((f.opacity - f.end) / 5);

        if (f.opacity <= (f.end + 1)) {
            window.stopFadeOut(f);
            return;
        }

        setOpacity(f.element, f.opacity);
        f.timoutID = setTimeout("decrementOpacity(" + arrayIndex + ")", 1);
    }

    window.stopFadeOut = function(fadeObject)
    {
        clearTimeout(fadeObject.timeoutID);
        setOpacity(fadeObject.element, fadeObject.end);
        fadeObject.element.style.display = 'none';
    }

    fadeObject.reset();
    var fIndex = findFade(fadeObject, fadeOutElements);
    if (fIndex < 0) {
        fIndex = fadeOutElements.push(fadeObject);
        fIndex--;
    }

    decrementOpacity(fIndex);

    return true;
}


/***********************************************/

//-->
