/*******************************************************************************
* Google Lib                                                                   *
* -----------------------------------------------------------------------------*
* Martin Fünning                                                               *
* 02.06.2009                                                                   *
*******************************************************************************/

/*******************************************************************************
* Löst anhand der Domain den API Key auf und läd die API
*******************************************************************************/
var rh_map = {};

Event.observe(window, 'load', function() {
	switch (window.location.host) {
		case "magento.jakota.de"                  : var GMapApiKey = 'ABQIAAAAdZTO_3Y_3aD8q5TPoacRWRStnUHqGiJG1XICa1ok5ixWy69LChS7kH0amg-2wsvdRNlpWhIl0PhE7w'; break;
		case "tms.jakota.net"                     : var GMapApiKey = 'ABQIAAAAdZTO_3Y_3aD8q5TPoacRWRS_STNXjbfes6focJ792wHCB80N3hQ0Ol_ggJxQWQtScG6GBVLIkejE3A'; break;
		case "radreisen-touristik-service.de"     : var GMapApiKey = 'ABQIAAAAdZTO_3Y_3aD8q5TPoacRWRSMynPKPtI4OjJmYhzjA4fvBoLmBRQujQ4jv3i0nz5BzIDXwbh3npejiQ'; break;
		case "www.radreisen-touristik-service.de" : var GMapApiKey = 'ABQIAAAAdZTO_3Y_3aD8q5TPoacRWRREOQoMvYloy9uBKucAVOPPHxKWOxQOxj9kzyygIKuYIStrdqUhKAUUmA'; break;
		default                                   : var GMapApiKey = ''; break;
	}
	includeGoogleMapAPI(GMapApiKey);
});

/*******************************************************************************
* Läd die API mit dem angegebenen API Key
*******************************************************************************/
function includeGoogleMapAPI(GMapApiKey) {
	var script  = document.createElement('script');
	script.type = "text/javascript";
	script.src  = "http://www.google.com/jsapi?key="+GMapApiKey+"&callback=loadMaps";
	script.setAttribute("charset", "utf-8");
	document.getElementsByTagName('head')[0].appendChild(script);
}

/*******************************************************************************
* Callback: nachdem die API geladen wurde, wird das MAPS Modul geladen
*******************************************************************************/
function loadMaps() {
	google.load("maps", "2", {"callback" : mapsLoaded});
}

/*******************************************************************************
* Callback: Nachdem das Maps Modul geladen wurde wird eine Karte initialisiert
*******************************************************************************/
function mapsLoaded() {
	rh_map = new leihradMap();
	rh_map.setMapContainerID('myMap');
	rh_map.loadMap();
}

var leihradMap = function() {

	this.MapContainerID = false;
	this.Map            = false;
	this.kmlUrl 		= 'leihrad.kml';
	this.Points         = {};

	this.pointClicked       = {};
	this.pointClickedFlag   = false;
	this.pointClicked2       = {};
	this.pointClicked2Flag   = false;

	this.route               = {};
	this.routeFlag           = false;

	this.tempRoute           = [];
	this.tempRouteFlag       = false;

	this.tourRoute           = {};
	this.tourRouteFlag       = false;

	this.markerNormal         = 'http://'+window.location.host+'/gmap/gepaeck_marker_koffer_std.png';
	this.markerNormalHover    = 'http://'+window.location.host+'/gmap/gepaeck_marker_koffer_stdover.png';
	this.markerStart          = 'http://'+window.location.host+'/gmap/gepaeck_marker_koffer_start.png';
	this.markerStartHover     = 'http://'+window.location.host+'/gmap/gepaeck_marker_koffer_startover.png';
	this.markerStop           = 'http://'+window.location.host+'/gmap/gepaeck_marker_koffer_stop.png';
	this.markerStopHover      = 'http://'+window.location.host+'/gmap/gepaeck_marker_koffer_stopover.png';
	this.markerStartStop      = 'http://'+window.location.host+'/gmap/gepaeck_marker_koffer_startstop.png';
	this.markerStartStopHover = 'http://'+window.location.host+'/gmap/gepaeck_marker_koffer_startstopover.png';
	this.markerActive         = 'http://'+window.location.host+'/gmap/gepaeck_marker_koffer_act.png';
	this.markerActiveHover    = 'http://'+window.location.host+'/gmap/gepaeck_marker_koffer_actover.png';
	this.markerDisable        = 'http://'+window.location.host+'/gmap/gepaeck_marker_koffer_inact.png';
	this.markerDisableHover   = 'http://'+window.location.host+'/gmap/gepaeck_marker_koffer_inact_over.png';

	this.baseIcon = new GIcon(G_DEFAULT_ICON);
    this.baseIcon.shadow = "";
    this.baseIcon.iconSize = new GSize(44, 44);
    this.baseIcon.iconAnchor = new GPoint(11, 36);
    this.baseIcon.infoWindowAnchor = new GPoint(11, 36);


	this.linecolorHover		= '#32A4F1';
	this.linecolorClick		= '#F0A721';
	this.linecolorDisable   = '#898989';

	this.citys				= [
		'Stralsund',
		'Rostock',
		'Lübeck'
	];

	this.geoLoc = {
		'Stralsund'     : [54.313772,13.085795],
		'Rostock'       : [54.089996,12.132946],
		'Lübeck'        : [53.869568,10.687547]
	}

	this.routes = {};

	this.citiesLoad = 0;

	this.url = '';



	this.setMapContainerID = function(MapContainerID) {
		this.MapContainerID = MapContainerID;
	};

	this.finishLoad = function() {
		var morpher = new Effect.Morph('selector', {style: 'height: 40px',  duration: 0.5});
		$('prelaod').replace('');
		$('tourselector').value = '';
		$('tourselector').setStyle({display:''});
	}

	this.gotoLeihrad = function(parentThis) {

		  for (var city in parentThis.Points) {
  			if (parentThis.Points[city].status == 'start') {
  				var von = parentThis.Points[city].ort;
  			}
  			if (parentThis.Points[city].status == 'stop') {
  				var bis = parentThis.Points[city].ort;
  			}
  		}
		window.location.href = 'http://'+window.location.host+'/index.php'+parentThis.url+'?von='+von+'&bis='+bis;
	}


	this.addTooltipp = function(parentThis, object) {

		if (Prototype.Browser.IE) {
			tooltippObject = object.U[1];
		} else {
			tooltippObject = object.nh.firstChild;
		}

		new Tip(tooltippObject, object.ort, {
			hook: { target: 'topMiddle', tip: 'bottomMiddle', mouse: false },
			stem: 'bottomMiddle',
			offset: { x: -9, y: 5 },
			style: 'protoblue',
			width: 'auto',
			delay: 0
		});
	}

	this.drawTour = function(strecke) {
		this.removeTempLine();
		this.drawTempLineFromCache(this, allJsonObj[strecke].all);
	}

	this.drawDetailTour = function(strecke) {
		switch (strecke) {
			case'Tour de Mecklenburg'                              : this.url="/gepaecktransport/gepacktransport-see-03-strecke-tour-de-mecklenburg.html"; break;
			case'Auf großer Tour durch Mecklenburg'                : this.url="/gepaecktransport/gepacktransport-see-06-strecke-auf-grosser-tour-durch-mecklenburg.html"; break;
			case'Durchs stille Land der 1000 Seen'                 : this.url="/gepaecktransport/gepacktransport-see-05-strecke-durchs-stille-land-der-1000-seen.html"; break;
			case'Rund um Müritz und Plauer See'                    : this.url="/gepaecktransport/see-02-strecke-rund-um-muritz-und-plauer-see.html"; break;
			case'Müritz Impressionen'                              : this.url="/gepaecktransport/gepacktransport.html"; break;
			case'Durch die Mecklenburgische Schweiz und Seenplatte': this.url="/gepaecktransport/gepacktransport-see-07-durch-die-mecklenburgische-schweiz-und-seenplatte.html"; break;
			case'Ostseeradweg von Lübeck nach Rostock'             : this.url="/gepaecktransport/gepacktransport-ostseeradweg.html"; break;
			case'Ostseeradweg von Rostock nach Stralsund'          : this.url="/gepaecktransport/gepacktransport-mec-04-ostseeradweg-von-rostock-nach-stralsund.html"; break;
			case'Ostseeradweg von Wismar nach Stralsund'           : this.url="/gepaecktransport/gepacktransport-mec-021-ostseeradweg-von-wismar-nach-stralsund.html"; break;
			case'Ostseeradweg von Lübeck nach Stralsund (Sportiv)' : this.url="/gepaecktransport/gepacktransport-mec-02s-ostseeradweg-von-lubeck-nach-stralsund.html"; break;
			case'Ostseeradweg von Lübeck nach Stralsund'           : this.url="/gepaecktransport/gepacktransport-fuer-ostseeradweg-von-lubeck-nach-rostock.html"; break;
		}
		$('bookingButton_gepaeck').setStyle({display:''});




		this.removeLine();
		this.removeTourLine();
		var polyline = this.drawLineFromCache(this, allJsonObj[strecke].all);
		var bounds = polyline.getBounds();
		var newCenter = bounds.getCenter();
		var newZoomlevel = this.Map.getBoundsZoomLevel(bounds)
		this.Map.setCenter(newCenter, newZoomlevel-1);
		this.removeLine();

		// Alte Punte Löschen
		this.removeCityPoints(this);

		// Punkte Suchen
		this.citys = [];
		this.geoLoc = {};
		for (var station in allJsonObj[strecke].subparts) {
			this.citys.push(allJsonObj[strecke].subparts[station]['von']);
			this.geoLoc[allJsonObj[strecke].subparts[station]['von']] = [];
			this.geoLoc[allJsonObj[strecke].subparts[station]['von']][1] = allJsonObj[strecke].subparts[station]['line'][0][0];
			this.geoLoc[allJsonObj[strecke].subparts[station]['von']][0] = allJsonObj[strecke].subparts[station]['line'][0][1];
			this.drawTourLineFromCache(
				this,
				allJsonObj[strecke].subparts[station]['line'],
				allJsonObj[strecke].subparts[station]['von'],
				allJsonObj[strecke].subparts[station]['bis']
			);
		}
		this.citys.push(allJsonObj[strecke].subparts[station]['bis']);
		var count = allJsonObj[strecke].subparts[station]['line'].length;
		this.geoLoc[allJsonObj[strecke].subparts[station]['bis']] = [];
		this.geoLoc[allJsonObj[strecke].subparts[station]['bis']][1] = allJsonObj[strecke].subparts[station]['line'][count-1][0];
		this.geoLoc[allJsonObj[strecke].subparts[station]['bis']][0] = allJsonObj[strecke].subparts[station]['line'][count-1][1];

		this.drawCityPoints(this);


	}

	this.removeCityPoints = function(parentThis) {

		if (typeof(parentThis) != 'object') {
			var parentThis = this;
		}

		for (var city in parentThis.Points) {
			parentThis.Map.removeOverlay(parentThis.Points[city]);
		}
		parentThis.Points = {};
	}

	this.drawCityPoints = function(parentThis) {

		if (typeof(parentThis) != 'object') {
			var parentThis = this;
		}

		for (i=0; i<this.citys.length; i++) {
			var city = this.citys[i];

			var latlngObj = new GLatLng(parentThis.geoLoc[city][0], parentThis.geoLoc[city][1]);

			var letteredIcon = new GIcon(parentThis.baseIcon);
			letteredIcon.image = parentThis.markerNormal;
			var markerOptions = { icon:letteredIcon };
			if (typeof(parentThis.Points[city]) != 'object') {

				parentThis.Points[city] = new GMarker(latlngObj, markerOptions);
		  		parentThis.Points[city].ort = city;
		  		parentThis.Points[city].status = 'normal';

		  		var binded_pointMouseOver = parentThis.pointMouseOver.bind('', parentThis, parentThis.Points[city]);
		  		GEvent.addListener(parentThis.Points[city], "mouseover", binded_pointMouseOver);

		  		var binded_pointMouseOut = parentThis.pointMouseOut.bind('', parentThis, parentThis.Points[city]);
		  		GEvent.addListener(parentThis.Points[city], "mouseout", binded_pointMouseOut);

		  		var binded_pointClick = parentThis.pointClick.bind('', parentThis, parentThis.Points[city]);
		  		GEvent.addListener(parentThis.Points[city], "click", binded_pointClick);


				parentThis.Map.addOverlay(parentThis.Points[city]);

				parentThis.addTooltipp(parentThis, parentThis.Points[city]);

				parentThis.Points[city].setImage(parentThis.markerNormal);
			}
		}
	}

	this.changeMap = function(parentThis, theSelect) {
		if ($('tourselector').value) {
			parentThis.drawDetailTour($('tourselector').value);
		}
	}

	this.loadMap = function() {
		$('tourselector').setStyle({display:'none'});
		$('selector').setStyle({height:'500px'});
		this.Map = new google.maps.Map2($(this.MapContainerID));
		this.Map.setCenter(new google.maps.LatLng(53.833808, 12.466054), 8);


		var binded_gotoLeihrad = this.gotoLeihrad.bind('', this);
		$('bookingButton_gepaeck').observe('click', binded_gotoLeihrad);


		// Kontrollen deaktivieren
		//this.Map.disableDragging();
		this.Map.disableDoubleClickZoom();
		this.Map.enableScrollWheelZoom();

		// Relieff Karte auswählen (G_PHYSICAL_MAP)
		this.Map.addMapType(G_PHYSICAL_MAP);
		this.Map.setMapType(G_PHYSICAL_MAP);

		//Touren Anzeigen
		i = 0;
		$('tourselector').insert('<option value="" id="noselect">--- Bitte wählen Sie eine Tour: ---</option>');
		var binded_changeMap = this.changeMap.bind('', this);
		$('tourselector').observe('change', binded_changeMap);
		for (var strecke in allJsonObj) {
			$('tourselector').insert('<option value="'+strecke+'">'+strecke+'</option>');

// Alte HTML Auswahl
//			$('tourlist').insert('<a href="javascript:void(0)" class="tourbutton" onclick="javascript:rh_map.drawDetailTour(\''+strecke+'\')" onmouseover="javascript:rh_map.drawTour(\''+strecke+'\')" onmouseout="javascript:rh_map.removeTempLine()" id="tour_'+i+'">'+strecke+'</a>')
//			var tourstations = '';
//			for (var station in allJsonObj[strecke].subparts) {
//				var von = allJsonObj[strecke].subparts[station]['von'];
//				var bis = allJsonObj[strecke].subparts[station]['bis'];
//				tourstations = tourstations+von+'<br />';
//			}
//			tourstations = '<div style="text-align:left;">'+tourstations+bis+"</div>";
//			new Tip($('tour_'+i+''), tourstations, {
//			title: 'Stationen',
//			hook: { target: 'leftMiddle', tip: 'rightMiddle', mouse: false },
//			stem: 'rightMiddle',
//			offset: { x: 0, y: -1 },
//			style: 'protoblue',
//			width: 'auto',
//			delay: 0
//			});

			i++;
		}


		//Punkte auflösen
		var geoDecoder = new GClientGeocoder();

		this.finishLoad();
	};




  	this.pointMouseOver = function(parentThis, point) {
  		var status = parentThis.getPointStatus(point);
  		switch (status) {
  			case 'normal'   : point.setImage(parentThis.markerNormalHover);    break;
  			case 'start'    : point.setImage(parentThis.markerStartHover);     break;
  			case 'stop'     : point.setImage(parentThis.markerStopHover);      break;
  			case 'startstop': point.setImage(parentThis.markerStartStopHover); break;
  			case 'disable'  : point.setImage(parentThis.markerDisableHover);   break;
  			case 'active'   : point.setImage(parentThis.markerActiveHover);    break;
  		}
  	}

  	this.pointMouseOut = function(parentThis, point) {
  		var status = parentThis.getPointStatus(point);
  		switch (status) {
  			case 'normal'   : point.setImage(parentThis.markerNormal);    break;
  			case 'start'    : point.setImage(parentThis.markerStart);     break;
  			case 'stop'     : point.setImage(parentThis.markerStop);      break;
  			case 'startstop': point.setImage(parentThis.markerStartStop); break;
  			case 'disable'  : point.setImage(parentThis.markerDisable);   break;
  			case 'active'   : point.setImage(parentThis.markerActive);    break;
  		}
  	}

  	this.pointClick = function(parentThis, point) {

  		var status = parentThis.getPointStatus(point);
  		var tracks = parentThis.getTracksFromPoint(point);
  		var startSelected = parentThis.isAlreadyStart(point);
  		var stopSelected = parentThis.isAlreadyStop(point);
		var isRoundCourse = parentThis.isRoundCourse();

  		switch (status) {
  			case 'normal':
  				//################################################################
  				//# Klick auf NORMAL
  				//################################################################
  				if (!startSelected && !startSelected) {
  					// Wenn noch kein start & stom selektiert wurden wähle einen start
					var setDiabled = true;
					for (i=0; i<parentThis.citys.length; i++) {
						if (parentThis.citys[i] == point.ort) {
							setDiabled = false;
						}
						if (setDiabled) {
							parentThis.setPointStatus(parentThis.Points[parentThis.citys[i]], 'disable');
							var pointTracks = parentThis.getTracksFromPoint(parentThis.Points[parentThis.citys[i]]);
							if (pointTracks[0]) {
								parentThis.setTrackStatus(pointTracks[0], 'disable');
							}
						} else {
							parentThis.setPointStatus(parentThis.Points[parentThis.citys[i]], 'normal');
							var pointTracks = parentThis.getTracksFromPoint(parentThis.Points[parentThis.citys[i]]);
							if (pointTracks[0]) {
								parentThis.setTrackStatus(pointTracks[0], 'normal');
							}
						}
					}
					parentThis.setPointStatus(point, 'start');
  				} else if (startSelected && !stopSelected){
  					var setActive = false;
  					parentThis.setPointStatus(point, 'stop');
  					for (i=0; i<parentThis.citys.length; i++) {
  						if (parentThis.Points[parentThis.citys[i]].status == 'start') {
  							setActive = true;
  						}
  						if (parentThis.Points[parentThis.citys[i]].status == 'stop') {
  							setActive = false;
  						}
  						if (setActive) {
							if (parentThis.Points[parentThis.citys[i]].status == 'normal') {
  								parentThis.setPointStatus(parentThis.Points[parentThis.citys[i]], 'active');
							}
							var pointTracks = parentThis.getTracksFromPoint(parentThis.Points[parentThis.citys[i]]);
							if (pointTracks[0]) {
								parentThis.setTrackStatus(pointTracks[0], 'active');
							}
  						}
  					}
  					parentThis.setPointStatus(point, 'stop');

  				} else {
					var setDiabled = true;
					for (i=0; i<parentThis.citys.length; i++) {
						if (parentThis.citys[i] == point.ort) {
							setDiabled = false;
						}
						if (setDiabled) {
							parentThis.setPointStatus(parentThis.Points[parentThis.citys[i]], 'disable');
							var pointTracks = parentThis.getTracksFromPoint(parentThis.Points[parentThis.citys[i]]);
							if (pointTracks[0]) {
								parentThis.setTrackStatus(pointTracks[0], 'disable');
							}
						} else {
							parentThis.setPointStatus(parentThis.Points[parentThis.citys[i]], 'normal');
							var pointTracks = parentThis.getTracksFromPoint(parentThis.Points[parentThis.citys[i]]);
							if (pointTracks[0]) {
								parentThis.setTrackStatus(pointTracks[0], 'normal');
							}
						}
					}
					parentThis.setPointStatus(point, 'start');
  				}



  			break;
  			case 'start':
  				//################################################################
  				//# Klick auf START
  				//################################################################
  				if (isRoundCourse && (point.ort == parentThis.citys[0]) && !stopSelected) {
  					// bei einem Rundkurs kann Start auch = Stopp sein
					var setActive = false;
  					for (i=0; i<parentThis.citys.length; i++) {
  						if (parentThis.Points[parentThis.citys[i]].status == 'start') {
  							setActive = true;
  						}
  						if (parentThis.Points[parentThis.citys[i]].status == 'stop') {
  							setActive = false;
  						}
  						if (setActive) {
							if (parentThis.Points[parentThis.citys[i]].status == 'normal') {
  								parentThis.setPointStatus(parentThis.Points[parentThis.citys[i]], 'active');
							}
							var pointTracks = parentThis.getTracksFromPoint(parentThis.Points[parentThis.citys[i]]);
							if (pointTracks[0]) {
								parentThis.setTrackStatus(pointTracks[0], 'active');
							}
  						}
  					}
  					parentThis.setPointStatus(point, 'startstop');

  				}

  				//parentThis.setPointStatus(point, 'stop');
  			break;
  			case 'stop':
  				//################################################################
  				//# Klick auf STOP
  				//################################################################
  				//parentThis.setPointStatus(point, 'startstop');
  			break;
  			case 'startstop':
  				//################################################################
  				//# Klick auf STARTSTOP
  				//################################################################
  				//parentThis.setPointStatus(point, 'disable');
  			break;
  			case 'disable':
  				//################################################################
  				//# Klick auf DISABLE
  				//################################################################
					var setDiabled = true;
					for (i=0; i<parentThis.citys.length; i++) {
						if (parentThis.citys[i] == point.ort) {
							setDiabled = false;
						}
						if (setDiabled) {
							parentThis.setPointStatus(parentThis.Points[parentThis.citys[i]], 'disable');
							var pointTracks = parentThis.getTracksFromPoint(parentThis.Points[parentThis.citys[i]]);
							if (pointTracks[0]) {
								parentThis.setTrackStatus(pointTracks[0], 'disable');
							}
						} else {
							parentThis.setPointStatus(parentThis.Points[parentThis.citys[i]], 'normal');
							var pointTracks = parentThis.getTracksFromPoint(parentThis.Points[parentThis.citys[i]]);
							if (pointTracks[0]) {
								parentThis.setTrackStatus(pointTracks[0], 'normal');
							}
						}
					}
					parentThis.setPointStatus(point, 'start');
  			break;
  			case 'active':
  				//################################################################
  				//# Klick auf ACTIVE
  				//################################################################
					var setDiabled = true;
					for (i=0; i<parentThis.citys.length; i++) {
						if (parentThis.citys[i] == point.ort) {
							setDiabled = false;
						}
						if (setDiabled) {
							parentThis.setPointStatus(parentThis.Points[parentThis.citys[i]], 'disable');
							var pointTracks = parentThis.getTracksFromPoint(parentThis.Points[parentThis.citys[i]]);
							if (pointTracks[0]) {
								parentThis.setTrackStatus(pointTracks[0], 'disable');
							}
						} else {
							parentThis.setPointStatus(parentThis.Points[parentThis.citys[i]], 'normal');
							var pointTracks = parentThis.getTracksFromPoint(parentThis.Points[parentThis.citys[i]]);
							if (pointTracks[0]) {
								parentThis.setTrackStatus(pointTracks[0], 'normal');
							}
						}
					}
					parentThis.setPointStatus(point, 'start');
  			break;
  		}

  		// Wenn auf einen Normalen marker geklickt wurde und es keinen aktiven marker gibt
//		console.log(status);
//  		if ((status == 'normal') && !anotherActive) {
//			var setDiabled = true;
//			for (i=0; i<parentThis.citys.length; i++) {
//				if (parentThis.citys[i] == point.ort) {
//					setDiabled = false;
//				}
//				if (setDiabled) {
//					parentThis.setPointDiable(parentThis.Points[parentThis.citys[i]]);
//				} else {
//					parentThis.setPointNormal(parentThis.Points[parentThis.citys[i]]);
//				}
//			}
//			parentThis.setPointActive(point);
//		}

  		return false;
  	}

  	this.getPointStatus = function(point) {
  		return point.status;
  	}


  	this.setPointStatus = function(point, status) {
  		switch (status) {
  			case 'normal'   : point.status = status; point.setImage(this.markerNormal);    break;
  			case 'start'    : point.status = status; point.setImage(this.markerStart);     break;
  			case 'stop'     : point.status = status; point.setImage(this.markerStop);      break;
  			case 'startstop': point.status = status; point.setImage(this.markerStartStop); break;
  			case 'disable'  : point.status = status; point.setImage(this.markerDisable);   break;
  			case 'active'   : point.status = status; point.setImage(this.markerActive);    break;
  		}
  		this.setTrackStatus();
  	}

  	this.setTrackStatus = function(track, status) {
  		switch (status) {
  			case 'normal'   : track.status = status; this.Map.removeOverlay(track); track.color = this.linecolorHover; this.Map.addOverlay(track);   break;
  			case 'disable'  : track.status = status; this.Map.removeOverlay(track); track.color = this.linecolorDisable; this.Map.addOverlay(track); break;
  			case 'active'   : track.status = status; this.Map.removeOverlay(track); track.color = this.linecolorClick; this.Map.addOverlay(track);   break;
  		}
  	}

  	this.getTracksFromPoint = function(point) {

  		var von = false;
  		var bis = false;

  		for (var strecke in this.tourRoute) {
  			if (typeof(this.tourRoute[strecke]['line']) == 'object') {
				if (this.tourRoute[strecke]['von'] == point.ort) {
					von = this.tourRoute[strecke]['line'];
				}
				if (this.tourRoute[strecke]['bis'] == point.ort) {
					bis = this.tourRoute[strecke]['line'];
				}
  			}
  		}

  		return [von,bis];
  	}

  	this.isAlreadyStart = function(point) {
  		for (var city in this.Points) {
  			if (this.Points[city].status == 'start' && (this.Points[city] != point)) {
  				return true;
  			}
  		}
  		return false;
  	}

  	this.isRoundCourse = function() {
  		if (this.citys[0] == this.citys[(this.citys.length-1)]) {
  			return true;
  		}
  		return false;
  	}

  	this.isAlreadyStop = function(point) {
  		for (var city in this.Points) {
  			if (this.Points[city].status == 'stop' && (this.Points[city] != point)) {
  				return true;
  			}
  		}
  		return false;
  	}

  	this.lineIsLoaded = function(parentThis, myWay) {
  		parentThis.route = myWay.getPolyline();
  		parentThis.route.color = parentThis.linecolorClick;
  		parentThis.routeFlag = true;
  		parentThis.Map.addOverlay(parentThis.route);
  	};

  	this.tempLineIsLoaded = function(parentThis, myWay) {
		var route = myWay.getPolyline();
		route.color = parentThis.linecolorHover;
  		parentThis.tempRouteFlag = true;
  		parentThis.Map.addOverlay(route);
  		parentThis.tempRoute.push(route)
  	};

  	this.removeLine = function() {
  		if (this.routeFlag) {
	  		this.Map.removeOverlay(this.route);
	  		this.route = {};
	  		this.routeFlag = false;
  		}
  	}

  	this.removeTempLine = function() {

  		if (this.tempRoute.length > 0) {
	  		for (i=0; i<this.tempRoute.length; i++) {
	  			this.Map.removeOverlay(this.tempRoute[i]);
	  		}
	  		this.tempRoute = [];
	  		this.tempRouteFlag = false;
  		}
  	}

  	this.removeTourLine = function() {

  		for (var station in this.tourRoute) {
	  		if (typeof(this.tourRoute[station]['line']) == 'object') {
  				this.Map.removeOverlay(this.tourRoute[station]['line']);
	  		}
  		}
	  	this.tourRoute = [];
	  	this.tourRouteFlag = false;
  	}

  	this.lineIsLoadedErr = function(parentThis, myWay) {

  	};

  	this.drawTempLineFromCache = function(parentThis, linearray) {
  		var polyArray = Array();
  		for(i=0; i<linearray.length; i++) {
  			var latlngObj = new GLatLng(linearray[i][1], linearray[i][0]);
  			polyArray.push(latlngObj);
  		}
  		var route = new GPolyline(polyArray, parentThis.linecolorHover, 5);
   		parentThis.tempRouteFlag = true;
  		parentThis.Map.addOverlay(route);
  		parentThis.tempRoute.push(route);
		return route;

  	}

  	this.drawTourLineFromCache = function(parentThis, linearray, von, bis) {
  		var polyArray = Array();
  		for(i=0; i<linearray.length; i++) {
  			var latlngObj = new GLatLng(linearray[i][1], linearray[i][0]);
  			polyArray.push(latlngObj);
  		}
  		var route = new GPolyline(polyArray, parentThis.linecolorHover, 5);
  		route.status = 'normal';
   		parentThis.tourRouteFlag = true;
  		parentThis.Map.addOverlay(route);
  		parentThis.tourRoute[von+'|'+bis] = {};
  		parentThis.tourRoute[von+'|'+bis]['von']  = von;
  		parentThis.tourRoute[von+'|'+bis]['bis']  = bis;
  		parentThis.tourRoute[von+'|'+bis]['line'] = route;
		return route;

  	}

  	this.drawLineFromCache = function(parentThis, linearray) {
  		var polyArray = Array();
  		for(i=0; i<linearray.length; i++) {
  			var latlngObj = new GLatLng(linearray[i][1], linearray[i][0]);
  			polyArray.push(latlngObj);
  		}
  		parentThis.route = new GPolyline(polyArray, parentThis.linecolorClick, 5);
  		parentThis.route.color = parentThis.linecolorClick;
  		parentThis.routeFlag = true;
  		parentThis.Map.addOverlay(parentThis.route);
  		return parentThis.route;

  	}

  	this.drawLineBetween = function(from, to) {
		var myWay = new GDirections();
		var binded_lineIsLoaded = this.lineIsLoaded.bind('', this);
		GEvent.addListener(myWay, "load", binded_lineIsLoaded);
		var binded_lineIsLoadedErr = this.lineIsLoadedErr.bind('', this);
		GEvent.addListener(myWay, "error", binded_lineIsLoadedErr);
		myWay.load("from: " + from + " to: " + to, {'getPolyline':true});
  	};

  	this.drawTempLineBetween = function(from, to) {
		var myWay = new GDirections();
		var binded_lineIsLoaded = this.tempLineIsLoaded.bind('', this);
		GEvent.addListener(myWay, "load", binded_lineIsLoaded);
		var binded_lineIsLoadedErr = this.lineIsLoadedErr.bind('', this);
		GEvent.addListener(myWay, "error", binded_lineIsLoadedErr);
		myWay.load("from: " + from + " to: " + to, {'getPolyline':true});
  	};
}





