var speedmap = {

	map: null,
	geocoder: null,
	centerLatitude: 50.553861,
	centerLongitude: 9.674339,
	startZoom: 6,
	showpoint: null,
	live_path: null,
	sess: null,
	anbieter: null,
	anschlussart: null,
	anschlusstyp: null,
	dslIcon: null,
	IMAGES: [ "1und1", "alice", "arcor", "congstar", "freenet", "kabelbw", "kabelde", "o2", "strato", "thome", "unitymedia", "versatel", "vodafone", "tele2", "gmx" ],
	ICONS: [],
	
	init_icons: function () {

		this.dslIcon = new GIcon(G_DEFAULT_ICON),
		this.dslIcon.image = this.live_path+"/speedtest/images/dslweb.gif";
		this.dslIcon.iconSize = new GSize(16, 16);
		this.dslIcon.shadow = this.live_path+"/speedtest/images/dslweb_shadow.png";
		this.dslIcon.shadowSize = new GSize(32, 32);
		
		// Individuelle Markersymbole erstellen
		
		for (var i = 0; i < this.IMAGES.length; i++) {
			if (!this.ICONS[i]) {
				var icon = new GIcon();
				icon.image = this.live_path + "/speedtest/images/icon_" + this.IMAGES[i] + ".png";
				icon.iconAnchor = new GPoint(16, 16);
				icon.infoWindowAnchor = new GPoint(16, 0);
				icon.iconSize = new GSize(16, 16);
				icon.shadow = this.live_path + "/speedtest/images/icon_shadow2.png";
				icon.shadowSize = new GSize(32, 32);
				this.ICONS[i] = icon;
			}	
		}
		
	},
	
	// Karte initialisieren
	init: function () {
		
		this.init_icons();
		
		this.map = new GMap2(document.getElementById("map"));
		this.map.enableDoubleClickZoom();
		this.map.addControl(new GSmallMapControl());
		this.map.setCenter(new GLatLng(this.centerLatitude, this.centerLongitude), this.startZoom);
		this.updateMarkers(true);
		
		GEvent.addListener(this.map,'zoomend',function() {
			speedmap.updateMarkers(true);
		});
		
		GEvent.addListener(this.map,'moveend',function() {
			speedmap.updateMarkers(false);
		}
		
		);
		// Geocoder initialisieren
		this.geocoder = new GClientGeocoder();
	},
	
	// Marker bei Veraenderung aktualisieren (Suche, Zoom, Kartenbewegung
	
	updateMarkers: function (clear_overlays) {
		
		//vorhandene Punkte entfernen
		if (clear_overlays) {
			this.map.clearOverlays();
		}
		//Grenzen f�r die Daten festlegen
		var bounds = this.map.getBounds();
		var zoomlevel = this.map.getBoundsZoomLevel(bounds);
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		var getVars = 'ne=' + northEast.toUrlValue()
			+ '&sw=' + southWest.toUrlValue()
			+ '&zoom=' + zoomlevel;
		
		$.ajax({
			url: this.live_path+"/speeduniversum/modules/speedmap/ajax-speedmap.php?sess="+this.sess+"&anbieter="+this.anbieter+"&anschlussart="+this.anschlussart+"&anschlusstyp="+this.anschlusstyp,
			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].lng);
									
					var marker = speedmap.createMarker(point,points[i].infopopup,points[i].icon);
					speedmap.map.addOverlay(marker);
					
					// Info-Window �ber Punkt �ffnen
					if (speedmap.showpoint != null) {
						if (speedmap.showpoint.y == points[i].lat && speedmap.showpoint.x == points[i].lng) {
							marker.openInfoWindowHtml(points[i].infopopup);
						}
					}
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
			}
		});
	},
	
	// Individuelles Icon zur�ckgeben
	
	getIcon: function (name) {
		for (var i = 0; i < this.IMAGES.length; i++) {
			if (this.IMAGES[i]==name && this.ICONS[i]) {
				return this.ICONS[i];
			}
		}
		return false;
	},
	
	
	// Marker erstellen
	
	createMarker: function (point, html, icon) {
		
		markerOptions = { icon: this.dslIcon };
		
		// Optionen fuer den Marker
		if (icon!="") {
			marker_icon = this.getIcon(icon); 
			if (marker_icon) {
				markerOptions = { icon:marker_icon };
			}
		}
			
		var marker = new GMarker(point,markerOptions);
		GEvent.addListener(marker, 'click', function() {
			var markerHTML = html;
			marker.openInfoWindowHtml(markerHTML);
		});
		return marker;
	},
	
	showAddress: function () {
	  address = document.getElementById("address").value;
	  if (this.geocoder) {
	    this.geocoder.getLatLng(
	      address,
	      function(point) {
	        if (!point) {
	          alert(address + " nicht gefunden");
	        } else {
	          //speedmap.updateMarkers(true);
	          speedmap.map.setCenter(point, 15);
	        }
	      }
	    );
	  }
	}	
};	

