//TODO
//
//Umkreissuche:
//
//- live system klassen anlegen


var admin_map='none';
var search_map='none';
var location_map='none';
var shadow_map='none';

function ErrorControl(){}
ErrorControl.prototype = new GControl;
ErrorControl.prototype.initialize = function(map){
	log("initializing error control");
	var me = this;
	log("0");
	me.panel = new Element("div", {
		'id' : "error-control",
		'class' : "error-control"
	});
	log("3");
	map.getContainer().appendChild(me.panel);
	GEvent.addDomListener(me.panel, "click", function(){
		map.removeControl(me);
	})
	log("returning");
	return me.panel;
};
ErrorControl.prototype.getDefaultPosition = function() {
		return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(30, 100));
};
ErrorControl.prototype.getPanel = function() {
	return me.panel;
};

var CHeim = new Class({

	initialize: function(geodata, title, desc, link, image, free, customer){
		this.point = splitGeoData(geodata);
		this.title = title;
		this.desc = desc;
		this.link = link;
		this.image = image;
		this.free = free;
		this.customer = customer;
	},
	
	createMarker: function(){
		if (this.point != null) {
//			log("creating marker");
			var marker = new GMarker(this.point, {
				title: this.title,
				icon: createHeimIcon(this.customer)
			});
			marker.html = '<h3>'+this.title+'</h3>';
			marker.html += '<div class="map-info-box">'
			
			if(this.customer == false){
				// NIX
				marker.html += this.desc+'<br />';
			}else{
				if(this.image != ''){
					marker.html += '<img src="'+this.image+'" class="marker-image"/>';
				}
				marker.html += '<p>';
				marker.html += this.desc+'<br />';

				if(this.free == 'true'){
					marker.html += '<span class="free">Freie Plätze vorhanden</span><br />';
				}else if(this.free == 'false'){
					marker.html += '<span class="not-free">Keine freien Plätze vorhanden</span><br />';
				}
			}
			
			marker.html += '<a href="'+this.link+'">Zur Einrichtung</a>';
			marker.html += '</p>';
			marker.html += '<hr style="clear:both;"/>';
			marker.html += '</div>';
				
			GEvent.addListener(marker, "click", function(){
				log(this);
				this.openInfoWindowHtml(this.html,{
					maxWidth: 300
				});
			});
			return marker;
		}else{
			log("no point");
			return null;
		}
	}
	
});

function formatGeoData(point){
	return "LAT=["+point.lat()+"]:LNG=["+point.lng()+"]";
}

function is_valid_geo_string(str){
	valid = str.test('^LAT=\[[0-9]{1,3}\.?[0-9]{0,10}\]:LNG=\[[0-9]{1,3}\.?[0-9]{0,10}\]$');
	if(!valid){
		log('WARNING: invalid geo string!');
	}
	return valid;
}

function splitGeoData(str){
	
	if(!is_valid_geo_string(str)){
		return null;
	}
	
//	log(str);
	
	parts = str.split(":");
	lat = parts[0].replace("LAT=", "").replace("LNG=", "").replace('\[', "").replace('\]', "");
//	log(lat);
	lng = parts[1].replace("LAT=", "").replace("LNG=", "").replace('\[', "").replace('\]', "");
//	log(lng);
	
	return new GLatLng(lat,lng);
}

function createHeimIcon(customer){
	icon = new GIcon(G_DEFAULT_ICON);
	if(customer == false){
		icon.image = "/extension/r_geo_google/design/standard/images/einrichtung_g.gif";
	}else{
		icon.image = "/extension/r_geo_google/design/standard/images/einrichtung_b2.gif";
	}
	icon.iconSize = new GSize(22,20);
//	icon.imageMap =
	icon.shadow = null;
	//	icon.shadowSize = new GSize(6,20);
	icon.iconAnchor = new GPoint(11,10);
	return icon;
}