﻿/*
 * All rights reserved. (C) 2004-2005 S. Andringa
 * andringa@quicknet.nl
 *
 */  

function findPosX(obj)
{
 var curleft = 0;
 if (document.getElementById || document.all)
 {
  while (obj.offsetParent)
  {
   curleft += obj.offsetLeft
   obj = obj.offsetParent;
  }
 }
 else if (document.layers)
  curleft += obj.x;
 return curleft;
}

function findPosY(obj)
{
 var curtop = 0;
 if (document.getElementById || document.all)
 {
  while (obj.offsetParent)
  {
   curtop += obj.offsetTop
   obj = obj.offsetParent;
  }
 }
 else if (document.layers)
  curtop += obj.y;
 return curtop;
}

var menus = new Array();

function resetMenuPositions(){
	for(var j = 0; j < getAmountOfLevels(); j++){
		for(var  i = 0; i < menus.length; i++){
			menus[i].setPosition();
		}
	}
}

function getAmountOfLevels(){
	var levels = 0;
	for(var  i = 0; i < menus.length; i++){
		if(menus[i].level > levels){
			levels = menus[i].level;
		}
	}	
	return levels;
}

function Menu(bgclr1, bgclr2, txtclr1, txtclr2, offX, offY, xSize, alignTo, paddingLeft, paddingRight, paddingTop, paddingBottom){	
	menus.push(this);
	
	this.tableID = "table"+menus.length;
	this.level = 1;
	
	this.items = new Array();
	this.subMenus = new Array();
	
	this.indexNr = menus.length-1;
	this.menuID = "menu"+menus.length;
	
	this.bgclr1 = bgclr1;
	this.bgclr2 = bgclr2;
	this.txtclr1 = txtclr1;
	this.txtclr2 = txtclr2;
	
	this.alignTo = alignTo;
	
	this.pl = paddingLeft;
	this.pr = paddingRight;
	this.pt = paddingTop;
	this.pb = paddingBottom;
	
	this.offX = offX;
	this.offY = offY;

	this.xsize = xSize;
}
Menu.prototype.addMenu = function(obj, linkName){
	obj.triggerName = "trigger"+this.subMenus.length+"-"+this.indexNr;
	obj.mother = this;	
	obj.level = this.level+1;
	
	this.items.push("<tr>"+
				"<td id=\""+obj.triggerName+"\" style=\"color:"+this.txtclr1+"; background-color:"+this.bgclr1+"; cursor:default\">"+
					"<table cellpadding=\"0\" cellspacing=\"0\" style=\"width:100%\">"+
						"<tr>"+
							"<td class=\"menuItem\" style=\"padding-left:"+this.pl+"px;padding-right:"+this.pr+"px;padding-top:"+this.pt+"px;"+
								"padding-bottom:"+this.pb+"px;text-align:"+this.alignTo+"; color:"+this.txtclr1+";\" "+
								"id=\""+obj.triggerName+"-1\">"
									+linkName+
							"</td>"+
							"<td class=\"menuItem\" style=\"padding-left:"+this.pl+";"+
								"padding-right:"+this.pr+"px;padding-top:"+this.pt+"px;padding-bottom:"+this.pb+"px;font-weight:bold;"+
								"color:"+this.txtclr1+";width:10px\" id=\""+obj.triggerName+"-2\">"+
									"&#187;"+
							"</td>"+
						"</tr>"+
					"</table>"+
				"</td>"+
			"</tr>");
        this.menuCode = this.getItemList();
        this.subMenus.push(obj);
}

Menu.prototype.attachTo = function(obj){
	var obj = document.getElementById(obj);
	this.trigger = obj;
	
	obj.onmouseover = function(){
		for(var c = 0; c < menus.length; c++)
			if(this == menus[c].trigger){
				if(menus[c].mother){
					this.style.backgroundColor = menus[c].bgclr2;	
					document.getElementById(this.id+"-1").style.color = menus[c].txtclr2;
					document.getElementById(this.id+"-2").style.color = menus[c].txtclr2;
				}
				menus[c].show();
			}
	}
	obj.onmouseout = function(){
		for(var c = 0; c < menus.length; c++)
			if(this == menus[c].trigger){
				if(menus[c].mother){
					this.style.backgroundColor = menus[c].bgclr1;	
					document.getElementById(this.id+"-1").style.color = menus[c].txtclr1;
					document.getElementById(this.id+"-2").style.color = menus[c].txtclr1;
				}
				menus[c].hide();
			}
	}
	document.writeln("<div style=\"overflow:hidden; position:absolute; vertical-align:bottom\""+
				" id=\""+this.menuID+"\" onmouseover=\"menus["+this.indexNr+"].show();\""+
				"onmouseout=\"menus["+this.indexNr+"].hide();\">"+
				"<table id=\""+this.tableID+"\" cellpadding=\"0\" cellspacing=\"0\" style=\"width:"+this.xsize+"px;\">\n"
					+this.menuCode+
			"</table></div>");	
	this.div = document.getElementById(this.menuID);
	this.setPosition();
	this.div.style.height = 1;
	this.div.style.visibility = 'hidden';
	this.setMouseOver(0);
	for(var i = 0; i < this.subMenus.length; i++){
		this.subMenus[i].attachTo(this.subMenus[i].triggerName);
			
	}	
}

Menu.prototype.getxPos = function(){
	return this.xpos;	
}
Menu.prototype.getyPos = function(){
	return this.ypos;	
}
Menu.prototype.addItem = function(linkName, linkURL){
	this.items.push("<tr>"+
				"<td class=\"menuItem\" style=\"padding-left:"+this.pl+"px;padding-right:"+this.pr+"px;padding-top:"+this.pt+"px;"+
					"padding-bottom:"+this.pb+"px;text-align:"+this.alignTo+"; color:"+this.txtclr1+";"+
					"background-color:"+this.bgclr1+"; cursor:hand; cursor:pointer\" "+
					"onmouseover=\"this.style.backgroundColor='"+this.bgclr2+"'; this.style.color='"+this.txtclr2+"';\" "+
					"onmouseout=\"this.style.backgroundColor='"+this.bgclr1+"';this.style.color='"+this.txtclr1+"'\" "+
					"onclick=\"window.location.href = '"+linkURL+"'\">"
						+linkName+
				"</td>"+
			"</tr>");
	this.menuCode = this.getItemList();
}
Menu.prototype.getItemList = function(){
	var list = "";
	for(var i = 0; i < this.items.length; i++){
		list += this.items[i]+"\n";	
	}

	return list;
}
Menu.prototype.setPosition = function(){
	this.div.style.left = findPosX(this.trigger) + this.offX; //+ this.trigger.offsetWidth
	this.div.style.top = findPosY(this.trigger) + this.offY;
}
Menu.prototype.hide = function(){
	if(this.div != null){
		this.setMouseOver(0);	
		if(this.mother)
			this.mother.hide();	
		window.setTimeout("hide2("+this.indexNr+")", 400);			
		
	}
}
function hide2(nr){
	if(!menus[nr].mouseOver){
		if(menus[nr].div.offsetHeight > 10){
			window.clearTimeout(menus[nr].timer);
			window.clearTimeout(menus[nr].timer2);
			menus[nr].div.style.height = menus[nr].div.offsetHeight - 10;
			menus[nr].timer = window.setTimeout("hide2("+nr+")", 10);
		}
		else {	
			menus[nr].div.style.visibility = 'hidden';
		}
	}
}
Menu.prototype.show = function(){
	if(this.div != null){
		this.setMouseOver(1);
		if(this.mother)
			this.mother.show();
		this.div.style.visibility = 'visible';
		show2(this.indexNr);
	}
		
}
function show2(nr){
	if(menus[nr].mouseOver){
		if(menus[nr].div.offsetHeight < document.getElementById(menus[nr].tableID).offsetHeight){
			window.clearTimeout(menus[nr].timer);
			window.clearTimeout(menus[nr].timer2);
			menus[nr].div.style.height = menus[nr].div.offsetHeight + 3;
			menus[nr].timer2 = window.setTimeout("show2("+nr+")", 10);
		}
		else{	
			menus[nr].div.style.height = document.getElementById(menus[nr].tableID).offsetHeight;
		}
	}
}
Menu.prototype.setMouseOver = function(bool){
	this.mouseOver = bool;
	if(this.mother)
		this.mother.setMouseOver(bool);	
}
window.onresize = resetMenuPositions;
window.onload = resetMenuPositions;


