if (window.addEventListener) {
	window.addEventListener("load", startup, false);
} else if (window.attachEvent) {
	window.attachEvent("onload", startup);
}

function startup() {
	// hide loading progress
	document.getElementById("loading").style.display = "none";
	
	// set wrapper width based on user resolution
	var ele = document.getElementById("wrapper").style;
	ele.display = "block";
	if (screen.width < 995) {
		ele.width = "800px";
	} else {
		ele.width = "995px";
	}
	
	buildsubmenus_horizontal();
	// highlight the current mainNav button
	var loc = window.location.toString();
	var aid;
	if (loc.indexOf(".browse.") != -1 || loc.indexOf(".details.") != -1) {
		aid = "browse";
	} else if (loc.indexOf(".search.") != -1 || loc.indexOf(".laboratory.") != -1) {
		aid = "search";
	} else if (loc.indexOf(".help.") != -1 || loc.indexOf(".exception.") != -1) {
		aid = "help";
	} else if (loc.indexOf(".whats.new.") != -1) {
		aid = "whatsnew";
	} else {
		aid = "home";
	}
	document.getElementById(aid).style.background = "#9A171C";
	
	// if browsing authors, include dropdown script
	if (loc.indexOf(".browse.authors.") != -1) {
		loadDropdowns('ddlNationality=nationality()people|ddlEthnicity=ethnicity()people|ddlGender=gender()people');
	} else if (loc.indexOf(".advanced.results.") != -1) {
		loadDropdowns('ddlAuthors=author()object|ddlGenres=genre()object');
	} else if (loc.indexOf("search.advanced.aspx") != -1) {
		ActivateAutoSuggest();
	}
}

// menu dropdown
var menuids = ["first"];
function buildsubmenus_horizontal() {
for (var i = 0 ; i < menuids.length ; i++) {
  var ultags = document.getElementById(menuids[i]).getElementsByTagName("ul");
    for (var t = 0 ; t < ultags.length ; t++) {
			if (ultags[t].parentNode.parentNode.id == menuids[i]) {
				ultags[t].style.top = ultags[t].parentNode.offsetHeight + "px";
			} else {
		  	ultags[t].style.left = ultags[t - 1].getElementsByTagName("a")[0].offsetWidth + "px";
			}
    	ultags[t].parentNode.onmouseover = function() {
    		this.getElementsByTagName("ul")[0].style.visibility = "visible";
    	}
    	ultags[t].parentNode.onmouseout = function() {
    		this.getElementsByTagName("ul")[0].style.visibility = "hidden";
    	}
    }
  }
}

// get variables from the querystring
function requestQueryString(field) {
	var qs = window.location.search.substring(1);
	var keys = qs.split('&');
	
	for (var i = 0; i < keys.length; i++) {
		var pair = keys[i].split('=');
		if (pair[0] == field) {
			if (pair[1] == '') {
				return '';
			}else{
				return pair[1].replace(/\+/g, ' ')
			}
		}
	}
	return '';
}

String.prototype.trim = function() {
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
};