(function(){ // wrapping in a lambda function so this function can be out of the way of other scripts
	if(!GBrowserIsCompatible()) return false;
	if(window.attachEvent) { window.attachEvent("onload", mapLoad); window.attachEvent("onunload", GUnload); }
	else if(window.addEventListener) { window.addEventListener("load", mapLoad, false); window.addEventListener("unload", GUnload, false); }
	
	function mapLoad() {
		var elname = "hospital"
var noun = "Hospital"
		// unhide the jump link if it was hidden
		if(document.getElementById("mapjumplink")) document.getElementById("mapjumplink").style.display = "";
		
		if(document.body.textContent) v = "textContent";  // the name of the DOM property that has the text inside an element
		else if(document.body.innerText) v = "innerText";
		
		var listings = [];
		var florists = [];
		var latsum = 0;
		var longsum = 0;
		
		var querystring = "";
		var tables = document.getElementsByTagName("span"); 
		for(var i = 0; i < tables.length; i++) {
			if(tables[i].getAttribute("__" + elname + "_id")) {
				listings.push(tables[i]);
				querystring += elname + "_id[]=" + tables[i].getAttribute("__" + elname + "_id") + "&";
			}
		}
		
		var links = document.getElementsByTagName("a");
		for(var i = 0; i < links.length; i++) {
			if(links[i].getAttribute("__florist_id")) {
				florists.push(links[i]);
				querystring += "florist_id[]=" + links[i].getAttribute("__florist_id") + "&";
			}
		}
		
		var req = GXmlHttp.create();
		req.open("POST", "/scripts/get_nearby_florists.xml.php", false);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send(querystring);
		var xdoc = req.responseXML;
		if(xdoc.normalize) xdoc.normalize();
		
		var els = xdoc.getElementsByTagName(elname);
		for(var i = 0; i < listings.length; i++) {
			var found = false;
			for(var j = 0; j < els.length && !found; j++) {
				if(els[j].getAttribute("id") == listings[i].getAttribute("__" + elname + "_id")) {
					found = true;
					if(els[j].getAttribute("longitude") != "" && els[j].getAttribute("latitude") != "") {
						listings[i].setAttribute("__name", els[j].getAttribute("name"));
						listings[i].setAttribute("__address", els[j].getAttribute("address_two"));
						listings[i].setAttribute("__city", els[j].getAttribute("city"));
						listings[i].setAttribute("__state", els[j].getAttribute("state"));
						listings[i].setAttribute("__zip_code", els[j].getAttribute("zip_code"));
						listings[i].setAttribute("__phone_number", els[j].getAttribute("phone_number"));
						listings[i].setAttribute("__tollfree_number", els[j].getAttribute("tollfree_number"));
						listings[i].setAttribute("__latitude", els[j].getAttribute("latitude"));
						listings[i].setAttribute("__longitude", els[j].getAttribute("longitude"));
					} 
				}
			}
		}
		var listings2 = [];
		for(var i = 0; i < listings.length; i++) {
			var lat = parseFloat(listings[i].getAttribute("__latitude"));
			var long = parseFloat(listings[i].getAttribute("__longitude"));
			if((lat > 0 || lat < 0) && (long > 0 || long < 0)) {
				latsum += parseFloat(listings[i].getAttribute("__latitude"));
				longsum += parseFloat(listings[i].getAttribute("__longitude"));
				listings2.push(listings[i]);
			}
		}
		listings = listings2;
		
		
		var els = xdoc.getElementsByTagName("florist");
		for(var i = 0; i < florists.length; i++) {
			var found = false;
			for(var j = 0; j < els.length && !found; j++) {
				if(els[j].getAttribute("id_num") == florists[i].getAttribute("__florist_id")) {
					found = true;
					if(els[j].getAttribute("longitude") != "" && els[j].getAttribute("latitude") != "") {
						florists[i].setAttribute("__name", els[j].getAttribute("name_shop"));
						florists[i].setAttribute("__address", els[j].getAttribute("address"));
						florists[i].setAttribute("__city", els[j].getAttribute("addr_city"));
						florists[i].setAttribute("__state", els[j].getAttribute("addr_state"));
						florists[i].setAttribute("__zip_code", els[j].getAttribute("addr_zip"));
						florists[i].setAttribute("__phone_number", els[j].getAttribute("id_num"));
						florists[i].setAttribute("__tollfree_number", els[j].getAttribute("tel_tollfree"));
						florists[i].setAttribute("__latitude", els[j].getAttribute("latitude"));
						florists[i].setAttribute("__longitude", els[j].getAttribute("longitude"));
					}
				}
			}
		}
		
		var florists2 = [];
		for(var i = 0; i < florists.length; i++) {
			var lat = parseFloat(florists[i].getAttribute("__latitude"));
			var long = parseFloat(florists[i].getAttribute("__longitude"));
			if((lat > 0 || lat < 0) && (long > 0 || long < 0)) {
				latsum += parseFloat(florists[i].getAttribute("__latitude"));
				longsum += parseFloat(florists[i].getAttribute("__longitude"));
				florists2.push(florists[i]);
			}
		}
		florists = florists2;
		
		if(listings.length + florists.length == 0) return false; // there's nothing to display
		
		listings.sort(function(a,b) { return parseFloat(b.getAttribute("__latitude")) - parseFloat(a.getAttribute("__latitude")); });
		florists.sort(function(a,b) { return parseFloat(b.getAttribute("__latitude")) - parseFloat(a.getAttribute("__latitude")); });
		var length = listings.length + florists.length;
		
		var maptable = document.getElementById("maptable");
		maptable.style.display = "block";
		var tableHead = document.createElement("thead");
		var tableRow = document.createElement("tr");
		var tableCell = document.createElement("th");
		tableCell.appendChild(document.createTextNode("Area " + noun + "s and Florists"));
		tableRow.appendChild(tableCell);
		tableHead.appendChild(tableRow);
		var tableCell = document.createElement("td");
		tableCell.style.textAlign = "center";
		tableCell.appendChild(document.createTextNode("(From North to South)"));
		tableRow.appendChild(tableCell);
		maptable.insertBefore(tableHead, maptable.firstChild);

		// maptable.innerHTML = '<tr><th>Area ' + noun + 's and Florists</th><td style="text-align: center;">(From North to South)</td></tr>' + maptable.innerHTML;
		var mapinfo = document.getElementById("mapinfo");
		var mapdiv = document.getElementById("map");
		var map = new GMap2(mapdiv);
		var latlong = new GLatLng(latsum / length, longsum / length);
		map.setCenter(latlong, 11);
		map.addControl(new GScaleControl());
		map.addControl(new GSmallZoomControl());
		map._markerManager = new GMarkerManager(map);
		
				var funeralIcon = new GIcon();
		funeralIcon.image = "/images/cross.png";
		funeralIcon.shadow = "/images/shadow.png";
		funeralIcon.iconSize = new GSize(28,28);
		funeralIcon.shadowSize = new GSize(45,30);
		funeralIcon.iconAnchor = new GPoint(14, 28);
		funeralIcon.infoWindowAnchor = new GPoint(14, 0);
				
		var floristIcon = new GIcon();
		floristIcon.image = "/images/flower2.gif";
		floristIcon.shadow = "/images/shadow2.png";
		floristIcon.iconSize = new GSize(30, 30);
		floristIcon.shadowSize = new GSize(44, 30);
		floristIcon.iconAnchor = new GPoint(15,30);
		floristIcon.infoWindowAnchor = new GPoint(15,15);
		
		var makeMarkerFromElement = function(el, icon) {
			var lat = el.getAttribute("__latitude");
			var long = el.getAttribute("__longitude");
			return new GMarker(new GLatLng(lat, long), icon);
		}
		var makeWindowNodeFromElement = function(el) {
			var n = document.createElement("div");
			n.style.fontSize = "11px";
			if(el.tagName.toUpperCase() == "A") var link = el;
			else if(el.getElementsByTagName("a").length) var link = el.getElementsByTagName("a")[0];
			if(link) {
				var a = document.createElement("a");
				a.href = link.href;
			} else var a = document.createElement("span");
			var b = document.createElement("b");
			b.appendChild(document.createTextNode(el.getAttribute("__name")));
			a.appendChild(b);
			n.appendChild(a);
			n.appendChild(document.createElement("br"));
			n.appendChild(document.createTextNode(el.getAttribute("__address")));
			n.appendChild(document.createElement("br"));
			n.appendChild(
				document.createTextNode(
					el.getAttribute("__city") + ", " +
					el.getAttribute("__state") + " " + 
					el.getAttribute("__zip_code")
				)
			);		
			if(el.getAttribute("__phone_number")) {
				n.appendChild(document.createElement("br"));
				n.appendChild(document.createTextNode("Local: " + el.getAttribute("__phone_number")));
			}
			if(el.getAttribute("__tollfree_number")) {
				n.appendChild(document.createElement("br"));
				n.appendChild(document.createTextNode("Tollfree: " + el.getAttribute("__tollfree_number")));
			}
			
			return n;
		}
		var activateMapInfoItem = function(el) {
			var nodes = mapinfo.childNodes;
			for(i = 0; i < nodes.length; i++) {
				nodes[i].id = "";
			}
			el.id = "mapInfoActiveItem";
		}
		
		for(var i = 0; listings && (i < listings.length); i++) {
			var l = listings[i];
			l._marker = makeMarkerFromElement(l, funeralIcon);
			var m = l._marker;
			m._windowNode = makeWindowNodeFromElement(l);
			m._windowNode.className = "listingNode";
			m._listNode = document.createElement("div");
			m._listNode.className = "listingNode";
			m._listNode._marker = m;
			m._listNode._image = document.createElement("img");
			m._listNode._image.height = "15";
			m._listNode._image.src = m.getIcon().image;
			m._listNode._image.style.verticalAlign = "middle";
			m._listNode.appendChild(m._listNode._image);
			var b = document.createElement("b");
			b.appendChild(document.createTextNode(String.fromCharCode(160,160) + 	l.getAttribute("__name")));
			m._listNode.appendChild(b);
			mapinfo.appendChild(m._listNode);
			
			GEvent.addListener(m, "click", function() { 
				this.openInfoWindow(this._windowNode); 
				activateMapInfoItem(this._listNode);
			});
			GEvent.addDomListener(m._listNode, "click", function() { 
				GEvent.trigger(this._marker, "click"); 
			});
			map._markerManager.addMarker(l._marker, 0);
			
		}
		for(var i = 0; florists && i < florists.length; i++) {
			var f = florists[i];
			f._marker = makeMarkerFromElement(f, floristIcon);
			var m = f._marker;
			m._windowNode = makeWindowNodeFromElement(f);
			m._windowNode.className = "floristNode";
			m._listNode = document.createElement("div");
			m._listNode.className = "listingNode";
			m._listNode._marker = m;
			m._listNode._image = document.createElement("img");
			m._listNode._image.height = "15";
			m._listNode._image.src = m.getIcon().image;
			m._listNode._image.style.verticalAlign = "middle";
			m._listNode.appendChild(m._listNode._image);
			var b = document.createElement("b");
			b.appendChild(document.createTextNode(String.fromCharCode(160,160) + 	f.getAttribute("__name")));
			m._listNode.appendChild(b);	
			mapinfo.appendChild(m._listNode);
			
			GEvent.addListener(m, "click", function() { 
				this.openInfoWindow(this._windowNode); 
				activateMapInfoItem(this._listNode);
			});
			
			GEvent.addDomListener(m._listNode, "click", function() { 
				GEvent.trigger(this._marker, "click"); 
				location = "#maptable";
			});			
			
			map._markerManager.addMarker(f._marker, 10);
		}
		mapLoad = null;
	}
})();