/********************************************************************************
 *	Copyright (C) 2003 Magna Entertainment Corp.
 *
 *	FileName:		CorporateScripts_DhtmlMenus.js
 *	Author:			Dylan Pope
 *	Incept:			13 October 2003
 *	Description:	DHTML Menu ECMAScript functions for MEC CMS.
 *	-----------------------------------------------------------------------------
 *	Edit History:
 *	ID		Date		Author		Description
 *	=============================================================================
 *	(a)		13 OCT 03	DJP			This file was derived from the CMS solution
 *									provided to MEC by Navantis Inc.
 ********************************************************************************/

function menuItem(){
	//..this.text="                " + arguments[0];
	this.text=arguments[0];
	this.url=arguments[1]
	this.group=(arguments[2]) ? (arguments[2]) : false
}

function BrowseObject(NAME){
	if (document.layers){
		//alert("NS4");
		return	document.layers(NAME);
	}else if (document.all && !document.getElementById){
		//alert("IE4")
		return document.all(NAME);
	}else if (document.all && document.getElementById) {
		//alert("IE5+");
		return document.all(NAME);
	}else if (!document.all && document.getElementById) {
		//alert("NS6")
		return document.getElementById(NAME);
	}
}

function newLocation(){
	URLs=this.getAttribute("urls")
	document.location.href=URLs;       
 }
 
function myObj(value){
	this.Value = value
}

function divout(){
	
	whichArray = this.getAttribute("openArray");
	waiter = this.getAttribute("openArraywait");

	this.className="menuItemOff";
	str_exec="deleteF(1,"+whichArray+","+eval(waiter).name+ ")";
	eval(waiter).value=setInterval(str_exec, 1000);
}

function showdiv(){

	objMENU = this.getAttribute("openArray");
	waitWhat = this.getAttribute("openArraywait");

	if (eval(waitWhat).value!=null){
		clearInterval(eval(waitWhat).value);
	}
	this.className="menuItemOn";
	objGroup=this.parentNode.id;
	prob=-1;

	for (c=0; c<eval(objMENU).length; c++){
		if (eval(objMENU)[c]==objGroup){
			prob=c;
			break
		}
	}
	var v=c+1;

	deleteF(v, objMENU, eval(waitWhat).name);
		
	if (prob==-1){
		eval(objMENU)[eval(objMENU).length] = objGroup;
	}
	
	if (this.getAttribute("group")){
		subGroup1=this.id+"_group";
		BrowseObject(subGroup1).className="show";
		eval(objMENU)[eval(objMENU).length] = subGroup1;
	}
	
}

function deleteF(vi, myArray,waitName){

	if (vi==null){
		vi=1;
	}
	gg=vi;

	for (vi; vi < eval(myArray).length; vi++){
		if (eval(myArray)[vi] != null){
			BrowseObject(eval(myArray)[vi]).className="hide";
			delete eval(myArray)[vi];
		}
	}

	if (eval(waitName).value != null){
		clearInterval(eval(waitName).value);
	}
	//BrowseObject("indicator").innerText=eval(myArray).toString();
}

//inicialization
k=0;

function MENU(LEVEL, TOP, LEFT, ItemWidth, ItemHeight ,ItemSpace, IDs, orientation, strLength){
	var mybody=BrowseObject("main");
	var MENUdiv = document.createElement("div");
	LEVEL0=eval(LEVEL);
	var TOPnew = TOP;
	var TOPlocal = 0;
	var LEFTlocal =0;
	var stHeight =0;

if (orientation== "horizontal"){
	var linesM=1;
	for (var p=0; p<LEVEL0.length; p++){
		var lines=Math.ceil(LEVEL0[p].text.length/strLength);
		if (lines > linesM){
			linesM=lines;
		} 
	}
	//alert(linesM)
	var STEP = ItemHeight * 0.9 * linesM;
	var TOPnew=TOPnew*1+STEP;
}



//------------------------------------------------------------------------------------------
	for (var j=0; j<LEVEL0.length; j++) {
		k++;

		//var STEP = 35;


			if (orientation== "vertical"){
				if (LEVEL0[j].text.length>strLength){
					var STEP = 1.55 * ItemHeight;
				}else{
					var STEP = ItemHeight;
				}
			}

		
		
		var myITEM = document.createElement("span");

		myITEM.setAttribute('id', veryTopLevel + '_'+k + '_' + j, 0);
		myITEM.className="menuItemOff";
		
		
		
		
		//begin style / position
		var stylePosition="width:"+ItemWidth+"px; height:"+STEP +"px;padding:2px 2px 2px 2px;"
		if (!document.all && document.getElementById){
			var hh=STEP - (2*ItemSpace);
			var ww=ItemWidth - (2*ItemSpace)-2;
			var stylePosition="width:"+ww +"px; height:" + hh + "px;padding:1px 0px 0px 2px;text-indent: 0px;"
		}
		

		if (LEVEL0[j].url){
			myITEM.setAttribute("urls", LEVEL0[j].url);
			myITEM.onclick  = newLocation;
		}

		switch (orientation){
			case "vertical" :
				var LEFTnew=LEFT+ItemWidth-ItemSpace;
				stylePosition += "top:" + TOPlocal + "px;";
				TOPlocal += STEP - ItemSpace;				
				break;
			case "horizontal": 
				stylePosition += " left:" + LEFTlocal + "px;";
				var LEFTnew=LEFTlocal+LEFT;
				LEFTlocal += ItemWidth-ItemSpace;				
				break;
		}
		
		myITEM.setAttribute("style", stylePosition);/* for NN*/
		myITEM.style.cssText=stylePosition; /*for MSIE*/
		//end Style & position
		
		myITEM.setAttribute("openArray", "open" + veryTopLevel);
		myITEM.setAttribute("openArraywait", "open"+veryTopLevel+"wait");
		myITEM.onmouseover=showdiv;
		myITEM.onmouseout=divout;
		
		if (LEVEL0[j].group){		
			var IDnew=veryTopLevel + '_'+k + '_' + j +"_group";			
			myITEM.setAttribute("group","yes");
			MENU(LEVEL0[j].group, TOPnew, LEFTnew, ItemWidth+20, ItemHeight ,ItemSpace, IDnew, 'vertical', strLength);
		}
	
//alert(LEVEL0[j].text);
		LEVEL0=eval(LEVEL);	
		//var myITEMtext=document.createTextNode(LEVEL0[j].text.length + "|" +LEVEL0[j].text);
		var myITEMtext=document.createTextNode(LEVEL0[j].text);
		myITEM.appendChild(myITEMtext);
		
		MENUdiv.appendChild(myITEM);
		switch (orientation){
			case "vertical":
			TOPnew=TOPnew+ STEP - ItemSpace;
			stHeight+=STEP - ItemSpace;
		}
	}
//------------------------------------------------------------------------------------------	
	MENUdiv.className="hide";

	var stWidth = ItemWidth  + ItemSpace
	menuDivStyle="left:"+LEFT+"px;" + "top:"+TOP + "px;" + "height:" + stHeight;
	MENUdiv.setAttribute("style", menuDivStyle); /* for NN*/
	MENUdiv.style.cssText=menuDivStyle;  /*for MSIE*/


	MENUdiv.setAttribute("TopNormal", TOP,0);
	MENUdiv.setAttribute("LeftNormal", LEFT,0);
	MENUdiv.setAttribute("divHEIGHT", stHeight,0);
	MENUdiv.setAttribute("divWIDTH", stWidth,0);


	MENUdiv.setAttribute("id",IDs,0);

	mybody.appendChild(MENUdiv);
}

//TODO: fix divID
function buildMENU(LEVEL, TOP, LEFT, ItemWidth, ItemHeight ,ItemSpace, IDs, orientation, divID, strLength){
	waiterStr="open" + IDs + "wait";
	eval(waiterStr +" = {}");
	eval(waiterStr).Value=null;
	eval(waiterStr).name= waiterStr;
	opennedArray="open" + IDs;
	eval(opennedArray +"= new Array()");
	veryTopLevel = IDs;
	MENU(LEVEL, TOP, LEFT, ItemWidth, ItemHeight ,ItemSpace, IDs, orientation,strLength/*, divID*/);
	BrowseObject(IDs).className="show";
}
