﻿// Google Maps

if(typeof GMap2 == "undefined") {
	document.write('<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAA1AsfBpdqXPr4TCNq8IWc4RQeCbgjY0VwLI8qjuDGf8xyFhZ7qBReiKFUGyIAQUoG_4LYn4EubZtgLA" type="text/javascript"></script>');
}

function googlemapsCreate(width, height, centerLat, centerLong, controls) {
	// make sure google maps script is here
	
	if(centerLat == null) centerLat = 46.836094;
	if(centerLong == null) centerLong = 9.284348;

	// create map
	var mapNode = document.createElement('div');

	if(GBrowserIsCompatible()) {
		// draw the map and center to flims
		var map = new GMap2(mapNode, {mapTypes:[G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP],size:new GSize(width,height)});
		map.setCenter(new GLatLng(centerLat, centerLong), 15);
		map.enableScrollWheelZoom();
		// we also need geocoder
		var geocoder = new GClientGeocoder();
		if(controls) map.addControl(new GSmallMapControl());
		// map.addControl(new GLargeMapControl())
		map.addControl(new GMapTypeControl());
		
		// add a search function
		/* mapNode.findCoordinates = function(addr) {
			geocoder.getLatLng(
				addr,
				function(point) {
					if(!point) {
						alert(addr + " not found");
					}
					else {
						if(description.setPosition) description.setPosition(point.lat(), point.lng());
						map.clearOverlays()
						map.setCenter(point, 14);
						var marker = new GMarker(point, {draggable: true});  
						map.addOverlay(marker);
					
						GEvent.addListener(marker, "dragend", function() {
							var pt = marker.getPoint();
							map.panTo(pt);
							if(description.setPosition) description.setPosition(pt.lat(), pt.lng());
						});
					}
				});
		} */

		mapNode.overlayBounds = new GLatLngBounds();

		mapNode.addOverlay = function(lat, long, iconName, callb) {
			if(iconName == null) iconName = "house";
			
			var ic = new GIcon();
			if(iconName == "target") {
				ic.image = "http://www.immowilhelm.ch/images/target.png";
				ic.iconSize = new GSize(72, 72);
				ic.iconAnchor = new GPoint(36, 36);
				ic.infoWindowAnchor = new GPoint(36, 36);
			}
			else {
				ic.image = "http://www.remax-graubuenden.ch/images/homesmalls.png";
				ic.iconSize = new GSize(32, 25);
				ic.iconAnchor = new GPoint(12, 10);
				ic.infoWindowAnchor = new GPoint(12, 10);
			}
			
			var point = new GLatLng(lat, long);
			var m = new GMarker(point, {icon: ic});
			
			if(callb != null) GEvent.addListener(m, "click", callb);

			map.addOverlay(m);
			mapNode.overlayBounds.extend(point);
		}
		
		mapNode.center = function() {
			map.setCenter(mapNode.overlayBounds.getCenter(), map.getBoundsZoomLevel(mapNode.overlayBounds));
		}
		
		mapNode.disableActions = function() {
			map.disableDragging();
			map.disableDoubleClickZoom();
			map.disableScrollWheelZoom();
		}
		
	}

	return mapNode;
}