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

/*******************************************************************************
* Löst anhand der Domain den API Key auf und läd die API
*******************************************************************************/
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() {
	var myLeihradMap = new leihradMap();
	myLeihradMap.setMapContainerID('myMap');
	myLeihradMap.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/leihrad_marker_punkt_std.png';
	this.markerHover         = 'http://'+window.location.host+'/gmap/leihrad_marker_punkt_over.png';
	this.markerClick         = 'http://'+window.location.host+'/gmap/leihrad_marker_start_act.png';
	this.markerClickHover    = 'http://'+window.location.host+'/gmap/leihrad_marker_start_act.png';
	this.markerClick2        = 'http://'+window.location.host+'/gmap/leihrad_marker_end_act.png';
	this.markerClick2Hover   = 'http://'+window.location.host+'/gmap/leihrad_marker_end_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				= [
		'Lübeck',
		'Wismar',
		'Kühlungsborn',
		'Rostock',
		'Graal-Müritz',
		'Dierhagen',
		'Wustrow',
		'Ahrenshoop',
		'Born am Darß',
		'Wieck am Darß',
		'Prerow',
		'Zingst',
		'Waren',
		'Stralsund',
	];

	this.geoLoc = {
		'Lübeck'        : [53.869568,10.687547],
		'Wismar'        : [53.888221,11.462040],
		'Kühlungsborn'  : [54.149081,11.757225],
		'Rostock'       : [54.089996,12.132946],
		'Graal-Müritz'  : [54.250210,12.248298],
		'Dierhagen'     : [54.291923,12.342500],
		'Wustrow'       : [54.346373,12.397699],
		'Ahrenshoop'    : [54.383598,12.424442],
		'Born am Darß'  : [54.387532,12.522418],
		'Wieck am Darß' : [54.409729,12.581053],
		'Prerow'        : [54.442728,12.578410],
		'Zingst'        : [54.435504,12.683085],
		'Waren'         : [53.520136,12.680831],
		'Stralsund'     : [54.313772,13.085795]
	}

	this.routes = {
		'Lübeck|Wismar' : [
			[10.69356673507816,53.87561901229022],
			[10.7044938136956,53.88303174486818],
			[10.71740654482963,53.88693127285379],
			[10.73462482492761,53.8918028428694],
			[10.76509063237037,53.89900431890035],
			[10.77999991162453,53.90484496810476],
			[10.78548448179817,53.90971651696449],
			[10.80638028261808,53.917695562717],
			[10.81733564120929,53.9247063891728],
			[10.82630744584346,53.9324986748356],
			[10.83495351762943,53.94126603471666],
			[10.8383011335989,53.95101558649374],
			[10.84231766255017,53.96213042785274],
			[10.84996059562699,53.96641180639378],
			[10.85626503211755,53.96718286387119],
			[10.8598914918725,53.96151941749147],
			[10.86119500109266,53.95566473959311],
			[10.87644049091405,53.95447017416913],
			[10.88837188212354,53.95386492935776],
			[10.90826977392324,53.9555848711076],
			[10.91139900471479,53.95101919938406],
			[10.91662956317742,53.95237477172109],
			[10.92418918065537,53.95509095761858],
			[10.93292863261276,53.96104715554523],
			[10.93816511247777,53.96325503624625],
			[10.94572561483662,53.96579894998147],
			[10.95359275327054,53.97090192379716],
			[10.95825246758956,53.97345154500016],
			[10.96436384821634,53.97599757447188],
			[10.977175763185,53.98211144317958],
			[10.984163466391,53.98516646917114],
			[10.99376964697219,53.988897172428],
			[11.00075189786847,53.99092723328825],
			[11.00977348279308,53.99380494495851],
			[11.01908794933228,53.99685201938248],
			[11.02374978941407,53.99888747303323],
			[11.02811897917274,54.0005822205426],
			[11.03248574813339,54.00193543849005],
			[11.03627205157457,54.00329023289609],
			[11.04441755217215,54.00497355365894],
			[11.0496537140885,54.00598230269213],
			[11.05576447931082,54.00732968469459],
			[11.06157294489165,54.00731192829189],
			[11.06651644607148,54.00797955919347],
			[11.07755943858049,54.00862774535387],
			[11.08338651819905,54.01048719490422],
			[11.08892384333935,54.01234743352497],
			[11.09502575765779,54.0124981948897],
			[11.10025463147422,54.01248087425831],
			[11.10548184801048,54.01229262927153],
			[11.11155734926875,54.00988159685288],
			[11.11939886097587,54.00968391477619],
			[11.12898368908924,54.00947968127811],
			[11.13537128106418,54.00911552059873],
			[11.13913848071651,54.00824826846314],
			[11.14233425365669,54.0082367409166],
			[11.14641150897945,54.0090757083527],
			[11.15300098688112,54.00818867279288],
			[11.15908248280601,54.00662855799645],
			[11.16139017961729,54.00525373555504],
			[11.165151648051,54.00404391292796],
			[11.16948170688185,54.0018074280385],
			[11.17209713317913,54.00196775880224],
			[11.17246177141506,54.00828323476095],
			[11.17566263273045,54.00878267113976],
			[11.18150638731127,54.01166177613525],
			[11.18839840668122,54.00514575568406],
			[11.1903894891291,54.00172305856495],
			[11.19210246153398,53.99932591616425],
			[11.19236134828811,53.99676410510944],
			[11.19668070280251,53.99384411301723],
			[11.20069207958527,53.98955970734964],
			[11.20415000456287,53.98749673605177],
			[11.20964611243477,53.98593711318321],
			[11.21512874170221,53.98335313594947],
			[11.21947629638495,53.98282223699769],
			[11.22323434822637,53.98161098027888],
			[11.22816459800288,53.98124793746451],
			[11.23251432808391,53.98088728929888],
			[11.2380298599401,53.98086267723416],
			[11.2432506449083,53.9804977662717],
			[11.24317651959981,53.9750357024805],
			[11.23967734265429,53.97385666373121],
			[11.2355911892203,53.97216812293004],
			[11.23628245293229,53.95868092638816],
			[11.23857395637123,53.95645170147206],
			[11.23526245288369,53.94746244485815],
			[11.23668108587369,53.94506659585618],
			[11.24069103734496,53.94129351839676],
			[11.24415667593215,53.94025351969674],
			[11.24704910841414,53.93972809385197],
			[11.24962856483789,53.9374974380334],
			[11.25395860073817,53.93611182341734],
			[11.25712178943506,53.93421961220896],
			[11.26029402465481,53.93300992874976],
			[11.26578015890777,53.93144775324295],
			[11.270403463422,53.9304014960433],
			[11.27561098300701,53.92969353087568],
			[11.28024833777966,53.92967078774864],
			[11.28517550767177,53.92964642683079],
			[11.28953284787234,53.93030735293643],
			[11.29609011520546,53.93368763623586],
			[11.30105061078422,53.93588106173445],
			[11.30600397734258,53.93756232272658],
			[11.31405701218521,53.93325410908809],
			[11.32038478306433,53.9299784478044],
			[11.32529634557847,53.92892856361353],
			[11.32907984427315,53.9299324232548],
			[11.33142221255145,53.93145586168768],
			[11.33608623669185,53.93313743032313],
			[11.34072659338015,53.933283003162],
			[11.34936274594855,53.92948129342102],
			[11.35600701539348,53.92807928229268],
			[11.36091200850168,53.92668663656255],
			[11.36697149803968,53.92955385392531],
			[11.37445935541999,53.92661009902405],
			[11.3793352749065,53.92351023006691],
			[11.38566225959021,53.92057237214804],
			[11.38881830645774,53.91867667552516],
			[11.39377027701779,53.92018367037004],
			[11.39322241446195,53.92206421593152],
			[11.39901826746648,53.92203004765664],
			[11.403060815327,53.92115270720862],
			[11.41138520723351,53.91649472372567],
			[11.42176929702273,53.91370080016298],
			[11.42667729723,53.91264655149019],
			[11.42761886161062,53.90291349472106],
			[11.43045466058756,53.89948244992849],
			[11.43013166677372,53.89760695567515],
			[11.43355528451905,53.89468412818047],
			[11.44974182039396,53.89287553590069],
			[11.46012008982073,53.89024864892457]
		],
		'Wismar|Kühlungsborn' : [
			[11.46383421153013,53.88749344287203],
			[11.47564969641854,53.89287875379404],
			[11.47779696974019,53.89918018878482],
			[11.47437619349033,53.90210412045983],
			[11.4764976790904,53.90703997084415],
			[11.48294578758497,53.91092315534127],
			[11.48473303010313,53.91347143232246],
			[11.48284821378008,53.92099333509251],
			[11.4846292826269,53.92320024103721],
			[11.48526761006151,53.92626801673166],
			[11.48619910721297,53.92950450152522],
			[11.48625788970304,53.93257610441515],
			[11.48632645421225,53.93615964907729],
			[11.48619643533303,53.94452326659673],
			[11.48869257053458,53.95372300546735],
			[11.48882660917233,53.96071999192554],
			[11.49064231258801,53.96463363672887],
			[11.49804512763847,53.96839504316275],
			[11.50389282092543,53.97057469877553],
			[11.51210989955975,53.97512740873854],
			[11.5165278013825,53.9783400957293],
			[11.52094619386051,53.98155255345944],
			[11.52418689782308,53.98391959379393],
			[11.53267350799057,53.98727387364814],
			[11.53416987470067,53.98948214677919],
			[11.54812923027312,54.00474404914139],
			[11.55525401602769,54.01220289013507],
			[11.56262531726138,54.0174407850557],
			[11.5684021480793,54.01586233113157],
			[11.57488612511527,54.02025265359721],
			[11.58255914060559,54.02582888056642],
			[11.58727727449673,54.02903711830675],
			[11.58879632881716,54.03209856653359],
			[11.59237195380934,54.03616899414304],
			[11.59502445944631,54.03785628566367],
			[11.60797724462582,54.0385912656463],
			[11.6123682090778,54.03992367512085],
			[11.61789896029217,54.04022296063087],
			[11.62366993760696,54.038301005528],
			[11.6297503183258,54.03722987668403],
			[11.62208817111645,54.04548249259742],
			[11.61865728554185,54.04806924280094],
			[11.61870339469195,54.05011720502422],
			[11.61790404989153,54.0533664662458],
			[11.61504220184659,54.05543665997281],
			[11.62301649614002,54.06083736257851],
			[11.62510612300504,54.06321077373958],
			[11.63377668314019,54.07355476926104],
			[11.63250424067802,54.08158676058576],
			[11.6317202809822,54.0855186307487],
			[11.6323756225848,54.08875651405553],
			[11.63448995103068,54.09215363658385],
			[11.63575080942378,54.09641094792807],
			[11.62942071062462,54.09970395195585],
			[11.62244988541524,54.10044127215454],
			[11.61585267530747,54.10493086039211],
			[11.6205830081091,54.10813753514488],
			[11.63501500263364,54.11536479520839],
			[11.64298199836457,54.11991089274605],
			[11.64830594621711,54.12345351829318],
			[11.6717140240835,54.11558156806822],
			[11.67787672082796,54.11757954806011],
			[11.68084115924754,54.11977441689315],
			[11.67956381267104,54.12729678035794],
			[11.6799727423304,54.13224475849884],
			[11.6780552209006,54.13738299642558],
			[11.6845621715727,54.14142744670053],
			[11.71606150203904,54.14252937577474],
			[11.72219277094712,54.14298950313082],
			[11.72520487442196,54.14689158799296],
			[11.72874032918821,54.14839836912903],
			[11.7311578959118,54.15179342498134],
			[11.7406873336405,54.15205408056883],
			[11.75031828370905,54.15248148034883],
			[11.75704785656608,54.15344654570746],
			[11.76258548689966,54.15339717155899],
			[11.76602989363481,54.1513167810095]
		]
	};

	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/leihraeder/leihrad.html?von='+von+'&bis='+bis;
	}

	this.addTooltipp = function(parentThis, object) {

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

		if (tooltippObject) {

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


	};

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

  		if(parentThis.pointClickedFlag && !parentThis.pointClicked2Flag && (point != parentThis.pointClickedFlag)) {
	  		parentThis.removeTempLine();
	  		if (typeof(parentThis.routes[parentThis.pointClicked.ort+'|'+point.ort]) == 'object'){
	  			parentThis.drawTempLineFromCache(
	  				parentThis,
	  				parentThis.routes[parentThis.pointClicked.ort+'|'+point.ort]
	  			);
	  		} else if (typeof(parentThis.routes[point.ort+'|'+parentThis.pointClicked.ort]) == 'object') {
	  			parentThis.drawTempLineFromCache(
	  				parentThis,
	  				parentThis.routes[point.ort+'|'+parentThis.pointClicked.ort]
	  			);
	  		} else {
		  		parentThis.drawTempLineBetween(
		  			parentThis.pointClicked.getLatLng().lat()+', '+parentThis.pointClicked.getLatLng().lng(),
		  			point.getLatLng().lat()+', '+point.getLatLng().lng()
		  		);
	  		}
  		}

  	}

  	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);
  		}
  		parentThis.removeTempLine();
  	}

  	this.pointClick = function(parentThis, point) {

  		// Es ist noch kein Punkt ausgewählt
  		if (!parentThis.pointClickedFlag && !parentThis.pointClicked2Flag) {
	  		parentThis.pointClickedFlag = true;
	  		parentThis.pointClicked = point;
	  		parentThis.pointClicked.setImage(parentThis.markerClick);
  			return false;
  		}


  		// es ist die 1 ausgeählt aber die 2 noch nicht
  		if (parentThis.pointClickedFlag && !parentThis.pointClicked2Flag && (point != parentThis.pointClickedFlag)) {
  			parentThis.pointClicked2Flag = true;
	  		parentThis.pointClicked2 = point;
	  		parentThis.pointClicked2.setImage(parentThis.markerClick2);
	  		parentThis.removeLine();
	  		parentThis.removeTempLine();
			if (typeof(parentThis.routes[parentThis.pointClicked.ort+'|'+parentThis.pointClicked2.ort]) == 'object') {
	  			parentThis.drawLineFromCache(
	  				parentThis,
	  				parentThis.routes[parentThis.pointClicked.ort+'|'+parentThis.pointClicked2.ort]
	  			);
	  		} else if(typeof(parentThis.routes[parentThis.pointClicked2.ort+'|'+parentThis.pointClicked.ort]) == 'object') {
	  			parentThis.drawLineFromCache(
	  				parentThis,
	  				parentThis.routes[parentThis.pointClicked2.ort+'|'+parentThis.pointClicked.ort]
	  			);
	  		} else {
	  			parentThis.drawLineBetween(
	  				parentThis.pointClicked.getLatLng().lat()+', '+parentThis.pointClicked.getLatLng().lng(),
	  				parentThis.pointClicked2.getLatLng().lat()+', '+parentThis.pointClicked2.getLatLng().lng()
	  			);
	  		}
	  		return false;
  		}

  		// es ist die 1 ausgeählt aber die 2 noch nicht aber es wurde nochmal auf 1 geklickt
  		if (parentThis.pointClickedFlag && !parentThis.pointClicked2Flag && (point == parentThis.pointClickedFlag)) {
  			parentThis.pointClickedFlag = false;
  			parentThis.pointClicked.setImage(parentThis.markerNormal);
	  		parentThis.pointClicked = {};
	  		return false;
  		}

  		// es sind beide ausgewählt
  		if (parentThis.pointClickedFlag && parentThis.pointClicked2Flag && (point != parentThis.pointClickedFlag) && (point != parentThis.pointClicked2Flag)) {

  			parentThis.removeLine();

  			parentThis.pointClicked2Flag = false;
  			parentThis.pointClicked2.setImage(parentThis.markerNormal);
	  		parentThis.pointClicked2 = {};

  			parentThis.pointClickedFlag = false;
  			parentThis.pointClicked.setImage(parentThis.markerNormal);
	  		parentThis.pointClicked = {};

	  		parentThis.pointClickedFlag = true;
	  		parentThis.pointClicked = point;
	  		parentThis.pointClicked.setImage(parentThis.markerClick);
	  		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});
  	};
}





