//create the markers for normal tracks, add a eventlistener 
//point is a GPoint, html is the html to be displayd in the infowindow and the link is the track to be opened
    	function createMarker(point,html,link) {
        	var marker = new GMarker(point, hike_icon);
        	GEvent.addListener(marker, "mouseover", function() {
          		marker.openInfoWindowHtml(html);
        	});
		GEvent.addListener(marker, "click", function() {
	  	   	showTrack(link)	
        	}); 
		GEvent.addListener(marker, "mouseout", function() {
	  		map.getInfoWindow().hide();
        	});	
        	return marker;
    	}

//create the markers for the zoompoints, klick will zoom in
//point is a gpoint, html the html for the infowindow
    	function createMarkerZoom(point,html) {
	        var marker = new GMarker(point,zoom_icon);
	        GEvent.addListener(marker, "mouseover", function() {
          		marker.openInfoWindowHtml(html);
        	});
	 	GEvent.addListener(marker, "click", function() {
	  		map.panTo(point);
	  		map.setZoom(10);
	        });
	 	GEvent.addListener(marker, "mouseout", function() {
	  		map.getInfoWindow().hide();
        	});	
        	return marker;
      	}

//create the markers for "world vierw"
    	function createMarkerWorld(point,html) {
        	var marker = new GMarker(point,zoom_icon);
        	GEvent.addListener(marker, "mouseover", function() {
          		marker.openInfoWindowHtml(html);
        	});
	 	GEvent.addListener(marker, "click", function() {
	  		map.panTo(point);
	  		map.setZoom(5);
	        });
		GEvent.addListener(marker, "mouseout", function() {
	  		map.getInfoWindow().hide();
        	});	
        	return marker;
      	}

//set map zoom to a
	function setMapZoom(a){
		map.setZoom(a);
	}

//reset the overview map to start values
	function resetMap(){
		setMapZoom(6);
		map.panTo(new GLatLng(51.133, 10.455278));
		map.removeControl(lsc);
	}

//shows the google locals serach to find a cafe etc.
	function showSearch(){
		lsc = new google.maps.LocalSearch(); 
		map.addControl(lsc);	
	}

//gets the track "wegID" and displays it in DIV printmap
	function showTrack(wegID){
		Element.hide('map');
		Element.hide('drawmap');
		var newAJAX=new Ajax.Updater('printmap', 'sys/print_map.php', {asynchronous:true, evalScripts:true , method: 'get', parameters: 'id='+wegID});
		Element.show('printmap');
	}

//closes the track-view  and reopens the overview
	function closePrintMap(){
		Element.hide('printmap');
		Element.update( 'printmap', '' );
		Element.show('map');
		Element.update('Options', mapOptionsStart);

	}

//resets the printmap to initial values, if al localsearch is displayd, it'll be removed
	function resetPrintMap(){
		printmap.returnToSavedPosition();
		if(typeof(printlsc)!="undefined"){
			printmap.removeControl(printlsc);
		}
	}

//shows the google locals serach to find a cafe etc.
	function printShowSearch(){
		printlsc = new google.maps.LocalSearch(); 
		printmap.addControl(printlsc);	
	}

//creates an clickhandler for the map, to upload photos, id is the id of the map displayd	
	function enableClick(id){
		clickHandle=GEvent.addListener(printmap, "click", function(overlay,point) {
 	 	  GEvent.removeListener(clickHandle)
		  Element.hide('insertPhotoAlert');
		  uploadImage(point,id)
		});

	}

//gets xml data from url and displays the markers
      	function readMap(url) {
		batch_flickr = [];

        	request = GXmlHttp.create();
        	request.open("GET", url, true);
        	request.onreadystatechange = function() {
          		if (request.readyState == 4) {
            			var xmlDoc = request.responseXML;
            			// obtain the array of markers and loop through it
            			var markers = xmlDoc.documentElement.getElementsByTagName("marker");
            			// hide the info window, otherwise it still stays open where the removed marker used to be
            			printmap.getInfoWindow().hide();
            			//map.clearOverlays();
         
            			for (var i = 0; i < markers.length; i++) {
              				// obtain the attribues of each marker
              				var lat = parseFloat(markers[i].getAttribute("lat"));
              				var lng = parseFloat(markers[i].getAttribute("lng"));
              				var point = new GLatLng(lat,lng);
              				var html = markers[i].getAttribute("html");
              				var label = markers[i].getAttribute("label");
              				// create the marker
              				var marker = createPhotoMarker(point,label,html);
				        batch_flickr.push(marker);
              				
            			}
			fromZoom=printmap.getZoom();
			fromZoom--;
			printmgr.addMarkers(batch_flickr,printmap.getZoom(),17);
			printmgr.refresh();
			Element.hide('loading');
            		Element.update('logo','');	
          		}
        	}
        	request.send(null);
     	}
	
//rounds to x.x 
	function myRound(float){
		float=float*10;
		float=Math.round(float);
		return(float/10);
		
	}

//removes the marker marker from the trackview
	function removehtemarker(marker){
		printmgr.removeMarker(marker);
	}
    
//creates the eventlistener for the photo markers, click will open infowindow, doubleclick will delete the marker
//point is a Gpoint, name is the name, html the html to be displayed in infowindow 
//doubleclick removes the marker
      	function createPhotoMarker(point,name,html) {
		var marker = new GMarker(point,photo_icon);
        	GEvent.addListener(marker, "click", function() {
          		marker.openInfoWindowHtml(html);
        	});
		GEvent.addListener(marker, "dblclick", function() { 
			removehtemarker(marker);
		} ); 

        	return marker;
      	}

//creates the ebventlisteners for the user uploaded images, see createPhotoMarker
      	function createUserPhotoMarker(point,name,html) {
		var marker = new GMarker(point,userPhotoIcon);
        	GEvent.addListener(marker, "click", function() {
          		marker.openInfoWindowHtml(html);
        	});
		GEvent.addListener(marker, "dblclick", function() { 
			removehtemarker(marker);
		} ); 
        	return marker;
      	}

//gets images from flickr.com
	function getImages(){
		showFlickr=1;
		Element.show('loading');
                Element.update('logo',"<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" width=\"245\" height=\"191\" id=\"spaziergaenger\" align=\"middle\"><param name=\"allowScriptAccess\" value=\"sameDomain\" /><param name=\"movie\" value=\"img/spaziergaenger.swf\" /><param name=\"quality\" value=\"high\" /><param name=\"bgcolor\" value=\"#ffffff\" /><embed src=\"img/spaziergaenger.swf\" quality=\"high\" bgcolor=\"#ffffff\" width=\"180\" height=\"143\" name=\"spaziergaenger\" align=\"middle\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" /></object>");
		mapBounds = printmap.getBounds();
	
		swlatlng=mapBounds.getSouthWest();
		nelatlng=mapBounds.getNorthEast();
		swlat=myRound(swlatlng.lat());
		swlng=myRound(swlatlng.lng());
		nelat=myRound(nelatlng.lat());
		nelng=myRound(nelatlng.lng());
		var url="http://spazier.gaengr.de/sys/get_flickr_images.php?sw_lat=" + swlat + "&sw_lng=" + swlng + "&ne_lat=" + nelat + "&ne_lng=" + nelng ;
		readMap(url);
//		Element.update('logo','');
	}

//opens the auth and the upload window 
	function uploadImage(point,id){
		lat=point.lat();
		lng=point.lng();
		url="http://spazier.gaengr.de/sys/upload_flickr_form.php?lat=" + lat + "&long=" + lng + "&weg-id=" + id;
		upload = window.open(url, "Bild hochladen", "width=600,height=480,left=100,top=200");
		flickrauth= window.open("http://spazier.gaengr.de/sys/flickrauth.php", "Flickr Auth", "width=600,height=480,left=100,top=200");
  		upload.focus();
		flickrauth.focus();
	}

//opens the draw map div, hides the others
	function drawNewMap(){
		Element.hide('map');
		Element.hide('printmap');
		var newAJAX=new Ajax.Updater('drawmap', 'sys/draw_map.php', {asynchronous:true, evalScripts:true });
		Element.show('drawmap');
		
	}
//cleses the draw map div
	function closeDrawMap(){
		Element.hide('drawmap');
		Element.update( 'drawmap', '' );
		Element.show('map');
		Element.update('Options', mapOptionsStart);
	}
//gets the map data into an hidden form element, saves the form via post to /sys/insert_data.php
	function saveMyMap(){
		$('changeme').innerHTML = 'Speichere...';
		$('dataarea').value = editor.getJSON();	
		new Ajax.Updater('changeme', '/sys/insert_data.php', {asynchronous:false,parameters:Form.serialize('draw-map')}); 
		window.setTimeout("window.location.href = \"http://spazier.gaengr.de\";", 800);
	}
//submitts a comment, needs the track id
	function commentSubmit(id){
		new Ajax.Updater('changeForm', '/sys/insert_comment.php', {asynchronous:true,parameters:Form.serialize('newComment')}); 	
		command ="showTrack("+id+")"
		window.setTimeout(command, 1500);
	}

//kind of easter egg, options, bottom menue and logo are draggables
	function startmeup(){
		//position_read();
	  		divauswahl = new Array();
			divauswahl.push('Options');
			divauswahl.push('betaState');
			divauswahl.push('logo');
			divauswahl.push('bottom');
		for (var i = 0; i < divauswahl.length; i++){
	 	 var myAjax3 = new Draggable(divauswahl[i]);
  		}
	}

//updates the user uploaded imges of the map, needs track id
	function updatePrintMapImages(id){
		
		url="http://spazier.gaengr.de/sys/print_map.php?update=1&id=";
		url+=id;
		batch_userflicker = [];
		var newAJAX=new Ajax.Updater('temp', url , {method:'get', asynchronous:false, evalScripts:true });
		//printmgr.addMarkers(batch_userflicker,0,17);
		//printmgr.refresh();	
	}

//updates the map editor if gps data is uploaded
	function updateDrawmap(json){
		editor.importJSON(json);
		editor.setCenter(new QLatLng(51, 11), 6, Q_NORMAL_MAP);
	}
	
