/*
Copyright, Woodland Engineering, 2009
For documentation, please request by email from (pardon format, but it's to avoid bots from picking it up).
  mail.kencurtis // AT // gmail // DOT COM //
Please identify yourself and tell us why you want it.
*/
var WeMenuBar =
{
menuType:                 0,
majorTagName:             [ "TABLE", "UL"],
elementTagName:           [ "TD", "LI" ],
menuSidePrefixStr:        ["hzm_", "lfm_", "rtm_"],
HORIZ_SIDE:               0,
VERT_LEFT_SIDE:           1,
VERT_RIGHT_SIDE:          2,
FOLDOUT_DOWN:             0,
FOLDOUT_TO_RIGHT:         1,
FOLDOUT_TO_LEFT:          2,
WATCHDOG_TIME_VALUE:      250,
CELL_IDLE_ST:             0,
CELL_ACTIVE_ST:           1,
CELL_CHILD_ACTIVE_ST:     2,
MENU_INACTIVE_ST:         0,
MENU_IDLE_ST:             1,
MENU_ITEM_ACTIVE_ST:      2,
MNUITEM_INACTIVE_ST:      0,
MNUITEM_IDLE_ST:          1,
MNUITEM_ACTIVE_ST:        2,
MNUITEM_CHILD_ACTIVE_ST:  3,
ESCAPE_KEY_CODE:          27,
FIRST_MENU_ZINDEX:        255,
linkPrefix:               "../"
};
WeMenuBar.SetMenuLocation = function
(
obj,
parRect,
foldoutDir
)
{
var objRect;
var objWidth;
var objHeight;
var parMidX;
objRect = WeMenuBar.GetElementRect(obj);
objWidth = 1 + objRect.right - objRect.left;
objHeight = 1 + objRect.bottom - objRect.top;
if (foldoutDir === WeMenuBar.FOLDOUT_DOWN)
{
parMidX = (parRect.left + parRect.right) / 2;
objRect.left = parMidX - (objWidth / 2);
objRect.top = parRect.bottom;
}
else if (foldoutDir === WeMenuBar.FOLDOUT_TO_RIGHT)
{
objRect.left = parRect.right;
objRect.top = parRect.top;
}
else
{
objRect.left = parRect.left - objWidth - 1;
objRect.top = parRect.top;
}
obj.style.top = objRect.top + "px";
obj.style.left = objRect.left + "px";
}
WeMenuBar.GetBodyRect = function()
{
var bodyRect = { left: 0, top: 0 };
bodyRect.right = innerWidth;
bodyRect.bottom = innerHeight;
return bodyRect;
};
WeMenuBar.GetElementRect = function
(
element
)
{
var rect = {left: 0, top: 0 };
rect.right = element.offsetWidth;
rect.bottom = element.offsetHeight;
while (element !== null && element.tagName !== "HTML")
{
rect.left += element.offsetLeft;
rect.top += element.offsetTop;
element = element.offsetParent;
}
rect.right += rect.left;
rect.bottom += rect.top;
return rect;
};
WeMenuBar.IsAnArray = function (a)
{
var notArray = typeof a !== "object" || typeof a.length !== "number" ||
typeof a.splice !== "function" || a.propertyIsEnumerable("length");
return !notArray;
};
WeMenuBar.NotifyOfMouseover = function
(
index
)
{
if (index <= WeMenuBar.menuBarCellObj.length && index !== WeMenuBar.currActiveIndex)
{
if (WeMenuBar.currActiveIndex !== -1)
WeMenuBar.menuBarCellObj[WeMenuBar.currActiveIndex].Deactivate();
WeMenuBar.currActiveIndex = index;
}
};
WeMenuBar.NotifyOfCellInactive = function
(
index
)
{
if (index <= WeMenuBar.menuBarCellObj.length && index === WeMenuBar.currActiveIndex)
WeMenuBar.currActiveIndex = -1;
};
WeMenuBar.PostUserAbandonEvent = function ()
{
if (WeMenuBar.currActiveIndex === -1)
return;
var activeNode = WeMenuBar.menuBarCellObj[WeMenuBar.currActiveIndex].node;
if (document.createEvent)
{
if (window.KeyEvent)
{
var evtObj = document.createEvent("KeyEvents");
evtObj.initKeyEvent("keydown",true,true,window,false,false,false,0,
WeMenuBar.ESCAPE_KEY_CODE,WeMenuBar.ESCAPE_KEY_CODE);
}
else
{
var evtObj = document.createEvent("UIEvents");
evtObj.initUIEvent("keydown", true, true, window,1);
evtObj.keyCode = WeMenuBar.ESCAPE_KEY_CODE;
evtObj.weKeyCode = WeMenuBar.ESCAPE_KEY_CODE;
}
activeNode.dispatchEvent(evtObj);
}
else
{
var evtObj = document.createEventObject();
evtObj.weKeyCode = WeMenuBar.ESCAPE_KEY_CODE;
activeNode.fireEvent("onkeydown",evtObj);
}
};
WeMenuBar.CreateMenuBarCellObj = function
(
parMnuBar,
node,
topMenuArray,
index
)
{
var idx;
var menuArray;
var nodeId = node.id;
for (idx = 0; idx < topMenuArray.length; idx++)
{
if (nodeId === topMenuArray[idx][0])
break;
}
if (idx >= topMenuArray.length)
return null;
var menuCellObject = function (parMnuBar,node,nodeId,menuArray,index)
{
var menuLayoutPrefix = nodeId.slice(0,4);
var that = this;
this.parentMenuBar = parMnuBar;
this.node = node;
this.nodeId = nodeId;
this.menuArray = menuArray;
this.hasSubMenu = WeMenuBar.IsAnArray(menuArray);
this.index = index;
this.state = WeMenuBar.CELL_IDLE_ST;
this.watchdogTimerId = -1;
this.rect;
this.foldoutMenu = null;
if (menuLayoutPrefix === WeMenuBar.menuSidePrefixStr[WeMenuBar.VERT_LEFT_SIDE])
this.foldoutDirection = WeMenuBar.FOLDOUT_TO_RIGHT;
else if (menuLayoutPrefix === WeMenuBar.menuSidePrefixStr[WeMenuBar.VERT_RIGHT_SIDE])
this.foldoutDirection = WeMenuBar.FOLDOUT_TO_LEFT;
else
this.foldoutDirection = WeMenuBar.FOLDOUT_DOWN;
this.Deactivate = function ()
{
if (that.state === WeMenuBar.CELL_ACTIVE_ST || that.state === WeMenuBar.CELL_CHILD_ACTIVE_ST)
{
if (that.watchdogTimerId !== -1)
{
clearTimeout(that.watchdogTimerId);
that.watchdogTimerId = -1;
}
if (that.foldoutMenu !== null)
{
that.foldoutMenu.Destruct();
delete that.foldoutMenu;
that.foldoutMenu = null;
}
that.node.className = WeMenuBar.mainMenuNormClassName;
that.state = WeMenuBar.CELL_IDLE_ST;
}
};
this.ClickAction = function ()
{
var nextPage;
if (that.watchdogTimerId !== -1)
clearTimeout(that.watchdogTimerId);
if (that.hasSubMenu === false)
{
WeMenuBar.PostUserAbandonEvent();
that.state = WeMenuBar.CELL_IDLE_ST;
nextPage = WeMenuBar.linkPrefix + that.menuArray;
window.open(nextPage,"_self");
}
};
this.node.onclick = this.ClickAction;
this.MouseoverAction = function ()
{
if (that.watchdogTimerId !== -1)
clearTimeout(that.watchdogTimerId);
if (that.state === WeMenuBar.CELL_IDLE_ST)
{
WeMenuBar.NotifyOfMouseover(that.index);
that.node.className = WeMenuBar.mainCellEmphClassName;
if (that.hasSubMenu === true)
{
that.rect = WeMenuBar.GetElementRect(that.node);
that.foldoutMenu = WeMenuBar.CreateFoldoutMenu(that,that.rect,that.foldoutDirection,that.menuArray);
}
}
else if (that.state === WeMenuBar.CELL_CHILD_ACTIVE_ST)
that.foldoutMenu.Deactivate();
that.state = WeMenuBar.CELL_ACTIVE_ST;
};
node.onmouseover = this.MouseoverAction;
this.MouseoutAction = function ()
{
that.watchdogTimerId = setTimeout(that.WatchdogTimeout, WeMenuBar.WATCHDOG_TIME_VALUE);
if (that.watchdogTimerId === -1)
{
clearTimeout(that.watchdogTimerId);
that.watchdogTimerId = setTimeout(that.WatchdogTimeout, WeMenuBar.WATCHDOG_TIME_VALUE);
}
};
node.onmouseout = this.MouseoutAction;
if (document.createEventObject)
{
this.KeydownAction = function ()
{
if (event.weKeyCode === WeMenuBar.ESCAPE_KEY_CODE)
that.NotifyOfUserAbandon();
};
}
else
{
this.KeydownAction = function (evt)
{
var key;
if (typeof(evt.weKeyCode) === "number")
key = evt.weKeyCode;
else if (typeof(evt.keyCode) === "number")
key = evt.keyCode;
if (key === WeMenuBar.ESCAPE_KEY_CODE)
that.NotifyOfUserAbandon();
};
}
node.onkeydown = this.KeydownAction;
this.NotifyOfChildMouseover = function ()
{
if (that.watchdogTimerId !== -1)
clearTimeout(that.watchdogTimerId);
if (that.state === WeMenuBar.CELL_ACTIVE_ST)
that.state = WeMenuBar.CELL_CHILD_ACTIVE_ST;
};
this.GetMenuZIndex = function ()
{
return WeMenuBar.menuZIndex;
};
this.NotifyOfUserAbandon = function ()
{
that.Deactivate();
that.parentMenuBar.NotifyOfCellInactive(that.index);
};
this.WatchdogTimeout = function ()
{
that.Deactivate();
that.parentMenuBar.NotifyOfCellInactive(that.index);
};
};
return new menuCellObject(parMnuBar,node,nodeId,topMenuArray[idx][1],index);
};
WeMenuBar.CreateFoldoutMenu = function
(
parent,
parRect,
foldoutDir,
menuArray
)
{
var idx;
var domListItem;
var arrayElem;
var menuObject = function (parent,parRect,foldoutDir,menuArray)
{
var that = this;
this.parent = parent;
this.menuZIndex = parent.GetMenuZIndex() + 1;
this.parRect = parRect;
this.foldoutDir = foldoutDir;
this.menuArray = menuArray;
this.isTerminalLeaf = false;
this.menuItemObj = [];
this.state = WeMenuBar.MENU_IDLE_ST;
this.currActiveIndex = -1;
this.unorderedMenuList = document.createElement("UL");
this.unorderedMenuList.className = WeMenuBar.unorderedListClassName;
for (idx = 0; idx < this.menuArray.length; idx++)
{
arrayElem = this.menuArray[idx];
domListItem = document.createElement("LI");
domListItem.className = WeMenuBar.listItemNormClassName;
domListItem.innerHTML = arrayElem[0];
domListItem = this.unorderedMenuList.appendChild(domListItem);
this.menuItemObj[idx] = WeMenuBar.CreateMenuItemObj(domListItem,arrayElem[1],this,idx);
}
this.unorderedMenuList.style.zIndex = this.menuZIndex;
document.body.appendChild(this.unorderedMenuList);
WeMenuBar.SetMenuLocation(this.unorderedMenuList,this.parRect,this.foldoutDir);
this.unorderedMenuList.style.visibility = "visible";
this.Destruct = function ()
{
if (that.state === WeMenuBar.MENU_IDLE_ST || that.state === WeMenuBar.MENU_ITEM_ACTIVE_ST)
{
that.unorderedMenuList.style.visibility = "hidden";
for (idx = menuArray.length - 1; idx >= 0; idx--)
{
that.menuItemObj[idx].Destruct();
delete that.menuItemObj[idx];
}
that.state = WeMenuBar.MENU_INACTIVE_ST;
}
};
this.Deactivate = function ()
{
if (that.state === WeMenuBar.MENU_ITEM_ACTIVE_ST && that.currActiveIndex !== -1)
{
that.menuItemObj[that.currActiveIndex].Deactivate();
that.currActiveIndex = -1;
that.state = WeMenuBar.MENU_IDLE_ST;
}
};
this.GetFoldoutDir = function ()
{
return that.foldoutDir;
};
this.GetMenuZIndex = function ()
{
return that.menuZIndex;
};
this.NotifyOfMouseover = function
(
index
)
{
if (that.state !== WeMenuBar.MENU_INACTIVE_ST)
{
if (index <= that.menuItemObj.length)
{
that.parent.NotifyOfChildMouseover();
if (index !== that.currActiveIndex)
{
if (that.currActiveIndex !== -1)
that.menuItemObj[that.currActiveIndex].Deactivate();
that.currActiveIndex = index;
}
that.state = WeMenuBar.MENU_ITEM_ACTIVE_ST;
}
}
};
};
return new menuObject(parent,parRect,foldoutDir,menuArray);
};
WeMenuBar.CreateMenuItemObj = function
(
domNode,
linkOrArrayObj,
parMenu,
index
)
{
var menuItem = function (domNode, linkOrArrayObj, parMenu, index)
{
var that = this;
this.parentMenu = parMenu;
this.nextBranch = linkOrArrayObj;
this.isTerminalLeaf = !WeMenuBar.IsAnArray(linkOrArrayObj);
this.liNode = domNode;
this.index = index;
this.expandDirection = WeMenuBar.FOLDOUT_TO_RIGHT;
this.state = WeMenuBar.MNUITEM_IDLE_ST;
this.watchdogTimerId = -1;
this.rect;
this.foldoutMenu = null;
this.ClickAction = function ()
{
var nextPage;
if (that.watchdogTimerId !== -1)
clearTimeout(that.watchdogTimerId);
if (that.isTerminalLeaf === true)
{
WeMenuBar.PostUserAbandonEvent();
that.state = WeMenuBar.MNUITEM_INACTIVE_ST;
nextPage = WeMenuBar.linkPrefix + that.nextBranch;
window.open(nextPage,"_self");
}
};
this.liNode.onclick = this.ClickAction;
this.MouseoverAction = function ()
{
if (that.watchdogTimerId !== -1)
clearTimeout(that.watchdogTimerId);
that.parentMenu.NotifyOfMouseover(that.index);
if (that.state === WeMenuBar.MNUITEM_IDLE_ST)
{
that.liNode.className = WeMenuBar.listItemEmphClassName;
if (that.isTerminalLeaf === false)
{
that.rect = WeMenuBar.GetElementRect(that.liNode);
var parFoldoutDir = that.parentMenu.GetFoldoutDir();
var foldoutDir = (parFoldoutDir === WeMenuBar.FOLDOUT_TO_LEFT) ?
WeMenuBar.FOLDOUT_TO_LEFT : WeMenuBar.FOLDOUT_TO_RIGHT;
that.foldoutMenu = WeMenuBar.CreateFoldoutMenu(that,that.rect,foldoutDir,that.nextBranch);
}
}
else if (that.state === WeMenuBar.MNUITEM_CHILD_ACTIVE_ST && that.foldoutMenu !== null)
that.foldoutMenu.Deactivate();
that.state = WeMenuBar.MNUITEM_ACTIVE_ST;
};
this.liNode.onmouseover = this.MouseoverAction;
this.MouseoutAction = function ()
{
that.watchdogTimerId = setTimeout(that.WatchdogTimeout, WeMenuBar.WATCHDOG_TIME_VALUE);
if (that.watchdogTimerId === -1)
{
clearTimeout(that.watchdogTimerId);
that.watchdogTimerId = setTimeout(that.WatchdogTimeout, WeMenuBar.WATCHDOG_TIME_VALUE);
}
};
this.liNode.onmouseout = this.MouseoutAction;
this.Deactivate = function ()
{
if (that.watchdogTimerId !== -1)
clearTimeout(that.watchdogTimerId);
that.liNode.className = WeMenuBar.listItemNormClassName;
if (that.isTerminalLeaf === false &&
(that.state === WeMenuBar.MNUITEM_ACTIVE_ST || that.state === WeMenuBar.MNUITEM_CHILD_ACTIVE_ST))
{
if (that.foldoutMenu !== null)
{
that.foldoutMenu.Destruct();
that.foldoutMenu = null;
}
}
that.state = WeMenuBar.MNUITEM_IDLE_ST;
};
this.Destruct = function ()
{
if (that.watchdogTimerId !== -1)
clearTimeout(that.watchdogTimerId);
if (that.isTerminalLeaf === false &&
(that.state === WeMenuBar.MNUITEM_ACTIVE_ST || that.state === WeMenuBar.MNUITEM_CHILD_ACTIVE_ST))
{
if (that.foldoutMenu !== null)
{
that.foldoutMenu.Destruct();
that.foldoutMenu = null;
}
}
that.state = WeMenuBar.MNUITEM_INACTIVE_ST;
};
this.GetMenuZIndex = function ()
{
return that.parentMenu.GetMenuZIndex();
};
this.NotifyOfChildMouseover = function ()
{
if (that.watchdogTimerId !== -1)
clearTimeout(that.watchdogTimerId);
if (that.isTerminalLeaf === false)
{
that.parentMenu.NotifyOfMouseover(that.index);
if (that.state === WeMenuBar.MNUITEM_ACTIVE_ST)
that.state = WeMenuBar.MNUITEM_CHILD_ACTIVE_ST;
}
};
this.WatchdogTimeout = function ()
{
WeMenuBar.PostUserAbandonEvent();
that.state = WeMenuBar.MNUITEM_INACTIVE_ST;
};
};
return new menuItem(domNode, linkOrArrayObj, parMenu, index);
};
WeMenuBar.Initialize = function ()
{
var menuTable = [];
var menuCellNode = [];
var idx, jdx, kdx;
WeMenuBar.mainMenuArray = WeMainMenuArr;
WeMenuBar.mainMenuNormClassName = 'mmenucell';
WeMenuBar.mainCellEmphClassName = 'mmenucell_emph';
WeMenuBar.unorderedListClassName = 'menublock';
WeMenuBar.listItemNormClassName = 'menuitem_norm';
WeMenuBar.listItemEmphClassName = 'menuitem_emph';
WeMenuBar.menuZIndex = WeMenuBar.FIRST_MENU_ZINDEX - 1;
if (WeMenuBar.menuType === undefined || WeMenuBar.menuType >= 1)
WeMenuBar.menuType = 0;
WeMenuBar.menuBarCellObj = [];
kdx = 0;
menuTable = document.getElementsByTagName(WeMenuBar.majorTagName[WeMenuBar.menuType]);
for (idx = 0; idx < menuTable.length; idx++)
{
if (menuTable[idx].className === 'mmenutable')
{
menuCellNode = menuTable[idx].getElementsByTagName(WeMenuBar.elementTagName[WeMenuBar.menuType]);
for (jdx = 0; jdx < menuCellNode.length; jdx++)
{
if (menuCellNode[jdx].className === WeMenuBar.mainMenuNormClassName)
{
WeMenuBar.menuBarCellObj[kdx] =
WeMenuBar.CreateMenuBarCellObj(WeMenuBar,menuCellNode[jdx],WeMenuBar.mainMenuArray,kdx);
kdx++;
}
}
}
}
WeMenuBar.currActiveIndex = -1;
};
WeMenuBar.Start = function()
{
if (document.addEventListener)
{
window.addEventListener("load",WeMenuBar.Initialize,false);
}
else
{
window.attachEvent("onload",WeMenuBar.Initialize);
}
};
WeMenuBar.Start();