window.addEvent('domready', function(){
	
	/* OBJEKTE */
	var Geocode = $('Geocode');
	var GeocodeSubmit = $('GeocodeSubmit');
	var MapContainer = $('MapContainer');
	
	var Map = null;
	var UserMarker = null;
	
	/* GMAP INIT */
	
	Map = new GMap2(MapContainer);
	Map.setCenter(new GLatLng(51.165691, 10.451526), 5);
	
	// Settings
	Map.enableContinuousZoom();
	Map.enableScrollWheelZoom();
		
	Map.addControl(new GMenuMapTypeControl());
	Map.addControl(new GLargeMapControl());
	
	// Geocoder
	var Geocoder = new GClientGeocoder();
	Geocoder.setBaseCountryCode('DE');
	
	// Base Image
	var bIcon = new GIcon();
	bIcon.image = CJGMap_Icon;
	bIcon.shadow = CJGMap_Shadow;
	bIcon.transparent = CJGMap_Transparent;
	bIcon.iconSize = new GSize(32, 32);
	bIcon.shadowSize = new GSize(32, 32);
	bIcon.iconAnchor = new GPoint(16, 34);
	bIcon.infoWindowAnchor = new GPoint(30, 8);
	
	var uIcon = new GIcon(bIcon);
	uIcon.image = CJGMap_UserIcon;
	/* INIT ENDE */
	
	/* MARKER MNGR*/
	
	var Manager = new MarkerManager(Map);
		
	/* AJAX */
	
	var LoadOverlays = new CJAjax(
	CJPath + 'cache/ajax/',
	{
		OnComplete: function(content){
			if(content != 'ERROR'){
				var xml = toXML(content);
				xml = xml.documentElement;
				
				var i;
				var Marker;
				var a = new Array();
				
				var string = null;
				for(i=0; i<xml.childNodes.length; i++){
					Marker = new GMarker(new GLatLng(xml.childNodes[i].childNodes[7].firstChild.nodeValue, xml.childNodes[i].childNodes[8].firstChild.nodeValue), { 
						icon: bIcon,
						title: xml.childNodes[i].childNodes[1].firstChild.nodeValue,
						draggable: false
					});
					string = '<p class="usrdetinf" style="padding-bottom:0;">'+ xml.childNodes[i].childNodes[1].firstChild.nodeValue +' :: <a href="'+ CJPath +'cache/details/[cache:'+ xml.childNodes[i].childNodes[0].firstChild.nodeValue +']"><span class="username">'+ xml.childNodes[i].childNodes[9].firstChild.nodeValue +'</span></a>';
						
					string += '<br /><br />Typ: ' + xml.childNodes[i].childNodes[4].firstChild.nodeValue + ' (Difficulty: '+ xml.childNodes[i].childNodes[2].firstChild.nodeValue +'; Terrain: '+ xml.childNodes[i].childNodes[3].firstChild.nodeValue +')';
					string += '<br /><br />Durchschnitt: '+ xml.childNodes[i].childNodes[5].firstChild.nodeValue.toString() +'/10';
					string += '<br />Anzahl Bewertungen: '+ xml.childNodes[i].childNodes[6].firstChild.nodeValue.toString(); + '</p>';
					
					Marker.bindInfoWindowHtml(string);
					
					a.push(Marker);
					string = null;
						
					Marker = null;
				}
				
				Manager.addMarkers(a, 9);
			}else{
				alert("Oha, da ist wohl was schief gegangen. Der Fehler wurde bei uns in der Datenbank gespeichert, wenn Du magst kannst Du uns aber trotzdem noch einen Bugreport senden. Danke!");
			}
		}
	});
		
	LoadOverlays.AddParameter('call', 'GetMapOverlays');
	//alert(LoadOverlays.GetURL());
	LoadOverlays.CallFull();
	
	/* ENDE AJAX */
	
	/* KRAM */
	
	// Style setzen
	function SetStyle(Element, valid){
		if(valid){
			Element.style.backgroundColor='#FFFFFF';
			Element.style.color='#4D610C';
		}else{
			Element.style.backgroundColor='#F89E00';
			Element.style.color='#FFF';
		}
	}
	
	/* ENDE KRAM */
	
	/* EVENTS */
	
	
	try{
		if($('ShowPosition')!=null){
			$('ShowPosition').addEvent('click', function(obj){
				ShowUser($('ShowPosition').checked);
			});
		}
		
		if($('JumpToUser')!=null){
			$('JumpToUser').addEvent('click', function(obj){
				if(!$('ShowPosition').checked){
					$('ShowPosition').checked=true;
					ShowUser(true);
				}
				MoveToUserCoords();
				UserMarker.showMapBlowup()
			});
		}
	}catch(e){}
	
	
	
	Geocode.addEvent('keyup', function(c){	
		if(Geocode.value.length > 0){
			if(Geocode.value.length<3){
				SetStyle(Geocode, false);	
			}else{
				SetStyle(Geocode, true);
				
				if(kc(c)==13){
					Geocoder.getLatLng(Geocode.value, MoveByGeocode);
					InfoMarker.closeInfoWindow();
					InfoMarker.hide();
				}
			}
		}else{
			SetStyle(Geocode, true);
		}
	});
	
	GeocodeSubmit.addEvent('click', function(){
		if(Geocode.value.length>=3){
			Geocoder.getLatLng(Geocode.value, MoveByGeocode);
			InfoMarker.closeInfoWindow();
			InfoMarker.hide();
		}
	});
	
	var MoveByGeocode = function(LatLng){
		Map.setCenter(LatLng, 9);
	}
	
	var MoveToUserCoords = function(){
		if(UserLat != 0 && UserLng != 0){
			MoveByGeocode(new GLatLng(UserLat, UserLng));
		}
	}
	
	/* Ende suche */
	
	/* IWindow */
	// TODO: größeres bild etc.
	var InfoMarker = new GMarker(Map.getCenter(), {icon: bIcon, title: 'Kleines Helferlein'});
	Map.addOverlay(InfoMarker);
	
	InfoMarker.openInfoWindowHtml('<b>Ich bin das kleine Helferlein...</b><br /><br />Um Caches zu finden kannst Du oben einfach einen Ort eingeben, die Karte springt dann dorthin<br />und zeigt vorhandene Caches an. Du kannst die Karte auch mit der Maus verschieben und mit <br />dem Mausrad oder der Steuerung (links) zoomen. Caches werden jedoch erst ab einer relativ <br />nahen Zoomstufe angezeigt. Probiers einfach aus ;)<br /><br />Übrigens, die kleinen cachejudges kann man auch anklicken ;)');
		
	/* ENDE */
	
	/* Google Events */
	
	GEvent.addListener(Map, 'click', function(){
		InfoMarker.closeInfoWindow();
		InfoMarker.hide();
	});
	
	/* Ende */
	
	/* Eigenen Benutzerpunkt anzeigen */
	function ShowUser(show){
		if(show && (UserLat != 0) && (UserLng != 0)){
			UserMarker = new GMarker(new GLatLng(UserLat, UserLng), { 
				icon: uIcon,
				title: 'Deine Position',
				draggable: false
			});
			var string = '';
			string = '<p class="usrdetinf" style="padding-bottom:0;">Deine Position :: <a href="'+ CJPath +'user/profile/"><span class="username">'+ UserGC +'</span></a>';
			
			string += '<br /><br />Latitude: ' + UserLat;
			string += '<br />Longitude: '+ UserLng +'</p>';
			
			UserMarker.bindInfoWindowHtml(string);
			Map.addOverlay(UserMarker);
		}else{
			Map.removeOverlay(UserMarker);
		}
	}
	
});