window.onload = initAll;
var xhr = false;
var vendorArray = new Array();

function initAll() {
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	}
	else {
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) { }
		}
	}

	if (xhr) {
		xhr.onreadystatechange = setVendorArray;
		xhr.open("GET", "data/vendors.xml", true);
		xhr.send(null);
	}
	else {
		alert("Sorry, but I couldn't create an XMLHttpRequest");
	}

	if (document.getElementById("searchField")) {
		document.getElementById("searchField").onkeypress = searchSuggest;
	}
	else {				// it's a hall page
		var allDivs = document.getElementsByTagName("div");
		for (var i=0; i<allDivs.length; i++) {
			allDivs[i].onclick = featureOneDiv;
		}
	}
}

function setVendorArray() {
	if (xhr.readyState == 4) {
		if (xhr.status == 200) {
			if (xhr.responseXML) {
				var allVendors = xhr.responseXML.getElementsByTagName("vendor");

				for (var i=0; i<allVendors.length; i++) {
					var vendorObj = new Object;
	
					vendorObj.vendorName = allVendors[i].getElementsByTagName("vendorName")[0].firstChild;
					vendorObj.booth = allVendors[i].getElementsByTagName("booth")[0].firstChild;
					vendorObj.vendorURL = allVendors[i].getElementsByTagName("vendorURL")[0].firstChild;
					vendorObj.longDesc = allVendors[i].getElementsByTagName("longdesc")[0].firstChild;
					vendorArray[i] = vendorObj;
				}
				
				if (document.location.hash != "") {		// if we got here via a search
					var thisId = document.location.hash.substring(1);
					var theDiv = document.getElementById(thisId);
					showDivInfo(theDiv);
				}
			}
		}
		else {
			alert("There was a problem with the request " + xhr.status);
		}
	}
}

function searchSuggest(evt) {
	var thisKey = (evt) ? evt.which : window.event.keyCode;
	if (thisKey >= 32 && thisKey <= 122) {		// stupid hacks to handle onkey problems
		setTimeout(realSearch, 0);
	}
}

function realSearch() {
	document.getElementById("searchField").className = "";
	document.getElementById("vendorFound").innerHTML = "";
	document.getElementById("popups").innerHTML = "";
	var str = document.getElementById("searchField").value;

	if (str != "") {
		for (var i=0; i<vendorArray.length; i++) {
			var thisVendor = vendorArray[i];
			var thisVendorName = vendorArray[i].vendorName.nodeValue;
	
			if (thisVendorName.toLowerCase().indexOf(str.toLowerCase()) == 0) {
				var tempDiv = document.createElement("div");
				tempDiv.vendorObj = thisVendor;
				tempDiv.innerHTML = thisVendorName;
				tempDiv.onclick = makeChoice;
				tempDiv.className = "suggestions";
				document.getElementById("popups").appendChild(tempDiv);
			}
		}
		var foundCt = document.getElementById("popups").childNodes.length;
		if (foundCt == 0) {
			document.getElementById("searchField").className = "error";
		}
		if (foundCt == 1) {
			foundOne(document.getElementById("popups").firstChild.vendorObj);
		}
	}
}

function makeChoice(evt) {
	var thisDiv = (evt) ? evt.target : window.event.srcElement;
	foundOne(thisDiv.vendorObj);
}

function foundOne(theVendor) {
	var theMsg = theVendor.vendorName.nodeValue;
	theMsg += "<p class='smInfo'>Booth: " + theVendor.booth.nodeValue;
	if (theVendor.longDesc && theVendor.longDesc.nodeValue) {
		theMsg += "<br />" + theVendor.longDesc.nodeValue;
	}
	theMsg += "</p><ul><li><a href='";
	theMsg += (theVendor.booth.nodeValue.indexOf("S-") >= 0) ? "south.html#s" : "west.html#w";
	theMsg += theVendor.booth.nodeValue.substring(2) + "'>View on map</a></li>";
	if (theVendor.vendorURL.nodeValue) {
		theMsg += "<li><a target='_new' href='http://" + theVendor.vendorURL.nodeValue;
		theMsg += "'>View their Web site</a></li>";
	}
	theMsg += "</ul>";
	
	document.getElementById("searchField").value = theVendor.vendorName.nodeValue;
	document.getElementById("vendorFound").innerHTML = theMsg;
	document.getElementById("popups").innerHTML = "";
}

function featureOneDiv(evt) {
	var allDivs = document.getElementsByTagName("div");
	for (var i=0; i<allDivs.length; i++) {
		allDivs[i].className = "";
	}

	var theDiv = (evt) ? evt.target : window.event.srcElement;
	showDivInfo(theDiv);
}

function showDivInfo(thisDiv) {
	thisDiv.className = "pickedDiv";

	var theVendor = getVendor(thisDiv.id);
	if (theVendor) {
		var boothTop = thisDiv.offsetTop + (thisDiv.offsetHeight/2);
		var boothLeft = thisDiv.offsetLeft + (thisDiv.offsetWidth/2);
		var theMsg = theVendor.vendorName.nodeValue + "<br />Booth: " + theVendor.booth.nodeValue;
		if (theVendor.longDesc && theVendor.longDesc.nodeValue) {
			theMsg += "<br />" + theVendor.longDesc.nodeValue;
		}
		if (theVendor.vendorURL && theVendor.vendorURL.nodeValue) {
			theMsg += '<p><a target="_new" href="http://' + theVendor.vendorURL.nodeValue + '">Web site</a></p>';
		}
		theMsg += '<div id="goHome"><a href="/mw/">Return to main page</a></div>';
		document.getElementById("vendorInfo").innerHTML = theMsg;
		
		document.getElementById("vendorInfo").style.top = (boothTop - 5) + "px";
		document.getElementById("vendorInfo").style.left = boothLeft + "px";

		document.getElementById("vendorInfo").onclick = function() {
			// workaround to keep vendor info from graying out
			document.getElementById("vendorInfo").style.visibility = "visible";
		}
		document.getElementById("vendorInfo").style.visibility = "visible";

		document.getElementById("closeBox").style.top = (boothTop - 15) + "px";
		document.getElementById("closeBox").style.left = (boothLeft - 10) + "px";
		
		document.getElementById("closeBox").onclick = function() {
			document.getElementById("vendorInfo").style.visibility = "hidden";
			document.getElementById("closeBox").style.visibility = "hidden";
		}
		document.getElementById("closeBox").style.visibility = "visible";
	}
}

function getVendor(boothNum) {
	for (var i=0; i<vendorArray.length; i++) {
		var thisBooth = vendorArray[i].booth.nodeValue.toLowerCase().replace(/-/,"");
		
		if (boothNum == thisBooth) {
			return (vendorArray[i]);
		}
	}
	return null;
}
