PHYSICAL_MAP = 0;
NORMAL_MAP = 1;
SATELLITE_MAP = 2;
HYBRID_MAP = 3;

setOnSpongebobMap = function(func) {
	if (window['SpongeBobMap'])
		func();
	else
		onSpongeBobMap = func;
}

google.load("maps", "2");
google.load("mootools", "1.2.2");
google.setOnLoadCallback(function() {
	
	SpongeBobMap = new Class({
		
		initialize: function(mapDiv, mapZoom, mapType, imgDir) {
			// map from div
			this.mapElement = $(mapDiv);
			this.map = new GMap2(this.mapElement);
			if (isNaN(mapZoom))
				mapZoom = 5;
			this.center = new google.maps.LatLng(51, 10.6);
			this.map.setCenter(this.center, mapZoom);
			this.imgDir = imgDir;

			// map types
			var mapTypes = [G_PHYSICAL_MAP, G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP];
			if (isNaN(mapType))
				mapType = 0;
			this.map.setMapType(mapTypes[mapType]);
			this.map.addMapType(G_PHYSICAL_MAP);
			
			// map controls
			this.map.addControl(new GSmallMapControl());
			this.map.addControl(new GMenuMapTypeControl());
			
			// default marker icon
			this.defaultIcon = new GIcon(G_DEFAULT_ICON);
			this.defaultIcon.image = this.imgDir+'sb_icon_normal.png';
			this.defaultIcon.iconSize = new GSize(49, 68);
			this.defaultIcon.iconAnchor = new GPoint(24, 68);
			this.defaultIcon.shadow = this.imgDir+'sb_icon_shadow.png';
			this.defaultIcon.shadowSize = new GSize(74, 68);
			this.defaultIcon.transparent = this.imgDir+'sb_icon_transparent.png';
			this.defaultIcon.imageMap = [8,49,  4,45,  4,8,  8,4,  40,4,  45,8,  45,45,  40,49,  31,49,  24,64,  18,49];
			
			// special marker icon
			this.specialIcon = new GIcon(G_DEFAULT_ICON);
			this.specialIcon.image = this.imgDir+'sb_icon_special.png';
			this.specialIcon.iconSize = new GSize(49, 68);
			this.specialIcon.iconAnchor = new GPoint(24, 68);
			this.specialIcon.shadow = this.imgDir+'sb_icon_shadow.png';
			this.specialIcon.shadowSize = new GSize(74, 68);
			this.specialIcon.transparent = this.imgDir+'sb_icon_transparent.png';
			this.specialIcon.imageMap = [8,49,  4,45,  4,8,  8,4,  40,4,  45,8,  45,45,  40,49,  31,49,  24,64,  18,49];
			
			this.formatTerms.delay(100, this);
		},
		
		formatTerms: function() {
			var copyr = $('copyright');
			if (!copyr)
				return this.formatTerms.delay(100, this);
			copyr.setStyle('width', '200px');
			copyr.setStyle('white-space', this.mapElement.getStyle('normal'));
			copyr.setStyle('font-size', '9px');
			copyr.setStyle('font-weight', 'bold');
			copyr.setStyle('bottom', '8px');
			copyr.setStyle('right', '4px');
			copyr.setStyle('wordWrap', 'break-word');
		},
		
		getPointByAddress: function(address, callback) {
			// use geocoding
			if (this.geocoder == undefined)
				this.geocoder = new GClientGeocoder();
			this.geocoder.getLatLng(address, callback);
		},
		
		createMarkerByAddress: function(address, title, link, center) {
			this.getPointByAddress(address, this.createMarkerByPoint.bindWithEvent(this, [title, link, center]));
		},
		
		createGermanyOutline: function() {
			var imgDir = this.imgDir;
			var path = window.location.href + '';
			path = path.substr(0, (path.lastIndexOf('/')));
			var geoXml = new GGeoXml(path+'/germany.kml?'+Math.round(Math.random()*99999));
			GEvent.addListener(this.map, "addoverlay", function(overlay) {
				if (overlay instanceof GMarker)
					overlay.setImage(imgDir+'sb_icon_over.png');
			});
			this.map.addOverlay(geoXml);
		},
		
		createMarkerByPoint: function(point, title, link, center, special) {
			var imgDir = this.imgDir;
			if (!point)
				return; // fails silently
			if (special == true)
				var marker = new GMarker(point, {icon: this.specialIcon, title: title, zIndexProcess: function() { GOverlay.getZIndex(50) }});
				//var marker = new GMarker(point, {icon: this.specialIcon, title: title});
			else
				var marker = new GMarker(point, {icon: this.defaultIcon, title: title});
			
			if (center)
				this.map.setCenter(point);
			GEvent.addListener(marker, 'click', function() {
				window.location.href = link;
			});
			
			if (special == true)
			{
				GEvent.addListener(marker, 'mouseover', function() {
					marker.setImage(imgDir+'sb_icon_over.png');
				});
				GEvent.addListener(marker, 'mouseout', function() {
					marker.setImage(imgDir+'sb_icon_special.png');
				});
				
				this.map.addOverlay(marker);
			}
			else
			{
				GEvent.addListener(marker, 'mouseover', function() {
					marker.setImage(imgDir+'sb_icon_over.png');
				});
				GEvent.addListener(marker, 'mouseout', function() {
					marker.setImage(imgDir+'sb_icon_normal.png');
				});
				
				this.map.addOverlay(marker);
			}
		},
		
		createSpecialMarker: function(lat, lng, title, link, center) {
			var point = new google.maps.LatLng(lat, lng);
			this.createMarkerByPoint(point, title, link, center, true);
		},
		
		createMarker: function(lat, lng, title, link, center) {
			var point = new google.maps.LatLng(lat, lng);
			this.createMarkerByPoint(point, title, link, center, false);
		},
		
		createDraggableMarker: function(lat, lng, callback) {
			var marker = new GMarker(this.center, {draggable: true, icon: this.defaultIcon, title: 'drag this marker!'});
			GEvent.addListener(marker, "dragstart", function() {
				marker.closeInfoWindow();
			});
			GEvent.addListener(marker, "dragend", function() {
				var point = marker.getPoint();
				callback(point);
				marker.openInfoWindowHtml("dropped at "+point);
			});
			this.map.addOverlay(marker);
			return marker;
		}
	});
	
	if (window['onSpongeBobMap'])
		onSpongeBobMap();
});
