var loadedFlag = false;
function loadNewMap(pageRoot, latitude, longitude, zoomFactor, mapDiv) {
	var mapPoint = new GLatLng (latitude, longitude);
  	if (GBrowserIsCompatible()) {
		listHTML = "";
		specificFlag = 0;
		matchBuildings = new Array();
        map = new GMap2(document.getElementById(mapDiv));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(mapPoint, zoomFactor);
		map.setZoom (zoomFactor);
//		mgr = new GMarkerManager (map, {trackMarkers:true});
//		document.getElementById("google-maps-form-div").style.display = "block";
		if (!loadedFlag) {
			timer = setTimeout ("createNewMap('" + pageRoot + "');", 10);
			loadedFlag = true;
		}
	}
}
function createMarkerNew (pageRoot, latitude, longitude, iconFile, iconWidth, iconHeight, basicInfo, fullDescription) {

	var point = new GLatLng (latitude, longitude);
	var icon = new GIcon();
	icon.image = iconFile;
	icon.iconSize = new GSize(iconWidth, iconHeight);
	icon.iconAnchor = new GPoint(7, 7);
	icon.infoWindowAnchor = new GPoint(5, 1);
	var marker = new GMarker (point, icon);
	if (basicInfo != "" && fullDescription != "") {
		var infoTabs = [new GInfoWindowTab("Details", basicInfo), new GInfoWindowTab("Branches", fullDescription)];
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowTabsHtml(infoTabs);
		});
	}
	map.addOverlay (marker);
	return marker;
}
function generateMarkers (pageRoot) {
	var name, address, description, telephone, basicInfo, courthouseTypes, icon, iconWidth, iconHeight, latitude, longitude, buildingId, courthouseTypeFields, ffDetect, validFlag, courtId, courtIdFields, curType, courtFieldLength, iconWidthFields, iconHeightFields, iconFields;
	var batch = [];
	var nameFields = optionsXML.getElementsByTagName('Name');
	var addressFields = optionsXML.getElementsByTagName('Address');
	var telephoneFields = optionsXML.getElementsByTagName('Telephone');
	var latitudeFields = optionsXML.getElementsByTagName('Latitude');
	var longitudeFields = optionsXML.getElementsByTagName('Longitude');
	var courtIdFields = optionsXML.getElementsByTagName('Court_ID');
	var iconWidthFields = optionsXML.getElementsByTagName ("Icon_Width");
	var iconHeightFields = optionsXML.getElementsByTagName ("Icon_Height");
	var iconFields = optionsXML.getElementsByTagName ("Icon");
	var courthouseTypeFields = optionsXML.getElementsByTagName('Courthouse_Type_Info');
	var addressLength = addressFields.length;
	var detailTab;
	var marker = null;
	var infoTabs = null;
	if (addressLength == 0) {
		alert ("We're sorry.  There are no buildings that meet your criteria.  Please try again.");	
	} else {
		for (var i = 0; i < addressLength; i++) {
			marker = null;
			name = nameFields[i].firstChild.data;
			address = addressFields[i].firstChild.data;
			address = address.replace(/\r|\n|\r\n/g, "<br />");
			telephone = telephoneFields[i].firstChild.data;
			latitude = parseFloat (latitudeFields[i].firstChild.data);
			longitude = parseFloat (longitudeFields[i].firstChild.data);
			if (specificFlag == 1) {
				specificLat = latitude;
				specificLng = longitude;
			}
			courtId = parseFloat (courtIdFields[i].firstChild.data);
			icon = iconFields[i].firstChild.data;
			iconWidth = iconWidthFields[i].firstChild.data;
			iconHeight = iconHeightFields[i].firstChild.data;
			courthouseTypesFields = courthouseTypeFields[i].childNodes;
			courthouseTypes = "<ul class=\"courthouse-types\">";
			var courtFieldLength = courthouseTypesFields.length;
			ffDetect = false;
			for (var j = 0; j < courtFieldLength; j++) {
				if (courthouseTypesFields[j].nodeName == "#text") {
					ffDetect = true;
					break;
				}
			}
			for (var j = 0; j < courtFieldLength; j++) {
				validFlag = false;
				if (!ffDetect) {
					curType = courthouseTypesFields[j].childNodes[0].text;
					validFlag = true;
				} else if (courthouseTypesFields[j].nodeName == "Courthouse_Type_Info") {
					curType =  courthouseTypesFields[j].childNodes[1].firstChild.data;
					validFlag = true;
				}
				if (validFlag) {
					courthouseTypes += "<li><span>" + curType + "</span></li>\n";
				}
			}
			courthouseTypes += "</ul>";
			detailTab = "<p>" + address + "</p>\n<p>Tel:  " + telephone + "</p>\n";
			basicInfo = "<h3>" + name + "</h3>\n" + detailTab + "\n";
//			basicInfo += buildingLink;
			basicInfo = "<div style=\"max-width:  300px;  height:  150px;  overflow-y:  visible;\">" + basicInfo + "</div>\n";
			var fullDescription = "<div style=\"max-width:  300px;  height:  150px;  overflow-y:  visible;\">\n";
			fullDescription += courthouseTypes;
			fullDescription += "</div>\n";
//			var newMarker = createMarker (fullDescription, certifications, pageRoot, icon, iconWidth, iconHeight, latitude, longitude);
//			newMarker = createMarker (latitude, longitude);
			marker = createMarkerNew (pageRoot, latitude, longitude, icon, iconWidth, iconHeight, basicInfo, fullDescription);
//			map.addOverlay (marker);
	   	}
   	}
}
function gatherResponseText (pageRoot) {
	optionsText = xmlHttp.responseText;
	if (optionsText != '') {
   		optionsXML = xmlHttp.responseXML.documentElement;
		generateMarkers(pageRoot);
	} else {
		var responseTimer = setTimeout ("gatherResponseText('" + pageRoot + "')", 10);
	}
}
function retrieveBuildings (pageRoot, queryString) {
  
  try {    
  	// Firefox, Opera 8.0+, Safari    
	xmlHttp=new XMLHttpRequest(); 
  } catch (e) {    
	  	// Internet Explorer
		try {      
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");     
		} catch (e) {      
		  	try	{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {       
			 return false;        
			}      
		}   
	}  
	xmlHttp.onreadystatechange=function() {
   		if(xmlHttp.readyState==4)
       	{
			gatherResponseText(pageRoot);
			clearTimeout (timer);
			timer = null;
			return false;
	   	}
	}
	if (pageRoot != undefined && pageRoot != '' && pageRoot != "undefined") {
		optionsText = '';
		var url = pageRoot + "xml-pages/retrieve-courthouses.asp";
		if (queryString != '' && queryString != "undefined" && queryString != undefined) {
			url += "?" + queryString;
		}
	   	xmlHttp.open("GET", url, true);
	   	xmlHttp.send(null); 
	}
	return false;
}
function createNewMap(pageRoot) {
	map.clearOverlays();
//	mgr = new GMarkerManager (map, {trackMarkers:true});
	retrieveBuildings (pageRoot);
}
function addCheckboxToQueryString (queryString, term, value) {
	var newString;
	if (value) {
		if (queryString != "") {
			newString = queryString + "%2C+%27" + term + "%27";
		} else {
			newString = "%27" + term + "%27";
		}
	} else {
		newString = queryString;
	}
	return newString;
}
function updateMap (pageRoot) {
	var theForm = document.getElementById("LegendForm");
	map.clearOverlays();
	Family = false;
	Civil = false;
	Criminal = theForm.Criminal.checked;
	queryString = "";
	queryString = addCheckboxToQueryString (queryString, "Criminal", Criminal);
	if (queryString != "") {
		queryString = "courthouse_type=" + queryString;
	}
	retrieveBuildings (pageRoot, queryString);
}
var Family, Civil, Criminal;
