/*
	Rearrange the menu - if browser doesnt support JS, then they will just see the UL 

	Since this is true, we are able to use the onclick feature to allow people to actually click on a div as if
	it is a link!
*/

/**
	There are times when we have issues calculating the proper position for the second level (first drop down) so we need
	to adjust the offsets. You can use these variables to tweak the positioning. the IE_ variables affect only IE, and the
	OT_ variables affect all other browser.
**/
var IE_SHIFT_RIGHT  = 0;
var IE_SHIFT_DOWN   = 0;
var OT_SHIFT_RIGHT  = 0;
var OT_SHIFT_DOWN   = 0;

/**
	You probably don't need to modify anything beyond here ever again.
**/

function wdl_mouseOver(obj, base_name) {
	if(obj.className.indexOf("_hover") == -1)
		obj.className += " " + base_name + "_hover";
	return true;
}
function wdl_mouseOut(obj, base_name) {
	obj.className = obj.className.replace(" " + base_name + "_hover", "");
	obj.className = obj.className.replace(base_name + "_hover", "");
	return true;
}

function wdl_popup(obj, shift_right, base_name) {
	wdl_mouseOver(obj, base_name);									// maintain the hover effect
	var kids = obj.getElementsByTagName("UL");
	if (kids[0]) {
		kids[0].style.display = "block";
	}
}
function wdl_popin(obj, base_name) {
	wdl_mouseOut(obj, base_name);									// maintain the hover effect
	var kids = obj.getElementsByTagName("UL");
	if (kids[0])
		kids[0].style.display = "none";
}

function prepareMenu(divId) {
	if ( navigator.userAgent.match(/Safari/) )
		setTimeout(new Function("jdinit('"+divId+"')"), 50);
	else
		jdinit(divId);
}

function jdinit(divId) {
	// Access the div wrapper
	var wDiv = document.getElementById(divId);
	if(!wDiv) return false;								// if there is no div with this ID, stop working
	
	// check if we are renderring for IE and calculate an extra offset for left positioning if needed
	var xOffset, yOffset;
	if(navigator.appName == "Microsoft Internet Explorer") {
		xOffset = IE_SHIFT_RIGHT;
		yOffset = wDiv.offsetTop + IE_SHIFT_DOWN;
	} else {
		xOffset = OT_SHIFT_RIGHT;
		yOffset = wDiv.offsetTop + OT_SHIFT_DOWN;
	}
	
	var allTop = wDiv.offsetTop;
	
	// Grab all anchor tags and surround them with a wrapper div
	var links = wDiv.getElementsByTagName("a");						// get all of the anchor tags in the menu
	for(ct = 0; ct < links.length; ct++) {							// loop through all of them
			// Move the anchor into a new wrapper
			var newDiv = document.createElement("div");				// create a DIV object that we will use as a wrapper
			var parent = links[ct].parentNode;						// get a reference to the link's parent
			var url = links[ct].href;								// save the URL to link to for reference
			parent.appendChild(newDiv);								// set that parent as the new div's parent
			newDiv.innerHTML = links[ct].innerHTML;					// copy the contents of the link into the div
			parent.removeChild(links[ct]);							// and delete the old anchor
			ct--;													// this corrects the counter because all other anchors just shifted

			// Assign some events to the div so that it acts like a link itself
			newDiv.onclick = new Function("window.location.href='"+url+"'; return true;");
			newDiv.title = url;
			parent.title = url;
	}

	// Hide all sub-menus
	var uls = wDiv.getElementsByTagName("ul");
	for(ct = 0; ct < uls.length; ct++) {
		// ** FIRST LEVEL OF MENU **
		if(uls[ct].parentNode.tagName == "DIV") {
			uls[ct].className += " first_ul";							// mark as 1st level
			var lis = uls[ct].getElementsByTagName("li");				// get list of all children
			for(j = 0; j < lis.length; j++)								// and crawl through that list
				if(lis[j].parentNode == uls[ct]) {						// if they are IMMEDIATE children
					lis[j].className += " first_li";					// then mark them as first level li's
					lis[j].onmouseover = new Function("return wdl_mouseOver(this, 'first_li');");
					lis[j].onmouseout = new Function("return wdl_mouseOut(this, 'first_li');");
				}
			// Set the height of the parent DIV to a static value
			wDiv.style.height = lis[0].clientHeight + "px";
		}
		
		// ** SECOND LEVEL OF MENU **
		else if(uls[ct].parentNode.parentNode.parentNode.tagName == "DIV") {
			uls[ct].className += " second_ul";							// mark as 2nd level
			var lis = uls[ct].getElementsByTagName("li");				// get all of its children
			var max_width = 0;											// stores the width of the widest LI inside this UL
			for(j = 0; j < lis.length; j++)								// crawl through the list of children
				if(lis[j].parentNode == uls[ct]) {						// if they are IMMEDIATE children
					lis[j].className += " second_li";					// then mark them as second level li's
					lis[j].onmouseover = new Function("return wdl_mouseOver(this, 'second_li');");
					lis[j].onmouseout = new Function("return wdl_mouseOut(this, 'second_li');");
					max_width = Math.max(lis[j].clientWidth, max_width);
				}
			// Set the width of the parent UL and all other LI's to be the same
			for(j = 0; j < lis.length; j++)
				if(lis[j].parentNode == uls[ct])
					lis[j].style.width = max_width + "px";
			uls[ct].style.width = max_width + "px";
			
			// add drop-down feature
			var parent = uls[ct].parentNode;							// get reference to its parent LI
			parent.className += " drop_down";
			parent.onmouseover = new Function("return wdl_popup(this, false, 'drop_down')");// add mouseover effect to show this entry
			parent.onmouseout = new Function ("return wdl_popin(this, 'drop_down')");// add mouseout effect to hide the entry again

			// Reposition the child
			uls[ct].style.position = "absolute";
			uls[ct].style.left     = (parent.offsetLeft + xOffset) + "px";
			uls[ct].style.top      = (parent.clientHeight + yOffset) + "px";
		}
		
		// ** THIRD LEVEL OF MENU **
		else {
			uls[ct].className += " third_ul";							// mark as 3rd level
			var lis = uls[ct].getElementsByTagName("li");				// get all of its children
			var max_width = 0;											// stores the width of the widest LI inside this UL
			for(j = 0; j < lis.length; j++)								// crawl through the children
				if(lis[j].parentNode == uls[ct]) {						// if they are IMMEDIATE children
					lis[j].className += " third_li";					// then mark them as third level LI's
					lis[j].onmouseover = new Function("return wdl_mouseOver(this, 'third_li');");
					lis[j].onmouseout = new Function("return wdl_mouseOut(this, 'third_li');");
					max_width = Math.max(lis[j].clientWidth, max_width);
				}
			// Set the width of the parent UL and all other LI's to be the same
			for(j = 0; j < lis.length; j++)
				if(lis[j].parentNode == uls[ct])
					lis[j].style.width = max_width + "px";
			uls[ct].style.width = max_width + "px";
			
			// add parent drop-down features
			var parent = uls[ct].parentNode;
			parent.className += " drop_right";
			parent.onmouseover = new Function("return wdl_popup(this, true, 'drop_right')");// add mouseover effect to show this entry
			parent.onmouseout = new Function ("return wdl_popin(this, 'drop_right')");      // add mouseout effect to hide the entry again
			
			// Reposition the child
			uls[ct].style.position = "absolute";
			uls[ct].style.left     = parent.clientWidth + "px";
			uls[ct].style.top      = parent.offsetTop + "px";
			uls[ct].style.display = "none";
		}
	}
	
	// Hide all of the lower level entries
	for(ct = 0; ct < uls.length; ct++) {
		// ** ANYTHING BUT FIRST LEVEL OF MENU **
		if(uls[ct].parentNode.tagName != "DIV")
			uls[ct].style.display = "none";
	}

	var links = wDiv.getElementsByTagName("li");					// get all of the li tags in the menu
	for(ct = 0; ct < links.length; ct++) {							// loop through all of them
		if(!links[ct].className.match(/drop_down/) && !links[ct].className.match(/drop_right/))
			links[ct].onclick = new Function("window.location.href='"+links[ct].title+"'; return true;");
	}
}