var map;
var greenIcon = new GIcon(G_DEFAULT_ICON);
greenIcon.image = "/images/green-dot.png";


function SelectSingleNode(xmlDoc, elementPath)
{
	if(window.ActiveXObject)
	{
		return xmlDoc.selectSingleNode(elementPath);
	}
	else
	{
	   var xpe = new XPathEvaluator();
	   var nsResolver = xpe.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
	   var results = xpe.evaluate(elementPath,xmlDoc,nsResolver,XPathResult.FIRST_ORDERED_NODE_TYPE, null);
	   return results.singleNodeValue;
	}
}

function nodeValue(node)
{
	if(window.ActiveXObject)
		return node.nodeTypedValue;
	else
		return node.textContent;
}

function createCity(gPoint, title, lost, found, handle)
{
	var newMarker = new GMarker(gPoint);
	GEvent.addListener(newMarker, 'click', function() {
		var htmlMessage = '<div class="citypopup"><h2>' + title + '</h2><p>Lost pets: ' + lost + '<br />Found pets: ' + found + '<br /><a href="/' + handle + '">More info...</a></p></div>';
		map.openInfoWindowHtml(gPoint, htmlMessage);
		});
	return newMarker;
}

function createPet(gPoint, status, name, animal, desc, postcode, lat, lon, image, pid)
{
	if(status == 'found') {
		var newMarker = new GMarker(gPoint, { icon:greenIcon });
	} else {
		var newMarker = new GMarker(gPoint);
	}
	GEvent.addListener(newMarker, 'click', function() {
		var htmlMessage =
			'<div class="lostpetpopup">' +
			'<h2>' + status + ' ' + animal + ' (' + postcode + ')</h2><p>';

		if(status == 'lost') {
			htmlMessage = htmlMessage +	'Named &quot;' + name + '&quot;<br />';
		}

		htmlMessage = htmlMessage + desc + '</p>';

		if(image != '') htmlMessage =
			htmlMessage + '<p><img src="' + image + '" width="133" height="100" /></p>';

		htmlMessage =
			htmlMessage + '</div>';
		map.openInfoWindowHtml(gPoint, htmlMessage);
	});
	return newMarker;
}

function Markers() {
}

Markers.prototype.mapMove = function ()
{
	map.clearOverlays();

	var bounds = map.getBounds();
	var z = map.getZoom();
	var sw = bounds.getSouthWest();
	var ne = bounds.getNorthEast();
	s = sw.lat(); var w = sw.lng(); var n = ne.lat(); var e = ne.lng();

	if (window.XMLHttpRequest)
		xhttp=new window.XMLHttpRequest();
	else
		xhttp=new ActiveXObject("Microsoft.XMLHTTP");

	var markerUrl = '/mapdata.php?action=local&s=' + s + '&w=' + w + '&n=' + n + '&e=' + e + '&z=' + z;

	xhttp.open("GET",markerUrl,false);
	xhttp.send("");
	xmlDoc=xhttp.responseXML;
	cities = SelectSingleNode(xmlDoc, 'map/cities');

	for(var i=0; i < cities.childNodes.length; i++)
	{
		if(cities.childNodes[i].tagName == 'city')
		{
			city = cities.childNodes[i];
			name = nodeValue(SelectSingleNode(city, 'name'));
			lost = nodeValue(SelectSingleNode(city, 'lost'));
			found = nodeValue(SelectSingleNode(city, 'found'));
			handle = nodeValue(SelectSingleNode(city, 'handle'));
			lon = nodeValue(SelectSingleNode(city, 'lon'));
			lat = nodeValue(SelectSingleNode(city, 'lat'));
			gPoint = new GLatLng(lat, lon);
			cityMarker = createCity(gPoint, name, lost, found, handle);
			map.addOverlay(cityMarker); 

		}
	}

	pets = SelectSingleNode(xmlDoc, 'map/pets');

	for(var i=0; i < pets.childNodes.length; i++)
	{
		if(pets.childNodes[i].tagName == 'pet')
		{
			pet = pets.childNodes[i];
			status = nodeValue(SelectSingleNode(pet, 'status'));
			animal = nodeValue(SelectSingleNode(pet, 'animal'));
			name = nodeValue(SelectSingleNode(pet, 'name'));
			desc = nodeValue(SelectSingleNode(pet, 'desc'));
			postcode = nodeValue(SelectSingleNode(pet, 'postcode'));
			lat = nodeValue(SelectSingleNode(pet, 'lat'));
			lon = nodeValue(SelectSingleNode(pet, 'lon'));
			image = nodeValue(SelectSingleNode(pet, 'image'));
			pid = nodeValue(SelectSingleNode(pet, 'pid'));
			gPoint = new GLatLng(lat, lon);
			petMarker = createPet(gPoint, status, name, animal, desc, postcode, lat, lon, image, pid);
			map.addOverlay(petMarker);

		}
	}

}

function initialize()
{
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("map_canvas"), {scrollwheel: false});

		if (window.XMLHttpRequest)
			xhttp=new window.XMLHttpRequest();
		else
			xhttp=new ActiveXObject("Microsoft.XMLHTTP");

		xhttp.open("GET",mapDataUrl,false);
		xhttp.send("");
		xmlDoc=xhttp.responseXML;
	
		lon = nodeValue(SelectSingleNode(xmlDoc, 'map/center/lon'));
		lat = nodeValue(SelectSingleNode(xmlDoc, 'map/center/lat'));
		zoom = parseFloat(nodeValue(SelectSingleNode(xmlDoc, 'map/center/zoom')));

		map.setCenter(new GLatLng(lat, lon), zoom);
		map.setUIToDefault();

		if(fixPosition)
		{
			map.disableDragging();
			map.disableScrollWheelZoom();
			map.disablePinchToZoom();
			map.disableDoubleClickZoom();
		}

		var myMarkers = new Markers();

 		GEvent.bind(map, "zoomend", myMarkers, myMarkers.mapMove );

		myMarkers.mapMove();


	}	
}


window.onload = initialize;
window.onunload = GUnload;

