var geocoder, map, addressMarker, gdir, fromAddress, toAddress;


var initial_price = 2.55; // for first 1/7 miles
var initial_ratio = 0.14 ; // that is 1/7 mile
var additional_ratio = 0.14 ; // that is 1/7 mile
var additional_price = 0.30; // for additional 1/7 miles



/*
**
* Bootstrap function to setup map and apply
* custom company marker
*/
function initialize(ip,ap,siteUrl) {

	initial_price=ip;
	additional_price=ap;
	

  if (GBrowserIsCompatible()) {
    //settings
	    var companyMarkerImage= siteUrl+"/wp-content/plugins/photoblogfiles/admin/quotes/form/icon_greenA.png";
	
    var companyLatLng     = new GLatLng(51.5052437, -0.0188475);
    var companyMarkerSize = new GSize(30, 30); //width, height
    
    var defaultZoomLevel  = 13;
    //end settings
    
    //setup elements
	 map = new GMap2(document.getElementById("map_canvas"),
            { size: new GSize(440,450) } );

    
    gdir  = new GDirections(map, document.getElementById("directions"),{travelMode:G_TRAVEL_MODE_DRIVING});
     GEvent.addListener(gdir, "load", onGDirectionsLoad);

    //error handler
    GEvent.addListener(gdir, "error", handleErrors);
    
    //set company marker
    var companyMarker = createMarker(companyLatLng, companyMarkerImage, companyMarkerSize);
    
    //set map center
    map.setCenter(companyLatLng, defaultZoomLevel);
    map.addOverlay(companyMarker);
	var customUI = map.getDefaultUI();
        // Remove MapType.G_HYBRID_MAP
        map.setUI(customUI);

  }
}

/*
**
* Looks up the directions, overlays route on map,
* and prints turn-by-turn to #directions.
*/

function overlayDirections(count)
{
    fromAddress = document.getElementById("origAddress").value + ", " + document.getElementById("origCity").value + ", " + document.getElementById("origStateProvince").value + " " + document.getElementById("origPostalCode").value;
	 
	  toAddress = document.getElementById("destAddress").value + ", " + document.getElementById("destCity").value + ", " + document.getElementById("destStateProvince").value + " " + document.getElementById("destPostalCode").value;
	  
   // fromAddress = "2121 W 29th Ter, Lawrence, KS  ";
	var language  = "en";
    
    gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": language });
}


/*
**
* Wrapper function to create/return a marker object
* with custom image
*/
function createMarker(latlng, imageURL, imageSize)
{
  
    var marker      = new GIcon(G_DEFAULT_ICON);
    marker.image    = imageURL;
    marker.iconSize = imageSize;
    
    var options     =  { icon: marker };
    
    return new GMarker(latlng, options);
  
}



/*
**
* Display error to user
*
*/
function handleErrors(){
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
     alert("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: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
     alert("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: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
   else alert("An unknown error occurred.");
}

function onGDirectionsLoad(){ 
	var duration = gdir.getDuration().seconds;
	duration = Math.round(((duration/3600)*100)/100,0);
	if(duration==0)
	{
		duration=1;
	}
	
	//totalduration = parseFloat(duration)+parseFloat($("#time").val());
	//alert(totalduration);
	//totalduration = totalduration/3600;
	//totalduration = Math.round(totalduration*100)/100;
	var distance = gdir.getDistance().meters;
	totaldistance = parseFloat(distance*.0006214);
	totaldistance = Math.round(totaldistance*100)/100;
	
	calculateFare(totaldistance);
	//displayDuration = duration;
	//displayDuration = Math.round(displayDuration*100/100,0);
	//$("#tdmiles"+descount).val(displayDistance);
	//$("#tdtimes"+descount).val(displayDuration);
	//$("#time"+descount).val(duration);
	//$("#distance"+descount).val(displayDistance);
	//document.getElementById("distance").value=totaldistance;
	//document.getElementById("time").value=totalduration;
	
	
}

function calculateFare(distance){
	ad_distance = distance-initial_ratio;
	initial_amount = initial_price;
	additional_amount = (ad_distance/additional_ratio)*additional_price;
	
	totalFare = initial_amount + additional_amount;
	var roundFare = Math.round(totalFare*100)/100 
	
	
	document.getElementById("displaydistance").innerHTML=distance + " miles";
	document.getElementById("displayFare").innerHTML= "$"+ roundFare ;
	
	

}
