
var map;
var isLining = false;
var lastX = 0;
var lastY = 0;
var myLine = [];

arrPoints = [];
arrThumbs = [];
var request;
var gicons;

var doLat, doLon;

function load() {
	if (GBrowserIsCompatible()) {
	
		
		
		map = new GMap2(document.getElementById("map"), {draggableCursor:"crosshair"});
		
		// Add the small controls
		map.addControl(new GLargeMapControl());
		
		// Add the switcher between Satellite and Map
		map.addControl(new GMapTypeControl());
		
		GEvent.addListener(map, 'click', handleMapClick);
		map.setCenter(new GLatLng(43.68, -79.43),10);
		
		
		gicons = [];
		
		// Create a base icon
		var baseIcon = new GIcon(); 
		baseIcon.image = 'http://maps.gstatic.com/intl/en_ALL/mapfiles/arrow.png';
		baseIcon.shadow = "shadow50.png";
		baseIcon.iconSize = new GSize(20, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);
		
		
		var ic = new GIcon(baseIcon);  ic.image = "marker1.png";
		gicons["1"] = ic;
		var ic = new GIcon(baseIcon);  ic.image = "marker2.png";
		gicons["2"] = ic;
		var ic = new GIcon(baseIcon);  ic.image = "marker3.png";
		gicons["3"] = ic;
		var ic = new GIcon(baseIcon);  ic.image = "marker4.png";
		gicons["4"] = ic;
		var ic = new GIcon(baseIcon);  ic.image = "marker5.png";
		gicons["5"] = ic;
		var ic = new GIcon(baseIcon);  ic.image = "marker6.png";
		gicons["6"] = ic;
		var ic = new GIcon(baseIcon);  ic.image = "marker7.png";
		gicons["7"] = ic;

		baseIcon.iconSize = new GSize(39, 34);

		if (doLat != null)
		{
			p = new GLatLng(doLat, doLon);
			var m = new GMarker(p, {icon:baseIcon});
				
			map.addOverlay(m)
			map.setCenter(p,10);
		}
		
		// Load XML
		request = GXmlHttp.create();
		request.open("GET", "feed.rss", true); // querystring array holds name/value pairs from querystring.
		
		request.onreadystatechange = requestHere;
		request.send(null);
	}
}

function requestHere()
{
	if (request.readyState == 4)
	{

		xml = request.responseXML;

		//
		//  MAP CODE ..........................
		//

		// get center point from meta info.

		
		for (var i = 0; i < xml.childNodes.length; i++) {
			var c1 = xml.childNodes[i];
			
			if (c1.nodeName == "rss") {
				for (var j = 0; j < c1.childNodes.length; j++) {
					var c2 = c1.childNodes[j];
					
					if (c2.nodeName == "livecad") {
						for (var k = 0; k < c2.childNodes.length; k++) {
							var c3 = c2.childNodes[k];
							
							if (c3.nodeName == "item") {
								
								var p;
								var info;
								p = null;
								info = {};
								
								for (var m = 0; m < c3.childNodes.length; m++) {
									var c4 = c3.childNodes[m];
									switch (c4.nodeName)
									{
										case "location": 	
											if (c4.firstChild)
											{	
												var v = c4.firstChild.nodeValue;
												v = v.split(",")
												p = new GLatLng(parseFloat(v[1]), parseFloat(v[0]));
											}
											break;
										case "type":  				info.typ			= c4.firstChild.nodeValue;  break;
										case "alarm":  			info.alarm 		= c4.firstChild.nodeValue;  break;
										case "units":  			info.units 		= c4.firstChild.nodeValue;  break;
										case "area":  				info.area		= c4.firstChild.nodeValue;  break;
										case "crossstreet":  	info.cross 		= c4.firstChild.nodeValue;  break;
										case "dispatchtime": 	info.dispatch 	= c4.firstChild.nodeValue;  break;
										
										
									}
								}
								
								if (p != null)
								{
									var ic;
									ic = gicons[info.alarm];
									
									var m = new GMarker(p, {icon:ic});
									m.info = info;
									
									map.addOverlay(m)
								}
							}
							
						}
					} else if (c2.nodeName == "regions") {
						/*
						for (var k = 0; k < c2.childNodes.length; k++) {
							var c3 = c2.childNodes[k];
							
							if (c3.nodeName == "region")
							{
								var points = [];
								
								for (var m = 0; m < c3.childNodes.length; m++) {
									var c4 = c3.childNodes[m];
									
									if (c4.nodeName == "point") {
										points.push(new GPoint(parseFloat(c4.getAttribute("lon")), parseFloat(c4.getAttribute("lat"))));
									}
								}
								
								map.addOverlay(new GPolyline(points));				
							}
					
						}
						*/
					}
					
				}
				
			}
		}

	}
}

function handleMapClick(overlay, point) 
{

	if (overlay)
	{
		if (overlay.info != null)
		{
			var nHTML =    "<SPAN STYLE=\"color:black;\"><B>" + overlay.info.typ + "</B><BR>"

			nHTML += "<B>Alarm:</B> " + overlay.info.alarm + "<BR>"
			nHTML += "<B>Dispatch Time:</B> " + overlay.info.dispatch + "<BR>"
			nHTML += "<B>Area:</B> " + overlay.info.area + "<BR>"
			nHTML += "<B>Cross Street:</B> " + overlay.info.cross + "<BR>"
			nHTML += "<B>Units:</B> " + overlay.info.units + "<BR>"
		
			nHTML += "</SPAN><BR>"
			
			var infoTabs = [
			  new GInfoWindowTab("CAD Event", nHTML),
			];							
			
			overlay.openInfoWindowTabsHtml(infoTabs);			
		
		}
	}
}


