
// MarkerManager initialisieren
mgr = null;

// aktuelle Marker
marker = new Array();

// aktuelle Routen
directions = new Array();

// Verfügbare Farben für die Routen
dircolors = [ '#FF0000','#00FF00','#0000FF' ];

function showRoute(id){

	new Ajax.Request('typo3conf/ext/bluemaps/bluemaps.php?action=getPoints&id='+id,{
		method: 'post',
		onSuccess: function(t){
			clearMap();
			setFahrplan(id);
			showPoints(t.responseJSON,true);
		}
	});

	return false;
}

function showPoints(points,route){

	mgr = new MarkerManager(map);
	var w = new Array();

	var h = '<ul>';

	$H(points).each(function(el){
		if(el.value.hidden == 0){
			marker[el.value.uid] = new GMarker(new GLatLng(el.value.b,el.value.l),G_DEFAULT_ICON);
			GEvent.addListener(marker[el.value.uid], "click",
							function(){
								marker[el.value.uid].openInfoWindowHtml("Haltestelle:<br /><strong>"+el.value.name+"</strong>");
							});
			mgr.addMarker(marker[el.value.uid],1,17);
			
			//h += '<li><a href="#" onclick="scrollToPoint('+el.value.uid+')">'+el.value.name+'</a></li>';
			h += '<li>'+el.value.name+'</li>';			
		}

		w.push(el.value.b+","+el.value.l);
	});

	h += '</ul>';

	if($('haltestellen_content')){
		$('haltestellen_content').update(h);
		//$('haltestellen').setStyle({display: 'block'});
	}

	if(route){

		d = new GDirections();
		d.color = dircolors[directions.length % dircolors.length];
		d.loadFromWaypoints(w,{locale: "de", getPolyline: true});
		directions.push(d);

		GEvent.addListener(d,'load',replacePolyline);

	}

}

function replacePolyline() {

	// Original Polyline verstecken
	this.getPolyline().hide();

	var points = [];
	var poly = this.getPolyline();
	for (var i = 0; i < poly.getVertexCount(); i++) {
		points[i] = poly.getVertex(i);
	}

	var p = new GPolyline(points, this.color, 5, 0.5);
	zoomAndMove(p.getBounds());
	map.addOverlay(p);

}

function clearMap(){
	if(mgr != null){
		marker.each(function(el){
			if(el){
				mgr.removeMarker(el);
			}
		});
	}

	marker = new Array();
	/*
	directions.each(function(el){
		el.clear();
	});*/

	directions = new Array();

	map.clearOverlays();
}

function showAllRoutes(){

	new Ajax.Request('typo3conf/ext/bluemaps/bluemaps.php?action=getAllPoints',{
		method: 'post',
		onSuccess: function(t){
			clearMap();
			var p = $H(t.responseJSON);
			p.each(function(el){
				showPoints(el.value,false);
			});

			var b = new GLatLngBounds();

			marker.each(function(el){
				b.extend(el.getLatLng());
			});

		//	var sw = b.getSouthWest();
		//	b.extend(new GLatLng(sw.lat(),sw.lng()));

		//	var ne = b.getNorthEast();
		//	b.extend(new GLatLng(ne.lat(),ne.lng()));

			zoomAndMove(b);

			//$('haltestellen').hide();
		}
	});

	return false;
}

// Karte richtig zentrieren und zoomen
function zoomAndMove(b){

	var clat = (b.getNorthEast().lat() + b.getSouthWest().lat()) /2;
	var clng = (b.getNorthEast().lng() + b.getSouthWest().lng()) /2;
	map.setCenter(new GLatLng(clat,clng),map.getBoundsZoomLevel(b));

}

function setFahrplan(id){

	new Ajax.Request('typo3conf/ext/bluemaps/bluemaps.php?action=getFahrplan&id='+id,{
		method: 'post',
		onSuccess: function(t){
			var d = t.responseJSON;
			$('fahrplan').update(d.name+'<br /><a target="_blank" href="'+d.file+'">'+d.file_short+'</a>');
		}
	});

}

function scrollToPoint(id){

	return false;
}

