/*
acm.js - AutoClose Menu
Author: Eugene Scripnik <Eugene.Scripnik@itgrp.net>
Version: 0.8.1
Copyright (c) 2003 Eugene Scripnik. All Rights Reserved.
*/

ACM_ISDOM = document.getElementById ? true : false;
ACM_ISIE = document.all ? true : false;
ACM_ISNS4 = document.layers ? true : false;
ACM_NS6 = (navigator.vendor == ("Netscape6") || navigator.product == ("Gecko"));
ACM_OP = navigator.userAgent.indexOf('Opera') != -1

ACM_PARENTS = {};
ACM_STACK = [];
ACM_CLOSE_TIMER = null;
ACM_CURRENT_MENU = null;
ACM_CURRENT_ITEM = null;
ACM_HIDE_TIMEOUT_OVER = 200;

function acmRealTarget(event) {
    if (ACM_ISIE) {
        return event.srcElement;
    }
    return event.currentTarget;
}

function acmGetById(elId) {
    if (ACM_ISDOM) {
        return document.getElementById(elId);
    } else if (ACM_ISIE) {
        return document.all[elId];
    } else if (ACM_ISNS4) {
        return window[elId];
    }
    return null;
}

function acmSetClass(element, cls) {
    if (ACM_ISDOM) {
        element.className = cls;
    }
}

function acmHide(elId) {
    var element = acmGetById(elId);
    if (!element) return;

    if (ACM_ISDOM || ACM_ISIE) {
        element.style.visibility = 'hidden';
    } else if (ACM_ISNS4) {
        element.visibility = 'hide';
    }
}

function acmShow(elId) {
    var element = acmGetById(elId);
    if (!element) return;

    if (ACM_ISDOM || ACM_ISIE) {
        element.style.visibility = 'visible';
    } else if (ACM_ISNS4) {
        element.visibility = 'show';
    }
}

function acmPageX() {
  if (window.pageXOffset) return window.pageXOffset;
  if (document.documentElement && document.documentElement.scrollLeft) return document.documentElement.scrollLeft;
  if (document.body && document.body.scrollLeft) return document.body.scrollLeft;
  return 0;
}

function acmPageY() {
  if (window.pageYOffset) return window.pageYOffset;
  if (document.documentElement && document.documentElement.scrollTop) return document.documentElement.scrollTop;
  if (document.body && document.body.scrollTop) return document.body.scrollTop;
  return 0;
}

function acmEventMouseX(e) {
    if (e.pageX) return e.pageX;
    if (window.opera) return e.clientX;
    return e.clientX + acmPageX();
}

function acmEventMouseY(e) {
    if (e.pageY) return e.pageY;
    if (window.opera) return e.clientY;
    return e.clientY + acmPageY();
}

function acmSetPosition(element, x, y) {
    if (ACM_ISDOM) {
        element.style.left = x;
        element.style.top = y;
    } else if (ACM_ISIE) {
        element.style.pixelLeft = x;
        element.style.pixelTop = y;
    } else if (ACM_ISNS4) {
        element.moveTo(x, y);
    }
}

function acmSetPositionRel(whereId, whatId, dx, dy) {
    var where = acmGetById(whereId);
    if (!where) return;
    var what = acmGetById(whatId);
    if (!what) return;

    var x = where.offsetLeft;
    var y = where.offsetTop;
    var parent = where.offsetParent;
    while (parent.tagName.toUpperCase() != "BODY") {
        x += parent.offsetLeft;
        y += parent.offsetTop;
        parent = parent.offsetParent;
    }
    acmSetPosition(what, x + dx, y + dy);
}

function acmSetPositionDown(item, menuId) {
    var menu = acmGetById(menuId);
    if (!menu) return;

    var x = item.offsetLeft;
    var y = item.offsetTop;
    var parent = item.offsetParent;
    while (parent.tagName.toUpperCase() != "BODY") {
        x += parent.offsetLeft;
        y += parent.offsetTop;
        parent = parent.offsetParent;
    }
    acmSetPosition(menu, x, y + item.offsetHeight);
}

function acmSetPositionRight(item, menuId) {
    var menu = acmGetById(menuId);
    if (!menu) return;

    var x = item.offsetLeft;
    var y = item.offsetTop;
    var parent = item.offsetParent;
    while (parent.tagName.toUpperCase() != "BODY") {
        x += parent.offsetLeft;
        y += parent.offsetTop;
        parent = parent.offsetParent;
    }
    acmSetPosition(menu, x + item.offsetWidth, y);
}

function acmOpenDown(item, hlclass, submenu, parentmenu) {
    if (!item.passiveClassName) item.passiveClassName = item.className;

    if (ACM_CLOSE_TIMER) clearTimeout(ACM_CLOSE_TIMER);
    ACM_CLOSE_TIMER = null;

    ACM_CURRENT_ITEM = item.id;
    ACM_CURRENT_MENU = parentmenu;

    for (var i = ACM_STACK.length - 1; i >= 0; i--) {
        if (ACM_STACK[i] == parentmenu) break;

        acmHide(ACM_STACK[i]);

        var ownerid = eval('ACM_PARENTS.' + ACM_STACK[i]);
        var owner = acmGetById(ownerid);
        if (owner && owner.passiveClassName) acmSetClass(owner, owner.passiveClassName);

        ACM_STACK[i] = null;
    }
    ACM_STACK.length = i + 1;

    ACM_STACK[ACM_STACK.length] = submenu;
    eval('ACM_PARENTS.' + submenu + '="' + item.id + '"');

    acmSetClass(item, hlclass);
    acmSetPositionDown(item, submenu);
    acmShow(submenu);
}

function acmOpenRight(item, hlclass, submenu, parentmenu) {
    if (!item.passiveClassName) item.passiveClassName = item.className;

    if (ACM_CLOSE_TIMER) clearTimeout(ACM_CLOSE_TIMER);
    ACM_CLOSE_TIMER = null;

    ACM_CURRENT_ITEM = item.id;
    ACM_CURRENT_MENU = parentmenu;

    for (var i = ACM_STACK.length - 1; i >= 0; i--) {
        if (ACM_STACK[i] == parentmenu) break;

        acmHide(ACM_STACK[i]);

        var ownerid = eval('ACM_PARENTS.' + ACM_STACK[i]);
        var owner = acmGetById(ownerid);
        if (owner && owner.passiveClassName) acmSetClass(owner, owner.passiveClassName);

        ACM_STACK[i] = null;
    }
    ACM_STACK.length = i + 1;

    ACM_STACK[ACM_STACK.length] = submenu;
    eval('ACM_PARENTS.' + submenu + '="' + item.id + '"');

    acmSetClass(item, hlclass);
    acmSetPositionRight(item, submenu);
    acmShow(submenu);
}

function acmOpenHere(item, event, submenu) {
    if (ACM_CLOSE_TIMER) clearTimeout(ACM_CLOSE_TIMER);
    ACM_CLOSE_TIMER = null;

    ACM_CURRENT_ITEM = item.id;
    ACM_CURRENT_MENU = null;

    for (var i = ACM_STACK.length - 1; i >= 0; i--) {
        acmHide(ACM_STACK[i]);

        var ownerid = eval('ACM_PARENTS.' + ACM_STACK[i]);
        var owner = acmGetById(ownerid);
        if (owner && owner.passiveClassName) acmSetClass(owner, owner.passiveClassName);

        ACM_STACK[i] = null;
    }
    ACM_STACK.length = 0;

    ACM_STACK[0] = submenu;
    eval('ACM_PARENTS.' + submenu + '="' + item.id + '"');

    var menu = acmGetById(submenu);
    acmSetPosition(menu, acmEventMouseX(event), acmEventMouseY(event));
    acmShow(submenu);
}

function acmClose() {
    if (ACM_CLOSE_TIMER) clearTimeout(ACM_CLOSE_TIMER);
    ACM_CURRENT_MENU = null;
    ACM_CURRENT_ITEM = null;
    ACM_CLOSE_TIMER = setTimeout(acmCloseLast, ACM_HIDE_TIMEOUT_OVER);
}

function acmActivate(menu) {
    ACM_CURRENT_MENU = menu.id;
    if (!ACM_CLOSE_TIMER) ACM_CLOSE_TIMER = setTimeout(acmCloseLast, ACM_HIDE_TIMEOUT_OVER);
}

function acmHighlight(item, hlclass, parentmenu) {
    if (!item.passiveClassName) item.passiveClassName = item.className;

    if (parentmenu) {
        if (ACM_CLOSE_TIMER) clearTimeout(ACM_CLOSE_TIMER);
        ACM_CLOSE_TIMER = null;

        ACM_CURRENT_ITEM = null;
        ACM_CURRENT_MENU = parentmenu;

        for (var i = ACM_STACK.length - 1; i >= 0; i--) {
            if (ACM_STACK[i] == parentmenu) break;

            acmHide(ACM_STACK[i]);

            var ownerid = eval('ACM_PARENTS.' + ACM_STACK[i]);
            var owner = acmGetById(ownerid);
            if (owner && owner.passiveClassName) acmSetClass(owner, owner.passiveClassName);

            ACM_STACK[i] = null;
        }
        ACM_STACK.length = i + 1;
    }
    acmSetClass(item, hlclass);
    item.onmouseout = acmDehighlight;
}

function acmDehighlight(e) {
    var element = ACM_ISIE ? event.srcElement : e.currentTarget;
    if (ACM_ISIE) {
        while (element && !element.passiveClassName) {
            element = element.parentNode;
        }
    }
    acmSetClass(element, element.passiveClassName);
}

function acmCloseLast() {
    if (ACM_STACK.length > 0) {
        var lastid = ACM_STACK[ACM_STACK.length - 1];
        var ownerid = eval('ACM_PARENTS.' + lastid);
        if (ACM_CURRENT_MENU != lastid && ACM_CURRENT_ITEM != ownerid) {
            var owner = acmGetById(ownerid);
            if (owner && owner.passiveClassName) acmSetClass(owner, owner.passiveClassName);

            acmHide(lastid);

            ACM_STACK[ACM_STACK.length - 1] = null;
            ACM_STACK.length--;

            ACM_CLOSE_TIMER = setTimeout(acmCloseLast, ACM_HIDE_TIMEOUT_OVER);
        }
    } else {
        ACM_CLOSE_TIMER = null;
    }
}

function acmClick(url) {
    if (ACM_ISIE && !ACM_OP) {
        var link = document.createElement('A');
        if (link) {
            link.href = url;
            document.body.insertBefore(link);
            link.click();
            return;
        }
    }
    document.location.href = url;
}