MENU_HORIZONTAL = 1;
MENU_VERTICAL = 2;

function generateRandomId() {
	return Math.floor(Math.random()*100000000);
}

function newMenu(x, y, orientation, isPopup){
	oTable = document.createElement("table");
	oTable.id = 'submenu'+generateRandomId();
	oTable.style.position = 'absolute';
	oTable.style.left = x;
	oTable.style.top = y;
	oTable.cellPadding = 0;
	oTable.cellSpacing = 0;
	oTable.isPopup = isPopup;
	oTable.pos = orientation;
	oTable.timer_id = null;
	oTable.parent_button = null;
	
	if(isPopup == 1)
		oTable.style.display = "none";

	oTable.addButtonObject = function(button)
	{
		var cell = null;
		if(this.pos == MENU_HORIZONTAL)
		{
			if(this.rows.length == 0)
				this.insertRow(0);
			cell = this.rows[0].insertCell(this.rows[0].cells.length);
		}
		else
		{
			var row = this.insertRow(this.rows.length);
			cell = row.insertCell(0);
		}
		
		button.setParent(this);
		cell.appendChild(button);
		
		return button;
	}
	
	oTable.addButton = function(text, image, isPopup, action) {
		var button = newMenuItem(text, image, isPopup, action);
		return this.addButtonObject(button);
	}
	
	if(oTable.isPopup)
	{
		oTable.className = sub_class;
		oTable.onmouseover = function()
		{
			this.show();
			var parent = this.parent_sub;
			while(parent && parent != 'underfined')
			{
				var obj = document.getElementById(parent);
				obj.show();
				parent = obj.parent_sub;
			}
		}
		
		oTable.onmouseout = function()
		{
			this.hide();
			var parent = this.parent_sub;
			while(parent && parent != 'underfined')
			{
				var obj = document.getElementById(parent);
				obj.hide();
				parent = obj.parent_sub;
			}
		}
	}
	
	oTable.show = function()
	{
		if( this.parent_button != null ) {
			this.parent_button.className = 'menu_td_over';
		}
		
		if(this.isPopup)
		{
			window.clearTimeout(this.timer_id);
			
			if(this.style.display == "none") {
				this.timer_id = window.setTimeout("show('"+this.id+"')",1);
			}
		}
	}
	
	oTable.hide = function()
	{
		if( this.parent_button != null ) {
			this.parent_button.className = 'menu_td_out';
		}
		
		if(this.isPopup)
		{
			window.clearTimeout(this.timer_id);
			if(this.style.display != "none")
			{
				this.timer_id = window.setTimeout("hide('"+this.id+"')",50);
			}
		}
	}
	
	document.body.appendChild(oTable);
	
	return oTable;
}


function newMenuItem(text, image, isPopup, action)
{
	var uniq_id = 'but_'+generateRandomId();
	
	//button parsing will be here
	var button = buttonTemplate;
	var bt_txt = /\{button_text\}/g;
	var bt_img = /\{button_image\}/g;
	var arr_img = /\{arrow_image\}/g;
	var selector = /\{selector\}/g;
	var but_link_action = /\{action\}/g;
	var but_link_class = /\{lnk_class\}/g;
	var but_mouseover = /\{button_over\}/g;
	var but_mouseout = /\{button_out\}/g;
	var but_id = /\{id\}/g;
	
	button = button.replace(bt_txt, text);
	button = button.replace(bt_img, image);
	button = button.replace(selector, 'selector'+generateRandomId());
	button = button.replace(but_link_action, action);
	
	button = button.replace(but_link_class, link_class);
	button = button.replace(but_mouseover, mouseover_action);
	button = button.replace(but_mouseout, mouseout_action);	
	
	button = (isPopup)?	button.replace( arr_img, "<img src='images/menu/arrow.gif' align='right' width='7' height='14' border='0'>"):
						button.replace( arr_img, "<img src='images/menu/empty.gif'align='right' width='7' height='14' border='0'>");

	button = button.replace(but_id, uniq_id);
//	alert(button);
	
	var oLink = document.createElement('a');
	
	oLink.id = 'a'+uniq_id;
	oLink.href = action;
	oLink.isPopup = isPopup;
	oLink.action = action;
	oLink.sub = null;
	oLink.parent = null;
	oLink.className = link_class;
	oLink.href = action == ''?'#':action;
	
	var oTable = document.createElement("table");
	oTable.cellPadding = 0;
	oTable.cellSpacing = 0;
	oTable.border = 0;
	oTable.width = '100%';
	
	var row = oTable.insertRow(0);
	var cell0 = row.insertCell(0);
	cell0.innerHTML = button;

	oTable.style.cursor = "hand";
	oLink.appendChild(oTable);
	
	oLink.onclick = function(){
		document.location.href = action;
	}
	
	
	oLink.onmouseover = function()
	{
		if(this.isPopup)
		{
			var x = parseInt(this.parent.style.left)+parseInt(this.offsetParent.offsetLeft)+4;
			var y = parseInt(this.parent.style.top)+parseInt(this.offsetParent.offsetTop);
			
			if(this.parent.pos == MENU_VERTICAL) {
				x += parseInt(oTable.offsetWidth);
			} else {
				y += parseInt(oTable.offsetHeight);
			}
			
			if( this.sub != null && this.sub != 'null' && this.sub != 'undefined') {
				if( (this.sub.rows.length+1)*18+y > parseInt(document.body.clientHeight) ) {
					y = parseInt(document.body.clientHeight) - (this.sub.rows.length+1)*18;
				}
				y+=2;
				x-=3;
				this.sub.style.left = x+'px';
				this.sub.style.top = y+'px';
				
				this.sub.show();
			}
		}
	}
	
	oLink.onmouseout = function()
	{
		if(this.isPopup)
		{
			if( this.sub != null && this.sub != 'null' && this.sub != 'undefined')
				this.sub.hide();
		}
	}
	
	oLink.setParent = function(parent){
		this.parent = parent;
	}
	
	oLink.addSubmenu = function(subMenu) {
		subMenu.parent_sub = this.parent.id;
		subMenu.parent_button = oTable;
		
		this.sub = subMenu;
		
		return this.sub;
	}
	
	oLink.addButton = function(id, text, image, isPopup, action) {
		var button = newMenuItem(id, text, image, isPopup, action);
		return this.addButtonObject(button);
	}
	
	oLink.addButtonObject = function(button) {
		if( this.sub == null ) {
			this.addSubmenu(newMenu(0, 0, MENU_VERTICAL, true, true));
		}
		return this.sub.addButtonObject(button);
	}
	
	oLink.removeSubmenu = function() {
		this.sub = null;
	}
	
	return oLink;
}

function show(obj)
{
	sub = document.getElementById(obj);
	sub.style.display = "block";
}

function hide(obj)
{
	sub = document.getElementById(obj);
	sub.style.display = "none";
}