/* basic.js */

var jrkmap;

var GMapJRK = Class.create();
GMapJRK.prototype = {
	initialize: function(adr) {
		this.adr = adr;
		if (GBrowserIsCompatible()) {
			this.map = new GMap2($('map'));
			this.map.addControl(new GSmallMapControl());
			this.map.addControl(new GMapTypeControl());
			this.geocoder = new GClientGeocoder();
			this.init();
			this.addPointByAddress(adr);
		}
	},
	_ERROR_NOTFOUND: 'Leider konnte keine Adresse zu Deiner Eingabe gefunden werden.',
	_KVHTML: '<p class="bubble">Deutsches Rotes Kreuz<br/>Kreisverband&nbsp;Hamburg&nbsp;Eimsbüttel&nbsp;e.V.<br/><br/>Monetastr. 3<br/>20146 Hamburg</p>',
	_LINECOLOR: '#FF0000',
	_LINEWIDTH: 5,
	_TIMEOUT: 5000,
	init: function() {
		this.icon = new GIcon();
		this.icon.image = "http://www.jrk-gl.de/img/marker_drk.png";
		this.icon.shadow = "http://www.jrk-gl.de/img/marker_drk.png";
		this.icon.iconSize = new GSize(37, 37);
		this.icon.shadowSize = new GSize(37, 37);
		this.icon.iconAnchor = new GPoint(6, 20);
		this.icon.infoWindowAnchor = new GPoint(5, 1);


		this.map.setCenter(new GLatLng(0,0),0);
		this.bounds = new GLatLngBounds();
		this.point  = new GLatLng(53.56963813783612, 9.976487159729004);
		this.addBound(this.point);
		this.marker = this.createMarker(this.point, this._KVHTML, true);
		this.map.addOverlay(this.marker);
		this.marker.openInfoWindowHtml(this._KVHTML);
	},
	addBound: function(p) {
		this.bounds.extend(p);
	},
	addLine: function(p) {
		var polyline = new GPolyline([this.point, p], this._LINECOLOR, this._LINEWIDTH);
		this.map.addOverlay(polyline);
	},
	addPointByAddress: function(adr) {
		Element.show('gmap');
		adr = (adr.toLowerCase().indexOf('deutschland') != -1)?(adr):(adr + ', Deutschland');
		this.geocoder.getLocations(
			adr, 
			this.__handleGetLocationsResponse.bind(this, adr));
	},
	createMarker: function(point, html) {
		var marker = (arguments[2] && arguments[2] == true)?(new GMarker(point, { icon: this.icon })):(new GMarker(point));
		GEvent.addListener(marker, 'click', function() {
			marker.openInfoWindowHtml(html);
		});
		return marker;
	},
	__handleGetLatLngResponse: function(adr, point) {
		if (!point) {
			this.showError(this._ERROR_NOTFOUND, this._TIMEOUT);
			Element.hide('gmap');
			Element.show('ginput');
		} else {
			Element.show('gmap');
			var marker = this.createMarker(point, adr);
			this.map.addOverlay(marker);
			this.addLine(point);
			this.addBound(point);
			this.setCenter();
		}
	},
	__handleGetLocationsResponse: function(adr, result) {
		this.map.clearOverlays();
		this.init();
		if (result.Status.code == 200) {
			if (result.Placemark.length == 0) {
				this.showError(this._ERROR_NOTFOUND, this._TIMEOUT);
				Element.hide('gmap');
				Element.show('ginput');
			} else { 
				if (result.Placemark.length > 1) {
					Element.hide('gmap');
					var msg = 'Bitte wähle Deine Adresse aus:<ul>';
					for (var i=0; i<result.Placemark.length; i++)
						msg += '<li><a href="javascript:setAddress(\'' + result.Placemark[i].address + '\')">' + result.Placemark[i].address + '</a></li>';
					
					msg += '</ul>';
					msg = msg.replace(/Germany/g, 'Deutschland');
					this.showMessage(msg);
				} else {
					var msg = '';
					adr = result.Placemark[0].address.replace(/Germany/g, 'Deutschland');
					$('adr').value = adr;
					this.geocoder.getLatLng(
						adr, 
						this.__handleGetLatLngResponse.bind(this, adr));
				}
			}
		} else {
			this.showError(this._ERROR_NOTFOUND, this._TIMEOUT);
			Element.hide('gmap');
			Element.show('ginput');
		}		
	},
	setCenter: function() {
		this.map.setZoom(this.map.getBoundsZoomLevel(this.bounds));
		this.map.setCenter(this.bounds.getCenter());
	},
	showError: function(msg) {
		$('error').innerHTML = msg;
		Effect.Appear('error');
		if (arguments[1])
			window.setTimeout(this.hideError.bind(this), arguments[1]);
	},
	showMessage: function(msg) {
		$('message').innerHTML = msg;
		Effect.Appear('message');
		if (arguments[1])
			window.setTimeout(this.hideMessage.bind(this), arguments[1]);
	},
	hideError: function(msg) {
		Effect.Fade('error');
		$('error').innerHTML = '';
	},
	hideMessage: function(msg) {
		Effect.Fade('message');
		$('message').innerHTML = '';
	}
}

function showContactData() {
	Element.hide('ginput');
	Element.hide('gmap');
	Element.show('address');
}

function showInput() {
	Element.hide('address');
	Element.hide('gmap');
	Element.show('ginput');
}

function showMap() {
	Element.hide('address');
	Element.hide('ginput');
	Element.show('gmap');
	var adr = $('adr').value;
	initMap(adr);
}

function initMap(adr) {
	jrkmap = new GMapJRK(adr);
}

function setAddress(adr) {
	jrkmap.addPointByAddress(adr);
	jrkmap.hideMessage();
}



