// Menu builder builder
// Builder: from a li/ul structure
// Copyright 2009 AutumnPress Inc.  All rights reserved.

// JavaScript Document

// Menu vars
var menuLVL1= new Array();
var menuLVL2= new Array();
var menuLVL3= new Array();
//
var lastLevelOver = false;
var timerMenuOff = null;
var overAnyMenu = false;
var	whereToGo = "content.asp?";

//Accordion Vars
var lastAccord = null;
//////////////////////////
/*hack to manage cached files in Opera 7.x> */
/*opera.setOverrideHistoryNavigationMode('compatible');
history.navigationMode = 'compatible';*/
//

function cerrarMenu(target) {
	target = $(target);
	target.style.zIndex = '1999';
	if (!lastLevelOver && !target.over) {
		for (j=0; j<target.childNodes.length; j++){
			if (target.childNodes[j].nodeName=="DIV") {
				table=target.childNodes[j];
				if (FXon) {
					//if still showing, clear the interval
					if (table.ti)
						clearInterval(table.ti);
					cl2(table,true,target.id);
				} else {
					table.style.visibility='hidden';
				}
//					table.style.visibility='hidden';
				//
				target.over=false;
				/* refresh array of menus */
				var tempArray = new Array;
				var menutocheck = "menuLVL"+target.level;
				menutocheck = eval(menutocheck);
				var ind = 0;
				for (var k = 0; k<menutocheck.length; k++) {
					var elem = menutocheck[k];
					if (elem != target) {
						tempArray[ind++] = menutocheck[k];
					} else {
						/*$("tracer").innerHTML = "";
						$("tracer").innerHTML = elem.id;*/
					}
				}
				menutocheck = tempArray;
				break;
			}
		}
		target.opened=false;
	}
};

function abrirMenu(target) {
	target = $(target);
	target.style.zIndex = '2000';
	if (target.over) {
		for (h=0; h<target.childNodes.length; h++){
			if (target.childNodes[h].nodeName=="DIV") {
				var table=target.childNodes[h];
				if (FXon) {
					//if still closing, clear the interval 
					if (table.to)
						clearInterval(table.to);
					ex2(table);
				} else {
					table.style.visibility='visible';
				}
//				table.style.visibility='visible';				
				break;
			}
		}
		target.opened = true;
	}
};

/////////////////
function isIn(elem,a) {
	var resp = false;
	for (var i = 1; i<=a.length; i++ ) {
		if (elem == a[i-1]) {
			resp = true;
			break;
		}
	}
	return resp;
};

/* this function is FF 1.5 specific, just to reset menu stated on cached files*/
function reset_menu() {
	var menucontent = $('menu');
	var lis=menucontent.getElementsByTagName("li");
	lastLevelOver = false;
	for (var i=0; i<lis.length; i++){
		//alert(lis[i].opened);		
		lis[i].timer = null;
		lis[i].timerOpen = null;
		lis[i].over = false;		
		/*correct zIndex inside IE 7.0*/
		lis[i].style.zIndex = (1000-i)+'';
		if (lis[i].opened) {
			cerrarMenu(lis[i].id);
			lis[i].opened = false;
		}
	}
}
function reset_toGo_overlay() {
	whereToGo = "content.asp?";
	$("overlaySet").style.visibility='hidden';
	$("wait").style.visibility='hidden';
}
////
function init_menu() {
	reset_menu();
	reset_toGo_overlay();
	var menucontent = $('menu');
	var lis=menucontent.getElementsByTagName("li");
	for (i=0; i<lis.length; i++){
		lis[i].id =  'menu_li'+i;
		lis[i].opened = false;
		lis[i].timer = null;
		lis[i].timerOpen = null;
		lis[i].over = false;
		/*correct zIndex inside IE 7.0*/
		lis[i].style.zIndex = (1000-i)+'';
		/* set correct CSS property for DIVs inside each LI */		
		for (h=0; h<lis[i].childNodes.length; h++){
			if (lis[i].childNodes[h].nodeName=="DIV") {
				//second div
				var div= lis[i].childNodes[h].childNodes[0];
				div.visibility='hidden';
			}
		}
	}

	/*Init Menu */
	var menucontent = $('menu');
	var lis=menucontent.getElementsByTagName("li");
	var uls=menucontent.getElementsByTagName("ul");
	var divs=menucontent.getElementsByTagName("div");
	var as=menucontent.getElementsByTagName("a");
	var spans = new Array();
	/* set id for As */ 
	for (i=0; i<as.length; i++){
		as[i].id =  'menu_a'+i;
		var newLink = as[i].href+"content.asp?ref="+as[i].id;
		as[i].href = newLink;
		as[i].href = '#';
	};
	/* set id for DIVs */ 
	for (i=0; i<divs.length; i++){
		divs[i].id =  'menu_div'+i;
		divs[i].maxh = sh(divs[i]);
		if ((i % 2)+1== 1) {
		}
/*		divs[i].style.width =  (sw(divs[i])+800)+'px';		
		divs[i].style.height =  sh(divs[i])+'px';		*/
	};
	/* set id for ULs */ 
	for (i=0; i<uls.length; i++){
		uls[i].id =  'menu_ul'+i;
	};
	/* set levels for LIs */ 
	for (i=0; i<lis.length; i++){
		var tg = lis[i];
		var level = 0;
		while (tg) {
			if (tg.nodeName=="DIV") {
				level++;			
			}
			tg= tg.parentNode;
		}
		/* there are 2 DIVs for each sub menu */
		lis[i].level = level/2;
		lis[i].submenu = false;
	};
	
	

	
	
	/* set id for LIs */ 
	for (var i=0; i<lis.length; i++){
		lis[i].id =  'menu_li'+i;
		lis[i].opened = false;
		lis[i].timer = null;
		lis[i].timerOpen = null;
		lis[i].over = false;
		/*correct zIndex inside IE 7.0*/
		lis[i].style.zIndex = (1000-i)+'';
		/* set correct CSS property for DIVs inside each LI */		
		for (h=0; h<lis[i].childNodes.length; h++){
			if (lis[i].childNodes[h].nodeName=="DIV") {
				//second div
				var div= lis[i].childNodes[h].childNodes[0];
				div.visibility='hidden';
			}
		}

/*		for (h=0; h<lis[i].childNodes.length; h++){
			if (lis[i].childNodes[h].nodeName=="DIV") {
				var div= lis[i].childNodes[h];
				div.style.zIndex='1';
				if (lis[i].level >0) {
					// clearFix not working in IE
					//div.className = div.className + ' clearfix';
					var layer = document.createElement('iframe');
					layer.tabIndex = '-1';
					layer.src = 'javascript:false;';
					//div.childNodes[0].appendChild(layer);
					//var atr = document.createAttribute('allowtransparency');
					//layer.setAttribute(atr,'true');
					//var atr = document.createAttribute('frameborder');
					//layer.setAttribute(atr,'0');
					//layer.className = 'iframe2';
					layer.className = 'li.menu div';
					layer.style.overflow = 'visible';
					layer.style.left = div.parentNode.offsetLeft + 'px';
					layer.style.top = div.parentNode.offsetTop + 'px';
					layer.style.width = div.parentNode.offsetWidth + 'px';
					layer.style.height = div.parentNode.offsetHeight + 'px';

					layer.style.left = div.offsetLeft + 'px';
					layer.style.top = div.offsetTop + 'px';
					layer.style.width = div.offsetWidth + 'px';
					layer.style.height = div.offsetHeight + 'px';
					div.appendChild(layer);
					
				} else {
					//div.style.display ='block';
				}
				//div.style.overflow = 'visible';
				break;
			}
		}*/

		
		spans = lis[i].getElementsByTagName("span");
		for (j=0; j<spans.length; j++){
			spans[j].id =  'menu_span'+i+"_"+j;
		};
		
		lis[i].closeMenu = function(t) {
			var idup = "cerrarMenu('"+this.id+"');";
			this.timer=setTimeout(idup,t);
		};
		lis[i].openMenu = function(t) {
			var idup = "abrirMenu('"+this.id+"');";
			this.timerOpen=setTimeout(idup,t);	
		};
		
		
		///// Mouse click
		lis[i].onmouseup  = function (e) {
			// build the next link to show content 
			// content must be showed on leaf nodes, so accordion is consistent
			// also we need to ensure that the id is from the main menu, 
			// so acc_ IDprefix need to be deleted
			
			
			// ONLY for accordian links
			// generate link from deep level to higher li3 --> l2 --> l1
			//alert(globalLinksTypes);
			if (this.level!= 1) {
				whereToGo = whereToGo + "l"+(this.level) + "=" + this.id+"&";//+"_"+ this.level+"&";
			} else {
				whereToGo = whereToGo + "l"+(this.level) + "=" + this.id;//+"_"+ this.level;
				var now = new Date();
				var seed = now.getSeconds();
				// cache fake
				whereToGo = whereToGo + "&" +"ids=" + Math.round(Math.random(seed)*10000000);	
			}
			// index of the clicked li, always the first
			var indx = whereToGo.indexOf("li",0)+2;
			var actualLI = "";
			var number = parseInt(whereToGo.charAt(indx));
			while (!isNaN(number)) {
				actualLI = actualLI + whereToGo.charAt(indx);
				indx = indx+1;
				number = parseInt(whereToGo.charAt(indx));
			}
			actualLI = parseInt(actualLI);
			//alert(whereToGo + "    " + indx);
			if (this.level!= 1) {
				// no need to buil accord at this level 
				return
			}
			if (globalLinksTypes[actualLI]=='n') {
				// no link, here we quit the mouseup function
				reset_toGo_overlay();
				return;
			}
			if (globalLinksTypes[actualLI]=='o') {
				//outside safapassages
				whereToGo = globalLinksURL[actualLI];
			}
			if (globalLinksTypes[actualLI]=='u') {
				//outside safapassages
				whereToGo = globalLinksURL[actualLI];
			}
			//
			// else go to c or a content type
			$("overlaySet").style.visibility='visible';
			$("wait").style.visibility='visible';
			// try to hack safari on click/cache 
			timeoff = setTimeout("window.location = whereToGo;",100);
//			window.location = whereToGo;
			return;
//			if (!this.submenu) {
				// we are in a leaf node, so create the link
//			} else {
				//no leaf
//				return;
//			}			
			//***************************************			
		}

			
		///// Mouse over
		lis[i].onmouseover  = function (e) {
			e = window.event||e;
		    e.cancelBubble = true;
		    if (e.stopPropagation) e.stopPropagation(); 
			var tg = (window.event) ? e.srcElement : e.target;
			var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
			/* cancel the timeout */
			if(this.timer)
			{
				window.clearTimeout(this.timer);
				this.timer = null;
			}
			if(this.timerOpen)
			{
				window.clearTimeout(this.timerOpen);
				this.timerOpen = null;
			}
			/*trace bubble events on DOM */
			while (tg) {
				if (tg) {
					lastLevelOver = false;
					if (tg.nodeName == "LI") {
						// now show the DIV inside the LI
						overAnyMenu = true;
						for (h=0; h<tg.childNodes.length; h++){
							if (tg.childNodes[h].nodeName=="DIV") {
								this.submenu = true;
								/*insert the submenu on the stack lvl */
								if (this.level ==1) {
									if (!isIn(this,menuLVL1))
										menuLVL1.push(this);
								} else if (this.level ==2) {
									if (!isIn(this,menuLVL2))
										menuLVL2.push(this);
								} else if (this.level ==3) {
									if (!isIn(this,menuLVL3))
										menuLVL3.push(this);
								}	

								if(!this.opened)
									this.openMenu(200);
								this.over = true;
								/* set the width to let IE interpret the DIV overflow*/
								//tg.childNodes[h].firstChild.style.height='350px';
								//tg.childNodes[h].style.width='800px';
								//alert(tg.childNodes[h].firstChild.currentStyle.hasLayout)
								//abrirMenu(this.id);

								
								
								/* close same level */
								for (kk=this.level; kk<=3; kk++) {
									menutocheck = "menuLVL"+kk;
									menutocheck = eval(menutocheck);
									if (kk>this.level) {
										//while (menutocheck.length>0) {
										for (var k=menutocheck.length; k>0; k--) {
											//cerrarMenu(menutocheck.shift().id);
											//menutocheck.shift().closeMenu(500);
											//menutocheck[0].closeMenu(500);										
											menutocheck[k-1].closeMenu(200);									
										}
									} else {
										//for (var k=menutocheck.length; k>1; k--) {
										for (var k=menutocheck.length; k>0; k--) {
											//cerrarMenu(menutocheck.shift().id);
											//menutocheck.shift().closeMenu(50);
											if (menutocheck[k-1] != this) 
												menutocheck[k-1].closeMenu(200);
										}
									}
								}
								break;
							}
						}						
						/* is LI alone with no sub menu*/
						if (this.level == 3) {
							lastLevelOver = true;
						} else {
							lastLevelOver = false;
						}
						if (!this.submenu) {
//							if (this.level > 1) {
								menutocheck = "menuLVL"+(this.level);
								menutocheck = eval(menutocheck);
								if (menutocheck.length>0) {
									/*close at same level any submenu active */
									for (var k=menutocheck.length; k>0; k--) {
										if (menutocheck[k-1] != this) 
											menutocheck[k-1].closeMenu(500);
									}
								}
//							}
						}
						break;
					}
				} else {
					break;
				}
				tg= tg.parentNode;				
			}
		};
		
		///// Mouse out
		lis[i].onmouseout  = function (e) {
			e = window.event||e;
		    e.cancelBubble = true;
		    if (e.stopPropagation) e.stopPropagation(); 
			var tg = (window.event) ? e.srcElement : e.target;
			var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
			//is out of this window?
			if (!reltg) {
					this.over = false;
					overAnyMenu = false;
					if(timerMenuOff)
					{
						window.clearTimeout(timerMenuOff);
						timerMenuOff = null;
					}
					var idup = "cerrarMenuAll('"+0+"');";
					timerMenuOff=setTimeout(idup,500);
					return;
			}
			/////
			if (reltg) {
				this.over = false;
				overAnyMenu = false;
				/* is the reltg part of the menu OR the overflow of the menu? */					
				if (reltg.id.substring(0,4) != "menu" || (reltg.className == "menu" && reltg.nodeName == "DIV")) {
					// here we close the whole menu, but we do it using timeOut to see if user re-enterd the mouse again */
					if(timerMenuOff)
					{
						window.clearTimeout(timerMenuOff);
						timerMenuOff = null;
					}
					var idup = "cerrarMenuAll('"+0+"');";
					timerMenuOff=setTimeout(idup,500);
				} else {
					// Still inside Menu
/*					$("tracer").innerHTML = "";
					$("tracer").innerHTML = $("tracer").innerHTML + " Is over " + reltg.id + " ID ---> " + this.id;*/
				}
			} else {
				// bad TAG				
			}
		}; ///// End Mouse Out
	};
};

function cerrarMenuAll(m) {
	if (!overAnyMenu) {
		lastLevelOver = false;						
		for (kk=1; kk<=3; kk++) {
			menutocheck = "menuLVL"+kk;
			menutocheck = eval(menutocheck);
			while (menutocheck.length>0) {
				var cerrar = menutocheck.shift();
				if(cerrar.timer) {
					window.clearTimeout(cerrar.timer);
					cerrar.timer = null;
				}
				cerrarMenu(cerrar.id);
				//cerrar.closeMenu(500);
			}
		}
	}
};

// Specific for adding links on outer elements (link that belongs to the menu)
// Any other links, you can use <a> tags
//liId could be:
//  --a number representing the LI ID
//  --the name of the file name
//  --the index in the array of links defined in links-colors.asp
var goLink;

function goToLink(liID) {
	var number = parseInt(liID);
	var i = 0;
	var linkID;
	if (isNaN(number)) {	
		for (i=0; i<globalLinksURL.length; i++) {
			if (globalLinksURL[i].toLowerCase() == liID.toLowerCase()) {
				linkID = $("menu_li"+i);
				liID = i;
				break;
			}
		}
	} else {
		linkID = $("menu_li"+liID);
	}
	//id to find
	var currLvl = linkID.level;
	if (currLvl == 1)
		var followQstring = "content.asp?l"+currLvl+"=menu_li"+liID;
	else
		var followQstring = "content.asp?l"+currLvl+"=menu_li"+liID+"&";
	var parnode = linkID.parentNode;
	while (parnode) { //(currLvl > 1) {
		if (parnode.nodeName == "LI") {
			var newLvl = parnode.level;
			if (newLvl != currLvl) {
				//level up in menu
				if (newLvl == 1) {
					followQstring = followQstring + "l"+newLvl+"="+parnode.id;
				} else {
					followQstring = followQstring + "l"+newLvl+"="+parnode.id+"&";
				}
				currLvl = newLvl;		
			}
		}
		parnode = parnode.parentNode;
	}
	var whereToGo = followQstring;
	var now = new Date();
	var seed = now.getSeconds();
	// cache fake
	whereToGo = whereToGo + "&" +"ids=" + Math.round(Math.random(seed)*10000000);	
	//
	if (globalLinksTypes[liID]=='n') {
		// no link, here we quit the mouseup function
		//reset_toGo_overlay();
		return;
	}
	if (globalLinksTypes[liID]=='o') {
		//outside safapassages
		whereToGo = globalLinksURL[liID];
	}
	if (globalLinksTypes[liID]=='u') {
		//outside safapassages
		whereToGo = globalLinksURL[liID];
	}
	$("overlaySet").style.visibility='visible';
	$("wait").style.visibility='visible';
	goLink = whereToGo;
	var timeoff = setTimeout("window.location = goLink;",200);
//	alert(followQstring);
	return;
}
