/*******************************************************/
/* Find the driving distance in miles between any two  */
/* points in the world - flightdistances.js            */
/* Draws Google Map with line showing distance         */

/* Created by: Grant Mills - HTML Brainchild           */
/* Contact at brains@htmlbrainchild.com                */
/* Last Modified - 11/30/2010                          */

/* Outputs data to these divs:                         */
/* #map_canvas - Draws map here                        */
/* #error  - Receives error messages                   */
/* #drive_distance - Display answer                            */
/*******************************************************/

var ttl_dist =0;

function displayIntoDiv(divName, info) {
	$('#' + divName).html(info);	
}
function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function Coordinates(){
	this.start = '';
	this.end= '';
	//Gathers info from page
	this.clickPass = function(){
		  var glocation;
		  var glocation2;
		  glocation = this.start;
		  glocation2 = this.end;
		  //Dumps user input into div
		  displayIntoDiv('location1', glocation);
		  displayIntoDiv('location2', glocation2);
		  //Enters user input into newDrive object
		  this.inputLocations = [];
		  this.inputLocations[0] = glocation;
		  this.inputLocations[1] = glocation2;
	};
	//holds drive distance 
	this.driveDistance = 0;
}
function googleDriveCall(newDrive){
	  //Render object
	  var directionDisplay;
	  //Directions object
	  var directionsService = new google.maps.DirectionsService();
	  //Setup request using user input
	  var gOrigin = newDrive.inputLocations[0];
	  var gFinal = newDrive.inputLocations[1];
	  //Create Direction Request object
	  var gReq = {
			origin: gOrigin, 
			destination: gFinal,
			travelMode: google.maps.DirectionsTravelMode.DRIVING
		  };
	  //Call Directions Service and use callback for the rest of the function
	  directionsService.route(gReq, function(results, status){
			if (status == google.maps.DirectionsStatus.OK) {
				  //console.log(results);
				  $('div#error').empty();
				  var google_d = results.routes[0].legs[0].distance.value;
				  var razs = (google_d/1000);
			 
				var start_point = jQuery('#start_point').val();
				var end =jQuery('#sel_dest').val();
				$.ajax({
					type: "POST",
					url: "http://www.meppelgezond.nl/wp-content/plugins/mcwalk/pages/save_start.php",
					data: "start="+start_point+"&end="+end+"&dist="+razs,
					success: function(){
						window.location.reload();
					  }
				});
				//jQuery('#ttl_dst').val(razs);
			}
			else
			{
				alert("We're sorry we couldn't calculate driving directions those locations. Please try different locations. Is there an ocean between your locations?");
			}
						  });
}

function calc_dist(){
	if(jQuery('#start_point').val()) {
		var newDrive = new Coordinates();
		newDrive.start= jQuery('#start_point').val();
		newDrive.end= jQuery('#sel_dest option:selected').text();	
		newDrive.clickPass();
		googleDriveCall(newDrive);
		//jQuery('#fd').submit();
		return true;
	} else {
		alert('vul uw startplek in');
		return false;
	}
}
