// JavaScript Document

function menu(Container, ArrayObj, SubWidth, Type){
	
	this.maxWidth = SubWidth;
	this.data = ArrayObj;
	this.container = Container;
	this.type = Type;
	
	this.currentButton = -1;
	this.divObj = document.createElement('div');
	this.tableObj = document.createElement('table');
	

	this.createMenu = function(){
		var table = document.createElement('table');
		table.setAttribute('cellPadding',0);
		table.setAttribute('cellSpacing',0);
		table.setAttribute('width','100%');
		table.setAttribute('height','100%');
		var tbody = document.createElement('tbody');
		var rowObj = document.createElement('tr');
		tbody.appendChild(rowObj);
		table.appendChild(tbody);
		this.container.appendChild(table);
				
		var nrButtons = this.data.length;
		
		var self = this.data;
		var bound = this;
		
		for (var i=1; i<=nrButtons; i++){
			var obj = this.data[i-1];
			var obj_name = obj.name;
			var obj_link = obj.link;
			
			var cellObj = document.createElement('td');
			cellObj.setAttribute('id','button_'+i);
			cellObj.setAttribute('name','button_'+i);
			cellObj.innerHTML = obj_name;
			rowObj.appendChild(cellObj);
			if (i==1){
				cellObj.className = "button_first" + this.getClassType();
				cellObj.onmouseover = function(){
					this.className = "button_first_over" + bound.getClassType();
					this.style.cursor = "hand";
					this.currentButton = this.cellIndex;
					
					bound.resetSubMenu();
					bound.setSubMenu(this,this.currentButton);
				}
				cellObj.onmouseout = function(){
					this.className = "button_first" + bound.getClassType();
					this.style.cursor = "pointer";
					this.currentButton = -1;
					
					if (bound.type == "under"){
						if (window.event != null){
							if ((event.y + document.body.scrollTop) <= parseInt(getTopPositionIE(this))){
								bound.hide();
							}
						}
						else{
							if (getMouseY()+document.body.scrollTop <= parseInt(getTopPosition(this))){
								bound.hide();
							}	
						}
					}
					else if (bound.type == "above"){
						if (window.event != null){
							if ((event.y + document.body.scrollTop) >= parseInt(getTopPositionIE(this))){
								bound.hide();
							}
						}
						else{
							if (getMouseY()+document.body.scrollTop >= parseInt(getTopPosition(this))){
								bound.hide();
							}	
						}
					}
				}
				cellObj.onclick = function(){
					this.className = "button_first_over" + bound.getClassType();
					this.style.cursor = "hand";
					this.currentButton = this.cellIndex;
					
					gotoLink(self[this.currentButton].link);
				}
				
			}
			else if (i==nrButtons){
				cellObj.className = "button_last" + this.getClassType();
				cellObj.onmouseover = function(){
					this.className = "button_last_over" + bound.getClassType();;
					this.style.cursor = "hand";
					this.currentButton = this.cellIndex;
					
					bound.resetSubMenu();
					bound.setSubMenu(this,this.currentButton);
				}
				cellObj.onmouseout = function(){
					this.className = "button_last" + bound.getClassType();
					this.style.cursor = "pointer";
					this.currentButton = -1;
					
					if (bound.type == "under"){
						if (window.event != null){
							if ((event.y + document.body.scrollTop) <= parseInt(getTopPositionIE(this))){
								bound.hide();
							}
						}
						else{
							if (getMouseY()+document.body.scrollTop <= parseInt(getTopPosition(this))){
								bound.hide();
							}	
						}
					}
					else if (bound.type == "above"){
						if (window.event != null){
							if ((event.y + document.body.scrollTop) >= parseInt(getTopPositionIE(this))){
								bound.hide();
							}
						}
						else{
							if (getMouseY()+document.body.scrollTop >= parseInt(getTopPosition(this))){
								bound.hide();
							}	
						}
					}
				}
				cellObj.onclick = function(){
					this.className = "button_last_over" + bound.getClassType();
					this.style.cursor = "hand";
					this.currentButton = this.cellIndex;
					
					gotoLink(self[this.currentButton].link);
				}
			}
			else{ 
				cellObj.className = "button_normal" + this.getClassType();
				cellObj.onmouseover = function(){
					this.className = "button_normal_over" + bound.getClassType();
					this.style.cursor = "hand";
					this.currentButton = this.cellIndex;
					
					bound.resetSubMenu();
					bound.setSubMenu(this,this.currentButton);
				}
				cellObj.onmouseout = function(){
					this.className = "button_normal" + bound.getClassType();
					this.style.cursor = "pointer";
					this.currentButton = -1;
					
					if (bound.type == "under"){
						if (window.event != null){
							if ((event.y + document.body.scrollTop) <= parseInt(getTopPositionIE(this))){
								bound.hide();
							}
						}
						else{
							if (getMouseY()+document.body.scrollTop <= parseInt(getTopPosition(this))){
								bound.hide();
							}	
						}
					}
					else if (bound.type == "above"){
						if (window.event != null){
							if ((event.y + document.body.scrollTop) >= parseInt(getTopPositionIE(this))){
								bound.hide();
							}
						}
						else{
							if (getMouseY()+document.body.scrollTop >= parseInt(getTopPosition(this))){
								bound.hide();
							}	
						}
					}
				}
				cellObj.onclick = function(){
					this.className = "button_normal_over" + bound.getClassType();
					this.style.cursor = "hand";
					this.currentButton = this.cellIndex;
					
					gotoLink(self[this.currentButton].link);
				}
			}
		}
	}
	
	this.createSubMenuHolder = function(){
		this.divObj.setAttribute('id','submenu');
		this.divObj.setAttribute('name','submenu');
		this.divObj.setAttribute('z-index',100);
		this.divObj.style.backgroundColor = '#0d1e88';
		this.divObj.style.visibility = 'hidden';
		this.divObj.style.position = 'absolute';
		this.divObj.style.top = '0 px';
		this.divObj.style.left = '0 px';
		this.divObj.style.width = 1 + 'px';
		this.divObj.style.height = 1 + 'px';
		
		this.tableObj.setAttribute('cellPadding',0);
		this.tableObj.setAttribute('cellSpacing',1);
		this.tableObj.setAttribute('width','100%');
		this.tableObj.setAttribute('height','100%');
		
		var tbody = document.createElement('tbody');
		this.tableObj.appendChild(tbody);
		this.divObj.appendChild(this.tableObj);
		document.body.appendChild(this.divObj);
		
		var bound = this;
		
		this.divObj.onmouseover = function(){
			this.style.backgroundColor = '#0d1e88';
		}
		this.divObj.onmouseout = function(){
			
			// for IE
			if (window.event != null){
				if ((event.x-2 <= parseInt(this.style.left)) || (event.x >= parseInt(this.style.left)+parseInt(this.style.width))){
					bound.hide();
				}
				if (bound.type == "under"){
					if ((event.y + document.body.scrollTop) >= parseInt(this.style.top)+parseInt(this.clientHeight)){
						bound.hide();
					}
				}
				else if (bound.type == "above"){
					if ((event.y + document.body.scrollTop-2) <= parseInt(this.style.top)){
						bound.hide();
					}
				}
			}
			// for other browsers
			else{
				if ((getMouseX()-2 <= parseInt(this.style.left)) || (getMouseX()+2 >= parseInt(this.style.left)+parseInt(this.scrollWidth))){
					bound.hide();
				}
				
				if (bound.type == "under"){
					if ((getMouseY()+document.body.scrollTop+3) >= parseInt(this.style.top)+parseInt(this.offsetHeight)){
						bound.hide();
					}
				}
				else if (bound.type == "above"){
					if ((getMouseY()+document.body.scrollTop-3) <= parseInt(this.style.top)){
						bound.hide();
					}
				}	
			}
		}
	}
	
	this.resetSubMenu = function(){
		var nrRows = this.tableObj.rows.length;
		for (var i=nrRows-1; i>=0; i--){
			this.tableObj.deleteRow(i);
		}
		this.divObj.style.height = 1;
		this.divObj.style.top = 0;
	}
				
	
	this.setSubMenu = function(handler,index){
		if (this.data[index].submenu != null){
			var nrButtons = this.data[index].submenu.length;
		}
		else var nrButtons = 0;
		
		var bound = this;
		
		for (var i=1; i<=nrButtons; i++){
			var obj_name = this.data[index].submenu[i-1].name;
			
			var rowObj = document.createElement('tr');
			this.tableObj.getElementsByTagName('tbody')[0].appendChild(rowObj);
			var cellObj = document.createElement('td');
			cellObj.setAttribute('id','button_'+i);
			cellObj.setAttribute('name','button_'+i);
			cellObj.innerHTML = obj_name;
			rowObj.appendChild(cellObj);
			cellObj.height = 22;
			cellObj.className = "subbutton" + this.getClassType();
			
			
			//var obj_link = this.data[index].submenu[i].link;
			
			cellObj.innerText = obj_name;
			cellObj.onmouseover = function(){
				this.className = "subbutton_over" + bound.getClassType();
				this.style.cursor = "hand";
			}
			cellObj.onmouseout = function(){
				this.className = "subbutton" + bound.getClassType();
				this.style.cursor = "pointer";
			}
			cellObj.onclick = function(){
				this.className = "subbutton" + bound.getClassType();
				this.style.cursor = "hand";
				
				var obj_link = bound.data[index].submenu[this.parentNode.rowIndex].link;
				gotoLink(obj_link);
			}
		}
		
		if (this.type == "under"){
			this.moveUnderObject(handler);
		}
		else{
			this.moveAboveObject(handler);
		}
		
		if (nrButtons > 0){
			this.modifyContainer(this.maxWidth);
			this.show();
		}
		else{ 
			this.modifyContainer(5);
			this.hide();
		}
		
		this.divObj.style.height = this.tableObj.offsetHeight;
	}
	
	
	this.modifyContainer = function(w){
		this.width = w;
		this.divObj.style.width = this.width + 'px';
	}
	
	this.moveUnderObject = function(obj){
		var left = obj.offsetLeft;
		var top = obj.offsetTop;
		var object = obj;
		while (object.offsetParent){
			object = object.offsetParent;
			left += object.offsetLeft;
			top += object.offsetTop;
		}
		top += (obj.clientHeight);
		
		this.moveToXY(left,top);
	}
	
	this.moveAboveObject = function(obj){
		var left = obj.offsetLeft;
		var top = obj.offsetTop;
		var object = obj;
		while (object.offsetParent){
			object = object.offsetParent;
			left += object.offsetLeft;
			top += object.offsetTop;
		}
		if (this.tableObj.rows.length > 0){
			top -= (this.tableObj.rows.length * this.tableObj.rows[0].cells[0].height + this.tableObj.rows.length+1);
		}
		
		this.moveToXY(left,top);
	}
	
	this.moveToXY = function(x,y){
		this.divObj.style.left = x + 'px';
		this.divObj.style.top = y + 'px';
	}
	
	this.changeSrc = function(){
		this.iframeObj.src = this.src;
	}
	
	this.show = function(){
		this.divObj.style.visibility = 'visible';
	}
	
	this.hide = function(){
		this.divObj.style.visibility = 'hidden';
	}
	
	this.isHidden = function(){
		if (this.divObj.style.visibility == 'hidden'){
			return true;
		}
		return false;
	}
	
	this.getClassType = function(){
		if (this.type == "under"){
			return "";	
		}
		else return "2";
	}
}


// -----------------------------------------------------------------------------------------------------------------------------------------------

function getTopPositionIE(obj){
	var top = obj.offsetTop;
	var object = obj;
	while (object.offsetParent){
		object = object.offsetParent;
		top += object.offsetTop;
	}
	top += (obj.clientHeight);
	return top;
}


// ------ Comun functions ------------------------------------------------------------------------------------------------------------------------------------------

function gotoLink(url){
	window.location.href = url;
}

var mouseX;
var mouseY;

document.onmousemove = getMousePos;
function getMousePos(e){
	if (window.event == null){
		mouseX = e.clientX;
		mouseY = e.clientY;
	}
}
function getMouseY(){
	return mouseY;	
}
function getMouseX(){
	return mouseX;	
}