//<![CDATA[
	GMap.prototype.centerAndZoomOnBounds = function(bounds) {
		var center_lat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2.0;
		var center_lng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2.0;
		var center = new GLatLng(center_lat,center_lng)
		map.setCenter(center, map.getBoundsZoomLevel(bounds));
	}

	if (GBrowserIsCompatible()) {    
		// arrays to hold copies of the markers and html used by the sidebar
		// because the function closure trick doesnt work there
		var gmarkers = [];

		var map = new GMap(document.getElementById("map"));
map.setUIToDefault();
		map.setCenter(new GLatLng(0,0), 0);

		
		var request = GXmlHttp.create();
		filename = "/docs/cuts2011.xml";	
		request.open("GET", filename, true);
		request.onreadystatechange = processTabbedXML;
		request.send(null);


		var bounds = new GLatLngBounds();
		
		// base icons
	    	var baseIcon = new GIcon();
	     	baseIcon.iconSize = new GSize (12,20);
	     	baseIcon.shadow = "/images/shadow.png";
     		baseIcon.shadowSize = new GSize(22,20);
	      	baseIcon.iconAnchor = new GPoint(6,20);
      		baseIcon.infoWindowAnchor = new GPoint (6,1);
      		baseIcon.infoShadowAnchor = new GPoint(13,13);
		         	
		// array of customized icons
	   	var gicons = [];
	     	gicons["less25"] = new GIcon(baseIcon, "/images/white.png");
      		gicons["less50"] = new GIcon(baseIcon, "/images/yellow.png");
	      	gicons["less75"] = new GIcon(baseIcon, "/images/blue.png");
	      	gicons["less100"]= new GIcon(baseIcon, "/images/green.png");
	      	gicons["more"] = new GIcon(baseIcon, "/images/red.png");
	      	gicons["unknown"] = new GIcon(baseIcon, "/images/black.png");


		// ==================================================
		// A function to create a tabbed marker and set up the event window
		// This version accepts a variable number of tabs, passed in the arrays htmls[] and labels[]
		function createTabbedMarker(point, paper, parent, icon, tabs) {
			var marker = new GMarker(point,gicons[icon]);
		
			var marker_num = gmarkers.length;
			marker.marker_num = marker_num;
			marker.tabs = tabs;
			gmarkers[marker_num] = marker;
        
			GEvent.addListener(gmarkers[marker_num], "click", function() {
				marker.openInfoWindowTabsHtml(gmarkers[marker_num].tabs);
			});
			
			return marker;
		}
		// ==================================================
      

		// This function picks up the click and opens the corresponding info window
		function myclick(i) {
			GEvent.trigger(gmarkers[i], "click");
		}

		function getNodeValue(Element) {
			if ((Element.length>0) && Element[0] && Element[0].firstChild && Element[0].firstChild.nodeValue)
			return Element[0].firstChild.nodeValue;
		}

	} else {
		alert("Sorry, the Google Maps API is not compatible with this browser");
	}


	function processTabbedXML() {
		if (request.readyState == 4) {
			// var xmlDoc = request.responseXML;
			var xmlDoc = GXml.parse(request.responseText);
			// obtain the array of markers and loop through it
			var markers = xmlDoc.documentElement.getElementsByTagName("marker");

			// alert("processing "+markers.length+" markers");
			// map.clearOverlays();
			// bug in clearOverlays
			for (var i=0; i<gmarkers.length; i++) {
				map.removeOverlay(gmarkers[i]);
			}
      
			gmarkers = new Array();
			for (var i = 0; i < markers.length; i++) {
				// obtain the attributes of each marker
				var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng")));

				var paper = markers[i].getAttribute("paper");
				var parent = markers[i].getAttribute("parent");
				var icon = markers[i].getAttribute("icon");

				// get the tab info
				tabInfo = markers[i].getElementsByTagName("tab");
				tabs = new Array();
				if (tabInfo.length > 0) {
					// alert("processing "+tabInfo.length+" tabs");
					for (var j = 0; j < tabInfo.length; j++) {
						var date = getNodeValue(tabInfo[j].getElementsByTagName("date"));
						var info = getNodeValue(tabInfo[j].getElementsByTagName("info"));
						var cuts = getNodeValue(tabInfo[j].getElementsByTagName("cuts"));
						var link = getNodeValue(tabInfo[j].getElementsByTagName("link"));

						// alert("point["+i+"] tab["+j+"] label="+date+", contents="+tabHtml);
						if ((j==0) && (tabInfo.length > 2)){ 
						//  adjust the width so that the info window is large enough for this many tabs
							info = '<div style="width:'+tabInfo.length*88+'px">' + info + '</div>';
						}

						var html = "<div class='mapinfo'><h3>" + paper + "</h3><strong>Owner:</strong> " + parent + "<br/><strong>Date:</strong> " + date + ", 2011 <br/><strong>Layoffs:</strong> <a href='/?p=" + link + "'>" + cuts + "</a><br/>" + info + "</div>";

						
						tabs.push(new GInfoWindowTab(date,html));
					}
				} else { 
					// alert("no tabs point "+i);
					var date = markers[i].getAttribute("date");
					var info = markers[i].getAttribute("info");
					tabs.push(new GInfoWindowTab(date,html));
				}      

				// create the marker
				var marker = createTabbedMarker(point, paper, parent, icon, tabs);
				bounds.extend(point);				
				map.addOverlay(marker);
			}
			map.centerAndZoomOnBounds(bounds);

		}
	}

    //]]>

