﻿/*
 *  This script adjusts the left navigation
 *  flyout menu so it is as viewable in the
 *  viewport as possible.
 *
 *  Jarrod Smith
 *  www.ascedia.com
 *  9/17/2007
 *
 *  http://www.onlamp.com/pub/a/onlamp/2007/07/05/writing-advanced-javascript.html
 *
 */ 

function getElementAbsPosX(el)
{
    var dx = 0;
    if (el.offsetParent) {
        dx = el.offsetLeft + 8;
        while (el = el.offsetParent) {
            dx += el.offsetLeft;
        }
    }
    return dx;
}

function getElementAbsPosY(el)
{
    var dy = 0;
    if (el.offsetParent) {
        dy = el.offsetTop + el.offsetHeight / 2;
        while (el = el.offsetParent) {
            dy += el.offsetTop;
        }
    }
    return dy;
}

function GetAbsWindowBottom()
{
    // Compute the bottom of the popup window and the bottom of
    // the browser window, in absolute co-ordinates - different
    // on all browsers but the below should be accurate usually!
 
    var abswindowbottom = 0;
    if (typeof(window.innerHeight) == 'number')
        abswindowbottom = window.innerHeight;
    else if (document.documentElement && document.documentElement.clientHeight)
        abswindowbottom = document.documentElement.clientHeight;
    else if (document.body && document.body.clientHeight)
        abswindowbottom = document.body.clientHeight;
 
    if (typeof(window.pageYOffset) == 'number')
        abswindowbottom = abswindowbottom + window.pageYOffset;
    else if (document.body && document.body.scrollTop)
        abswindowbottom = abswindowbottom + document.body.scrollTop;
    else if (document.documentElement && document.documentElement.scrollTop)
        abswindowbottom = abswindowbottom + document.documentElement.scrollTop;
    return abswindowbottom;
}

function PopupMenu(name)
{
    var el = name + 'menu';
    var tag = name + 'menuroot';
    
    if (!document.getElementById(el))  // menu object not found
        return;

    // Get main leftNav element position
    var dy = getElementAbsPosY(document.getElementById(tag));
    
    // Get subMenu element position
    var abssubmenutop = getElementAbsPosY(document.getElementById(el));

    // Compare bottom of leftNav nested ul to bottom of window
    var abspopupbottom = dy + document.getElementById(el).clientHeight;
    var abswindowbottom = GetAbsWindowBottom();
    
    // If menu goes below bottom of window, move it up!
    if (abspopupbottom > abswindowbottom)
    {
        if (document.getElementById(el).clientHeight <= abswindowbottom)
        {
            dy = dy - (abspopupbottom - abswindowbottom);
            if (getElementAbsPosY(document.getElementById(tag)) < dy + document.getElementById(el).clientHeight)
            {
                dy = getElementAbsPosY(el) - (abspopupbottom - abswindowbottom) + 14;
                document.getElementById(el).style.top = dy + 'px';
            }
            else
            {
                dy = dy - getElementAbsPosY(document.getElementById(tag)) + 14;
                document.getElementById(el).style.top = dy + 'px';
            }
        }
        else
        {
            dy = dy - getElementAbsPosY(document.getElementById(tag));
            document.getElementById(el).style.top = dy + 'px';
            document.getElementById(el).style.height = abswindowbottom + 'px';
            document.getElementById(el).style.width = '133px';
        }
    }
    else
    {
        document.getElementById(el).style.top = "auto";
    }
}