// Uses Google Maps API
// See http://www.google.com/apis/maps/documentation/

// Global map and some marker icons
var map = null;
var yellowMarker = null;
var redMarker = null;

// Create Google Map
function createMap(lng, lat, zoom, bind, maptype) {
	if(!GBrowserIsCompatible()) {
		alert("Your browser is not supported.\nCurrently only FireFox, Mozilla, IE 5.5+ and Safari 1.2+ are supported.");
	}
	else if(document.getElementById("map")) {
		map = new GMap(document.getElementById("map"));
		map.setMapType(maptype); // G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP
	  	map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		// Create markers
		yellowMarker = createIcon("yellow");
		redMarker = createIcon("red");
		var marker = null;
		// Bind event handlers
		if(bind == true) {
			GEvent.bind(map, "moveend", null, onMoveEnd);
		}
		// Set marker if location is specified
		if(lng != null && lat != null) {
			marker = new GMarker(new GPoint(lng, lat), redMarker);
		}
		else {
			lng = 0;
			lat = 25;
		}
		if(zoom == null) {
			zoom = 16;
		}
		// Set initial view
		map.centerAndZoom(new GPoint(lng, lat), zoom);
		// Place marker
		if(marker != null) {
			map.addOverlay(marker);
		}
	}
}

// Clear info fields
function clearInfo() {
	if(document.getElementById("coords")) {
		document.getElementById("coords").innerHTML = "";
	}
	if(document.getElementById("location")) {
		document.getElementById("location").innerHTML = "";
	}
	if(document.getElementById("name")) {
		document.getElementById("name").innerHTML = "";
	}
	if(document.getElementById("description")) {
		document.getElementById("description").innerHTML = "";
	}
}

// Zoom out to world view
function viewAll() {
	map.centerAndZoom(new GPoint(0, 25), 16);
}

// Create small teardrop marker icon
function createIcon(color) {
	var icon = new GIcon();
	icon.image = "images/mm_20_" + color + ".png";
	icon.shadow = "images/mm_20_shadow.png";
	icon.iconSize = new GSize(12, 20);
	icon.shadowSize = new GSize(22, 20);
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);
	return icon;
}

// Event handler for "moveend" event
function onMoveEnd() {
	// Request object list for new bounds
	var request = GXmlHttp.create();
	var bounds = map.getBoundsLatLng();
	var query = bounds.minX + "," + bounds.minY + "," + bounds.maxX + "," + bounds.maxY;
	request.open("GET", "objects.php?" + query, false);
	request.send(null);
	// Update info
	clearInfo();
	var center = this.map.getCenterLatLng();
	var span = this.map.getSpanLatLng();
	if(document.getElementById("zoom")) {
		document.getElementById("zoom").innerHTML = "Zoom: " + this.map.getZoomLevel();
	}
	if(document.getElementById("center")) {
		document.getElementById("center").innerHTML = "Center: " + center.x + "," + center.y;
	}
	if(document.getElementById("bounds")) {
		document.getElementById("bounds").innerHTML = "Bounds: " + query;
	}
	if(document.getElementById("span")) {
		document.getElementById("span").innerHTML = "Span: " + span.width + "," + span.height;
	}
	// Remove previous markers
	map.clearOverlays();
	// Place markers for new objects
	var objects = request.responseXML.documentElement.getElementsByTagName("object");
	for (var i = 0; i < objects.length; i++) {
		var point = new GPoint(parseFloat(objects[i].getAttribute("lng")), parseFloat(objects[i].getAttribute("lat")));
		var marker = new GMarker(point, redMarker);
		map.addOverlay(marker);
		GEvent.bind(marker, "click", objects[i], onMarkerClick);
	}
}

// Event handler for "click" event on marker
function onMarkerClick() {
	// Update coordinates
	var coords = new GPoint(parseFloat(this.getAttribute("lng")), parseFloat(this.getAttribute("lat")));
	// Center and zoom if far away
	if(map.getZoomLevel() > 6) {
		map.centerAndZoom(coords, 6);
	}
	if(document.getElementById("coords")) {
		document.getElementById("coords").innerHTML = "Coords: " + coords.x + "," + coords.y;
	}
	// Request object data
	var request = GXmlHttp.create();
	request.open("GET", "object.php?" + this.getAttribute("id"), false);
	request.send(null);
	// Update fields
	for (var node = request.responseXML.documentElement.firstChild; node != null; node = node.nextSibling) {
		if(document.getElementById(node.nodeName)) {
			document.getElementById(node.nodeName).innerHTML = node.firstChild.nodeValue;
		}
	}
}
