var lastMenuId = 0;
var timer;
var mSheets = new Array();
var currZ = 100;
var mReady = false;
var menu_path = new Array();
var is_ie6 =(/msie[\s\t]+6/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent));

function setClass(obj, cl){
	if (obj.className!=cl) obj.className = cl;
}



function MenuLink(textVal, linkVal, subVal){
	this.text = textVal;
	this.action = linkVal;
	this.submenu = subVal;
}


function menuHideAll(){
	mSheets.collect(function(mSheetscollect,c) {mSheetscollect.hide();});
}


function menuHideTimerSet(){
	timer = window.setTimeout(menuHideAll, 300);
}



function menuHideTimerReset(){
	if (timer) window.clearTimeout(timer);
}


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));
}


function menuShow(leftVal, topVal, obj){
	if( is_ie6 ){
		if(! this.hider){
			this.hider =this.createHider();
		}
	}
	this.block.style.left = leftVal + "px";
	this.block.style.top = (topVal+30) + "px";
	this.block.style.display = "block";
	this.title.style.left = leftVal + "px";
	this.title.style.top = topVal + "px";
	this.title.style.display = "block";	
	if( is_ie6 && this.hider){
		this.setupHider(this.hider, leftVal, topVal+30, this.block.offsetWidth, this.block.offsetHeight);
	}
}


function menuHide(){
	if( is_ie6 ){
		this.closeHider(this.hider);
	}
	this.hideCh();
	this.block.style.display = "none";
	this.title.style.display = "none";
}


function menuFlip(leftVal, topVal){
	var disp = this.block.style.display;
	if (disp == "none") this.show(leftVal, topVal);
	else this.hide();

}


function menuHideCh(){
	for (var c in this.links){
		curLink = this.links[c];
		if (curLink.submenu) curLink.submenu.hide();
	}
}


function menuCreate(path){
	//if (!mSheets) return;
	var res = "<div class=\"menu-sh\" onmouseout=\"menuHideTimerSet()\" onmouseover=\"menuHideTimerReset()\">";
	var me = this;
	var curLink;
	var newPath;
	var count=0;
	if (path == null) path = "mSheets[" + this.id + "]";
	this.links.collect(function(linkscollect,c){
		curLink = linkscollect
		if (count==0) {
			if(me.ElTitle == "")
			{

			}
			else
			{
				me.title.innerHTML = "<div><a href=\""+me.ElTitleUrl+"\" title=\"\" onmouseout=\"menuHideTimerSet()\" onmouseover=\"menuHideTimerReset()\" class=\"a-over\" style=\"width: "+ me.ElWidth +"px;\">"+me.ElTitle+"</a></div>";
			}
			res += "<table cellpadding=\"0\" cellspacing=\"0\" class=\"tab-menu-sh\" style=\" border-top:#d0d0d0 1px solid;\">";			
				
		}
		count = 1;
		res += "<tr valign=\"center\"><td class=\"blk-menu-sh";

		if (curLink.submenu) res += " blk-menu-arr";
		res += "\" onmouseover=\"setClass(this, 'blk-menu-sh-act";

		if (curLink.submenu) res += " blk-menu-arr-act";

		res += "'); ";

		res += path + ".hideCh()";

		if (curLink.submenu){
			newPath = path + ".links[" + c + "].submenu";
			res += "; " + newPath + ".show(getLeftPos(this)-1 + this.offsetWidth, getTopPos(this)-25,this)";
			curLink.submenu.create_menu(newPath);
		}

		res += "\" onmouseout=\"setClass(this, 'blk-menu-sh";

		if (curLink.submenu) res += " blk-menu-arr";

		res += "')\" onclick=\"gotoURL('"+curLink.action+"')\" nowrap=\"nowrap\">" + curLink.text + "</td></tr>";
		
	});
	
	res += "</table></div>";
	this.block.innerHTML = res;
}



function MenuSheet(parentObj){
	this.setupHider =setupHider;
	this.createHider =createHider;
	this.closeHider =closeHider;

	this.links = new Array();
	this.addLink = menuAddLink;
	this.addSubmenu = menuAddSubmenu;
	this.ElTitle = "";	
	this.ElTitleUrl = "";	
	this.create_menu = menuCreate;
	this.show = menuShow;
	this.hide = menuHide;
	this.flip = menuFlip;
	this.hideCh = menuHideCh;
	this.id = lastMenuId;

	lastMenuId++;

	this.parent = parentObj; 
	this.block = document.createElement("DIV");
	this.block.className = "blk-menu";
	this.block.style.position = "absolute";
	this.block.style.display = "none";
	this.block.style.zIndex = currZ;
	
	document.body.appendChild(this.block);

	currZ++;

	this.title = document.createElement("DIV");
	this.title.id = "ms" + this.id;
	this.title = document.createElement("DIV");
	this.title.className = "blk-menu";
	this.title.style.position = "absolute";
	this.title.style.display = "none";
	this.title.style.zIndex = currZ;
	this.title.id = "ms" + this.id;

	document.body.appendChild(this.title);
}

function showMenu(objVal, numVal){
	if (mReady){
		menuHideAll();

		/*objVal.className = "menu_item_selected";

		

		if ( document.getElementById(obj_left) )

			document.getElementById(obj_left).className='menu_sep_selected';

		

		if ( document.getElementById(obj_right) )

			document.getElementById(obj_right).className='menu_sep_selected';

		*/

		if ( numVal >= 0 && mSheets[numVal] ) {
			if( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) )
			{
				mSheets[numVal].show(getLeftPos(objVal)-1, getTopPos(objVal)-2,objVal);
			}
			else
			{
				mSheets[numVal].show(getLeftPos(objVal)-1, getTopPos(objVal)-1, objVal);
			}
			menuHideTimerReset();

		}
	}
}



function hideMenu(objVal, numVal){
	if (mReady){
		menuHideTimerSet();
	}
}


function createHider()
{
	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;
}

function setupHider(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";
	}
}

function closeHider(f)
{
	if (f)
		f.style.visibility="hidden";
}
