myMarker={};
window.name="willingenMap";
window.focus();

var myLocation;
gmarkers=[];

// Create marker icon 
var baseIcon = new GIcon();
baseIcon.image = "mm_20_red.png";
baseIcon.shadow = "mm_20_shadow.png";
baseIcon.iconSize = new GSize(12, 20);
baseIcon.shadowSize = new GSize(22, 20);
baseIcon.iconAnchor = new GPoint(6, 20);
baseIcon.infoWindowAnchor = new GPoint(1, 1);
//baseIcon.imageMap = [4,0,0,4,0,7,3,11,4,19,7,19,8,11,11,7,11,4,7,0]; 
baseIcon.transparent = "mm_20_transparent.png";

function load()
{
  if (GBrowserIsCompatible())
  {
    var map = new GMap2(document.getElementById("map"));
    myMap=map;
    geocoder = new GClientGeocoder();

    /* #### adding controls to the map #### */
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GOverviewMapControl());
    map.addControl(new GScaleControl());
    
    /* koordinaten bestimmen */
    GEvent.addListener(map, "click", function(market, point) {
      if(point)
      {
        var center = map.getCenter();
        document.getElementById("breite").value = point.x;
        document.getElementById("laenge").value = point.y;

/*		bei klick marker setzen und vorher alle anderen entfernen 
		map.clearOverlays(); 
		map.addOverlay(new GMarker(point,'green'));
		map.panTo(point);
*/
		
      }
      if(market)
      {
        document.getElementById("breite").value = market.getPoint().lng();
        document.getElementById("laenge").value = market.getPoint().lat();
      }
    })

	/* google map anzeigen */
	//map.setCenter(new GLatLng(51.29402192643134,8.61027717590332), 15);
	//map.setCenter(new GLatLng(51.29214336812802,8.638601303100586), 14);
	
		map.setCenter(new GLatLng(51.2953637067335,8.627443313598633), 14);
	
	/* G_NORMAL_MAP, G_SATELLITE_MAP und G_HYBRID_MAP */
		//map.setMapType(G_NORMAL_MAP); 
		map.setMapType(G_SATELLITE_MAP); 
		
		
// hier einzelnen gastgeber anzeigen...
if ( gastgeberdaten ) {
	map.setMapType(G_HYBRID_MAP); 
	var koordinaten = gastgeberdaten[3].split(",");
	var point = new GLatLng(koordinaten[0],koordinaten[1]);
	var marker = createTabbedMarker(point, [gastgeberdaten[0]+gastgeberdaten[1]+gastgeberdaten[2],gastgeberdaten[4],gastgeberdaten[5]],["Adresse","Lastminute","Anfrage"]);
	map.addOverlay(marker);
	map.setCenter(new GLatLng(koordinaten[0],koordinaten[1]), 16);
}


// wenn parameter all = yes dann alle gastgeber laden...

if ( !getURLParam('id') && !getURLParam('standort')  && !getURLParam('freizeit')) {
  		window.setTimeout("showLocationAll()",100);
}


if ( getURLParam('standort') ) {
	myMap.setMapType(G_HYBRID_MAP); 
	myMap.clearOverlays();
	i = getURLParam('standort');
	MarkerImage = standorte[i][6];
	iconWidth = standorte[i][7];
	iconHeight = standorte[i][8];
	var point = new GPoint(standorte[i][1],standorte[i][2]);
	var marker = createMarker(point, '<div style="width:300px;font-size:1em;font-weight:bold;">'+standorte[i][3]+'</div><div style="width:300px;font-size:0.75em">'+standorte[i][5]+'</div><div style="width:300px;font-size:0.75em"><a href="'+standorte[i][4]+'" target="_blank">'+standorte[i][4]+'</a></div>','hotel','exklusiv',1,32,32);
	marker.type = MarkerImage;
	myMap.addOverlay(marker);
	map.setCenter(new GLatLng(standorte[i][2],standorte[i][1]), 16);
  
}	else if ( getURLParam('freizeit') ) {
	myMap.setMapType(G_HYBRID_MAP); 
	myMap.clearOverlays();
	i = getURLParam('freizeit');
	MarkerImage = freizeit[i][6];
	iconWidth = freizeit[i][7];
	iconHeight = freizeit[i][8];
	var point = new GPoint(freizeit[i][1],freizeit[i][2]);
	var marker = createMarker(point, '<div style="width:300px;font-size:1em;font-weight:bold;">'+freizeit[i][3]+'</div><div style="width:300px;font-size:0.75em">'+freizeit[i][5]+'</div><div style="width:300px;font-size:0.75em"><a href="'+freizeit[i][4]+'" target="_blank">'+freizeit[i][4]+'</a></div>','hotel','exklusiv',1,32,32);
	marker.type = MarkerImage;
	myMap.addOverlay(marker);
	map.setCenter(new GLatLng(freizeit[i][2],freizeit[i][1]), 17);
	window.setTimeout("webcamEinblenden()",50);
  
} else {

		window.setTimeout("standorteEinblenden()",50);
		window.setTimeout("freizeitEinblenden()",50);
		window.setTimeout("webcamEinblenden()",50);
}	

}


  if(window.location.search){
    var QS=window.location.search.replace(/\?/,"");
    if (standorte[QS]){
      myClick(QS);
      toggle('sucheTitel','suche')      
    }
  }

}

// Creates a marker whose info window displays the given number
function createMarker(point, number, color, typ, window, iconWidth, iconHeight)
{

  if (!iconHeight) {iconHeight = 20;}
  if (!iconWidth) {iconWidth = 12;}
  var icon = new GIcon(baseIcon);
  icon.image = "mm_20_"+color+".png";
  icon.iconSize = new GSize(iconWidth, iconHeight);
  icon.infoWindowAnchor = new GPoint(10, 7);
  var marker = new GMarker(point, icon);
  gmarkers.push(marker);
  marker.type = typ; 
  if(window)
  {
    // Show this markers index in the info window when it is clicked
    var html = number;
    GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });
  }
  return marker;
}




function showLocation()
{
  var address = document.getElementById("strasse").value+","+document.getElementById("plz").value+" "+document.getElementById("ort").value+","+document.getElementById("land").value;
  geocoder.getLocations(address, GeoCoordinates);
}



function GeoCoordinates(response)
{
  if (!response || response.Status.code != 200)
  {
    alert("Entschuldigung, es wurde kein passender Ort gefunden!");
  }
  else
  {
    place = response.Placemark[0];

    geoL = place.Point.coordinates[1];
    geoB = place.Point.coordinates[0];
    address = place.address;

    point = new GLatLng(geoL,geoB);
    myMap.panTo(point);  

    if(myLocation) myMap.removeOverlay(myLocation);
    myLocation = createMarker(point, '', 'red', 'myLocation', '');
    myMap.addOverlay(myLocation);
	
  }
}


// #################################################
// speichern der geodaten
// 

function saveAddress() {
		geocoder = new GClientGeocoder();
    var xx = 1;
    for(i in adresse){
    	var address = adresse[i][1];
    	var gid = adresse[i][0];
    	getCoords(address, gid);
    	xx++;
		}    
}

function getCoords(address, gid) {    

    var icon = new GIcon(baseIcon);
		icon.image = "mm_20_hotel.png";
		icon.iconSize = new GSize(32, 32);

		geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              // wenn nicht gefunden...
            } else {
       				//alert(address + ' - ' + gid + ' - ' +point);
              //new Ajax.Request('/cgi-bin/willingen_map.cgi', {asynchronous:true,method:'post', onSuccess:function(t) {}, onFailure:errFunc, postBody:'id=' + gid + '&geo=' + point});
            }

          }
        );
    
}
// #################################################



// #################################################
// hier alle gastgeber anzeigen...
// #################################################
function showLocationAll() {
	
	// cluster nutzen marker werden dynamisch geladen
	var clusterer = new Clusterer( myMap );	
	
	for(i in adresse){
			var point = new GPoint(adresse[i][1],adresse[i][2]);
			var haus = adresse[i][5];
    	var marker = createTabbedMarker(point,[adresse[i][4],'<div id="lastminute"><img src="loading.gif" width="16" height="16" border="0">  <a href="javascript:void(0);" onclick="lastminuteDaten('+adresse[i][0]+')">Klicken Sie hier um Freimeldungen anzuzeigen...</a><br><script type="text/javascript">lastminuteDaten('+adresse[i][0]+');</script></div>',''],["Adresse",'Lastminute','']);
    	myMarker[i]=marker;
     	//myMap.addOverlay(marker);
     	clusterer.AddMarker( marker, haus )
	}
	myMap.setCenter(new GLatLng(51.2953637067335,8.627443313598633), 13);
	document.getElementById('suche1').innerHTML = '<br>Fuer weitere Informationen klicken Sie die Haussymbole mit der Maus an.<br><br>Zoomen Sie mit den Schiebern links weiter in die Karte.';
}


function AdressenGeoCoordinates(response)
{
  if (!response || response.Status.code != 200)
  {
		return;
  }
  else
  {
    place = response.Placemark[0];
    geoL = place.Point.coordinates[1];
    geoB = place.Point.coordinates[0];
    address = place.address;

    point = new GLatLng(geoL,geoB);
    //myMap.panTo(point);  

    //if(myLocation) myMap.removeOverlay(myLocation);
    myLocation[gastgeberID] = createMarker(point, '', 'yellow', 'myLocation', '');
    myMap.addOverlay(myLocation);
    myLocation.openInfoWindowHtml('<div>'+address+'</div>');
    
  }
}


var oldMarker;
function myClick(i){
//	myMap.clearOverlays();
	myMap.setCenter(new GLatLng(standorte[i][2],standorte[i][1]), 16);
	//if(oldMarker) myMap.removeOverlay(oldMarker); 
	oldMarker = myMarker[i];
//	myMap.addOverlay(myMarker[i]);
	myMarker[i].openInfoWindowHtml('<div style="width:300px;font-size:1em;font-weight:bold;">'+standorte[i][3]+'</div><div style="width:300px;font-size:0.75em">'+standorte[i][5]+'</div><div style="width:300px;font-size:0.75em"><a href="'+standorte[i][4]+'" target="_blank" >'+standorte[i][4]+'</a></div>');
}


var oldMarkerFreizeit;
function myClickFreizeit(i){
	myMap.clearOverlays();
	myMap.setCenter(new GLatLng(freizeit[i][2],freizeit[i][1]), 16);
	if(oldMarkerFreizeit) myMap.removeOverlay(oldMarkerFreizeit);
	oldMarkerFreizeit = myMarker[i];
	myMap.addOverlay(myMarker[i]);
	myMarker[i].openInfoWindowHtml('<div style="width:200px;font-size:1em;font-weight:bold;>'+freizeit[i][3]+'</div><div style="width:200px;font-size:0.75em">'+freizeit[i][5]+'</div><div style="width:200px;font-size:0.75em"><a href="'+freizeit[i][4]+'" target=link >'+freizeit[i][4]+'</a></div>');
	
}

var oldMarkerWebcam;
function myClickWebcam(i){
	//myMap.clearOverlays();
	myMap.setCenter(new GLatLng(webcam[i][2],webcam[i][1]), 16);
	if(oldMarkerWebcam) myMap.removeOverlay(oldMarkerWebcam);
	oldMarkerWebcam = myMarker[i];
	myMap.addOverlay(myMarker[i]);
	myMarker[i].openInfoWindowHtml('<div style="width:300px;font-size:1em;font-weight:bold;">'+webcam[i][3]+'</div><div style="width:300px;font-size:0.75em">'+webcam[i][5]+'</div><div style="width:300px;font-size:0.75em"><a href="'+webcam[i][4]+'" target=link >'+webcam[i][4]+'</a></div>');
}

function clearMarkers() {
	myMap.clearOverlays();
}


function Markers(marker) {
  myMap.closeInfoWindow();
  if (document.getElementById(marker).checked==false) {
    for (var i = 0; i < gmarkers.length; i++) {
      if (gmarkers[i].type==marker)  {
        myMap.removeOverlay(gmarkers[i]);
      }
    }
  } else {
    for (var i = 0; i < gmarkers.length; i++) {
      if (gmarkers[i].type==marker)  {
        myMap.addOverlay(gmarkers[i]);
      }
    }
  }
}


var oldMarker1;

// hier wichtige standorte anzeigen
function standorteEinblenden() {
	
	//myMap.clearOverlays();
	for(i in standorte){
	  var iconWidth,iconHeight;
		if ( !standorte[i][6] ) {
			MarkerImage = "red"; 
		} else {
			MarkerImage = standorte[i][6];
			iconWidth = standorte[i][7];
			iconHeight = standorte[i][8];
		}
		var point = new GPoint(standorte[i][1],standorte[i][2]);
		//var marker = createMarker(point, '<div style="width:200px;font-size:0.85em">'+standorte[i][3]+'</div><div style="width:200px;font-size:0.75em">'+standorte[i][5]+'</div><div style="width:200px;font-size:0.75em"><a href="'+standorte[i][4]+'" target=link>'+standorte[i][4]+'</a></div>',MarkerImage,'exklusiv',1,iconWidth,iconHeight);
		var marker = createMarker(point, '<div style="width:300px;font-size:1em;font-weight:bold;">'+standorte[i][3]+'</div><div style="width:300px;font-size:0.75em">'+standorte[i][5]+'</div><div style="width:300px;font-size:0.75em"><a href="'+standorte[i][4]+'" target="_blank">'+standorte[i][4]+'</a></div>',MarkerImage,'exklusiv',1,iconWidth,iconHeight);
		marker.type = MarkerImage;
		myMarker[i]=marker;
		oldMarker1 = myMarker[i];
		myMap.addOverlay(marker);
    }
// willingen zentrieren
	//myMap.setCenter(new GLatLng(51.29402192643134,8.61027717590332), 15);
}



function freizeitEinblenden() {
	//myMap.clearOverlays();
	for(i in freizeit){
		var point = new GPoint(freizeit[i][1],freizeit[i][2]);
	    var marker = createMarker(point, '<div style="width:300px;font-size:1em;font-weight:bold;">'+freizeit[i][3]+'</div><div style="width:300px;font-size:0.75em">'+freizeit[i][5]+'</div><div style="width:300px;font-size:0.75em"><a href="'+freizeit[i][4]+'" target="_blank">'+freizeit[i][4]+'</a></div>','green','exklusiv',1);
    	myMarker[i]=marker;
      	myMap.addOverlay(marker);
    }
// willingen zentrieren
	//myMap.setCenter(new GLatLng(51.29402192643134,8.61027717590332), 15);
}



function webcamEinblenden() {
	//myMap.clearOverlays();
	for(i in webcam){

		var iconWidth,iconHeight;
		if ( !webcam[i][6] ) {
			MarkerImage = "webcam"; 
		} else {
			MarkerImage = webcam[i][6];
			iconWidth = webcam[i][7];
			iconHeight = webcam[i][8];
		}

		var point = new GPoint(webcam[i][1],webcam[i][2]);
	    var marker = createMarker(point, '<div style="width:300px;font-size:1em;font-weight:bold;">'+webcam[i][3]+'</div><div style="width:300px;font-size:0.75em">'+webcam[i][5]+'</div><div style="width:300px;font-size:0.75em"><a href="'+webcam[i][4]+'" target=link >'+webcam[i][4]+'</a></div>',MarkerImage,'exklusiv',1,iconWidth,iconHeight);
    	myMarker[i]=marker;
      myMap.addOverlay(marker);
    }
// willingen zentrieren
	//myMap.setCenter(new GLatLng(51.29402192643134,8.61027717590332), 15);

}


// ==================================================
      // A function to create a tabbed marker and set up the event window
      // This version accepts a variable number of tabs, passed in the arrays htmls[] and labels[]
function createTabbedMarker(point,htmls,labels) {
		var icon = new GIcon(baseIcon);
		icon.image = "mm_20_hotel.png";
		icon.iconSize = new GSize(32, 32);

		var marker = new GMarker(point,icon);
        GEvent.addListener(marker, "click", function() {
          // adjust the width so that the info window is large enough for this many tabs
          if (htmls.length > 2) {
            htmls[0] = '<div style="width:380px;">' + htmls[0] + '</div>';
          }
          var tabs = [];
          for (var i=0; i<htmls.length; i++) {
            tabs.push(new GInfoWindowTab(labels[i],htmls[i]));
          }
			marker.openInfoWindowTabsHtml(tabs);
        });
        return marker;
}
// ==================================================
	  
	  
	  
function gastgeberAnzeigen(gastgeberdaten) {

		// hier einzelnen gastgeber anzeigen....
		if ( gastgeberdaten ) {
			var koordinaten = gastgeberdaten[3].split(",");
			var point = new GLatLng(koordinaten[0],koordinaten[1]);
		//	alert(gastgeberdaten[0] + " ---- " + gastgeberdaten[1]);
			var marker = createTabbedMarker(point, [gastgeberdaten[0]+gastgeberdaten[1]+gastgeberdaten[2],gastgeberdaten[4],gastgeberdaten[5]],["Adresse","Lastminute","Anfrage"]);
		    marker.type = "exklusiv";
			myMap.addOverlay(marker);
			myMap.setCenter(new GLatLng(koordinaten[0],koordinaten[1]), 16);
		}
		// hier einzelnen gastgeber anzeigen....

}	  


function lastminuteDaten(gid) {
	new Ajax.Request('/cgi-bin/willingen_map.cgi', {asynchronous:true,method:'post', onSuccess:function(t) {document.getElementById("lastminute").innerHTML = t.responseText;}, onFailure:errFunc, postBody:'lastminute=' + gid });
}