isMZ = isIE = false;
isIE = document.all ? true : false;
isMZ = document.compareDocumentPosition && !isIE;
isOther = !isMZ && !isIE;

dump = function( msg){
	document.getElementById("debug").value += msg + " ";
}

function Menu( menuArray, menuDisposition, baseMenuElements){
	if(!Menu.menu){
		Menu.menu = [];
	}
	Menu.menu[Menu.menu.length] = this;
	
	this.menuId					= Menu.menu.length - 1;
	this.menuDisposition		= menuDisposition;
	this.baseMenuElements	= baseMenuElements;
	this.TIMEOUT1 				= 500;
	this.lists 					= [];
	this.menuArray 			= menuArray;
	this.baseMenu;

	if( this.baseMenuElements){
		var menuHost1 = document.createElement("span");
		menuHost1.innerHTML = this.construct();
		document.body.appendChild( menuHost1);
	}
	else{
		document.write(this.construct());
	}
}

Menu.VERTICAL					= 10;
Menu.HORIZONTAL				= 11;
Menu.MENU_ARRAY_ID_INDEX	= 0;
Menu.MENU_ARRAY_TEXT_INDEX	= 1;
Menu.MENU_ARRAY_LINK_INDEX	= 2;
Menu.MENU_ARRAY_DESC_INDEX	= 3;
Menu.BASE_MENU_ID 			= "main";

if( !window.cat)
	SelectID 					= "";
else
	SelectID 					= cat;

Menu.prototype.construct = function(){
	var menuHTML = "";
	for(var i in this.menuArray){
		if(i == Menu.BASE_MENU_ID){
			if(this.baseMenuElements){
				this.lists[i] = this.baseMenu = new List( this, i, List.BASE_MENU, this.baseMenuElements, 
									(this.menuDisposition == Menu.HORIZONTAL) ? List.HORIZONTAL_ALIGNMENT : List.VERTICAL_ALIGNMENT);
			}
			else{
				this.lists[i] = this.baseMenu = new List( this, i, List.BASE_MENU, null, List.VERTICAL_ALIGNMENT);
			}
		}
		else{
			if(i == SelectID)
				this.lists[i] = new List( this, i, List.SUB_MENU, null, List.VERTICAL_ALIGNMENT);
			else
				this.lists[i] = new List( this, i, List.REGULAR_MENU, null, List.VERTICAL_ALIGNMENT);
		}
	}

	for(var i in this.menuArray){
		if(i != SelectID)
			menuHTML += this.lists[i].construct();
	}
	return menuHTML;
}

Menu.prototype.hide = function(){
	if(this.baseMenu.selectedItem && this.baseMenu.selectedItem.childList)
		this.baseMenu.selectedItem.childList.hide();
}

Menu.prototype.show = function( fromItem){
	this.baseMenu.show( fromItem);
}

function List( menu, listId, type, HTMLelements, alignment){
	this.listId				= listId;
	this.menu 				= menu;
	this.HTMLelements		= HTMLelements;
	if(this.HTMLelements){
		this.id				= HTMLelements[0].parentNode.id;
	}
	else{
		this.id 				= "L" + List.counter++;
	}
	this.type				= type;
	this.subMenuAlignment= alignment;
	this.parentItem;
	this.selectedItem;
	this.className;
	this.listArray			= menu.menuArray[listId];
	if(this.type != List.SUB_MENU){
		if( this.type == List.BASE_MENU){
			this.className	= List.BASE_MENU_CLASS;
		}
		else{
			this.className	= List.SUBMENU_CLASS;
		}
	}
}

List.BASE_MENU_CLASS 		= "menu";
List.SUBMENU_CLASS 			= "subMenu";
List.BASE_MENU					= 1;
List.REGULAR_MENU				= 2;
List.SUB_MENU					= 3;
List.counter					= 0;
List.VERTICAL_ALIGNMENT 	= 1;
List.HORIZONTAL_ALIGNMENT 	= 2;

List.prototype.construct = function(mod){
	var len 	= this.listArray.length;
	var ar 	= this.listArray;

	if( this.HTMLelements){
		for( var i=0; i < len; i++){
			(new Item( this, ar[i], i, this.HTMLelements[i])).construct();
		}
		return "";
	}
	else {
		var wrapLyr = "<DIV class=" + this.className + 
								" id=" + this.id + 
								" style='" + (this.type == List.BASE_MENU ? 'z-index:1' : 'z-index:0') + "'>\n";
		for( var i=0; i < len; i++){
			if( ar[i][0] == SelectID){
				wrapLyr += (new Item( this, ar[i], i)).construct( mod);
				wrapLyr += this.menu.lists[SelectID].construct("internal");
			}
			else
				wrapLyr += (new Item( this, ar[i], i)).construct( mod);
		}
		return wrapLyr + "</DIV>\n";
	}
}

List.prototype.show = function( fromItem){
	if(this.type == List.SUB_MENU)
		return
	
	if(this.isVisible())
		return;
	
	if(fromItem.parentList && !document.getElementById(this.id).style.zIndex){
		document.getElementById(this.id).style.zIndex = document.getElementById( fromItem.parentList.id).style.zIndex + 1;
	}
	var coords = Item.getObjectCoords(document.getElementById(fromItem.id));
	
	if(fromItem.parentList.subMenuAlignment == List.VERTICAL_ALIGNMENT)
		this.moveTo(coords[0] + (document.getElementById(fromItem.id)).offsetWidth - 5, coords[1] + 5);
	else if( fromItem.parentList.subMenuAlignment == List.HORIZONTAL_ALIGNMENT)
		this.moveTo(coords[0], coords[1] + document.getElementById(fromItem.id).offsetHeight);
	
	document.getElementById(this.id).style.display = "block";
	this.parentItem = fromItem;
	
	for(var i=0; i < Menu.menu.length; i++){
		if(this.menu == Menu.menu[i])
			continue;
		Menu.menu[i].hide();
	}
}

List.prototype.hide = function(){
	if(this.type == List.SUB_MENU)
		return;

	if( this.selectedItem && this.selectedItem.childList)
		this.selectedItem.childList.hide();
	this.selectedItem = null;
	document.getElementById(this.id).style.display = "none";
	if(this.parentItem){
		this.parentItem.putOff();
		this.parentItem = null;
	}
}

List.prototype.isVisible = function(){
	return document.getElementById(this.id).style.display == "block" ? true : false;
}

List.prototype.moveTo = function( x, y){
	document.getElementById(this.id).style.left = x;
	document.getElementById(this.id).style.top 	= y;
}

List.prototype.putOn = function(){
}

List.prototype.putOff = function(){
}

function Item( parentList, menuArray, listIndex, HTMLelement){
	this.HTMLelement				= HTMLelement;
	if(this.HTMLelement){
		this.id 						= Item.registerItem(this, this.HTMLelement.id);
	}
	else{
		this.id 						= Item.registerItem(this);
	}
	this.BASE_ITEM_CLASS 		= "menuItem";
	this.BASE_ITEM_FIRST_CLASS = "menuItemFirst";
	this.ITEM_CLASS_ON 			= "menuItemON";
	this.ITEM_CLASS_OFF 			= "menuItemOFF";
	this.ITEM_SUB_CLASS_ON 		= "menuSubItemON";
	this.ITEM_SUB_CLASS_OFF 	= "menuSubItemOFF";
	this.ITEM_POINTER_IMG		= "/simgs/pointer.gif";
	this.ITEM_POINTER_IMG_ON	= "/simgs/pointer_on.gif";
	this.ITEM_POINTER_IMG_CLASS= "menuPointer";
	this.ITEM_DESCRIPTION_CLASS= "menuDescriptionText";
	this.listIndex					= listIndex;
	this.menu						= parentList.menu;
	this.parentList 				= parentList;
	this.menuArray					= menuArray;
	this.url							= menuArray[ Menu.MENU_ARRAY_LINK_INDEX];
//	this.childList					= false;
	this.childList 				= this.menu.lists[ menuArray[ Menu.MENU_ARRAY_ID_INDEX]];
}

Item.prototype.setClass = function(mod){
	if(mod == "internal"){
		this.OFF_CLASS 				= this.BASE_ITEM_CLASS + " " + this.ITEM_SUB_CLASS_OFF + (this.listIndex==0 ? (" " + this.BASE_ITEM_FIRST_CLASS) : "");
		this.ON_CLASS 					= this.BASE_ITEM_CLASS + " " + this.ITEM_SUB_CLASS_ON + (this.listIndex==0 ? (" " + this.BASE_ITEM_FIRST_CLASS) : "");
	}
	else{
		this.OFF_CLASS 				= this.BASE_ITEM_CLASS + " " + this.ITEM_CLASS_OFF + (this.listIndex==0 ? (" " + this.BASE_ITEM_FIRST_CLASS) : "");
		this.ON_CLASS 					= this.BASE_ITEM_CLASS + " " + this.ITEM_CLASS_ON + (this.listIndex==0 ? (" " + this.BASE_ITEM_FIRST_CLASS) : "");
	}
}

Item.prototype.construct = function(mod){
	
	this.setClass(mod);
	var _this = this;
	if( this.HTMLelement){
		if(this.HTMLelement.onmouseover)
			this.putOn = this.HTMLelement.onmouseover;
		else
			this.putOn = function(){}
		if(this.HTMLelement.onmouseout)
			this.putOff = this.HTMLelement.onmouseout;
		else
			this.putOff = function(){}
		
		this.HTMLelement.onmouseover	= function( event){ Item.onMouseOver(_this.HTMLelement, event)};
		this.HTMLelement.onmouseout	= function( event){ Item.onMouseOut(_this.HTMLelement, event)};
	}
	else{
		var el = "<DIV class='" + this.OFF_CLASS + "'";
		if(this.listIndex == 0){
			el += " name=first";
		}
		el += " id=" + this.id;
		el += " onmouseover='Item.onMouseOver(this,event)'";
		el += " onmouseout='Item.onMouseOut(this,event)'";
		el += " onclick='Item.onClick(this)'>";
		if(this.childList){
			//alert()
			el += "<img src=" + this.ITEM_POINTER_IMG + " class=" + this.ITEM_POINTER_IMG_CLASS + ">";
		}
		if(mod == "internal")
			el += "<div class=menuSubItemText>" + this.menuArray[Menu.MENU_ARRAY_TEXT_INDEX] + "</div>";
		else
			el += "<div class=menuItemText>" + this.menuArray[Menu.MENU_ARRAY_TEXT_INDEX] + "</div>";
		if(this.menuArray[Menu.MENU_ARRAY_DESC_INDEX])
			el += "<div class='menuItemText " + this.ITEM_DESCRIPTION_CLASS + "' style='display:none'>"+this.menuArray[Menu.MENU_ARRAY_DESC_INDEX]+"</div>";
		el += "</div>\n";
	
		return el;
	}
}

Item.prototype.loadURL = function(){
	location = this.url;
}

Item.prototype.putOn = function(){
	if(this.HTMLelement)
		return;
	document.getElementById(this.id).className = this.ON_CLASS;
	if(this.childList){
		document.getElementById(this.id).getElementsByTagName("img")[0].src = this.ITEM_POINTER_IMG_ON;
	}
	if(this.menuArray[Menu.MENU_ARRAY_DESC_INDEX]){
		if(!document.getElementById(this.id).getElementsByTagName("DIV")[1].style.width)
			document.getElementById(this.id).getElementsByTagName("DIV")[1].style.width = document.getElementById(this.id).clientWidth;
		document.getElementById(this.id).getElementsByTagName("DIV")[1].style.display="block";
	}
	this.parentList.putOn( this);
}

Item.prototype.putOff = function(){
	if(this.HTMLelement)
		return;
	document.getElementById(this.id).className = this.OFF_CLASS;
	if(this.childList)
		document.getElementById(this.id).getElementsByTagName("img")[0].src = this.ITEM_POINTER_IMG;
	if(this.menuArray[Menu.MENU_ARRAY_DESC_INDEX]){
		document.getElementById(this.id).getElementsByTagName("DIV")[1].style.display="none";
	}
	this.parentList.putOff( this);
}


Item.onMouseOver = function( HTMLelement, e){
	if(isMZ && (
		HTMLelement.compareDocumentPosition( e.target) == 10 || 
		HTMLelement.compareDocumentPosition( e.relatedTarget) == 20 )){
			e.stopPropagation();
			return;
	}
	var item = Item.items[HTMLelement.id]
	if( item.parentList.type == List.SUB_MENU){
		item.menu.hide();
	}	
	clearTimeout(item.menu.timer1);	
	
	if(item.parentList.selectedItem && item.parentList.selectedItem.childList)
		item.parentList.selectedItem.childList.hide();
	if(item.childList)
		item.childList.show( item);
	item.parentList.selectedItem = item;
	if(item.parentList.parentItem)
		item.parentList.parentItem.putOn();
	item.putOn();
}

Item.onMouseOut = function( HTMLelement, e){
	if(isMZ && (
		HTMLelement.compareDocumentPosition( e.relatedTarget) == 20 ||
		HTMLelement.compareDocumentPosition( e.relatedTarget) == 0 )){
			e.stopPropagation();
			return;
	}
	var item = Item.items[HTMLelement.id];
	item.menu.timer1 = setTimeout( function(){item.menu.hide()}, item.menu.TIMEOUT1);	
	item.putOff();
}

Item.onClick = function( HTMLelement){
	Item.items[HTMLelement.id].loadURL();
}

Item.registerItem = function(item, id){
	if(!this.items){
		this.items = [];
	}
	
	if(id){
		this.items[id] = item;
		return id;
	}
	this.counter++;
	this.items["i" + this.counter] = item;
	return "i" + this.counter;
}

Item.counter = 0;

Item.getObjectCoords = function( HTMLelement){
   var o 		= HTMLelement;
   var accTop  = o.offsetTop;
   var accLeft = o.offsetLeft;
   
	while((o = o.offsetParent) != null){
		if(isIE){
			if(o.tagName == "CENTER"	||
				o.tagName == "TR"			||
				o.tagName == "TBODY")	continue;
		}
		accTop  += o.offsetTop;
		accLeft += o.offsetLeft;
	}
	
	return [parseInt(accLeft), parseInt(accTop)];
}

