﻿SubMenuPopupTimer = 0;

// Menu Object
function Menu(MenuName, TableClass, RowClass, ActiveClass) {
    if (!window.Menus) Menus = new Array();
    
    this.menuName = MenuName;
    this.items = new Array();
    this.tableClass = TableClass;
    this.rowClass = RowClass;
    this.activeClass = ActiveClass;
    this.container = null;
    
    this.addItem = MenuAddItem;
    this.addJSItem = MenuAddJSItem;
    this.addBreak = MenuAddBreak;
    this.Render = MenuRender;
    this.Hide = MenuHide;
    
    Menus[MenuName] = this;
}

function MenuAddItem(Display,Link) {
    if (Link.indexOf(':') == -1){
        Link = SiteURL + Link;
    }
    this.items.push (new MenuItem(this, Display, Link));
}

function MenuAddJSItem(Display, Link) {
    this.items.push (new MenuJSItem(this, Display, Link));
}

function MenuAddBreak() {
    this.items.push (new MenuBreak());
}

function MenuRender(x, y) {
    if (this.container == null) {
        this.container = document.createElement('DIV');
        this.container.style.position = 'ABSOLUTE';
        this.container.style.zIndex = '999999';
        
        document.body.appendChild(this.container);
        
        var table = document.createElement('TABLE'); 
        table.className = this.tableClass;
        table.cellSpacing=0;
        table.cellPadding=0;
        this.container.appendChild(table);
        
        var TableBody = document.createElement('TBODY');
        table.appendChild (TableBody);
        
        for (var i=0; i<this.items.length; i++) {
            this.items[i].Render(TableBody, this);
        }
        
    } else {
        this.container.style.display='block';
    }
    this.container.style.left = x+'px';
    this.container.style.top = y+'px';
}

function MenuHide() {
    this.container.style.display='none';
    window.status='';
}

// MenuItem Object

function MenuItem(Parent,Display,Link) {
    this.parent = Parent;
    this.display = Display;
    this.link = Link;
    this.Render = MenuItemRender;
}

function MenuItemRender(aTableParent, aParent) {
    var NewRow=document.createElement('TR');
    aTableParent.appendChild (NewRow);
    
    var NewCell=document.createElement('TD');
    NewCell.innerHTML = '&nbsp;<a href="' +this.link+ '">' + this.display + '</a>&nbsp;';
    NewCell.className = aParent.rowClass;
    //NewCell.innerHTML = '&nbsp;<span>'+ this.display + '</span>&nbsp;';
    //NewCell.onclick=MenuItemClick;
    //NewCell.onmouseover = MenuItemMouseOver;
    //NewCell.onmouseout = MenuItemMouseOut;
    NewCell.onmouseover = ClearTimer;
    NewCell.onmouseout = SetNewTimer;
    NewCell.parent = aParent;
    NewCell.menuItem = this;
    NewRow.appendChild(NewCell);
}

function MenuItemMouseOver() {
    this.className = this.parent.activeClass;
    window.status = this.menuItem.link;
    ClearTimer();
}

function MenuItemMouseOut() {
    this.className = this.parent.rowClass;
    SetNewTimer();
}

function MenuItemClick() {
    window.navigate(this.menuItem.link);
}

// MenuJSItem Object

function MenuJSItem(Parent,Display,Link) {
    this.parent = Parent;
    this.display = Display;
    this.link = Link;
    this.Render = MenuJSItemRender;
}

function MenuJSItemRender(aTableParent, aParent) {
    var NewRow=document.createElement('TR');
    aTableParent.appendChild (NewRow);
    
    var NewCell=document.createElement('TD');
    NewCell.innerHTML = '&nbsp;<a href="#">' + this.display + '</a>&nbsp;';
    NewCell.onclick=MenuJSItemClick;
    //NewCell.innerHTML = '&nbsp;<span>' + this.display + '</span>&nbsp;';
    NewCell.className = aParent.rowClass;
    //NewCell.onmouseover = MenuItemMouseOver;
    //NewCell.onmouseout = MenuItemMouseOut;
    NewCell.onmouseover = ClearTimer;
    NewCell.onmouseout = SetNewTimer;
    NewCell.parent = aParent;
    NewCell.menuItem = this;
    NewRow.appendChild(NewCell);
}

function MenuJSItemClick() {
    HideLastMenu();
    eval(this.menuItem.link);
    return false;
}

// MenuBreak Object

function MenuBreak() {
    this.Render = MenuBreakRender;
}

function MenuBreakRender(aTableParent, aParent) {
    var NewRow=document.createElement('TR');
    aTableParent.appendChild (NewRow);
    
    var NewCell=document.createElement('TD');
    NewCell.innerHTML=' ';
    NewCell.className = 'MenuRule';
    NewRow.appendChild(NewCell);
}

// IFramePopup Object

function IFramePopup(IframeName, Class, URL, XOffset) {
    if (!window.Menus) Menus = new Array();    
    this.iframeName = IframeName;
    this.styleClass = Class;
    this.url = URL;
    this.xOffset = (XOffset) ? XOffset : 0;
    this.Render = IFramePopupRender;
    this.Hide = MenuHide;
    Menus[IframeName] = this;
}

function IFramePopupRender(x, y) {
    if (this.container == null) {
        this.container = document.createElement('DIV'); 
        this.container.onmouseover=ClearTimer;
        this.container.onmouseout=SetNewTimer;
        this.container.style.position = 'ABSOLUTE';
        document.body.appendChild(this.container);
        
        var iframe = document.createElement('IFRAME');
        var isSecure = window.location.protocol == 'https:';
        var siteUrl = (isSecure) ? SecureSiteURL : SiteURL;
        iframe.src = siteUrl + this.url;
        iframe.className = this.styleClass;
        iframe.frameBorder=0;
        iframe.marginWidth='0px';
        iframe.marginHeight='0px';
        iframe.scrolling='no'
        this.container.appendChild(iframe);
        
    } else {
        this.container.style.display='block';
    }
    x += this.xOffset;
    this.container.style.left = x+'px';
    this.container.style.top = y+'px';
}

// Timers

function ClearTimer() {
    if (SubMenuPopupTimer!=0){
	    window.clearTimeout(SubMenuPopupTimer);
	    SubMenuPopupTimer=0;
    }
}

function SetNewTimer() {
    ClearTimer();
    SubMenuPopupTimer=window.setTimeout("HideLastMenu()",100,"JavaScript");
}

var currentEventTarget = null;
var currentMenuMouseOutEvent = null;
var currentMenuName = null;

function ShowMenu (MenuName,SrcObject) {
    HideLastMenu();
    var EventTarget = SrcObject;
    var AnchorDiv = document.getElementById('anchorDiv');
    CurrentMenu = Menus[MenuName];
    var x = EventTarget.offsetLeft + AnchorDiv.offsetLeft;
    var y = (MenuName == "IFrameTimeZone") ? EventTarget.offsetTop : AnchorDiv.offsetTop;
    var offSet = 0;
    if (MenuName == 'MenuHelp') offSet = 147;
    if (MenuName == 'IFrameTimeZone') {
        offSet = document.getElementById('cellUnreadContainer').innerHTML == '' ? - 900 : - 550;
        y = y + 20;
    }
    x = x - offSet;
    if (CurrentMenu) {
        currentEventTarget = EventTarget;
        currentMenuMouseOutEvent = EventTarget.onmouseout;
        currentMenuName = MenuName;
        EventTarget.onmouseout = null;
        CurrentMenu.Render(x, y);
    }
}

function HideLastMenu() {
    ClearTimer();
    if (window.CurrentMenu) {
        CurrentMenu.Hide();
        var img = document.getElementById('img' + currentMenuName);
        if (img) {
            if (currentMenuMouseOutEvent) {
                currentEventTarget.onmouseout = currentMenuMouseOutEvent;
                img.src = img.src.replace('Ov', 'Ac');
            }
        }
    }
}