﻿var timeout;
var menus = Array();
//var $j = jQuery.noConflict();
var isIE = jQuery.browser.msie;
var pageLoaded = false;

if (window.addEventListener)
window.addEventListener("load", pageLoad, false);
else if (window.attachEvent)
window.attachEvent("onload", pageLoad);

function pageLoad() {
pageLoaded = true;
if (isIE) {
// Finesse IE into not displaying "Click to activate and use this control"
objects = document.getElementsByTagName("object");
for (var i = 0; i < objects.length; i++) {
objects[i].outerHTML = objects[i].outerHTML;
}
}
}
function DOMBrowser() {
return document.body && document.body.style && document.getElementById;
}
function getX(elem) {
var x = 0;
if (elem.offsetParent)
x = getX(elem.offsetParent);
return x + elem.offsetLeft;
}
function getY(elem) {
var y = 0;
if (elem.offsetParent)
y = getY(elem.offsetParent);
return y + elem.offsetTop;
}
var lastMenu;
function enterMenu(elem, below) {
if (DOMBrowser()) {
parentDiv = elem.offsetParent.id;
hideMenus(parentDiv, elem);
showSubmenu(elem, below);
if (timeout)
clearTimeout(timeout);
}
return true;
}
function leaveMenu(elem) {
if (DOMBrowser()) {
lastMenu = "";
if (elem)
lastMenu = elem.offsetParent.id;
timeout = setTimeout("hideMenus()", 500);
}
return true;
}
function getXPos(elem, child, below) {
child.visible = false;
// Temporarily set the display to block so that we can calculate offsetWidth/Height
child.style.display = "block";
parentX = getX(elem);
if (below)
x = parentX + 5;
else
x = parentX + elem.offsetWidth - 5;
if (x + child.offsetWidth > document.body.clientWidth + document.body.scrollLeft) { // If the menu would appear offscreen:
if (parentX - child.offsetWidth - document.body.scrollLeft + 5 > 0) { // If there's room to the left of the parent menu:
x = parentX - child.offsetWidth + document.body.scrollLeft + 5;
} else { // Fit as much in as possible:
availSpace = (document.body.clientWidth - elem.offsetWidth) / 2;
x = document.body.clientWidth + document.body.scrollLeft - child.offsetWidth;
if (parentX > availSpace)
x = 0;
//Don't overlap the parent:
if (x == parentX) x -= 5;
}
}
child.style.display = "none";
return x;
}
function getYPos(elem, child, below) {
child.visible = false;
// Temporarily set the display to block so that we can calculate offsetWidth/Height
child.style.display = "block";
parentY = getY(elem);
if (below)
y = parentY + elem.offsetHeight - 5;
else
y = parentY + 5;
if (y + child.offsetHeight > document.body.clientHeight + document.body.scrollTop) { // If the menu would appear offscreen:
if (parentY - child.offsetHeight - document.body.scrollTop + 20 > 0) { // If there's room above the parent menu:
y = parentY - child.offsetHeight + document.body.scrollTop + 20;
} else { // Fit as much in as possible:
y = document.body.clientHeight + document.body.scrollTop - child.offsetHeight - 5;
if (y < 0) y = 0;
}
}
child.style.display = "none";
return y;
}
function showSubMenu(id) { }
function showSubmenu(elem, below) {
var id = elem.id + "_submenu";
menu = document.getElementById(id);

if (jQuery(menu).hasClass("selected_menu")) {
    window.status = 'ok';
    return;
}
else {
    window.status = 'false';
}

if (menu && lastMenu != id) {
// Reset display so that browsers are not confused when we postion (Opera in particular)
// No longer needed?  11/30/05 MO
//menu.style.display = "none";

//menu.style.top = getYPos(elem, menu, below);
jQuery(menu).css("top", getYPos(elem, menu, below));
//menu.style.left = getXPos(elem, menu, below);
jQuery(menu).css("left", getXPos(elem, menu, below));
//menu.style.zIndex = 1000;
jQuery(menu).css("zIndex", 1000);
//menu.style.display = "block";
jQuery(menu).css("display", "block");

tab = document.getElementById(elem.id + "_img");
if (tab)
tab.src = tab.src.replace(/(.*)_off\.(.*)/, "$1_on.$2");
// Only create the iframe element if the page has been loaded.  Otherwise, 
// "Operation aborted" messages appear and the page won't load.
if (isIE && pageLoaded) {
var ifr = document.createElement("IFRAME");
if (ifr) {
document.body.appendChild(ifr);
ifr.style.position = "absolute";
ifr.style.width = menu.offsetWidth;
ifr.style.height = menu.offsetHeight;
ifr.style.top = menu.style.top;
ifr.style.left = menu.style.left;
ifr.style.zIndex = menu.style.zIndex - 1;
ifr.style.display = "block";
menu.iframe = ifr;
}
}
menus.push(menu);
}
}
function hideMenus(elemID, currentTarget) {
if (currentTarget) {
var nextChild = document.getElementById(currentTarget.id + "_submenu");
// If we're about to re-show the currently displayed menu, bail early to
// avoid flickering
if (nextChild && nextChild.id == lastMenu)
return;
}
// Hide up to the passed element:
var elem;
if (elemID)
elem = document.getElementById(elemID);

if (jQuery(elem).hasClass("selected_menu")) {
    alert('ok');
    return;
}

while (men = menus.pop()) {
if (elem && men.id == elem.id) {
menus.push(men); // Put the match back into the array
return;
} else {
men.style.display = "none";
if (isIE && men.iframe)
document.body.removeChild(men.iframe);
tab = document.getElementById(men.id.replace(/(.*)_submenu/, "$1") + "_img");
if (tab)
tab.src = tab.src.replace(/(.*)_on\.(.*)/, "$1_off.$2");
}
}
}    
