﻿var map = null;
var gmm = null;
function gmload() {

			
      	if (GBrowserIsCompatible()) {
	        map = new GMap2(document.getElementById("map"));
	        
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			gecoder = new GClientGeocoder();
		 	gecoder.getLatLng('kassel',function(point) {
		 		if (point) {
					map.setCenter(point,5);
				} else {
					map.setCenter(new GLatLng(51.631657, 10.480957),7);
				}
				});	

			GEvent.addListener(map, "click", function(overlay, point) {
		      		if (overlay){	// marker clicked
			      		overlay.openInfoWindowHtml(overlay.infowindow);	// open InfoWindow
		      		} else if (point) {	// background clicked
			      
		      		}
	      		});
      	}


}


function getMarkers(){
		    var urlstr="markers.php";
		    var request = GXmlHttp.create();
		    
		    request.open('GET', urlstr , true);	// request XML from PHP with AJAX call
		    request.onreadystatechange = function () {
				if (request.readyState == 4) {
					
					var xmlDoc = request.responseXML;
										
					locations = xmlDoc.documentElement.getElementsByTagName("location");
					markers = [];
					doCentera=document.getElementById('doCenter');
					ac=doCentera.getAttribute('value');
					
					x=1;
					mark = null;
					if (locations.length){
						for (var i = 0; i < locations.length; i++) { // cycle thru locations
							markers[i] = new GMarker(new GLatLng(locations[i].getAttribute("lat"),locations[i].getAttribute("lng")));
							// Add attributes to the marker so we can poll them later.
							// When clicked, an overlay will have these properties.
bild1 = locations[i].getAttribute("bild1");
bild2 = locations[i].getAttribute("bild2");
bild3 = locations[i].getAttribute("bild3");
if (bild1!="")
	bild1 = "<img border='0' src='bilder/k_"+locations[i].getAttribute("bild1")+"'>";
if (bild2!="")
	bild2 = "<img border='0' src='bilder/k_"+locations[i].getAttribute("bild2")+"'>";
if (bild3!="")
	bild3 = "<img border='0' src='bilder/k_"+locations[i].getAttribute("bild3")+"'>";

markers[i].infowindow =  "<table>";
markers[i].infowindow += "<tr><td colspan=1>Tipp von:</td><td colspan=3>"+locations[i].getAttribute("name")+"</td></tr>";
markers[i].infowindow += "<tr><td colspan=1>Ort:</td><td colspan=3>"+locations[i].getAttribute("ort")+"</td></tr>";
markers[i].infowindow += "<tr><td>KBS:</td><td>"+locations[i].getAttribute("kbs")+"</td><td>Streckennummer:</td><td>"+locations[i].getAttribute("str")+"</td></tr>";
markers[i].infowindow += "<tr><td colspan=4>"+bild1+bild2+bild3+"</td></tr>";
markers[i].infowindow += "<tr><td colspan=4><a href='/kbsauswahl.php?KBS[0]="+locations[i].getAttribute("kbs")+"#"+locations[i].getAttribute("id")+"'>zeige vollständigen Tipp</a></td></tr>";
markers[i].infowindow += "</table>";

							
							// Useful things to store on a marker (Not needed for this example, could be removed)
							// Tells you what index in the markers[] array an overlay is
							markers[i].markerindex = i;
							// Store the location_id of the location the marker represents.
							// Very useful to know the true id of a marker, you could then make
							// AJAX calls to the database to update the information if you had it's location_id
							markers[i].db_id = locations[i].getAttribute("id");
							
							map.addOverlay(markers[i]);
							if (ac=='1')
								if (document.getElementById('doCenterID').value == markers[i].db_id)
									mark = markers[i];
							
							
						}
					}
					
					if (ac=='1') {
						centerMap(document.getElementById('doCenterX').value,document.getElementById('doCenterY').value);
						mark.openInfoWindowHtml(mark.infowindow);
					}
				}
			}
			request.send(null);
	    }

 
function setupMarker() {
	gmm = new GMarkerManager(map);
	
	
	pointx = new GLatLng(50.780326138892775, 6.1077117919921875);
	markerx = new GMarker(pointx);
	gmm.addMarker(markerx,6);
	map.setCenter(pointx,6);
	gmm.refresh();

	
}

function centerMap(x,y) {
	map.setCenter(new GLatLng(x,y),13)
}

function loadGMAP() {
    
    
    gmload();
    window.setTimeout(getMarkers,1000);
   
    
}

window.onload = function() {

  loadGMAP();

};
