﻿/**
 * Loads a google map with a custom route for multiple locations.
 * 
 * @author Lee Boonstra <lee.boonstra@efocus.nl>
 * @author Klaas Dieleman
 * @version 2.01, 14 oct, 2010
 * @requires MooTools 1.2.4 Core, <http://www.mootools.net>
 * @requires Google Maps script: http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true&amp;key=...
 * @return void
 */
RoutePlanner = new Class({
	Implements: [Options],
	options: {
		blnOpenMapInSamePagePanel: "true",
		strElIdRoutePlanner: "routemap",
		strElIdRouteDirections: "routedirections",
		strElIdRouteForm: "routeinput",
		strElIdRouteButton: "routebutton",
		strLocale: "nl_NL", 
		strElClassName: "span.company_name",
		strElClassStreet: "span.company_street",
		strElClassPostal: "span.company_postal",
		strElClassCity: "span.company_city",
		strElClassRoutePanel: "div.routeplanner",
		strElClassPlanButton: "a.plan"
	},
	initialize: function(options) {
		this.setOptions(options);
		
		if($(this.options.strElIdRoutePlanner)){
			if (GBrowserIsCompatible()){
				this.url = window.location.href;
				this.objMap = new GMap2($(this.options.strElIdRoutePlanner));
				this.objGeocoder = new GClientGeocoder();
				this.objGdir = new GDirections(this.objMap, $(this.options.strElIdRouteDirections));
				this.strAddress = "";
				this.setMap(0);
				this.getEvents();		
			}	
		}
	},
	getName: function(i){
		return $$(this.options.strElClassName)[i].get('html');
	},
	getStreet: function(i){
		return $$(this.options.strElClassStreet)[i].get('html');
	},
	getPostal: function(i){
		var strPostal = $$(this.options.strElClassPostal)[i].get('html');
		return strPostal.slice(0,4);
	},
	getCity: function(i){
		return $$(this.options.strElClassCity)[i].get('html');		
	},
	getAddress: function(i){
		return this.getStreet(i) + ', ' + this.getPostal(i) + ' ' + this.getCity(i);	
	},
	getBalloonContent: function(i){
		return '<h4>' + this.getName(i) + '</h4>' + '<p>' + this.getStreet(i) + '<br />' + this.getCity(i) + '</p>';
	},
	getEvents: function(){
		var arrPlanRouteBtn = $$(this.options.strElClassPlanButton);
		var elRouteForm = $(this.options.strElIdRouteForm);
		var elRouteBtn = $(this.options.strElIdRouteButton);
				
		//multiple route links
		arrPlanRouteBtn.each(function(item,index){
			item.addEvent('click', function(){
				if(this.options.blnOpenMapInSamePagePanel == "true"){
					$$(this.options.strElClassRoutePanel)[0].setStyle("height","auto");	
					$$(this.options.strElClassRoutePanel)[0].setStyle('visibility', 'visible');
				}
				this.setMap(index);
				if(elRouteForm.value != ""){
					this.objGdir.load('from: ' + elRouteForm.value + ' to: ' + this.strAddress, { 'locale': this.options.strLocale });
				}
				window.location.href = this.url + "#route";
			}.bind(this));
		}.bind(this));
				
		//route planner form submit
		elRouteBtn.addEvent('click', function(){
			this.objGdir.load('from: ' + elRouteForm.value + ' to: ' + this.strAddress, { 'locale': this.options.strLocale });
			elRouteBtn.removeEvent('click');
		}.bind(this));
		elRouteForm.addEvent('keydown', function(e){
			if(e.key == 'enter') {
				this.objGdir.load('from: ' + elRouteForm.value + ' to: ' + this.strAddress, { 'locale': this.options.strLocale });	
			}
			elRouteBtn.removeEvent('keydown');
		}.bind(this));
				
	},
	setMap: function(i){
		this.strAddress = this.getAddress(i);
		var strBalloonContent = this.getBalloonContent(i);
		this.objGeocoder.getLatLng(
			this.strAddress,
			function(point) {
				if (!point) {
					alert(this.strAddress + " not found");
				} else {
					this.objMap.setCenter(point, 12);
											
					var objMarker = new GMarker(point, {title: this.getName(i)});
					this.objMap.addOverlay(objMarker);

					this.objMap.setCenter();
					this.objMap.setUIToDefault();
					
					GEvent.addListener(objMarker, "click", function() {
						objMarker.openInfoWindowHtml(strBalloonContent);
					});		
				}
			}.bind(this)
		);	
	}
});
