<!--
// File:   menu_proto.js
// Author: Graeme S. McAfee
// Email:  gmcafee@sympatico.ca
// Copyright 2006 Graeme S. McAfee. All rights reserved.
//
// Modification of this code is not permitted without the express permission
// from the author.

var menuOpen = "";
var timeoutInterval = 3000; // 3000 milliseconds = 3 seconds

// ----------------------------------------------------------------------------
// Menu & prototype functions
// ----------------------------------------------------------------------------

// ----------------------------------------------------------------------------
// Container for main menus and their submenus 
// 10 -------------------------------------------------------------------------
function Menu(name, origin, submenuOffset, mStyle, sStyle) {
	this.name = name;
	this.origin = origin;
	this.submenuOffset = submenuOffset;
	this.mStyle = mStyle;
	this.sStyle = (!sStyle) ? mStyle : sStyle;	
	this.menuRegistry = [];
	this.main  = {};                     // menu hash
	this.sub   = {};                     // submenu hash
	this.chain = new chain(this.name);   // current open submenu chain
	this.div = createMenuDivElement(name);

	ON  = 1;
	OFF = 0;	
}

//---------------------------------------------------------------------------------------
// Menu.addMainmenuItem()
// sample: menu.addMainmenuItem( "1" , T , "Menu 1" , "" );
// 30 -----------------------------------------------------------------------------------
Menu.prototype.addMainmenuItem = function(index, hasSubmenu, menuText, url) {
      mStyle = this.mStyle;
	sStyle = this.sStyle;
	submenuOffset = this.submenuOffset;
	
	if (this.main[index]) return;
	
	var menuitem;
	
	if (is.dom) {
		menuitem = createMenuItemDivElement(this.name, index, "mainmenu", menuText);
		this.div.appendChild(menuitem);
	}
	
//	menuitem.onmouseover = Function("menu = this.id.substring(0, this.id.indexOf('_'));"+
// "if ( menuOpen == 'menu1') 
// {"+"if (menu == 'menu2') return;"+"}"
// +"menuOpen = menu;"+"hilite(this, ON, 0);
// "+"closeMenus();"+"if ("+ hasSubmenu +"){"+"eval(menu).sub['"+ index +"'].show();"+"eval(menu).chain.push('"+ index +"');"+"}");

menuitem.onmouseover = Function("menu = this.id.substring(0, this.id.indexOf('_'));"+"if ( menuOpen == 'menu1'){"+"if (menu != 'menu1') return;"+"}"+"if ( menuOpen == 'menu2'){"+"if (menu != 'menu2') return;"+"}"+"menuOpen = menu;"+"hilite(this, ON, 0);"+"closeMenus();"+"if ("+ hasSubmenu +"){"+"eval(menu).sub['"+ index +"'].show();"+"eval(menu).chain.push('"+ index +"');"+"}");

	menuitem.onmouseout  = Function('hilite(this, OFF, 0); menuOpen = "";');
	menuitem.onmousedown = Function(
		'if ("'+ url +'".indexOf("javascript") == -1 ){'+
			'location.href = "'+ url +'";'+
		'} else {'+
			'eval("'+ url +'");'+
			'eval(menu).chain.close();'+
		'}'
	);

	this.main[index] = new menuObject(menuitem.id, index);
	this.menuRegistry[this.menuRegistry.length] = this.main[index];
}

//---------------------------------------------------------------------------------------
// Menu.addSubmenuItem()
// sample: menu.addSubmenuItem( "1_1", "1", T, "Menu 1.1", "" );
//---------------------------------------------------------------------------------------
Menu.prototype.addSubmenuItem = function(index, parentIndex, hasSubmenu, menuText, url) {
	
	if (!this.sub[parentIndex]) {
		var submenu;
		var parent;
		
		if (is.dom) {
			submenu = createMenuItemDivElement(this.name, parentIndex, "submenu");
			this.div.appendChild(submenu);
		}
		
		if (!parentIndex.match("_"))
			parent = this.main[parentIndex];
		else
			parent = this.sub[parentIndex.substring(0, parentIndex.length-2)];
				
		this.sub[parentIndex] = new menuObject(submenu.id, parentIndex, parent, sStyle);
		submenu.onmouseover = Function(this.name +'.sub["'+ parentIndex +'"].show();');
	}

	this.addSubmenuItemItem(index, parentIndex, hasSubmenu, menuText, url);
}

//---------------------------------------------------------------------------------------
// Menu.addSubmenuItemItem()
//---------------------------------------------------------------------------------------
Menu.prototype.addSubmenuItemItem = function(index, parentIndex, hasSubmenu, menuText, url) {
	var submenuItem;
	
	if (is.dom) {
		if (document.getElementById(this.name +"_submenuItem_"+ index)) return;
		
		submenuItem = createMenuItemDivElement(this.name, index, "submenuItem", menuText, hasSubmenu);
		this.sub[parentIndex].elem.appendChild(submenuItem);
	}

	submenuItem.onmouseover = Function(
		'menu = this.id.substring(0, this.id.indexOf("_"));'+
		'hilite(this, ON, 1);'+
		'eval(menu).chain.close("'+ parentIndex +'");'+
		
		'if ('+ hasSubmenu +') {'+
			'eval(menu).sub["'+ index +'"].show();'+
			'eval(menu).chain.push("'+ index +'");'+
		'}'
	);
						
	submenuItem.onmouseout  = Function('hilite(this, OFF, 1);');
	
	submenuItem.onmousedown = Function(
		'menu = this.id.substring(0, this.id.indexOf("_"));'+
	    
		'if ('+ hasSubmenu +'){'+
			'eval(menu).chain.push("'+ parentIndex +'");'+
			'eval(menu).sub["'+ index +'"].show();'+
			'eval(menu).chain.push("'+ index +'");'+
		'} else if ("'+ url +'"){'+

			'if ("'+ url +'".indexOf("javascript") == -1 )'+
				'location.href = "'+ url +'";'+
		      'else '+
				'eval("'+ url +'");'+
				'eval(menu).chain.close();'+
		'}'
	);
}

//---------------------------------------------------------------------------------------
// Menu.layoutMenu()
//---------------------------------------------------------------------------------------
Menu.prototype.layoutMenu = function() {	
	this.menuRegistry[0].style.left = this.origin.x + "px";
	this.menuRegistry[0].style.top  = this.origin.y + "px";
	
	var dx = (this.mStyle.spacing == 0) ? -this.mStyle.border : this.mStyle.spacing;
	
	// Position each main menu item relative to the preceding menu item
	for (var i = 1; i < this.menuRegistry.length; i++) {
//		if ((this.name == "menu1") && i==1) {  // menu1 2nd menu item offset = 250px
//		    this.menuRegistry[i].style.left = this.menuRegistry[i-1].offX() + this.menuRegistry[i-1].offW() + dx + 250 + "px";
//		} else {
//		   	this.menuRegistry[i].style.left = this.menuRegistry[i-1].offX() + this.menuRegistry[i-1].offW() + dx + "px";
//		}

		switch (i) {
		  case 1:
						 this.menuRegistry[i].style.left = 100 + "px";
                         break;
		  case 7:
                         this.menuRegistry[i].style.left = 90 + "px";
                         break;

          case 2:
                         this.menuRegistry[i].style.left = 270 + "px";
                         break;
		  case 8:
                         this.menuRegistry[i].style.left = 110 + "px";
                         break;
          case 3:
                         this.menuRegistry[i].style.left = 430 + "px";
                         break;
          case 9:
                         this.menuRegistry[i].style.left = 235 + "px";
                         break;
		  case 4:
                         this.menuRegistry[i].style.left = 600 + "px";
                         break;
          case 10:
                         this.menuRegistry[i].style.left = 540 + "px";
                         break;
          case 5:
                         this.menuRegistry[i].style.left = 635 + "px";
                         break;
     	  case 11:
                         this.menuRegistry[i].style.left = 475 + "px";
                         break;
          case 6:
                         this.menuRegistry[i].style.left = 770 + "px";
						 break;
		  case 12:
                         this.menuRegistry[i].style.left = 635 + "px";
                         break;
		  }
		this.menuRegistry[i].style.top  = this.menuRegistry[i-1].offY() + "px";
	}
}

// End prototype functions


// ----------------------------------------------------------------------------
// Container for currently open mainmenu-submenu chain
// ----------------------------------------------------------------------------
function chain(parent) {
	this.chain = [];
	this.parent = parent;
}

chain.prototype.getLength = function() {
	return this.chain.length;
}

chain.prototype.push = function(index) {
	var len = this.getLength();
	if (len == 0 || this.chain[len-1] != index) this.chain.push(index);
}

chain.prototype.pop = function() {
	this.chain.pop();
}

chain.prototype.close = function(indexLimit) {
	var len = this.getLength();
	var done = false;
	var item;

	if (len) {
		if (indexLimit){ // close chain to indexLimit
			while (!done){
				len = this.getLength();
				
				if (this.chain[len-1] != indexLimit) {
		
					item = this.chain.pop();
					eval(this.parent +".sub[\""+ item +"\"].hide()");
				} else
					done = true;
			}
		} else { // close chain completely
			for (var i=len; i>=1; i--){
				item = this.chain.pop();
				eval(this.parent +".sub[\""+ item +"\"].hide()");
			}
		}
	}
}


// ----------------------------------------------------------------------------
// menuObject
// sample: menuObject("menu_mainmenu_1", "1", "", "")
// ----------------------------------------------------------------------------
function menuObject(obj, parentIndex, parent, mStyle) {
	this.menu = obj.substring(0, obj.indexOf("_"));   // REWORK
   
	this.elem = (is.dom) ? document.getElementById(obj) : document.all[obj];
	this.style = this.elem.style;
	
	if (parent) this.parent = parent;
	if (mStyle) this.mStyle = mStyle;
	
	this.obj = obj + "Obj";
	
	for (var c=0; c < parentIndex.length; c++) {
		if (parentIndex.charAt(c) === "_"){
			this.level += 1;
		}
	}

	this.subitemNum = (parentIndex.lastIndexOf("_") != -1) ? parseInt(parentIndex.substr(parentIndex.lastIndexOf("_")+1)) : 0;

	eval(this.obj + "= this");
}

menuObject.prototype.offX   = Function('oX = (is.ie || is.ns6 || is.moz) ? this.elem.offsetLeft : parseInt(this.style.left); return oX;');
menuObject.prototype.offY   = Function('oY = (is.ie || is.ns6 || is.moz) ? this.elem.offsetTop : parseInt(this.style.top);   return oY;');
menuObject.prototype.offW   = Function('oW = this.elem.offsetWidth;  return oW;'); // main menu offset width
menuObject.prototype.offH   = Function('oH = this.elem.offsetHeight; return oH;'); // main menu offset height
menuObject.prototype.dYmax  = Function('return this.offH() + 10;');
menuObject.prototype.dY     = 0;
menuObject.prototype.level  = 0;
menuObject.prototype.subitemNum = 0;
menuObject.prototype.inChild = false;
menuObject.prototype.isOpen = false;
menuObject.prototype.timer  = null;

menuObject.prototype.show = function() {
	// close menu if no selection made
	clearTimeout(this.timer);
	this.timer = setTimeout(this.obj + ".hide()", timeoutInterval); 

	if (this.level == 0) {
	    this.style.left = this.parent.offX() + "px";
	    this.style.top  = this.parent.offY() + this.parent.offH()  + eval(menu).submenuOffset + this.mStyle.spacing + "px"; // top of submenus
	} else { 
	    this.style.left = this.parent.offX() + this.parent.offW()  + this.mStyle.spacing + "px";
	    this.style.top  = this.parent.offY() + (this.subitemNum-1) * this.mStyle.height  + "px";
	}

	this.style.visibility = "visible";
	this.style.zIndex = "100";
	this.isOpen = true;
	menuOpen = this.menu;
}

menuObject.prototype.hide = function() {
	clearTimeout(this.timer);
	this.style.visibility = "hidden";
	this.isOpen = false;
	menuOpen = "";
}

// hilite the menu item
function hilite (obj, state, type) {
	if(state == ON){
		obj.style.background  = type ? this.sStyle.bg_on  : this.mStyle.bg_on;
		obj.style.color       = type ? this.sStyle.fg_on  : this.mStyle.fg_on;
	} else {
		obj.style.background  = type ? this.sStyle.bg_off : this.mStyle.bg_off;
		obj.style.color       = type ? this.sStyle.fg_off : this.mStyle.fg_off;
	}
}
//-->
