var req = false;
var _self = false;
var markers = new Array();
var baseIcon = false;

function directoryMap () {
	this.map = false;
	this.kml = false;
	this.points = false;
	this.autozoom = false;
	this.controls = true;
	
	this.init = function (lat, lng, zoom) {
		_self = this;
		var self = this;
		this.map = new google.maps.Map2(document.getElementById("map"));
		this.map.setCenter(new GLatLng(lat, lng), zoom);
		if (this.controls) {
			this.map.addControl(new GLargeMapControl());
			this.map.addControl(new GMapTypeControl());
		}
		
		// Base Icon
		baseIcon = new GIcon();
		baseIcon.shadow = "http://www.google.com/mapfiles/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);
		
		if (this.kml != false) {
			this.loadKML(this.kml);
		}
		else {
			this.loadPoints(this.points);
		}
		
	}
	
	this.loadKML = function (url) {
		this.geoXml = new GGeoXml(url);
		this.map.addOverlay(this.geoXml);
	}
	
	this.loadPoints = function () {
		var url = this.points + '&BBOX=' + this.map.getBounds();
		
		// Create Google XmlHttpRequest object
		req = GXmlHttp.create();
			
		req.onreadystatechange = function () {
			if (req.readyState == 4) {
				if (req.status == 200) { 
					var bounds = new GLatLngBounds();
					var points = req.responseXML.getElementsByTagName("Placemark");
					if (points.length > 0) {
						_self.map.clearOverlays();
						for (i=0; i<points.length; i++) {
							
							/*
								Create Marker
							*/
							var lat = parseFloat(points[i].getElementsByTagName('Point')[0].getElementsByTagName('latitude')[0].firstChild.data.substring(0,13));
							var lng = parseFloat(points[i].getElementsByTagName('Point')[0].getElementsByTagName('longitude')[0].firstChild.data.substring(0,13));
							var title = points[i].getElementsByTagName('name')[0].firstChild.data;
							var info = points[i].getElementsByTagName('description')[0].firstChild.data;
							var icon = points[i].getElementsByTagName('icon');
							
							// Create Marker
							if (icon.length > 0) {
								var customIcon = new GIcon(baseIcon);
								customIcon.image = icon[0].firstChild.data;
								var markerOptions = { icon:customIcon, title:title };
							}
							else {
								var markerOptions = { title:title };
							}
							
							markers[i] = new GMarker(new GLatLng(lat, lng), markerOptions);
							
							GEvent.addListener(markers[i], "click",openMarker(markers[i], title, info));
							
							// Extend Bounds
							bounds.extend(markers[i].getPoint());
							_self.map.addOverlay(markers[i]);
						}
						
						if (_self.autozoom) {
							_self.map.setZoom(_self.map.getBoundsZoomLevel(bounds));
							var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
							var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
							_self.map.setCenter(new GLatLng(clat,clng));
							_self.autozoom = false;
						}
					}
				}
			}
		}
	  
		req.open("GET", url,true);
		req.send(null);
	}
}

function openMarker (marker, title, info) {
	return function() {marker.openInfoWindowHtml('<div class="balloon" style="width:200px;"><div style="font-weight: bold; font-size: medium; margin-bottom: 0em;">' + title + '</div>' + info + '</div>');}
}