var currZ = 100;
var hiderLeftShift = 0;
var hiderWidthShift = 3; 
var onMouseOut = 'mouseout';
var onMouseOver = 'mouseover';

//***** Cross Browser support;
var is_ie = (!window.addEventListener && window.attachEvent);

if (is_ie)
{	
	onMouseOut = 'mouseleave';
	onMouseOver = 'mouseenter';
}

function addEventHandler(oTarget, sEventType, fnHandler) 
{
	if (is_ie)
	{
		if (sEventType == "mouseover") sEventType = "mouseenter"; 
		else if (sEventType == "mouseout") sEventType = "mouseleave";
	}
	
	if (oTarget.addEventListener) { 
		oTarget.addEventListener(sEventType, fnHandler, false); 
	} else if (oTarget.attachEvent) { 
		oTarget.attachEvent('on' + sEventType, fnHandler);
	} else { 
		oTarget['on' + sEventType] = fnHandler;
	}
}	

//----------------- Classes definitions ------------------		   
function MenuInfo(name, containerName, cssClasses)
{
	this.name = name;
	this.mSheets = containerName;
	this.timer = new UniqueTimer(null);
	this.lastMenuId = 0;
	this.menuDisplayLeftShift = -1;
	this.menuDisplayTopShift = 0;
	this.activeHeaderItem = null;
	this.cssSheet			= cssClasses[0];
	this.cssBorderTop		= cssClasses[1];
	this.cssBorderTopLeft	= cssClasses[2];
	this.cssBorderTopCenter = cssClasses[3];
	this.cssBorderTopRight	= cssClasses[4];
	this.cssBorderLeft	    = cssClasses[5];
	this.cssBorderRight	    = cssClasses[6];
	this.cssBorderBottom	= cssClasses[7];
	this.cssBorderBottomLeft = cssClasses[8];
	this.cssBorderBottomCenter = cssClasses[9];
	this.cssBorderBottomRight = cssClasses[10];
	this.cssSheetCenter		= cssClasses[11];
	this.cssItemUnselected	= cssClasses[12];
	this.cssItemSelected	= cssClasses[13];
	this.cssItem		    = cssClasses[14];
	this.cssItemLink		= cssClasses[15];
	this.cssItemMarker	    = cssClasses[16];
	this.cssImagePreloading = cssClasses[17];
	this.cssHeaderItem = cssClasses[18];
}

function UniqueTimer(timerId)
{
	this.id = timerId;
}

function MenuLink(textVal, linkVal, subVal){
	this.text = textVal;
	this.action = linkVal;
	this.submenu = subVal;
}

function MenuSheet(parentObj, menuInfo)
{
	this.links = new Array(); 
	this.addL = menuAddLink; 
	this.addS = menuAddSubmenu;
	this.create = menuCreate; 
	this.show = menuShow; 
	this.hide = menuHide; 
	this.hideCh = menuHideCh; 
	this.parent = parentObj; 
	this.menuInfo = menuInfo;
	if (parentObj)
	{
		this.menuInfo = parentObj.menuInfo
	}
	
	this.id = menuInfo.lastMenuId;
	if (!parentObj)
	{
		menuInfo.lastMenuId++;
	}
	
	this.block = document.createElement("DIV");
	if (!parentObj)
	{
		this.block.className = this.menuInfo.cssImagePreloading;	
	}
	else
	{
		this.block.className = this.menuInfo.cssSheet;
	}
	this.block.style.zIndex = currZ++;
	this.block.id = menuInfo.mSheets + "MenuSheet";
	
	if (is_ie)
	{
		this.hider = this._createHider();
	}
	
	document.body.appendChild(this.block); 
}

//------------------- General Functions --------------------
function menuHideAll(menuInfo)
{
	var info = eval(menuInfo);
	if (info)
	{
		var mSheets = eval(info.mSheets);
		if (info.activeHeaderItem)
		{
			info.activeHeaderItem.className = info.cssHeaderItem;
			info.activeHeaderItem = null;
		}
		for (var c in mSheets) mSheets[c].hide();
	}
}

function hideAll(menuInfo)
{	
	var info = eval(menuInfo);
	if (info)
	{
		var mSheets = eval(info.mSheets);
		for (var c in mSheets) mSheets[c].hide();
	}
}	


function menuHideTimerSet(menuInfo)
{
	var info = eval(menuInfo);
	if (info)
	{
		var timer = info.timer;
		var mSheetsUniqueName = eval(menuInfo).mSheets;
		timer.id = window.setTimeout(new Function("menuHideAll('" + menuInfo + "')"), 100);
	}
}

function menuHideTimerReset(menuInfo)
{
	var info = eval(menuInfo);
	if (info)
	{
		var timer = info.timer;
		if (timer.id)
		{
			 window.clearTimeout(timer.id);
		}
	}
}

function changeActiveItem(obj, menuInfo)
{
	var info = eval(menuInfo);
	if (info)
	{
		if (info.activeHeaderItem)
		{
			info.activeHeaderItem.className = info.cssHeaderItem;
		}
		if (obj)
		{
			info.activeHeaderItem = obj;
		}
	}
}

function setOriginalClasses(mSheetsUniqueName)
{
	var mSheets = eval(mSheetsUniqueName);
	for (var c in mSheets) 
	{
		mSheets[c].block.className = mSheets[c].menuInfo.cssSheet;
	}
}

function menuAddLink(textVal, linkVal){
	this.links[this.links.length] = new MenuLink(textVal, linkVal, null);
}

function menuAddSubmenu(textVal, linkVal){
	this.links[this.links.length] = new MenuLink(textVal, linkVal, new MenuSheet(this, this.menuInfo));
}

function menuShow(leftVal, topVal){
	this.block.style.left = leftVal + "px";
	this.block.style.top = topVal + "px";
	this.block.style.display = "block";

	if (is_ie)
	{
		var menuSheet;
		var childs = this.block.children[0].children[0].children;
		if (childs.length > 2)
		{
			topVal += 5;
			menuSheet = childs[1].children[1];
		}
		else
		{
			menuSheet = childs[0].children[1];
		}
		this._setupHider(this.hider, leftVal + hiderLeftShift, topVal, menuSheet.offsetWidth + hiderWidthShift, menuSheet.offsetHeight + hiderWidthShift);
	}
}

function menuHide(){
	if (this.block.style.display != 'none')
	{
		if (is_ie)
			this._closeHider(this.hider);
		this.hideCh();
		this.block.style.display = "none";
	}
}

function menuHideCh(){
	for (var c in this.links)
	{
		curLink = this.links[c];
		if (curLink.submenu && curLink.submenu.block.style.display != 'none')
		{
			 curLink.submenu.hide();
		}
	}
}

function IsRootMenuSheet(sheet)
{
	var result = new Boolean(true);
	if(sheet.parent)
	{
		result = false;
	}
	return result;
}

function GetBottomBorder(menuInfo)
{
		var trBottomBorder = document.createElement("TR");		
		trBottomBorder.className = menuInfo.cssBorderBottom;
		
		var td1BottomBorder = trBottomBorder.appendChild(document.createElement("TD"));
		
		var td2BottomBorder = trBottomBorder.appendChild(document.createElement("TD"));	
		td2BottomBorder.vAlign = "top";
		td2BottomBorder.valign = "top";
		
		var tableBottomBorder = td2BottomBorder.appendChild(document.createElement("TABLE")); 
				
		var tbodyBottomBorder = tableBottomBorder.appendChild(document.createElement("TBODY")); 
		
		var tr2BottomBorder = tbodyBottomBorder.appendChild(document.createElement("TR")); 
		var tdBottomLeft = tr2BottomBorder.appendChild(document.createElement("TD")); 
		tdBottomLeft.className = menuInfo.cssBorderBottomLeft; 
		tdBottomLeft.innerHTML = "&nbsp;"
		
		var tdBottomCenter = tr2BottomBorder.appendChild(document.createElement("TD")); 
		tdBottomCenter.className = menuInfo.cssBorderBottomCenter;
		tdBottomCenter.vAlign = "top";
		tdBottomCenter.innerHTML = "&nbsp;"
		
		var tdBottomRight = trBottomBorder.appendChild(document.createElement("TD"));	
		tdBottomRight.innerHTML = "&nbsp;";
		tdBottomRight.vAlign = "top";
		tdBottomRight.className = menuInfo.cssBorderBottomRight//"menu-bot";

		return trBottomBorder;
}

function GetTopGradientBorder(menuInfo)
{
		var trTopBorder = document.createElement("TR");
		trTopBorder.className = menuInfo.cssBorderTop;
		
		var tdBorderTopLeft = trTopBorder.appendChild(document.createElement("TD"));
		tdBorderTopLeft.className = menuInfo.cssBorderTopLeft//"menu-fill";
		
		var td2TopBorder = trTopBorder.appendChild(document.createElement("TD"));
		td2TopBorder.className = menuInfo.cssBorderTopCenter;//"menu-ruller";
		addEventHandler(td2TopBorder, onMouseOver, new Function("menuHideTimerReset('" + menuInfo.name + "')"));
		addEventHandler(td2TopBorder, onMouseOut, new Function("menuHideTimerSet('" + menuInfo.name + "')"));
		td2TopBorder.innerHTML = "&nbsp;";
		
		var td3TopBorder = trTopBorder.appendChild(document.createElement("TD"));
				
		var divTopBorder = td3TopBorder.appendChild(document.createElement("DIV"));
		divTopBorder.className = menuInfo.cssBorderTopRight//"menu-rtop";
		divTopBorder.innerHTML = "&nbsp;"; 
		
		return trTopBorder;
} 

function GetMenuItem(sheet, curLink, path, menuLink)		
{
	var functionBody;

	var trMainItem = document.createElement("TR");
	
	var tdMenuItems = trMainItem.appendChild(document.createElement("TD"));
	tdMenuItems.className = sheet.menuInfo.cssItemUnselected//"menu-items";
	tdMenuItems.onclick = new Function("hideAll(" + sheet.menuInfo.name + "); gotoURL('" + curLink.action + "')");
	
	// TODO: try to refactor this, using addEventHandler
	functionBody = "this.className='" + sheet.menuInfo.cssItemUnselected + "';";
	tdMenuItems["on" + onMouseOut] = new Function(functionBody);
	
	functionBody = "this.className = '" + sheet.menuInfo.cssItemSelected + "';" + path + ".hideCh();";
	tdMenuItems["on" + onMouseOver] = new Function(functionBody);
			
	tdMenuItems.colspan = 2;
	if (curLink.submenu)
	{
		newPath = path + ".links[" + menuLink + "].submenu";
		functionBody = "this.className='" + sheet.menuInfo.cssItemSelected + "';" + path + ".hideCh();" + 
			newPath + ".show(getLeftPos(this) + this.offsetWidth + " + sheet.menuInfo.menuDisplayLeftShift + ", getTopPos(this) + " + sheet.menuInfo.menuDisplayTopShift+ ")";
		tdMenuItems["on" + onMouseOver] = new Function(functionBody);
		curLink.submenu.create(newPath);
	}

	
	var tableMenuItem = tdMenuItems.appendChild(document.createElement("TABLE"));
	tableMenuItem.className = sheet.menuInfo.cssItem//"menu-item";
	
	var tbodyMenuItem = tableMenuItem.appendChild(document.createElement("TBODY"));
	var trMenuItem = tbodyMenuItem.appendChild(document.createElement("TR"));
	var tdMenuItem = trMenuItem.appendChild(document.createElement("TD"));
	var divItemLink = tdMenuItem.appendChild(document.createElement("DIV"));
	divItemLink.className = sheet.menuInfo.cssItemLink ;
	divItemLink.innerHTML = curLink.text;
	
	if (curLink.submenu)	
	{	
		var tdMarker = trMenuItem.appendChild(document.createElement("TD"));
		tdMarker.className = sheet.menuInfo.cssItemMarker; //; "menu-marker";
		tdMarker.innerHTML = "&nbsp;"; 
	}

	return trMainItem;
}

function menuCreate(path)
{
	var curLink;
	var res = "";
	var newPath;
	if (path == null) path = this.menuInfo.mSheets + "[" + this.id + "]";

	var tableMenuSheet = document.createElement("TABLE");
	
	
	var tbodyMenuSheet = tableMenuSheet.appendChild(document.createElement("TBODY"));
		
	if (IsRootMenuSheet(this))
	{
		 tbodyMenuSheet.appendChild(GetTopGradientBorder(this.menuInfo));
	}
	
	var trMenuSheet = tbodyMenuSheet.appendChild(document.createElement("TR"));

	var tdLeftBorder = trMenuSheet.appendChild(document.createElement("TD"));
	tdLeftBorder.className = this.menuInfo.cssBorderLeft;//"menu-lb";
	
	var tdGradientFill = trMenuSheet.appendChild(document.createElement("TD"));
	tdGradientFill.className = this.menuInfo.cssSheetCenter ;//"fill-gradient";
	addEventHandler(tdGradientFill, onMouseOver, new Function("menuHideTimerReset('" + this.menuInfo.name + "')"));
	addEventHandler(tdGradientFill, onMouseOut, new Function("menuHideTimerSet('" + this.menuInfo.name + "')"));
	
	var tableMenuItems = tdGradientFill.appendChild(document.createElement("TABLE"));
		
	var tbodyMenuItems = tableMenuItems.appendChild(document.createElement("TBODY"));
	
	for (var c in this.links)
	{
		curLink = this.links[c];
		tbodyMenuItems.appendChild(GetMenuItem(this, curLink , path, c));
	}
	
	var tdRightBorder = trMenuSheet.appendChild(document.createElement("TD"));		
	tdRightBorder.className = this.menuInfo.cssBorderRight;//"menu-rb";
	tdRightBorder.innerHTML = "&nbsp;"
	
	tbodyMenuSheet.appendChild(GetBottomBorder(this.menuInfo));
	
	this.block.appendChild(tableMenuSheet);
}



MenuSheet.prototype._createHider = function ()
{
	var f = null;
	var filter = 'filter:progid:DXImageTransform.Microsoft.alpha(style=0,opacity=0);';
	var id = 'dynarch-menu-hider-' + (this.id);
	window.self.document.body.insertAdjacentHTML('beforeEnd', '<iframe id="'+id+'" scroll="no" frameborder="0" '+'style="position:absolute;visibility:hidden;'+filter+'border:0;top:0;left:0;width:0;height:0;" '+'src="javascript:false;"></iframe>');
	f = window.self.document.getElementById(id);
	return f;
};

MenuSheet.prototype._setupHider = function (f, x, y, w, h)
{
	if (f)
	{
		var s=f.style;
		s.left=x+"px";
		s.top=y+"px";
		s.width=w+"px";
		s.height=h+"px";
		s.visibility="visible";
	}
};

MenuSheet.prototype._closeHider = function (f)
{
	if (f)
		f.style.visibility="hidden";
};

 
function getLeftPos(obj)
{
    var res = 0;
    while (obj)
    {
        res += obj.offsetLeft;
        obj = obj.offsetParent;
    }
    return res;
}

function getTopPos(obj)
{
    var res = 0;
    while (obj)
    {
         res += obj.offsetTop;
         obj = obj.offsetParent;
    }
    return res;
}

function gotoURL(url)
{
	//document.body.fireEvent('onunload');
	//disableEvents(document.body);	
	//document.body.style.visibility='hidden';
	window.location.href = url;	
	return true;
}

function disableEvents(obj)
{
	var child;
	if (obj.childNodes)
	{
		for (i = 0; i < obj.childNodes.length; i++) 
		{
			child = obj.childNodes.item(i);
			if (child.onmouseover)
				child.onmouseover = null;
			if (child.onmouseenter)
				child.onmouseenter = null;
			disableEvents(child);					
		}		
	}
}