// JavaScript Document
/*Driving Directions Functions*/

function DrivingDirections(mapName,map){
	this.mapName = mapName;
	this.map = map;
	this.gmap = map.gmap;
	
	this.directionsRow = null;
	this.ddContainer = null;

	//Placemark ID Fields for src/dst
	this.placemarkFromField = null;
	this.placemarkToField = null;
	this.src = null;
	this.dst = null;

	this.ddPolyline = null;

	this.init = function(useMapName){
		this.placemarkFromField = $('directionsFromID' + (useMapName ? ('_' + this.mapName) : ''));
		this.placemarkToField = $('directionsToID' + (useMapName ? ('_' + this.mapName) : ''));
		
		this.directionsRow = $('ddRow' + (useMapName ? ('_' + this.mapName) : ''));
		this.ddContainer = $('ddContainer' + (useMapName ? ('_' + this.mapName) : ''));
		this.gdirections = new GDirections(null);
		GEvent.bind(this.gdirections,"error",this,this.handleDrivingErrors);
		GEvent.bind(this.gdirections, "load", this,this.onGDirectionsLoad);	
	}

	this.getDirectionsTo = function(driveTo,driveFrom){
		if(!driveFrom)
			var driveFrom = findPlacemark(this.mapName,this.placemarkFromField.value);
		//this.placemarkToField.value = driveTo.prikey;
		var dirString = '' + driveFrom.latitude + ',' + driveFrom.longitude + '' + ' to ' +
		'' + driveTo.latitude + ',' + driveTo.longitude + '';
		this.directionsRow.style.display = '';	
		if(this.ddPolyline != null)
			this.gmap.removeOverlay(this.ddPolyline);
		this.gdirections.load(dirString,{getPolyline:true,getSteps:true});
	}

	this.getDirections = function(){
		var dirString = this.addressFromField.value + ' to ' + $('directionsTo').value;
		this.directionsRow.style.display = '';	
		if(this.ddPolyline != null)
			this.gmap.removeOverlay(this.ddPolyline);
		this.gdirections.load(dirString,{getPolyline:true,getSteps:true});
	}

	//Handle Successful Directions Attempts
	this.onGDirectionsLoad = function(){
		/*Begin Try to set icons*/
		var placemark1 = this.src;
		var placemark2 = this.dst;
		
		/*End Try to set icons*/
		var route = this.gdirections.getRoute(0);
		//Output starting point
		this.ddContainer.innerHTML = this.formatRoute(route,placemark1,placemark2);
		
		this.ddPolyline = this.gdirections.getPolyline();
		this.gmap.addOverlay(this.ddPolyline);
		this.map.additionalBounds = this.ddPolyline.getBounds();
		this.zoomAdjust = 0;

		this.map.setBoundsCenterAndZoom();		
		
//		if(placemark2.name == 'Best Western Mount Vernon'){
			this.map.adjustForIcons(placemark1.marker);
			this.map.adjustForIcons(placemark2.marker);
//		}
		
		this.map.setBoundsCenterAndZoom();
	}
	
	//Handle Errors acquiring directions
	this.handleDrivingErrors = function(){
		if (this.gdirections.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
			this.ddContainer.innerHTML = ("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + this.gdirections.getStatus().code);
		else if (this.gdirections.getStatus().code == G_GEO_SERVER_ERROR)
			this.ddContainer.innerHTML = ("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + this.gdirections.getStatus().code);
		
		else if (this.gdirections.getStatus().code == G_GEO_MISSING_QUERY)
			this.ddContainer.innerHTML = ("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + this.gdirections.getStatus().code);
	
		else if (this.gdirections.getStatus().code == G_GEO_BAD_KEY)
			this.ddContainer.innerHTML = ("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + this.gdirections.getStatus().code);
	
		else if (this.gdirections.getStatus().code == G_GEO_BAD_REQUEST)
			this.ddContainer.innerHTML = ("A directions request could not be successfully parsed.\n Error code: " + this.gdirections.getStatus().code);
		
		else 
			this.ddContainer.innerHTML = ("An unknown error occurred." + '(' + this.gdirections.getStatus().code + ')');
	}

	//Set Destination Marker
	this.directionsTo = function(prikey){
		var placemark = findPlacemark(this.mapName,prikey);
		var a = new Address();
		a.loadFromObject(placemark);
		this.placemarkFromField.value = prikey;
		placemark.marker.closeInfoWindow();
	}
	
	//Set Source Marker
	this.directionsFrom = function(prikey){
		var placemark = findPlacemark(this.mapName,prikey);
		var a = new Address();
		a.loadFromObject(placemark);	
		this.placemarkToField.value = prikey;
		placemark.marker.closeInfoWindow();
	}

	//Set Destination Marker
	this.setToPlacemark = function(placemark){
		this.src = placemark;
		var a = new Address();
		a.loadFromObject(placemark);
		this.placemarkFromField.value = placemark.prikey;
	}
	
	//Set Source Marker
	this.setFromPlacemark = function(placemark){
		this.dst = placemark;		
		var a = new Address();
		a.loadFromObject(placemark);
		this.placemarkToField.value = placemark.prikey;
	}

	//Clear Current Driving Directions
	this.clearDirections = function(){
		if(this.ddPolyline != null){
			this.gmap.removeOverlay(this.ddPolyline);
			this.ddPolyline = null;
			this.ddContainer.innerHTML = '';
		}
		this.directionsRow.style.display = 'none';
	}

	//Begin Route Formatting Functions
	//Output an individual step
	this.outputStep = function(step,stepNumber){
		var str = '';
		str = '<li class=\"MapExplorer_Step' + (stepNumber % 2 == 0 ? ' evenrow' : ' oddrow') + '\">';
			str += '<div class=\"MapExplorer_StepDescription\">' + step.getDescriptionHtml() + '</div>';
			str += '<div class=\"MapExplorer_Distance\">' + step.getDistance().html + '</div>';
		str += '</li>';
		return str;
	}
	
	this.formatRoute = function(r,p1,p2){
		var numSteps = r.getNumSteps();
		//var s = this.showPlacemarkStep(p1,true);
		var s = this.showPlacemarkStep(p2,true);
		s += '<ol style=\"clear: both\">';		
		for (var i = 0; i < numSteps; i++){
			var step = r.getStep(i);
			s += this.outputStep(step,i+1) + '<br>';
		}
		s += '</ol>';		
		//s += this.showPlacemarkStep(p2,false);
		s += this.showPlacemarkStep(p1,false);

		return s;
	}

	this.showPlacemarkStep = function(placemark,start){
		var imgfile = placemark.marker.getIcon().image;
		var s = '<div class=\"MapExplorer_Directions' + (start ? 'Start' : 'End') + '\">';
		s += '<img class=\"PlacemarkStepIcon\" align="left" src=\"' + imgfile + '\">';
		s += '<div class=\"MapExplorer_DirectionsPlacemark\">' + placemark.name + '</div>';
		s += '</div>';
		return s;
	}
	//End Route Formatting Functions

}


