/*
 DMXzone Google Maps Directions Add-on
 Version: 1.1.2
 (c) 2012 DMXzone.com
 @build 21-06-2012 16:37:16
*/
(function(g){var t={width:570,height:320,useMap:"",startLoc:"",endLoc:"",useCurrentLocation:!1,directionsDisplay:!0,provideRouteAlternatives:!1,draggable:!1,distanceFieldValue:"",distanceFieldText:"",durationFieldValue:"",durationFieldText:"",travelMode:"DRIVING",unitSystem:0,debug:!1};g.fn.dmxGMDirections=function(n){function o(c,d,a){b.debug&&console.log("calcRoute start",c,d);c&&d&&(p=!0,c={origin:c,destination:d,provideRouteAlternatives:b.provideRouteAlternatives,travelMode:b.travelMode,unitSystem:b.unitSystem},
a?e.setDirections(a):u.route(c,function(b,c){switch(c){case google.maps.DirectionsStatus.OK:e.setDirections(b);break;case google.maps.DirectionsStatus.ZERO_RESULTS:alert("No route could be found between the origin and destination!");break;case google.maps.DirectionsStatus.NOT_FOUND:alert("At least one of the locations specified in the requests's origin, destination, or waypoints could not be geocoded.!");break;default:alert("Google returned route error: "+c)}}))}function i(){var b;a&&a.markers&&0<
a.markers.length&&(b=a.markers[a.markers.length-1].getPosition(),a.markers[a.markers.length-1].setMap(null),a.markers.pop());return b}function q(b,d,a){var h=d.formatted_address;g("#"+b).val(h);h=d.formatted_address;g("#"+b).trigger("select",[h,h,{data:h,value:h,latLng:d.geometry.location,result:h,obj:d},a])}function m(c,d){null==d&&(d=a.markers.length-1);if(a&&a.markers&&0<a.markers.length){if(1==a.markers.length)k=a.markers[d].getPosition();else{for(var l=c.lat(),h=c.lng(),g=[],f=-1,e=0;e<a.markers.length;e++){var i=
a.markers[e].position.lat(),j=a.markers[e].position.lng(),i=(i-l)*Math.PI/180,j=(j-h)*Math.PI/180,j=Math.sin(i/2)*Math.sin(i/2)+Math.cos(l*Math.PI/180)*Math.cos(l*Math.PI/180)*Math.sin(j/2)*Math.sin(j/2),j=6371*2*Math.atan2(Math.sqrt(j),Math.sqrt(1-j));g[e]=j;if(-1==f||j<g[f])f=e}b.debug&&console.log("found nearest "+f+", distance="+g[f]);k=a.markers[f].getPosition()}o(c,k)}}function v(c){var a;navigator.geolocation?navigator.geolocation.getCurrentPosition(function(l){b.debug&&console.log("got browser current location ..");
a=new google.maps.LatLng(l.coords.latitude,l.coords.longitude);c(a)}):google.gears&&google.gears.factory.create("beta.geolocation").getCurrentPosition(function(b){a=new google.maps.LatLng(b.latitude,b.longitude);c(a)})}var b=g.extend(t,n,{}),e,n=g(this.selector),a=null,r=null,f=null,k=null,s=g("#"+b.useMap);if(s&&s.gMap)a=s.data("gmap"),r=a.gmap,b.endLoc&&""!=b.endLoc&&g("#"+b.endLoc).bind("result",function(c,d,e,h){b.debug&&(console.log("got AAC end result",h),console.log("end markers",a.markers));
if(!b.startLoc||b.startLoc&&""!=g("#"+b.startLoc).val())k=i(),f||(f=i()),o(f,k)});var u=new google.maps.DirectionsService,p=!0;e=new google.maps.DirectionsRenderer({draggable:b.draggable,hideRouteList:!b.provideRouteAlternatives});r&&e.setMap(r);b.directionsDisplay&&e.setPanel(n.get(0));b.draggable&&google.maps.event.addListener(e,"directions_changed",function(){if(!p){var a=e.getDirections();if(a&&a.routes[0]&&a.routes[0].legs[0]){var d=a.routes[0].legs[0];b.startLoc&&(a=new google.maps.Geocoder,
a.geocode({location:d.start_location},function(a,c){c==google.maps.GeocoderStatus.OK&&(q(b.startLoc,a[0],!0),m(d.start_location))}));b.endLoc&&(a=new google.maps.Geocoder,a.geocode({location:d.end_location},function(a,c){c==google.maps.GeocoderStatus.OK&&(q(b.endLoc,a[0],!0),m(d.end_location))}))}}p=!1});b.startLoc&&g("#"+b.startLoc).bind("result",function(c,d,e,h){b.debug&&(console.log("got AAC start result",h),console.log("start markers",a.markers));b.endLoc?""!=g("#"+b.endLoc).val()&&(f=i(),k||
(k=i()),o(f,k)):(f=i(),m(f))});b.useCurrentLocation&&(b.debug&&console.log("use current location"),g(window).load(function(){v(function(a){b.debug&&console.log("got current location ",a);f=a;b.startLoc?(new google.maps.Geocoder).geocode({location:a},function(a,c){c==google.maps.GeocoderStatus.OK&&(b.debug&&console.log("trigger autocomplete ",b.startLoc),q(b.startLoc,a[0]))}):m(f)})}))}})(jQuery);
