// BROWSERCHECK
var isMoz2 = false;
var isIE2 = false;
userAgent = navigator.userAgent.toLowerCase();
if(userAgent.indexOf("gecko")!=-1) isMoz2 = true;
else isIE2 = true;

var divWidth = 174;
var divHeight = 20;
var divContainerFix = 2;
var menuPosTop = 2;
var menuPosLeft = 5;
if(isMoz2){
	divWidth = 167;
	divHeight = 17;
	divContainerFix =3;
	menuPosTop = 2;
	menuPosLeft = 2;	
}
var menuCloseTime = 1000;


// MENU CLASS
function Menu(oMenu){
	this.id = oMenu;
   
    this.itemCounter = 0;   
   
    this.arrItemId = new Array();
    this.arrItemText = new Array();   
	this.arrItemUrl = new Array();
    this.arrItemParent = new Array();
	
	this.addItem = addItem;
	this.writeMenu = writeMenu;
	this.openMenu = openMenu;
	this.closeMenu = closeMenu;
	this.subClose = subClose;
	this.changeBG = changeBG;
	this.goUrl = goUrl;
	this.writeArrows = writeArrows;
}

function addItem(id, text, url, parent){
    this.itemCounter++;
    this.arrItemId[this.itemCounter] = id;
    this.arrItemText[this.itemCounter] = text;
	this.arrItemUrl[this.itemCounter] = url;
    this.arrItemParent[this.itemCounter] = parent;
}

function show2(id){
	document.getElementById(id).style.display = "inline";
}

function hide2(id){
	document.getElementById(id).style.display = "none";
}

function changeBG(id,alt){
	if(alt==1){
		if(id!=0) document.getElementById(this.id+"_"+id).className = "right-bottom2";
	}
	else{
		if(id!=0) document.getElementById(this.id+"_"+id).className = "right-bottom";
	}
}

function goUrl(url){
	check=false;
	doCoseMenu();
	if(url!="") {
		if (!(url.toLowerCase().indexOf('http://')))
			window.open(url, "Window");
		else
			location.href = url;
	}
}

var timerID;
function startTimer(){
	timerID=setTimeout("doCoseMenu()", menuCloseTime);
}

function clearTimer(){
	clearTimeout(timerID);
}

function doCoseMenu(){
	oMenu1.closeMenu(0);
	//oMenu2.closeMenu(0);
	
	// Fix to hide selectboxes	
	if(!check){
		if(document.getElementById('formDisplay1')) show2("formDisplay1");
		if(document.getElementById('formDisplay2')) show2("formDisplay2");			
	}
}

var check = false;
function openMenu(id){
	check = true;
	this.closeMenu(id);
	this.changeBG(id,1);
		
	// Fix to hide selectboxes	
	if(document.getElementById('formDisplay1')) hide2("formDisplay1");
	if(document.getElementById('formDisplay2')) hide2("formDisplay2");

	haveSubs = false;
	
	for(var item in this.arrItemId){
		if(!haveSubs){
			if(this.arrItemParent[item] == id) haveSubs = true;
			else haveSubs = false;
			if(id==0) haveSubs = true;
		}
		else{
			break;
		}
	}
	
	if(haveSubs){
		if(id!=0){
		
			if(isIE2){		
				tmpWidth = parseInt(document.getElementById(this.id+"_"+id).style.posWidth);
				tmpTop = parseInt(document.getElementById(this.id+"_"+id).style.posTop);
			}
			else{
				tmpWidth = parseInt(document.getElementById(this.id+"_"+id).style.width);
				tmpTop = parseInt(document.getElementById(this.id+"_"+id).style.top);			
			}
		
			for(var item in this.arrItemId){
				if(this.arrItemId[item] == id){
					tmpParentId = this.arrItemParent[item];
				//	if(tmpParentId!=0){
						
					if(isIE2){
						tmpLeft = parseInt(document.getElementById(this.id+"container_"+tmpParentId).style.posLeft);
						tmpTop += parseInt(document.getElementById(this.id+"container_"+tmpParentId).style.posTop);
					}
					else{
						tmpLeft = parseInt(document.getElementById(this.id+"container_"+tmpParentId).style.left);
						tmpTop += parseInt(document.getElementById(this.id+"container_"+tmpParentId).style.top);					
					}
					
				//	}
				}
			}
			document.getElementById(this.id+"container_"+id).style.top = parseInt(tmpTop)+menuPosTop;
			document.getElementById(this.id+"container_"+id).style.left = parseInt(tmpWidth)+parseInt(tmpLeft)-menuPosLeft;
		}
		show2(this.id+"container_"+id);	
	}
}


function closeMenu(id){
	if(id == 0){	
		if(!check){
			for(var item in this.arrItemId){
				hide2(this.id+"container_"+this.arrItemParent[item]);
				this.changeBG(this.arrItemId[item],0);
			}
		}
	}
	else{
		// Kör igenom alla
		for(var item in this.arrItemId){
			// Letar posten som har samma ID som Id som skickas in
			if(this.arrItemId[item] == id){
				// Sparar det aktuella id's ParentID
				var tmpParentId = this.arrItemParent[item];
				// kör igenom alla igen
				for(var item2 in this.arrItemId){
					// letar alla poster som har CurrentID som parentID
					if(tmpParentId == this.arrItemParent[item2]){
						// om ej current ID
						if(this.arrItemId[item2]!=id){
							// Sparar det aktuella ID på menyalternativ på samma nivå
							var tmpCloseId = this.arrItemId[item2];
							this.changeBG(tmpCloseId,0);
							this.subClose(tmpCloseId);
						}
					}
				}
			}
		}
	}
}

function subClose(id) {
	// kor igenom alla igen
	for(var item3 in this.arrItemId){
		// Om closeid finns med i nagon annan post så anropas funktionen igen.
		if(this.arrItemParent[item3] == id){
			hide2(this.id+"container_"+id);
			this.subClose(this.arrItemId[item3])	
				this.changeBG(this.arrItemId[item3],0);								
		}
	}
}

function writeMenu(){
	var arrContainers = new Array();
	var strOut = "";
	var strShow = "";

	for(var item in this.arrItemId){
		if(this.arrItemParent[item] == 0) strShow = "none";//"inline";
		else strShow = "none";

		if(arrContainers[this.arrItemParent[item]]!="" && arrContainers[this.arrItemParent[item]]!= null){
			arrContainers[this.arrItemParent[item]] += "    <div style=\"position:absolute;top:px;left:0px;width:"+divWidth+"px;height:"+divHeight+"px\" class=\"right-bottom\" onmouseover=\""+this.id+".openMenu("+this.arrItemId[item]+");clearTimer();\" onclick=\""+this.id+".goUrl('"+this.arrItemUrl[item]+"')\" id=\""+this.id+"_"+this.arrItemId[item]+"\"><span style=\"float:left\">"+this.arrItemText[item]+"</span><span style=\"float:right\">["+this.arrItemId[item]+"]</span></div>\n"
		}else{
			arrContainers[this.arrItemParent[item]] = "<div onmouseout=\"openCheck();\" class=\"left-top\" id=\""+this.id+"container_"+this.arrItemParent[item]+"\" style=\"left:0px;top:0px;height:px;width:176px;position:absolute; display:"+strShow+";\">\n"
			arrContainers[this.arrItemParent[item]] += "    <div style=\"position:absolute;top:px;left:0px;width:"+divWidth+"px;height:"+divHeight+"px\" class=\"right-bottom\" onmouseover=\""+this.id+".openMenu("+this.arrItemId[item]+");clearTimer();\" onclick=\""+this.id+".goUrl('"+this.arrItemUrl[item]+"')\" id=\""+this.id+"_"+this.arrItemId[item]+"\"><span style=\"float:left\">"+this.arrItemText[item]+"</span><span style=\"float:right\">["+this.arrItemId[item]+"]</span></div>\n"
		}	
	}

	for(var item in arrContainers){
		arrContainers[item] += "</div>\n"
	
		var intTopPos = 0;
		var innerDivCount = 0;
		while(arrContainers[item].indexOf('top:px')!=-1){
			var strReplace = "top:"+intTopPos.toString()+"px";
			arrContainers[item] = arrContainers[item].replace("top:px",strReplace);
			intTopPos += divHeight;		
			innerDivCount++;
		}
		arrContainers[item] = arrContainers[item].replace("height:px","height:"+((innerDivCount*divHeight)+divContainerFix)+"px");
		strOut += arrContainers[item]
	}
	document.write(this.writeArrows(strOut));
}

function writeArrows(txt){
		var strReplace = "";
		for(var item in this.arrItemId){
			strReplace = "["+this.arrItemId[item]+"]";
			for(var item2 in this.arrItemId){
				if(this.arrItemParent[item2]==this.arrItemId[item]) txt = txt.replace(strReplace,"&raquo;&nbsp;&nbsp;&nbsp;");
			}
		}
		for(var item in this.arrItemId){
			strReplace = "["+this.arrItemId[item]+"]";
			if(txt.indexOf(strReplace)!=-1)	txt = txt.replace(strReplace,"");
		}
		return txt;
}

function openCheck(){
	startTimer();
	check = false;
}
