var yellowIcon = new GIcon();
yellowIcon.image = "/maps/images/mm_20_yellow.png";
yellowIcon.shadow = "/maps/images/mm_20_shadow.png";
yellowIcon.iconSize = new GSize(12, 20);
yellowIcon.shadowSize = new GSize(22, 20);
yellowIcon.iconAnchor = new GPoint(6, 20);
yellowIcon.infoWindowAnchor = new GPoint(5, 1);

var redIcon = new GIcon();
redIcon.image = "/maps/images/mm_20_red.png";
redIcon.shadow = "/maps/images/mm_20_shadow.png";
redIcon.iconSize = new GSize(12, 20);
redIcon.shadowSize = new GSize(22, 20);
redIcon.iconAnchor = new GPoint(6, 20);
redIcon.infoWindowAnchor = new GPoint(5, 1);

var map;
var panel;

var markerList;
var infoList;
var addedList;
var latList;
var lngList;
var idList;
var numOffenders = 0;
var loadingCity = 0;

function onLoad() {
	panel = document.getElementById('panel');

	map = new GMap(document.getElementById("map"));
	map.addControl(new GLargeMapControl());

	var lastCity = ReadCookie("LastCity");
	lastCity = unescape(lastCity);
	lastCity = StringReplace(lastCity, "\\\+", " ");

	if (lastCity == ";") {
		lastCity = "";
	}

	loadCity(lastCity);

	GEvent.addListener(map, "moveend", function() {
		if (loadingCity == 0) {
			addMarkers();
		}
	});
}

function infoopener(i){
	if (addedList[i] == 0) {
		map.addOverlay(markerList[i]);
		addedList[i] = 1;
	}

	markerList[i].openInfoWindowHtml( infoList[i] );
}

function makeOpenerCaller( i )
{
	return function() { infoopener( i ); };
}

function ReadCookie(cookieName) {
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return ""; 
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length; 
	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function SetCookie(cookieName,cookieValue,nDays) {
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue)
	+ ";expires="+expire.toGMTString();
}

function StringReplace(sOrgVal,sSearchVal,sReplaceVal)
{
	var sVal;
	try
	{
		sVal = new String(sOrgVal);
		if (sVal.length < 1) { return sVal; }
		var sRegExp = eval("/" + sSearchVal + "/g");
		sVal = sVal.replace(sRegExp,sReplaceVal);
	}
	catch (exception)  { }
	return sVal;
}

function loadCity(city) {
	loadingCity = 1;

	SetCookie("LastCity", city, 365);

	map.clearOverlays();

//	if (city.length > 0) {
//		panel.innerHTML = "<br/><I><B>Loading " + city + "...</B></I>";
//	} else {
//		panel.innerHTML = "<br/><I><B>Loading...</B></I>";
//	}

	panel.innerHTML = "<br/>&nbsp;&nbsp;&nbsp;<img src='http://www.georgia-sex-offenders.com/maps/loading.gif' width='32' height='32'/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

	var request = GXmlHttp.create();

	if (city.length > 0) {
		request.open("GET", "getData.php?city=" + city, true);
	} else {
		request.open("GET", "getData.php?city=Marietta", true);
	}

	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			var xmlDoc = request.responseXML;
			var offenders = xmlDoc.documentElement.getElementsByTagName("offender");

			var centerlat = xmlDoc.documentElement.getAttribute("centerlat");
			var centerlng = xmlDoc.documentElement.getAttribute("centerlng");

			if (centerlat == null) {
				// problem with xml document
				panel.innerHTML = "<br/><b>Error</b><br/><br/>There is a problem with the data for this city.";
				return;
			}

			var panelHtml = "";

			delete markerList;
			delete infoList;
			delete addedList;
			delete latList;
			delete lngList;
			delete idList;

			// global arrays
			markerList = new Array(offenders.length);
			infoList = new Array(offenders.length);
			addedList = new Array(offenders.length);
			latList = new Array(offenders.length);
			lngList = new Array(offenders.length);
			idList = new Array(offenders.length);

			// local arrays
			nameList = new Array(offenders.length);
			addressList = new Array(offenders.length);
			addressList = new Array(offenders.length);
			widthList = new Array(offenders.length);
			heightList = new Array(offenders.length);
			crimeList = new Array(offenders.length);
			predatorList = new Array(offenders.length);

			numOffenders = offenders.length;

			for (var i = 0; i < offenders.length; i++) {
				nameList[i] = offenders[i].getAttribute("name");
				addressList[i] = offenders[i].getAttribute("address");
				latList[i] = offenders[i].getAttribute("lat");
				lngList[i] = offenders[i].getAttribute("lng");
				idList[i] = offenders[i].getAttribute("id");

				if (latList[i] != 0) {
					widthList[i] = offenders[i].getAttribute("width");
					heightList[i] = offenders[i].getAttribute("height");
					predatorList[i] = offenders[i].getAttribute("predator");
					crimeList[i] = offenders[i].getAttribute("crime");
					addedList[i] = 0;
				}
			}

			//
			//	Create content for pop-up info windows
			//

			for (var i = 0; i < offenders.length; i++) {
				if (latList[i] != 0) {
					var html = "<div id='infoWindow' style='font-family:Verdana,Helvetica;width:250;" + 
						"font-size:13px'>" + 
						nameList[i] + "<br/>" + addressList[i] + "<br/>";

					if (heightList[i] > 0) {
						html += "<br/><center><img src='http://emails.georgia-sex-offenders.com/" +
							idList[i] + ".jpg' height='" + 
							heightList[i] + "' width='" + widthList[i] + "'></center>";
					}

					html += "<br/>Crime: " + crimeList[i] + "<br/>";

					if (predatorList[i] == "1") {
						html += "<br/><font color='Red'>" +
							"This person is a sexual predator.</font><br/>";
					}

					html += "<br/>Link to <a href='/maps/offender.php?id=" + idList[i] + "&city=" + city + "'>more info";

					if (heightList[i] > 0) {
						html += " and a larger picture</a>";
					}

					html += "</a></div>";

					infoList[i] = html;
				}
			}

			//
			//	Add text to side panel
			//

			var numDisplayable = xmlDoc.documentElement.getAttribute("displayable");
			var numNotDisplayable = xmlDoc.documentElement.getAttribute("notDisplayable");

			if (numDisplayable > 0) {
				panelHtml += "<tr><td colspan='2' style='font-family:Verdana,Helvetica; font-size:13px'>Offenders shown on the map:</td></tr>";
			} else if (numNotDisplayable > 0) {
				panelHtml += "<tr><td colspan='2' style='font-family:Verdana,Helvetica; font-size:13px'>None of the sex offenders in this city could be displayed on the map, below are the offenders that could not be displayed:<br/>&nbsp;</td></tr>";
			}

			for (var i = 0; i < offenders.length; i++) {
				if (latList[i] != 0) {
					panelHtml += "<tr><td><a href='javascript:infoopener(" + i + ")'>";

					if (predatorList[i] == "1") {
						panelHtml += "<img src='/maps/images/circle_red.gif' alt='Predator' border='0' width='18' height='18'>";
					} else {
						panelHtml += "<img src='/maps/images/circle_yellow.gif' alt='Offender' border='0' width='18' height='18'>";
					}

					panelHtml += "</td><td style='font-family:Verdana,Helvetica; font-size:13px'>" +
						"<a href='javascript:infoopener(" + i + ")'>" +
						nameList[i] + "<br/>" + addressList[i] + "</a></td></tr>";
				}
			}

			if (numDisplayable > 0 && numNotDisplayable > 0) {
				panelHtml += "<tr><td colspan='2' style='font-family:Verdana,Helvetica; font-size:13px'><br>Offenders below not shown on map:</td></tr>";
			}

			for (var i = 0; i < offenders.length; i++) {
				if (latList[i] == 0) {
					panelHtml += "<tr><td></td>" +
						"<td style='font-family:Verdana,Helvetica; font-size:13px'>" +
						"<a href='/maps/offender.php?id=" + idList[i] + "'>" +
						nameList[i] + "<br/>" + addressList[i] + "</a></td></tr>";
				}
			}

			if (numDisplayable == 0 && numNotDisplayable == 0) {
    				panelHtml += "<tr><td colspan='2' style='font-family:Verdana,Helvetica; font-size:13px'>There are no sex offenders in " + city + ".</td></tr>";
			}

			if (city.length > 0) {
				panel.innerHTML = "<br/><table>" + panelHtml + "</table>";
			} else {
				panel.innerHTML = 
				  "<br/>To see where sex offenders live <b>select a city using the selector above</b>.<br/><br/>"
				+ "You can click and drag the map to move it or double-click to recenter it.<br/><br/>"
				+ "The <img src='/maps/images/yellow_small.png' width='12' height='20'>'s and "
				+ "<img src='/maps/images/red_small.png' width='12' height='20'>'s show the locations "
				+ "of sex offenders and sexual predators.<br/><br/>"
				+ "Click on a balloon to see an offender's name, address, photo, and a link to more information about "
				+ "them.";
			}

			//
			//	Create markers
			//

			for (var i = 0; i < offenders.length; i++) {
				if (latList[i] != 0) {
					markerList[i] = createMarker(latList[i], lngList[i],
						predatorList[i] == "1" ? redIcon : yellowIcon, 
						infoList[i]);

				}
			}

			// center map and zoom map
			var zoom = 5;

			if (city == "Atlanta") {
				zoom = 6;
			}

			map.centerAndZoom(new GPoint(centerlng, centerlat), zoom);

			addMarkers();

			if (numDisplayable == 1) {
				markerList[0].openInfoWindowHtml( infoList[0] );
			}

			loadingCity = 0;

		}
	}

	document.getElementById('linktome').href = '/maps/offenders.php?city=' + city;
	document.getElementById('mugshots').href = '/mugshots/?city=' + city;
 
	request.send(null);
}

function addMarkers() {
	var bounds = map.getBoundsLatLng()

	for (var i = 0; i < numOffenders; i++) {
		if (addedList[i] == 0 &&
			lngList[i] >= bounds.minX && lngList[i] <= bounds.maxX &&
			latList[i] >= bounds.minY && latList[i] <= bounds.maxY) 
		{
			addedList[i] = 2;
		}
	}

	for (i=0;i<markerList.length;i++) {
		if (addedList[i] == 2) {
			map.addOverlay(markerList[i]);
			addedList[i] = 1;
		}
	}
}

function createMarker(lat, lng, icon, html) {
	var point = new GPoint(lng, lat);
	var marker = new GMarker(point, icon);

	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});

	return(marker);
}
