/*******************************************************************************
* 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.markerNormal        = 'http://'+window.location.host+'/gmap/rueckholservice_marker_bus_std.png';
	this.markerHover         = 'http://'+window.location.host+'/gmap/rueckholservice_marker_bus_over.png';
	this.markerClick         = 'http://'+window.location.host+'/gmap/rueckholservice_marker_bus_act.png';
	this.markerClickHover    = 'http://'+window.location.host+'/gmap/rueckholservice_marker_bus_act.png';
	this.markerClick2        = 'http://'+window.location.host+'/gmap/rueckholservice_marker_bus_act.png';
	this.markerClick2Hover   = 'http://'+window.location.host+'/gmap/rueckholservice_marker_bus_act.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.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.setMapContainerID = function(MapContainerID) {
		this.MapContainerID = MapContainerID;
	};

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

	this.gotoLeihrad = function(parentThis) {
		var von = parentThis.pointClicked.ort;
		var bis = parentThis.pointClicked2.ort;
		window.location.href = 'http://'+window.location.host+'/index.php/rueckholservice/rueckholservice.html?von='+von+'&bis='+bis;
	}

	this.showStrecke = function(von, bis) {
		Tips.hideAll();
		this.removeTempLine();
		this.removeLine();
		this.Points['Rostock'].setImage(this.markerNormal);
		this.Points['Stralsund'].setImage(this.markerNormal);
		this.Points['Lübeck'].setImage(this.markerNormal);
		this.pointClicked       = {};
		this.pointClickedFlag   = false;
		this.pointClicked2       = {};
		this.pointClicked2Flag   = false;

		this.Points[von].setImage(this.markerClick);
		this.pointClicked       = this.Points[von];
		this.pointClickedFlag   = true;
		this.Points[bis].setImage(this.markerClick2);
		this.pointClicked2      = this.Points[bis];
		this.pointClicked2Flag  = true;

		this.drawLineBetween(
		  	this.geoLoc[von][0]+', '+this.geoLoc[von][1],
		  	this.geoLoc[bis][0]+', '+this.geoLoc[bis][1]
		);


	}

	this.addTooltipp = function(parentThis, object) {
		switch (object.ort) {
			case 'Rostock':
				var tooltippContent =
				'<ul class="linkmenu">'+
				'<li><a href="javascript:void(0);" onclick="javascript:rh_map.showStrecke(\'Stralsund\', \'Rostock\')">von Stralsund nach Rostock</a></li>'+
				'<li><a href="javascript:void(0);" onclick="javascript:rh_map.showStrecke(\'Rostock\', \'Lübeck\')">von Rostock nach Lübeck</a></li>'+
				'</ul>';
				break;
			case 'Stralsund':
				var tooltippContent =
				'<ul class="linkmenu">'+
				'<li><a href="javascript:void(0);" onclick="javascript:rh_map.showStrecke(\'Stralsund\', \'Rostock\')">von Stralsund nach Rostock</a></li>'+
				'<li><a href="javascript:void(0);" onclick="javascript:rh_map.showStrecke(\'Stralsund\', \'Lübeck\')">von Stralsund nach Lübeck</a></li>'+
				'</ul>';
				break;
			case 'Lübeck':
				var tooltippContent =
				'<ul>'+
				'<li><a href="javascript:void(0);" onclick="javascript:rh_map.showStrecke(\'Stralsund\', \'Lübeck\')">von Stralsund nach Lübeck</a></li>'+
				'<li><a href="javascript:void(0);" onclick="javascript:rh_map.showStrecke(\'Rostock\', \'Lübeck\')">von Rostock nach Lübeck</a></li>'+
				'</ul>';
				break;
		}

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



		var tip1 = new Tip(tooltippObject, tooltippContent, {
			title: object.ort,
			hook: { target: 'rightMiddle', tip: 'leftMiddle', mouse: false },
			stem: 'leftMiddle',
			offset: { x: -23, y: -10 },
			style: 'protoblueXtend',
			width: 'auto',
			delay: 0,
			hideOn: false,
			hideAfter: 0.3

		});




	}

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


		var binded_gotoLeihrad = this.gotoLeihrad.bind('', this);
		$('bookingButton_ruckhol').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);

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

		for (i=0; i<this.citys.length; i++) {
			var city = this.citys[i];
			if (typeof(this.geoLoc[city]) == 'object') {
				this.drawPoint(this, city, 1, '')
			} else {
				var binded_drawPoint = this.drawPoint.bind('', this, city, 1);
				geoDecoder.getLocations(city+", Deutschland", binded_drawPoint);
			}
		}

		//Routen Zeichen
		this.drawTempLineBetween(
		  	this.geoLoc['Stralsund'][0]+', '+this.geoLoc['Stralsund'][1],
		  	this.geoLoc['Rostock'][0]+', '+this.geoLoc['Rostock'][1]
		);

		this.drawTempLineBetween(
		  	this.geoLoc['Rostock'][0]+', '+this.geoLoc['Rostock'][1],
		  	this.geoLoc['Lübeck'][0]+', '+this.geoLoc['Lübeck'][1]
		);


	};

	this.drawPoint = function(parentThis, city, counter, latLng) {
		if (typeof(latLng.Placemark) != 'object' && (typeof(parentThis.geoLoc[city]) != 'object')) {
			if (counter <= 7) {
				var geoDecoder = new GClientGeocoder();
				var binded_drawPoint = parentThis.drawPoint.bind('', parentThis, city, (counter+1));
				geoDecoder.getLocations(city+", Deutschland", binded_drawPoint);
			} else {
				parentThis.citiesLoad++
			}
		} else {
			if (typeof(latLng.Placemark) != 'object') {
				var latlngObj = new GLatLng(parentThis.geoLoc[city][0], parentThis.geoLoc[city][1]);
			} else {
				var latlngObj = new GLatLng(latLng.Placemark[0].Point.coordinates[1], latLng.Placemark[0].Point.coordinates[0]);
			}
			var letteredIcon = new GIcon(parentThis.baseIcon);
			letteredIcon.image = parentThis.markerNormal;
			var markerOptions = { icon:letteredIcon };
			parentThis.Points[city] = new GMarker(latlngObj, markerOptions);
	  		parentThis.Points[city].ort = city;

	  		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);
			parentThis.citiesLoad++

		}
		if (parentThis.citiesLoad == parentThis.citys.length) {
			parentThis.finishLoad();
		}

	}


  	this.pointMouseOver = function(parentThis, point) {
  		if (parentThis.pointClicked == point) {
  			point.setImage(parentThis.markerClickHover);
  		} else if (parentThis.pointClicked2 == point) {
  			point.setImage(parentThis.markerClick2Hover);
  		} else {
  			point.setImage(parentThis.markerHover);
  		}
  	}

  	this.pointMouseOut = function(parentThis, point) {
  		if (parentThis.pointClicked == point) {
  			point.setImage(parentThis.markerClick);
  		} else if (parentThis.pointClicked2 == point) {
  			point.setImage(parentThis.markerClick2);
  		} else {
			point.setImage(parentThis.markerNormal);
  		}
  	}

  	this.pointClick = function(parentThis, point) {
		// Open Submenu

  		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.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)
  	}

  	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);
  	}

  	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});
  	};
}





