  var geocoder;
  var map;
  var addresses = new Array();
  var addressesForWindow = new Array();
  var your_coords = '';
  var markersArray = new Array();

  function load() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("ID_344_exploder_map"));

        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl());
        map.enableScrollWheelZoom();
        geocoder = new GClientGeocoder();
        

        prepareMarker();

        if(markersArray.length) {
          var cluster = new ClusterMarker(map, { markers:markersArray } );
          cluster.fitMapToMarkers();
          cluster.intersectPadding = 25;
          cluster.clusterMarkerTitle='%count ' + 'Einträge';
        }
        setViewPoint();
      }
  }
  
  gotoAddress = function() {
	  address = $('form_location_select').value;
	  findLocationByAddress(address);
	}
	
	findLocationByAddress = function(address) {
	    var geocoder = new GClientGeocoder();
	    geocoder.getLatLng(address, function (point) {
	      if(point) {
	        map.setCenter(point, 11);
	      } else {
	        alert('Adresse konnte nicht gefunden werden');
	      }
	    });
	}
  
  
  function setViewPoint() {
  	map.setCenter(new GLatLng(47.420159, 13.139648) , 7);
  }


  Array.prototype.foreach = function (callback) {
    for(var k=0; k<this.length; k++) {
      callback(k, this[k]);
    }
  }


  function prepareMarker() {
    var coordarray = null;
    var point = null;
    var icon_src = null;

    if (geocoder) {
      coords.foreach( function (k, v) {
        //Werte aus dem Array temporär in Variablen speichern (notwendig für die Callback-Funktion)
        if(coords[k]) {

          coordarray = coords[k].split(",");
          if (coordarray[0] != 0 && coordarray[1] != 0 && coordarray[0] && coordarray[1]) {
            point = new GLatLng(coordarray[0],coordarray[1]);
						icon_src = cats[k];
            marker = createMarker(point, icon_src, k);
            markersArray.push(marker);
          }
        }
      });
    }
  }
 
  function createMarker(point, icon, key) {
    var marker=null;
    var icon_obj = new GIcon(G_DEFAULT_ICON, icon);
    icon_obj.iconSize=new GSize(39, 34);
    marker = new GMarker(point, icon_obj);

    if(key) {
      GEvent.addListener(marker, "click", function() {
        new Ajax.Request("/exploder/get_user_data.php?key="+key, {
          method: 'get',
          onSuccess: function(transport) {
            if (transport.responseText) {
              marker.openInfoWindowHtml(transport.responseText);
            } else {
              marker.openInfoWindowHtml('Fehler beim Auslesen');
            }
          }
        });
      });
    }
    return marker;
  }
