/*
** ci_menu meniu valdiklis
** Corpus Integrum 2008-01
**
** Marius Grauzinis
*/

function CI_Menu(par)
{
	this.parentObj = false;
	
	if (par)
	{
		if (typeof(par) == 'object')
		{
			this.parentObj = par;
		}
		else
		{
			this.parentObj = document.getElementById(par);
		}
	}
	
	this.items = new Array();
	
	this.expandOn = 'click';
}

CI_Menu.prototype.setExpansionTrigger = function(val)
{	
	if (val == 'click' || val == 'mouseover')
	{
		this.expandOn = val;
	}
	else
	{
		this.expandOn = 'click';
	}
}

//pridedam meniu punkta
CI_Menu.prototype.addItem = function(id, parent_id, html_id, submenu_id)
{
	var tmp = new Array();
	tmp['id'] = id;
	tmp['parent_id'] = parent_id;
	tmp['html_id'] = html_id;
	tmp['submenu_id'] = submenu_id;
	tmp['opened'] = false;
	tmp['locked'] = false;
	
	this.items[id] = tmp;
}

CI_Menu.prototype.openItem = function(id, lock)
{
	var curr = this.items[id];
	
	if (curr)
	{
		if (curr['parent_id'] > 0)
		{
			this.openItem(curr['parent_id'], lock);
		}
		
		if (!curr['locked'] && curr['submenu_id'] != '')
		{
			var sb = document.getElementById(curr['submenu_id']);
			
			if (sb)
			{
				curr['opened'] = true;
				curr['locked'] = lock;
				sb.style.display = 'block';
			}
		}
	}
}

CI_Menu.prototype.closeAll = function()
{
	for (i in this.items)
	{
		var curr = this.items[i];
		
		if (!curr['locked'] && curr['submenu_id'] != '')
		{
			var sb = document.getElementById(curr['submenu_id']);
			
			if (sb)
			{
				curr['opened'] = false;
				sb.style.display = 'none';
			}
		}
	}
}

CI_Menu.prototype.openMenu = function(id, lock)
{
	this.closeAll();
	this.openItem(id, lock);
}

CI_Menu.prototype.addAction = function(id)
{
	curr = this.items[id];
	
	var itm = document.getElementById(curr['html_id']);
			
	if (itm)
	{
		var menu = this;
		if (this.expandOn == 'click')
		{
			var act = itm.onclick;
			itm.onclick = function() {menu.openMenu(id); if (act && typeof(act) == 'function') {act();}};
		}
		else if (this.expandOn == 'mouseover')
		{
			var overAct = itm.onmouseover;
			
			itm.onmouseover = function() {menu.openMenu(id); if (overAct && typeof(overAct) == 'function') {overAct();}};
			
			if (false && this.parentObj)
			{
				var outAct = this.parentObj.onmouseout;
//				this.parentObj.style.border = '5px solid yellow;';
				this.parentObj.onmouseout = function(e) 
					{
						if (!e) 
						{
							var e = window.event; 
						}
						
						if (e)
						{
/*							e.cancelBubble = true;
							
							if (e.stopPropagation) 
							{
								e.stopPropagation();
							}*/
							
	//						alert(this.innerHTML);
							if (menu.parentObj == this)
							{
	//							setTimeout('menu.closeAll();', 1000);
								menu.closeAll();
								
								if (outAct && typeof(outAct) == 'function') 
								{
									outAct();
								}
								
								this.style.backgroundColor = '#00FF00';
							}
						}
					/*	for (i in e)
						{
							alert(i + ' ' + e[i]);
						} */
					};
//				this.parentObj.onmouseout = function() {menu.closeAll(); alert('clsd'); if (outAct && typeof(outAct) == 'function') {outAct();}};
			}
		}
	}

}

CI_Menu.prototype.applyActions = function()
{	
	for (i in this.items)
	{
		var curr = this.items[i];		
		this.addAction(curr['id']);
	}
}