var geo;
var gdir;
var map;
var res = []; //did you mean
var loc = new Array; //final address

function getQuote(){

	if (loc["1check"] && loc["2check"]) {
		calculateDistance();
	}else{
		if(!loc["1check"]){
			findAddress(1);
		}
		if(!loc["2check"]){
			findAddress(2);
		}
	}
}
function reserveRide(is_limo){
	var page = "";
	if(is_limo){
		page = "limo_reservations";
	}else{
		page = "reservations";
	}
	var respage = "/"+page+"?a1="+$('addr1').innerHTML+"&a2="+$('addr2').innerHTML;
	window.location =respage;
	
}
function resetQuote(){
	loc = new Array;
	res = new Array;
	$$("ol li input").each(function(name) {
	  name.clear();
	});
	[1,2].each(function(x) {
	  $('addr'+x).innerHTML = "";
	  $('res'+x).innerHTML = "";
	  $('info'+x).show();
	});
	$('price').hide();
	$('price').innerHTML = "";
	$('quotebutton').show();
	$('reserve').hide();
}


function loadmap() {
	map = new GMap2($("map"));
	gdir = new GDirections(map, $("map"));
  	geo = new GClientGeocoder(); 
  
}

  function setLoc(type, i){
  	if (typeof i != "undefined") { 	
	  	loc[type + 'lat'] = res[type + 'lat' + i];
	  	loc[type + 'lon'] = res[type + 'lon' + i];
		loc[type+'address'] = res[type+'address'+i];
  	}
	
	$("addr"+type).innerHTML = loc[type+'address'];
	$("info"+type).hide();
	$("res"+type).innerHTML = "";
	loc[type+"check"]=true;
  }

  // ====== Geocoding ======
  function findAddress(type) {

    var search = $("address"+type).value + ", " + $("city"+type).value + ", " + $("state"+type).value + ", " + $("zipcode"+type).value;
    geo.getLocations(search, function (result)
      {
       
        if (result.Status.code == G_GEO_SUCCESS) {
          // ===== If there was more than one result, "ask did you mean" on them all =====
          if (result.Placemark.length > 1) { 
            $("res"+type).innerHTML = "Did you mean:";
            // Loop through the results

            for (var i=0; i<result.Placemark.length; i++) {  
			  res[type+'lat'+i]=result.Placemark[i].Point.coordinates[1];
			  res[type+'lon'+i]=result.Placemark[i].Point.coordinates[0];
			  res[type+'address'+i]=result.Placemark[i].address;
              $("res"+type).innerHTML += "<br>"+(i+1)+": <a href='javascript:setLoc(\""+ type +"\","+ i +")'>"+ res[type+'address'+i]+"<\/a>";
            }
          }
          // ===== If there was a single marker =====
          else {
            $("res"+type).innerHTML = "";

			loc[type+'lat']=result.Placemark[0].Point.coordinates[1];
			loc[type+'lon']=result.Placemark[0].Point.coordinates[0];
			loc[type+'address']=result.Placemark[0].address;
			setLoc(type)
			
          }
        }
        else {           
          $("res"+type).innerHTML = "<span class = 'valid_msg'>Could not find address, please try again.</span>";
        }
	
		calculateDistance();
	
      }
    );
  }


function calculateDistance()
{
	if (loc['1lat'] && loc['1lon'] && loc['2lat'] && loc['2lon']) {
		try {
			GEvent.addListener(gdir, "load", onGDirectionsLoad);
			gdir.load("from: " + loc['1lat'] + ", " + loc['1lon'] + " to: " + loc['2lat'] + ", " + loc['2lon'],
                { "locale": "en_US" });
			/*var glatlng1 = new GLatLng(loc['1lat'], loc['1lon']);
			var glatlng2 = new GLatLng(loc['2lat'], loc['2lon']);
			var miles = glatlng1.distanceFrom(glatlng2, 3963.19059).toFixed(1);
			var price = Math.round(miles * 3)+3;
			$('price').innerHTML = "Estimated Fare: <b>$"+price+"</b>";
			$('price').show();*/
			$('quotebutton').hide();
			$('reserve').show();
		} 
		catch (error) {
			alert(error);
		}
	}
}

 



function onGDirectionsLoad(){ 
	var miles = gdir.getDistance().meters * 0.000621371192;
	var price = Math.round(miles * 3)+3;
	$('price').innerHTML = "Estimated Fare: <b>$"+price+"</b>";
	$('price').show();
}

