/* Google Maps Karte einbinden */

var map;

if(land == "DE") {
    var kml_file = "http://www.opencaching.de/search.php?searchto=searchbydistance&showresult=1&expert=0&output=HTML&utf8=1&sort=bydistance&orderRatingFirst=0&f_userowner=0&f_userfound=0&f_inactive=1&f_ignored=0&country=&cachetype=&cache_attribs=59&cache_attribs_not=7&latNS=N&lat_h=" + lat_h + "&lat_min=" + lat_min + "&lonEW=E&lon_h=" + lon_h + "&lon_min=" + lon_min + "&distance=100&unit=km&output=kml&zip=1&count=max";
}
else {
    var kml_file = "http://www.opencaching.de/search.php?searchto=searchbyname&showresult=1&expert=0&output=HTML&utf8=1&sort=bydistance&orderRatingFirst=0&f_userowner=0&f_userfound=0&f_inactive=1&f_ignored=0&country=" + land + "&cachetype=&cache_attribs=59&cache_attribs_not=7&cachename=%25&output=kml&zip=1&count=max";
}

function initialize() {
   if (GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById("googlemap"));
      map.setCenter(new GLatLng(50.708634, 10.327148), 6);
      map.addControl(new GLargeMapControl());
      map.enableDoubleClickZoom(); 
      map.enableContinuousZoom(); 
      map.disableScrollWheelZoom(); 
      map.enableDragging(); 
      var geoXml = new GGeoXml(kml_file, function() {
        if (land == "DE" && geoXml.loadedCorrectly()) {
            geoXml.gotoDefaultViewport(map);
        }
      });
      map.addOverlay(geoXml);
      if(land == "AT") {
        map.setCenter(new GLatLng(47.635783, 14.74365), 6);
      }
      else if(land == "CH") {
        map.setCenter(new GLatLng(46.830133, 8.261718), 7);
      }
    }
    //console.log(kml_file);
}

window.onload = initialize;

/* Suchfeld */
$(document).ready(function() {
	var $suchfeld = $("div.map_container div.googlemap_searchfield input");
	// onClick --> leeren
	$suchfeld.bind("click",function() { 
		$(this).val(""); 
	});
	$suchfeld.bind("blur",function() {
		if(jQuery.trim($(this).val()) == "") {
			$(this).val("Gehe zu ...");
		}
	});
	// onEnter --> suchen
	$suchfeld.bind("keypress",function(e) {
		if(e.keyCode == 13 && jQuery.trim($suchfeld.val()) != "") {
			$("div.map_container").append("<div class='ajax_loader'><img src='images/ajax-loader.gif' alt='Loading...' /></div>");
			$.ajax({
				 url: "ajax/findGeo.php"
				,typ: "GET"
				,data: { suche: $suchfeld.val() }
				,dataType: "json"
				,success: function(data,status) {
					if(status == "success") {
						if(data.http_status == 200) {
							// Marker setzen
							var myIcon 				= new GIcon(G_DEFAULT_ICON);
							myIcon.image 			= "images/home.png";
							myIcon.iconSize 		= new GSize(22,22);
							myIcon.shadow			= "";
							var myPos = new GLatLng(parseFloat(data.lat), parseFloat(data.lng));
							var myPin = new GMarker(myPos,myIcon);
							map.addOverlay(myPin);
							// Zentrieren
							map.setCenter(myPos,12);
						}
					}
				 	$("div.ajax_loader").remove();
				 }
				 ,error: function () { 
					$("div.ajax_loader").remove();
				 	alert("Deine Suche ergab leider keinen Treffer.");
				 }
			});
		}
	});
});

function changeView(value) {
	var location = "karte.php";
	if(value == "AT") {
		location += "?land=AT";
	}
	else if(value == "CH") {
		location += "?land=CH";
	}
	else {
		location += "?stadt=" + value;
	}
	window.location.href = location;
}
