var gmarkers = [];


// This function does all the heavy lifting of creating the labels, icons, and markers on the map

function parseXml(doc) 
{
	// create the icons and store in array
	var icons = [];
	var iconUrls = [];
	var type = 0;
	var id = 0;
	var xmlDoc = google.maps.Xml.parse(doc);

	var types = xmlDoc.documentElement.getElementsByTagName("type");
	for (var i = 0; i < types.length; i++) {
		type = types[i].getAttribute("id");
		iconUrls[type] = types[i].getAttribute("icon_url");
		icons[type] = createIcon(iconUrls[type],types[i].getAttribute("shadow_url"));
		
		var html = '<img src="' + iconUrls[type] + '"/>';
		document.getElementById('service_icon' + type).innerHTML = html;
	}
	
	// create the map
	var map = new google.maps.Map2(document.getElementById("map"));
	map.addControl(new google.maps.LargeMapControl());
	map.addControl(new google.maps.MapTypeControl());
	
	// get the center tag from xml doc (there is only one, hence the [0])
	
	var center = xmlDoc.documentElement.getElementsByTagName("center")[0];
	
	map.setCenter(new google.maps.LatLng(parseFloat(center.getAttribute("lat")),parseFloat(center.getAttribute("lng"))), parseInt(center.getAttribute("zoom")));

	// add the points
	var markers = xmlDoc.documentElement.getElementsByTagName("marker");
	var i = 0;
	  
	while (i < markers.length) {

		var point = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
		var html = markers[i].getElementsByTagName("html")[0].firstChild.data;
		var types = markers[i].getAttribute("type").split(',');
		for (j=0; j< types.length; j++) {
			type = types[j];
			
			var marker = createMarker(point,html,icons[type]);
			map.addOverlay(marker);
			
			mi = new markerInfo(marker,type);

			gmarkers.push(mi);
		}
		i++;
	}
	
	clearAll();

}


// This function creates an object that associates a gmaps marker with a label type.

function markerInfo(marker, type) {
	this.marker = marker;
	this.type = type;	
}


// A function to create the marker and set up the event window.

function createMarker(point,html,icon) 
{
	var marker = new google.maps.Marker(point, {icon:icon});
	
	if (html == undefined || html.length == 0) return;
	
	google.maps.Event.addListener(marker, "click", function() {
		  marker.openInfoWindowHtml(html);
	});

	google.maps.Event.addListener(marker, "mouseover", function() {
		  marker.openInfoWindowHtml(html);
	});

	return marker;
}


// Creates an icon object with the specified icon and shadow.
// Note: currently assumes size of icons!!

function createIcon(iconUrl, shadowUrl)
{
	var icon = new google.maps.Icon();
	icon.image = iconUrl;
	icon.shadow = shadowUrl;
	icon.iconSize = new google.maps.Size(12, 20);
	icon.shadowSize = new google.maps.Size(22, 20);
	icon.iconAnchor = new google.maps.Point(6, 20);
	icon.infoWindowAnchor = new google.maps.Point(5, 1);
	
	return icon;
}


// This function is called when a label is clicked.

function clickLabel(type)
{
	var num_markers = 0;
		
	for (i=0; i<gmarkers.length; i++) {
		if (gmarkers[i].type == type) {
			gmarkers[i].marker.show();
			num_markers++;
		}
		else {
			gmarkers[i].marker.hide();
		}
	}
		
	if (num_markers == 0) {
		alert("No businesses listed for this service.");
	}
}


// This function is called when the "Show All" button is clicked.

function clickShowAll(type)
{
	for (i in gmarkers) {
		gmarkers[i].marker.show();
	}
}

function clearAll()
{
	for (i in gmarkers) {
		gmarkers[i].marker.hide();
	}
}

function init_map_api(id)
{	
	if (google.maps.BrowserIsCompatible()) {
	
		// create timestamp to be used so that xml file doesn't get cached
		var ts = new Date().getTime();

		var file_url = 'http://www.downtowndoglover.com/db/app/webroot/xml/ddl_map_category' + id + '.xml?nocache=' + ts;
		//var file_url = 'http://localhost/ddl/db/app/webroot/xml/ddl_map_category' + id + '.xml?nocache=' + ts;
		google.maps.DownloadUrl(file_url, parseXml);
	
	}
	else {
		alert("Sorry, the Google Maps API is not compatible with this browser");
	}
}


