var lp_input = null;
var currentMouseOverTarget = null;
var currentMouseOverTargetVersionNumber = 0;
var currentlyOpenedMenus = new Array();
var ajaxLocationIds = new Array();
var lastEventTarget = null;

function navMouseOver(e) {
	var evt = e || window.event;
	var evtTarget = evt.target || evt.srcElement;
		
	// set global mouseOverTarget that will be used to conditional execute timout functions	
	currentMouseOverTarget = evtTarget;
	currentMouseOverTargetVersionNumber++;	
	var oldMouseOverTarget = evtTarget;
	var relatedNode = evt.relatedTarget || evt.fromElement;
	
	//YAHOO.log("navMouseOver: e="+e+" evtTarget="+evtTarget.nodeName+" lastEventTarget="+lastEventTarget+" relatedNode="+relatedNode.nodeName);
	
	// Avoid stream of mouseover calls due to Safari text nodes 
	if (BrowserDetect.browser == 'Safari') {
		if (evtTarget == lastEventTarget && relatedNode.nodeName == '#text') {
			return;	
		} else {	
			// remember the last mouseover event so next navMouseOver or navMouseOut can reason about last event
			lastEventTarget = evtTarget;			
		}
	}
	
	// Just handle LIs
	if (evtTarget.nodeName == 'UL') return;

	while (evtTarget.nodeName != 'LI')
		evtTarget = evtTarget.parentNode;
	
	if (evtTarget.nodeName != 'LI') return;
	
	foldMenuIn(evtTarget);
	
	// Bail early if there are no kids anyway
	var a = evtTarget.getElementsByTagName('A')[0];

	var topParent = false;
	if (a.getAttribute("topParent") == 'true') {
		topParent = true;
	}
	
	var hasKids = a.getAttribute("hasKids");
	
	if(hasKids!='true' && !topParent) {
		return;
	}
	
	var ul = createUL(evtTarget);
	reinitNavigation();
	foldMenuOut(evtTarget);	
	
	// delay for a brief time in case mouse is moving rapidly to avoid expensive AJAX calls
	var timeout = 1;
	if(a.getAttribute("isParent")=='true') {
		timeout = 1;
	}
	if(topParent) {
		timeout = 0;
	}
	if(topParent) {
			showMenuDelayed(oldMouseOverTarget, evtTarget, ul, topParent);
	} else {
		evtTarget.outTimeout = setTimeout(function() { showMenuDelayed(oldMouseOverTarget, evtTarget, ul, topParent); }, timeout);
	}
}

function navMouseOut(e) {
	
	var evt = e || window.event;
	var evtTarget = evt.target || evt.srcElement;	
	var relatedNode = evt.relatedTarget || evt.toElement;
		
	// Safari browser streams events for #text nodes so trap them;
	// install onmouseover/out handlers on the text nodes as an alternative
	if (BrowserDetect.browser == 'Safari') {
		if (relatedNode == lastEventTarget && evtTarget.nodeName == '#text') {
			return;
		} else {	
			// remember the last mouseover event so next navMouseOver or navMouseOut can reason about last event
			lastEventTarget = evtTarget;			
		}
	}
	
	var version = currentMouseOverTargetVersionNumber;
	clearTimeout(evtTarget.outTimeout);
	if (relatedNode) {
		relatedNode.outTimeout = setTimeout(function() { foldMenuInDelayed(relatedNode, version, relatedNode); }, 1000);
	}
	// set currentMouseOverTarget to where the mouse is going to (this might be a non picker location)
	currentMouseOverTarget = relatedNode;
}

/**
 * evtTarget is an LI
 */
function foldMenuInDelayed(oldMouseOverTarget, version, evtTarget) {
	// Check version in case of ABA situation
	if (oldMouseOverTarget == currentMouseOverTarget && version == currentMouseOverTargetVersionNumber) {
		foldMenuIn(evtTarget);
	}
}

/**
 * evtTarget is an LI
 */
function showMenuDelayed(oldMouseOverTarget, evtTarget, ul, top) {
	if (oldMouseOverTarget == currentMouseOverTarget) {
		showMenu(evtTarget, ul, top);
	}
}

function showMenu(li, ul, top) {
	var a = li.getElementsByTagName('A')[0];
	var locationId = a.getAttribute("locId");
	var hasKids = a.getAttribute("hasKids");
	if(hasKids!='true' && !top) {
		return;
	} 
	if (locationId == -1) {
		locationId = lp_input.value;
	}
	// create lookup key from locationId int 
	var key = locationId+"";	
	var getParents = "false";
	// only show parents if this is the selected parent
	if (top) {
		getParents = "true";
		key = key+"selected";
	}
	var responseXML = ajaxLocationIds[key];
	if (responseXML) {		
		parseAjaxPickerResponseForChildren(responseXML, ul); 
	} else {
	/*
		var loading = document.createElement('li');
		YAHOO.log('loading');
		loading.innerHTML = 'loading';
		ul.appendChild(loading);
		ul.style.width = '100px';
		if(ul.firstChild) {
			ul.style.visibility == 'visible';
		}
		var div_inner = document.createElement("div");
		div_inner.style.width = '100px';
		div_inner.style.cssFloat = 'left';
		div_inner.style.styleFloat = 'left';
		ul.appendChild(div_inner);
		*/
		
		// not cached so need to ajax
		var action = "ajaxAddPicker";
		var getContextLocation = "true";
		var getChildren = "true";
		var getLeafs = lp_input.getAttribute("leaf");
		//YAHOO.log('GETLEAF:'+getLeafs);
		var url = "autocomplete?action="+action+"&locationId="+locationId+"&getContextLocation="+getContextLocation+"&getParents="+getParents+"&getChildren="+getChildren+"&getLeafs="+getLeafs;
		var req = initRequest(url);
		req.onreadystatechange = function() {
		    if (req.readyState == 4) {
		        if (req.status == 200) {
   		//            ul.removeChild(loading);
          //  		YAHOO.log('loadingdone');
		        	parseAjaxPickerResponseForChildren(req.responseXML, ul);
		            ajaxLocationIds[key] = req.responseXML;
		        } else if (req.status == 204){
		        	
		        }
		    }
		};
		req.open("GET", url, true);
		req.send(null);
	}
}

function isSelectedDiv(node) {
	while (node) {
		try {
			if (node.id == 'selected' || node.id == 'selected2') {
				return true;
			}
			if (node.id == 'unselected') {
				return false;
			}
		} catch (e) {
			return false;
		}
		node = node.parentNode;
	}
	return false;
}

function foldMenuIn(targetNode) {
		if (!targetNode) return;
		var newCurrentlyOpenedMenus = new Array();
		for (var i=0;i<currentlyOpenedMenus.length;i++) {
			if (!containsElement(currentlyOpenedMenus[i],targetNode)) {
				currentlyOpenedMenus[i].className = '';
				currentlyOpenedMenus[i].opened = false;
				if (currentlyOpenedMenus[i] &&  currentlyOpenedMenus[i].parentNode && currentlyOpenedMenus[i].parentNode.className) {
					currentlyOpenedMenus[i].parentNode.className = '';
				}
			} else 
				newCurrentlyOpenedMenus.push(currentlyOpenedMenus[i]);
		}
		currentlyOpenedMenus = newCurrentlyOpenedMenus;
		
}

function foldMenuOut(evtTarget) {
	//new ExclusiveAction( function() {
		if (evtTarget.relatedItem && !evtTarget.relatedItem.opened) {
			evtTarget.className = 'highlight';
			evtTarget.relatedItem.className = 'foldOut';
			evtTarget.relatedItem.opened = true;
			//YAHOO.log("pushing relatedItem="+evtTarget.relatedItem);
			currentlyOpenedMenus.push(evtTarget.relatedItem);
		}
	//} );
}

/*
function foldMenusIn() {
	var lists = document.getElementsByTagName('ul');
	for (var i=0;i<lists.length;i++) {
		if (lists[i].className != 'menutree') {
			continue;
		}
		//YAHOO.log("foldMenusIn menutree="+lists[i]);
		var uls = lists[i].getElementsByTagName('ul');
		for (var j=0;j<uls.length;j++) {
			//YAHOO.log("foldMenusIn uls["+j+"]="+uls[j]+" className="+uls[j].className+" innerHTML="+uls[j].innerHTML);
			if (uls[j].className != 'menutree') {
				uls[j].className = '';
				//YAHOO.log("foldMenusIn uls["+j+"]="+uls[j]+" className="+uls[j].className);
			}
			uls[j].opened = false;
		}
	}
}
*/

/**
 * obj2's superchain is iterated to see if obj1 matches ancestors
 */
function containsElement(obj1,obj2) {
	try {
		while (obj2.nodeName != 'HTML') {
			if (obj2 == obj1) return true;
			obj2 = obj2.parentNode;
		}
		return false;
	} catch (e) {
		return false;
	}
}

function createUL(li) {
	var ul = document.createElement("ul");	
	var ulOld = li.getElementsByTagName('UL')[0];
	if (ulOld) {
  		li.replaceChild(ul, ulOld);
	} else {
		li.appendChild(ul);
	}
	ul.style.visibility = 'hidden';
	//ul.style.borderWidth = '0';
	return ul;
}

/**
 * Parses childSet from responseXML.
 * 
 * <locationSet>
 * 		<parentSet>
 * 			<location>
 * 				<title></title>
 * 				<id></id>
 * 			</location>
 * 		</parentSet>
 * 		<childSet>
 * 			<location>
 * 				<title></title>
 * 				<id></id>
 * 			</location>
 * 		</childSet>
 * </locationSet>
 * 				
 */
function parseAjaxPickerResponseForChildren(responseXML, ul) {
  	//new ExclusiveAction( function() {
//  	  YAHOO.log("************* parseAjaxPickerResponseForChildren *********");
  //	  YAHOO.log("responseXML="+responseXML);
  	  if (responseXML && responseXML.getElementsByTagName("childSet")) {
/*	  	  var parentTipCard = responseXML.getElementsByTagName("parentTipCard")[0].childNodes[0];
    	  var cardLi = document.createElement("li");
    	  cardLi.appendChild(parentTipCard);
	  	  ul.appendChild(cardLi);
	  	  */
		  var childSet = responseXML.getElementsByTagName("childSet")[0];
		  // set number of ul columns based on how many children are present
		  var oneColPx = 120;
		  var rowPerCol = 10;
		  var maxCols = 6;
	  	  var parentSet = responseXML.getElementsByTagName("parentSet")[0];
	  	  var nParents = 0;
	  	  if (parentSet && parentSet.childNodes) {
	  	  	nParents = parentSet.childNodes.length;
	  	  }
		  var numCols = parseInt((childSet.childNodes.length + nParents) / rowPerCol);
		  /*
		  var actual = numCols*rowPerCol;
	      var remainder = childSet.childNodes.length-actual;
	      if(remainder>0) {
	    	numCols = numCols + 1;	
	      }*/
		  var smaller = false;
		  if (numCols > maxCols) {
		  	rowPerCol = rowPerCol*2;
		    numCols = parseInt((childSet.childNodes.length + nParents) / rowPerCol);		    
		  	smaller = true;
		  }
		  if (numCols < 1) {
		  	numCols = 1;
		  } else {
		  	numCols++;
		  }
		  ul.style.width = ((numCols*oneColPx)+3)+'px';
		  if (childSet.childNodes.length > 0) {
		  	var child = 0;		  	
			for (var c = 0; c < numCols; c++ ) {
				var div_inner = document.createElement("div");
				div_inner.style.width = oneColPx+'px';
				div_inner.style.cssFloat = 'left';
				div_inner.style.styleFloat = 'left';
				ul.appendChild(div_inner);
				var limit = (c+1)*rowPerCol;
				if (limit > childSet.childNodes.length) {
					limit = childSet.childNodes.length;
				}
				for (var i = c*rowPerCol; i < limit; i++) {
				//for (var i = 0; i < childSet.childNodes.length; i++) {
					var location = childSet.childNodes[i];
					var title = location.getElementsByTagName("title")[0];
					var locId = location.getElementsByTagName("id")[0];
		   			var hasKids = location.getElementsByTagName("hasKids")[0];
		    		//var tipCard = location.getElementsByTagName("tipCard")[0].childNodes[0];	   			    
		    		if(title.childNodes[0]!=null) {
						var li_inner = createChildLocationLi(title.childNodes[0].nodeValue, locId.childNodes[0].nodeValue,hasKids.childNodes[0].nodeValue);
						//ul.appendChild(li_inner);
						li_inner.style.width = oneColPx+'px';					
						div_inner.appendChild(li_inner);  		
					}
				}		
				/*
				if (div_inner.firstChild) {
			  		div_inner.style.visibility = 'visible';
			  	}	 
			  	*/ 
			}
		  }

	}
	//}); 

	  // add parents
	  if (parentSet && parentSet.childNodes && parentSet.childNodes.length > 0) {
			for (var c = 0; c < parentSet.childNodes.length; c++ ) {
				var div_inner = document.createElement("div");
				div_inner.style.width = oneColPx+'px';
				div_inner.style.cssFloat = 'left';
				div_inner.style.styleFloat = 'left';
				ul.appendChild(div_inner);
				var location = parentSet.childNodes[c];
				var title = location.getElementsByTagName("title")[0];
				var locId = location.getElementsByTagName("id")[0];
	   			var hasKids = location.getElementsByTagName("hasKids")[0];
	    		if(title.childNodes[0]!=null) {
					var li_inner = createChildLocationLi(title.childNodes[0].nodeValue, locId.childNodes[0].nodeValue,hasKids.childNodes[0].nodeValue);
					li_inner.style.width = oneColPx+'px';					
					div_inner.appendChild(li_inner);  		
				}
			}	  
	  }
		  // YAHOO.log("width of cols for ul="+ul.style.width+" numcols="+numCols+" childNodes="+childSet.childNodes.length);
		  // ony make visible if there's a child in UL or else a small dot appears
		  if (ul.firstChild) {
		  	ul.style.visibility = 'visible';
		  }
		  
		  if (smaller) {
		  	for (var j = 0; j < ul.childNodes.length; j++) {
		  		var child = ul.childNodes[j];
		  		child.style.fontSize = 'smaller';
		  	}		  	
		  }
	
	  aTags = ul.getElementsByTagName('a');
	  for(e in aTags) {
 		  sweetTitles.checkAndAdd(aTags[e]);
	  }

}

/**
 * Parses parentSet from responseXML.
 * 
 * <locationSet>
 * 		<parentSet>
 * 			<location>
 * 				<title></title>
 * 				<id></id>
 * 			</location>
 * 		</parentSet>
 * 		<childSet>
 * 			<location>
 * 				<title></title>
 * 				<id></id>
 * 			</location>
 * 		</childSet>
 * </locationSet>
 * 				
 */
function parseAjaxPickerResponseForParents(locId, locIdStr, parentHasKids,responseXML, div) {
  	//new ExclusiveAction( function() {
  	 
	  var parentSet = responseXML.getElementsByTagName("parentSet")[0];

	  		

	  	var parentTipCard = null;
	  if (parentSet && parentSet.childNodes && parentSet.childNodes.length > 0) {
	  	
	  		
	  		// Address book 
  		  	var location = parentSet.childNodes[0];
		    var title = location.getElementsByTagName("title")[0];
		    var lid = location.getElementsByTagName("id")[0];
		    var hasKids = location.getElementsByTagName("hasKids")[0];
		    var tipCard = null; //location.getElementsByTagName("tipCard")[0];
		    if(tipCard!=null) {
		    	tipCard = tipCard.childNodes[0];
		    }
		    //YAHOO.log("tipCard1:"+tipCard);
		    /*
			var div_inner = createParentLocationDiv(false, title.childNodes[0].nodeValue, lid.childNodes[0].nodeValue, hasKids.childNodes[0].nodeValue,tipCard);
			div_inner.id = 'unselected';
			div_inner.style.padding = '0px 5px 5px 0px';
			div.appendChild(div_inner);	
			
			*/
						
			/*
			// then the rest
			for (var i = parentSet.childNodes.length-1; i > 0; i--) {
			  var location = parentSet.childNodes[i];
			  var title = location.getElementsByTagName("title")[0];
			  var lid = location.getElementsByTagName("id")[0];
			  var hasKids = location.getElementsByTagName("hasKids")[0];
			  var tipCard = location.getElementsByTagName("tipCard")[0];
			  if(tipCard!=null) {
			  	tipCard = tipCard.childNodes[0];
			  }
			  //YAHOO.log("tipCard1:"+tipCard);
			  var div_inner = createParentLocationDiv(title.childNodes[0].nodeValue, lid.childNodes[0].nodeValue, hasKids.childNodes[0].nodeValue,tipCard);
			  div_inner.id = 'unselected';
			  div.appendChild(div_inner);		  		
			}
			*/
			parTipCardElem = null; //responseXML.getElementsByTagName("parentTipCard")[0];
			if (parTipCardElem) {
				parentTipCard = parTipCardElem.childNodes[0];
			}
	  }
	  
	  // add target parent location	  
		
			
			var targetDiv = createParentLocationDiv(true, locIdStr, locId,parentHasKids,parentTipCard);
			targetDiv.id = 'selected2';
			div.appendChild(targetDiv);	
			
			
	  reinitNavigation();  
	  aTags = div.getElementsByTagName('a');
	  for(e in aTags) {
 		  sweetTitles.checkAndAdd(aTags[e]);
	  }

	//}); 
}




function createChildLocationLi(title, locId,hasKids,tipCard) {
	var newLi = document.createElement("li");
	var linkElement = document.createElement("a");
	if(hasKids=='true') {
		linkElement.className = "hasKids";
	} else {
		linkElement.className = "noKids";
	}
//	linkElement.className = "noKids";
	linkElement.setAttribute("locId", locId);
	linkElement.setAttribute("hasKids",hasKids);
   	//linkElement.setAttribute("href", "");
   	linkElement.setAttribute("href", "#");
    linkElement.innerHTML = linkElement.innerHTML+title;
    //linkElement.tipCard = tipCard;
    /*
    if(tipCard!=null) {
	    var tipElement = document.createElement("div");
	    var tipul = document.createElement("ul");
	    var tipli = document.createElement("li");
	    tipul.style.display = 'block';
	    tipli.style.display = 'block';
	    tipul.appendChild(tipli);
	    tipli.appendChild(tipElement);
	    newLi.appendChild(tipul);
	    tipElement.style.display = 'block';
	    tipElement.appendChild(tipCard);
	    tipElement.id = locId+'tip';
	    newLi.appendChild(tipul);
    } else {
	    var	lk = document.getElementById(locId+'tip');
    }
    */
//    linkElement.setAttribute('tipDivId',locId+'tip');
    
    
	newLi.appendChild(linkElement);
	//linkElement.onmousedown = navMouseDown;  
	return newLi;
}

/**
 * Creates a DIV element for parent location.
 * 
 * DOM structure:
 * 
 * <div id=\""+id+"\" class=\"nav1\">");
 * <input type=\"hidden\" id=\"lp_input\" value=\"" + location.getEntityId() + "\"/>
 * <ul class=\"menutree\">
 * <li id=\"" + newPrefix + "\" class=\"first\" locId=\""+locId+"\">
 * <a href=\""+location.getEntityId() + "\" locId=\""+location.getEntityId()+"\">" + location.getTitle()/* + " <img class=\"middleAlign\" src=\"images/star.gif\" border=\"0\">"+location.getApplauseCount()</a>
 * </ul>
 * </div>
 * 
 */
function createParentLocationDiv(topParent, title, locId, hasKids,tipCard) {
	var div = document.createElement("div");
	div.className = "nav1";
	var ul = document.createElement("ul");
	ul.className = "menutree";
	var li = document.createElement("li");
	var linkElement = document.createElement("a");
	
	//YAHOO.log("parentVille:"+hasKids);
	linkElement.setAttribute("hasKids",hasKids);
	
//	linkElement.className = "hasKids";
	linkElement.setAttribute("isParent","true");
	linkElement.setAttribute("locId", locId);
	linkElement.setAttribute("topParent", topParent);		
	
						
	if(locId>0) {
	   	linkElement.setAttribute("href", "#");
	} else {
		linkElement.setAttribute("href", "#");
	}
   	
   	
   	/*
   	var titleGif = "<img class=\"middleAlign\" src=\"media?action=generateText&size=24&text=\""+title+"/>";
   	linkElement = titleGif;
   	*/
   	
   	if(locId<0) {
//	   	linkElement.innerHTML = "<img class=\"middleAlign\" src=\"images/home24.png\" border=\"0\">";
	} else {
		if (topParent) {
			linkElement.innerHTML = "<img class=\"middleAlign\" src=\"images/down_arrow.gif\" border=\"0\">";
		} else {
    		linkElement.innerHTML = linkElement.innerHTML+title;
		}
    }
    /*
    var arrowImg = document.createElement('IMG');
	arrowImg.setAttribute('src', 'images/down_arrow.gif');
	arrowImg.setAttribute('border', '0');
	arrowImg.className = 'middleAlign';
	linkElement.appendChild(arrowImg);	
    */

	li.appendChild(linkElement);
	    
	/*
    if(tipCard!=null) {
	    var tipElement = document.createElement("div");
	    tipElement.style.display = 'none';
	    tipElement.id = locId+'tip';
	    li.appendChild(tipElement);
        linkElement.setAttribute('tipDivId',locId+'tip');
    } else {
	    var	lk = document.getElementById(locId+'tip');
    }
	*/
    
	ul.appendChild(li);
	div.appendChild(ul);
	return div;
}

function showParentsMenu(locId, locIdStr, hasKids, div) {

	// create lookup key from locationId int 
	var key = locId+"Parents";	
	var responseXML = ajaxLocationIds[key];
	if (responseXML) {
//		YAHOO.log("************ showParentsMenu CACHED ************");
	//	YAHOO.log("locId="+locId+" locIdStr="+locIdStr);
		parseAjaxPickerResponseForParents(locId, locIdStr,hasKids, responseXML, div);
	} else {
		//YAHOO.log("************ showParentsMenu START AJAX ************");
		//YAHOO.log("locId="+locId+" locIdStr="+locIdStr);
		var action = "ajaxAddPicker";
		var getContextLocation = "true";
		var getAddressBook = "true";
		var getParents = "false";
		var getChildren = "false";
		var getLeafs = lp_input.getAttribute("leaf");
		var url = "autocomplete?action="+action+"&locationId="+locId+"&getContextLocation="+getContextLocation+"&getAddressBook="+getAddressBook+"&getParents="+getParents+"&getChildren="+getChildren+"&getLeafs="+getLeafs;
		var req = initRequest(url);
		req.onreadystatechange = function() {
		    if (req.readyState == 4) {
		        if (req.status == 200) {
		        	parseAjaxPickerResponseForParents(locId, locIdStr, hasKids,req.responseXML, div);
		            ajaxLocationIds[key] = req.responseXML;
		        } else if (req.status == 204){
		        	
		        }
		    }
		};
		req.open("GET", url, true);
		req.send(null);
	}
}

function resetLocation(locId, locIdStr, hasKids) {	
	// Trigger lp_input listener on locId change to reset context of other page divs
	var searchloc = document.getElementById('searchLocation');
	if(searchloc!=null) {
		searchloc.value = locIdStr;
	}
	var searchlocId = document.getElementById('searchLocationId');
	if(searchlocId!=null) {
		searchlocId.value = locId;
		if(searchloc.listener) {
			searchloc.listener();
		}
	} else {
		loader();
		}
	
	// Replace all DOM nodes with result of AJAX that rebuilds menus from scratch
	var fullContext = document.getElementById('fullContext');
	if(fullContext!=null) {
		// remove all fullContext div's children except for lp_input
		var foundlp = false;
		while (fullContext.hasChildNodes())
		{		
	  		fullContext.removeChild(fullContext.firstChild);
		}
		var lpi = document.getElementById('lp_input');
		if (lpi == null) {
		/*
			lp_input = document.createElement("hidden");
			lp_input.value = locId;
			*/
			fullContext.appendChild(lp_input);
		}
	}
	
	// AJAX call to rebuild unselected and selected nodes
//	showParentsMenu(locId, locIdStr, hasKids,fullContext);
}

function navMouseDown(e) {

	//YAHOO.log("************************ navMouseDown ******************************");
	var evt = e || window.event;
	var evtTarget = evt.target || evt.srcElement;
	//YAHOO.log("evtTarget.nodeName="+evtTarget.nodeName);
	if (evtTarget.nodeName == 'A') { 
		var locId = evtTarget.getAttribute("locId");
		var hasKids = evtTarget.getAttribute("hasKids");
		//YAHOO.log("navMouseDown locId="+locId);
		if (locId > 0) {
			var locIdStr = evtTarget.firstChild.nodeValue;
			//YAHOO.log("navMouseDown locId="+locId+" locIdStr="+locIdStr);
			resetLocation(locId, locIdStr,hasKids);	
		}
	}
}

function reinitNavigation() {
	//new ExclusiveAction( function() {	
		var uls = document.getElementsByTagName('ul');
		for (var i=0;i<uls.length;i++) {
			if (uls[i].className != 'menutree') {
				continue;
			}
			if (!uls[i].movAttach) {
				 addEventSimple(uls[i],'mouseover',navMouseOver);
				 uls[i].movAttach = true;
			}
			if (!uls[i].moutAttach) {
  				 addEventSimple(uls[i],'mouseout',navMouseOut);
  				 uls[i].moutAttach = true;
			}
			if (!uls[i].mdoAttach) {
				 addEventSimple(uls[i],'mousedown',navMouseDown);
				 uls[i].mdoAttach = true;
			}		
			var lis = uls[i].getElementsByTagName('li');
			for (var j=0;j<lis.length;j++) {
				var ul = lis[j].getElementsByTagName('ul')[0];
				if (ul) {
					// relates Parent <LI> to Child <UL>
					lis[j].relatedItem = ul;
					var a = lis[j].getElementsByTagName('a')[0];
					//YAHOO.log("BOLDDDDDDDDDDDDDDDDDDDDDDDDD FONT "+lis[j]);
				}
			}
		}
		if(lp_input==null) {
		    lp_input = document.getElementById('lp_input');
		    if(lp_input!=null && !lp_input.listener) {
		    	lp_input.listener = function() {
		    		self.location = 'today?id='+lp_input.value;
					// self.location = 'location?action=showLocation&id='+lp_input.value;
		    	}
		    }
		}

	//});
}

function addEventSimple(obj,evt,fn) {
	if (obj.addEventListener)
		obj.addEventListener(evt,fn,false);
	else if (obj.attachEvent)
		obj.attachEvent('on'+evt,fn);
}

/** PUSH AND SHIFT FOR IE5 **/

function Array_push() {
	var A_p = 0
	for (A_p = 0; A_p < arguments.length; A_p++) {
		this[this.length] = arguments[A_p]
	}
	return this.length
}

if (typeof Array.prototype.push == "undefined") {
	Array.prototype.push = Array_push
}

function Array_shift() {
	var A_s = 0
	var response = this[0]
	for (A_s = 0; A_s < this.length-1; A_s++) {
		this[A_s] = this[A_s + 1]
	}
	this.length--
	return response
}

if (typeof Array.prototype.shift == "undefined") {
	Array.prototype.shift = Array_shift
}


function cachedAjax(locId) {
	var i = 0;
	for (i = 0; i < ajaxLocationIds.length; i++) {
		if (ajaxLocationIds[i] == locId) {
			return true;
		}
	}
	return false;
}



addEvent(window, 'load', reinitNavigation, false);
// Stop event bubbling
function stopBubbles(e) {
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

function getNav1ContainingNode(node) {
	while (node) {
		try {
			if (node.className == 'nav1') {
				return node;
			}
		} catch (e) {
			return null;
		}
		node = node.parentNode;
	}
	return null;
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init(); 


/*
onerror=handleErr
function handleErr(msg,url,l)
{
	//Handle the error here
	//YAHOO.log("msg="+msg+" url="+url+" l="+l);
	return false;
}
*/