// Variablen initialisieren

var map = null;
var startZoom = 12;
var searchZoom = 15;
var geocoder = null;
var showpoint = null;
var path = "http://www.dsltester.de/images/speedtest/";

// Standard Marker erstellen

var dslIcon = new GIcon(G_DEFAULT_ICON);
dslIcon.image = path + "dsltester.png";	
dslIcon.iconSize = new GSize(16, 16);
dslIcon.shadow = path + "shadow.png";
dslIcon.shadowSize = new GSize(32, 32);

// Individuelle Markersymbole erstellen

var IMAGES = [ "", "kabelbw", "kabeldeu", "unitymedia", "1und1", "alice", "arcor", "congstar", "freenet", "o2", "strato", "tele2", "t-home", "versatel", "vodafone", "ewe-tel", "moobicent" ];
var ICONS = [];

for (var i = 0; i < IMAGES.length; i++) {
	if (!ICONS[i]) {
		var icon = new GIcon();
		icon.image = path + IMAGES[i] + ".png";
		icon.iconAnchor = new GPoint(16, 16);
		icon.infoWindowAnchor = new GPoint(16, 0);
		icon.iconSize = new GSize(16, 16);
		icon.shadow = path + "shadow.png";
		icon.shadowSize = new GSize(32, 32);
		ICONS[i] = icon;
	}	
}

// Karte initialisieren

function init() {
	map = new GMap2(document.getElementById("map"));
	map.enableDoubleClickZoom();
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(document.getElementById("lat").value, document.getElementById("lon").value), startZoom);
	updateMarkers();
	GEvent.addListener(map,'zoomend',function() {
		updateMarkers();
	});
	
	GEvent.addListener(map,'moveend',function() {
		updateMarkers();
	}
	
	);
	// Geocoder initialisieren
	geocoder = new GClientGeocoder();
}

function init2() {
	map = new GMap2(document.getElementById("map"));
	map.enableDoubleClickZoom();
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(document.getElementById("lat").value, document.getElementById("lon").value), startZoom);
	GEvent.addListener(map,'zoomend',function() {
		updateMarkers();
	});
	
	GEvent.addListener(map,'moveend',function() {
		updateMarkers();
	}
	
	);
	// Geocoder initialisieren
	geocoder = new GClientGeocoder();
}

// Marker bei Veraenderung aktualisieren (Suche, Zoom, Kartenbewegung

function updateMarkers() {
	
	//vorhandene Punkte entfernen
	map.clearOverlays();
	//Grenzen für die Daten festlegen
	var bounds = map.getBounds();
	var zoomlevel = map.getBoundsZoomLevel(bounds);
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var getVars = 'ne=' + northEast.toUrlValue()
		+ '&sw=' + southWest.toUrlValue()
		+ '&zoom=' + zoomlevel;
	
	$.ajax({
		url: "/ajax/get_marker.php?sid="+sid,
		data: getVars,
		cache: false,
		complete: function(data){
			var jscript = data.responseText;
			var points;
			eval(jscript);
			//Einzelne Punkte der Liste erzeugen
			for (i in points) {
				var point = new GLatLng(points[i].lat,points[i].lon);
									
				var marker = createMarker(point,points[i].p,points[i].url,points[i].t,points[i].turl,points[i].count,points[i].city,points[i].down,points[i].up,points[i].ping,points[i].icon);
				//var marker = new GMarker(point);
				map.addOverlay(marker);
					
				// Info-Window über Punkt öffnen
				/*if (showpoint != null) {
					if (showpoint.y == points[i].lat && showpoint.x == points[i].lon) {
						marker.openInfoWindowHtml(points[i].infopopup);
					}
				}*/
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			errors = errors + 1;	// A error has happened, test is not submittable
		}
	});
}

// Marker erstellen

function createMarker(point,p,url,t,turl,count,city,down,up,ping,icon) {
	
	markerOptions = { icon:dslIcon };
	
	// Optionen fuer den Marker
	if (icon!="") {
		marker_icon = ICONS[icon]; 
		if (marker_icon) {
			markerOptions = { icon:marker_icon };
		}
	}
		
	var marker = new GMarker(point,markerOptions);
	GEvent.addListener(marker, 'click', function() {
		var markerHTML = '<strong>'+city+'</strong><br/><br/><strong>'+count+'</strong> Messung(en)<br/>Down: <strong>'+down+'</strong> kbit/s<br/>Up: <strong>'+up+'</strong> kbit/s<br/>Ping: <strong>'+ping+'</strong> ms';
		if(p != "")
			markerHTML += '<br/><br/>Provider: <strong><a href="http://www.dsltester.de'+url+'">'+p+'</a></strong>';
		if(t != "")
			markerHTML += '<br/>Tarif: <strong><a href="http://www.dsltester.de/dsl/tarifdetails/'+turl+'/">'+t+'</a></strong>';
		marker.openInfoWindowHtml(markerHTML);
	});
	return marker;
}

// Adresse geocodieren und anzeigen

function showAddress() {
  address = document.getElementById("address").value;
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert(address + " nicht gefunden");
        } else {
          //updateMarkers(true);        	
          map.setCenter(point, searchZoom);
        }
      }
    );
  }
}

window.onunload = GUnload;