// Default no menu was selected (see hideSpan and liteCell)
isNotSelected = true;

// Set initial page settings
function setPage() {
	// Allow only IE or Netscape 6x
	if (!document.getElementById) {
		return;
	}

	// Store drop menu object as a global
	dropMenuSpan = document.getElementById('dropmenu').style;
	// Be sure span is hidden
	dropMenuSpan.visiblity = 'hidden';
	// Set hide function to fire if the user clicks anywhere on the page
	document.onmousedown = hideSpan;
	//This is here to allow the users to instantly navigate the links without having to click first.
	dropMenuSpan.visibility = 'visible';
}

// Populates the drop list and makes it visible based on menu position
function dropList(obj, evt, showDropList) {
	if (!showDropList) {
		return;
	}

	// Set drop menu cell colors based on linked in stylesheet
	setDropMenuColors();
	
	var evt = getBrowserEvent(evt);
	
	if (dropMenuSpan.visibility == 'visible' && evt.type == 'mouseout') {
		// Do not fire menu to open when user moves mouse off of item and onto dropmenu
		return;
	}
	
	// Be sure span is hidden
	dropMenuSpan.visiblity = 'hidden';
	// Id of calling menu item
	var menuItem = obj.id;
	// Get span object for writing
	var dropListObj = document.getElementById("dropmenu");
	
	// Populate drop list based on incoming hash and master data structure
	var html = populateDropList(menuItem);

	// Assign finished html to span
	dropListObj.innerHTML = html;
	// Position and show populated drop list
	
	showPopulatedDropList(obj, dropListObj);

	return false;
}

// Populate drop list based on incoming hash and master data structure
function populateDropList(menuItem) {
	// Loop through data array and write nessessary markup for span
	var html = '<TABLE border="0" cellspacing="0">';
	
	i = true;
	for (priHash in topMenu[menuItem]) {
		if (i) {
			html += printDropCells(topMenu[menuItem]['title'], topMenu[menuItem]['href'])
			i = false;
		}
		if (priHash != 'title' || priHash != 'href') {
			var dropItem = topMenu[menuItem][priHash];
			for (secHash in dropItem) {
				if (dropItem['title']) {
					html += printDropCells(dropItem['title'], dropItem['href']);
				}
				break;
			}
		} else {
			continue;
		}
	}
	html += '</TABLE>';

	return html;
}

function printDropCells(title, href_str) {
	var pattern = new RegExp("'", "g");
	href_str 	= href_str.replace(pattern, "\\'");
	
	html = '<TR>\n';
	html += '<TD onMouseOver="lightCell(this, event, \'' + colors['cellOver'] + '\', \'' + colors['textOver'] + '\', \'' + title + '\')"';
	html += ' onMouseOut="lightCell(this, event, \'' + colors['cellOut'] + '\', \'' + colors['textOut'] + '\', \'' + title + '\')"';
	html += " onClick=\"launchPage(this, '" + href_str + "')\">\n";
	html += '<A href="' + href_str + '" id="' + title + '" class="dropLinks">' + title + '</A></TD>\n'
	html += '</TR>\n';
	return html;
}

// Display and position populated drop list
function showPopulatedDropList(obj, dropListObj) {
	// Get parent object information
	var parentInfo = getParentInfo(obj);
	
	// Determine right coordinate of drop menu
	rightCord = parentInfo['parentCellObj'].offsetLeft + parentInfo['parentCellObj'].offsetWidth;
	
	if (rightCord > parentInfo['winW']) {
		// If window has been resized, calculate available room for menu
		dropListObj.style.left = parentInfo['winW'] - parentInfo['parentCellObj'].offsetWidth;
	} else {
		// Set left hand coordinate for drop menu
		dropListObj.style.left = parentInfo['parentCellObj'].offsetLeft;
	}

	// Set top coordinate for drop menu
	dropListObj.style.top = parentInfo['parentCellObj'].offsetTop + parentInfo['parentCellObj'].offsetHeight;
	dropListObj.style.visibility = 'visible';
}

// Gets parent object information of a particular drop menu link
function getParentInfo(obj) {
	var parentInfo = new Array();
	
	if (document.all) {
		// Width of browser window
		parentInfo['winW'] = document.body.clientWidth;
		// Find parent cell of originating menu request
		parentInfo['parentCellObj'] = obj.parentElement.parentElement;
		// Determine right coordinate of drop menu
	} else {
		// Width of browser window
		parentInfo['winW'] = window.innerWidth;
		// Find parent cell of originating menu request
		parentInfo['parentCellObj'] = obj.parentNode.parentNode;
	}
	
	return parentInfo;
}

// Hide the drop menu
function hideSpan() {
	// Don't hide span if user has selected an item
	if (isNotSelected) {
		dropMenuSpan.visibility = 'hidden';
	}
}

// Highlight cell background
function lightCell(obj, evt, bgColor, clr, linkId) {
	evt = getBrowserEvent(evt);
	isNotSelected = (evt.type == 'mouseout') ? true : false;
	obj.style.backgroundColor = bgColor;
	document.getElementById(linkId).style.color = clr;
}

// Open new page when cell has been clicked
function launchPage(obj, page) {
	if (document.all) {
		var tableObj = obj.parentElement.parentElement;
		var cellObj = tableObj.parentElement.parentElement;
	} else {
		var tableObj = obj.parentNode.parentNode;
		var cellObj = tableObj.parentNode.parentNode;
	}
	
	cellObj.style.visibility = 'hidden';
	document.location.href = page;
}

// Change link style properties based on event
function changeLink(obj, evt, showDropList) {
	// Allow only IE or Netscape 6x
	if (!document.getElementById) {
		return;
	}
	var evt = getBrowserEvent(evt);
	var parentInfo = getParentInfo(obj);
	
	if (evt.type == 'mouseover') {
		parentInfo['parentCellObj'].style.borderColor = '#D4C8B2';
	} else {
		parentInfo['parentCellObj'].style.borderColor = '#5C64A1';
	}
	
	// Allows drop menu to be redisplayed if another item is moused over
	if (dropMenuSpan.visibility == 'visible') {
		dropList(obj, evt, showDropList);
	}
	dropMenuSpan.zIndex = '0';
}

// Filter incoming event based on browser type
function getBrowserEvent(evt) {
	if (document.all) {
		return window.event;
	} else {
		return evt;
	}
}

// Debug utility
function getProps(obj) {
	var s = "", i = 0;

	for (i in obj) {
		s += " [" + i + "] = " + obj[i] + "<BR>";
	}

	window.open('', '', 'height=350,width=360,scrollbars,resizeable').document.write(s);
}

// Sets drop menu cell colors based on linked in stylesheet
function setDropMenuColors() {
	colors = new Array();	
	if (document.all) {
		var ruleObjs = document.styleSheets[0].rules;
		var s = "", i = 0;
		// IE code
		setDropColorsIE(ruleObjs);
	} else {
		var ruleObjs = document.styleSheets[0].cssRules;
		// N7 Code
		setDropColorsN7(ruleObjs);
	}
}

function setDropColorsIE(ruleObjs) {
	// Loop through available rules
	for (i in ruleObjs) {
		var ruleSty = ruleObjs[i].style;
		if (ruleObjs[i].selectorText == 'A.dropLinks:hover') {
			colors['textOver'] = ruleSty.color;
			colors['cellOver'] = ruleSty.backgroundColor;
		} else if (ruleObjs[i].selectorText == 'A.dropLinks:link') {
			colors['textOut'] = ruleSty.color;
		} else if (ruleObjs[i].selectorText == '.dropMenu') {
			colors['cellOut'] = ruleSty.backgroundColor;
		}
	}
}

function setDropColorsN7(ruleObjs) {
	// Loop through available rules
	for (i=0;i<ruleObjs.length;i++) {
		// Loop through rule properties
		for (k in ruleObjs[i]) {
			ruleSty = ruleObjs[i].style;
			if (k == 'selectorText' && ruleObjs[i][k] == 'a.dropLinks:hover') {
				colors['textOver'] = ruleSty.color;
				colors['cellOver'] = ruleSty.backgroundColor;
			} else if (k == 'selectorText' && ruleObjs[i][k] == 'a.dropLinks:link') {
				colors['textOut'] = ruleSty.color;
			} else if (k == 'selectorText' && ruleObjs[i][k] == '.dropMenu') {
				colors['cellOut'] = ruleSty.backgroundColor;
			}
		}
	}
}

function void_out() {
	return;	
}