// Authored by Ara Jamgochian of Highland Consulting Group Inc.
// Copyright (c) 2004-2007 Many Mind LLC. All rights reserved.
// Copying or any other use of this material is strictly prohibited.

// prevent error messages to player
window.onerror=new Function("return true");

// general control variables
var mIE          = 0;
var visibleId    = "";
var timerId      = "";
var sinceCounter = 0;

// menu coordinates
var menuTop      = 0;
var menuBottom   = 0;
var menuLeft     = 0;
var menuRight    = 0;
var menuWidth    = 0;
var menuHeight   = 0;
var mouseX       = 0;
var mouseY       = 0;
var downX        = -1;
var downY        = -1;

// ******************************************************************************
// show drop down menu

function onMenu(name)
{
    // if the same menu is already visible (fail safe code)
    if ((document.all[name].style.visibility == 'visible')  ||
        (visibleId == name))
        return;

    // menu is on -- initialize
    sinceCounter = 0;
    downX        = -1;
    downY        = -1;
    if (navigator.appName.indexOf("Internet Explorer") != -1)
        mIE = 1;

    // if different window is visible
    if (visibleId != "")
        killMenu (visibleId);
    visibleId = name;

    // get page information
    var pageL, pageR, pageT, pageB;
    if (mIE > 0)
    {
        pageL = window.document.body.scrollLeft;
        pageR = pageL + window.document.body.offsetWidth - 1;
        pageT = window.document.body.scrollTop;
        pageB = pageT + window.document.body.offsetHeight - 1;
    }
    else
    {
        pageL = window.pageXOffset;
        pageR = pageL + window.innerWidth - 1;
        pageT = window.pageYOffset;
        pageB = pageT + window.innerHeight - 1;
    }

    // get menu size
    menuHeight = document.all[name].style.posHeight;
    menuWidth  = document.all[name].style.posWidth;
    menuTop    = pageT + window.event.y - 10;
    menuLeft   = pageL + window.event.x - (menuWidth / 2);
    menuBottom = menuTop + menuHeight - 1;
    menuRight  = menuLeft + menuWidth - 1;

    // try to fit the inner window
    var chgX   = 0;
    var chgY   = 0;
    if (menuRight > pageR)
        chgX   = pageR - menuRight;
    if (menuLeft < pageL)
        chgX   = pageL - menuLeft;
    if (menuBottom > pageB)
        chgY   = pageB - menuBottom;
    if (menuTop < pageT)
        chgY   = pageT - menuTop;

    // reposition as necessary
    menuLeft   += chgX;
    menuRight  += chgX;
    menuTop    += chgY;
    menuBottom += chgY;

    // set menu position
    document.all[name].style.posLeft = menuLeft;
    document.all[name].style.posTop  = menuTop;

    // activate menu
    document.all[name].style.visibility   = 'visible';
    document.all[name].style.zIndex       = 1;
    document.all['theWhole'].style.zIndex = 2;

    // start timer function
    timerId = window.setInterval("timerFunction()", 50, "JavaScript");

}


// ******************************************************************************
// self starting function to monitor mouse position

function timerFunction()
{

    // if menu already gone off
    if (visibleId == "")
        return;

    // count how long outside
    if ((mouseY < menuTop)     ||
        (mouseY > menuBottom)  ||
        (mouseX < menuLeft)    ||
        (mouseX > menuRight))
        ++sinceCounter;
    else
        sinceCounter = 0;

    // if long enough outside, kill menu
    if (sinceCounter > 4)
        killMenu(visibleId);

}


// ******************************************************************************
// get mouse position, and drag menu if necessary

function moveMouse()
{

    // if no menu...
    if (visibleId == "")
        return;

    // get page information
    var pageL, pageT;
    if (mIE > 0)
    {
        pageL = window.document.body.scrollLeft;
        pageT = window.document.body.scrollTop;
    }
    else
    {
        pageL = window.pageXOffset;
        pageT = window.pageYOffset;
    }

    // save position
    mouseX = pageL + window.event.x;
    mouseY = pageT + window.event.y;

    // if not on menu...
    if ((mouseY < menuTop)     ||
        (mouseY > menuBottom)  ||
        (mouseX < menuLeft)    ||
        (mouseX > menuRight))
        return;

    // if mouse not down
    if ((downX < 0)  ||
        (downY < 0))
        return;

    // drag menu -- set and save new menu position
    var chgX    = mouseX - downX;
    var chgY    = mouseY - downY;
    menuTop    += chgY;
    menuLeft   += chgX;
    menuBottom += chgY;
    menuRight  += chgX;
    downX       = mouseX;
    downY       = mouseY;

    // drag menu -- reposition menu
    document.all[visibleId].style.posLeft = menuLeft;
    document.all[visibleId].style.posTop  = menuTop;

}


// ******************************************************************************
// mouse is pressesed, start dragging the menu

function mouseDown()
{

    downX = window.event.x;
    downY = window.event.y;

}


// ******************************************************************************
// mouse is released, stop dragging the menu

function mouseUp()
{

    downX = -1;
    downY = -1;

}


// ******************************************************************************
// kill drop down menu

function killMenu (name)
{

    // stop timer
    window.clearInterval(timerId);
    timerId   = "";
    visibleId = "";

    // hide menu
    document.all[name].style.zIndex       = 2;
    document.all['theWhole'].style.zIndex = 1;
    document.all[name].style.visibility   = 'hidden';

}


