/*
 DMXzone Google Maps Directions Add-on
 Version: 1.0.1
 (c) 2011 DMXzone.com
 @build 26-07-2011 14:30:59
*/
(function(e){var u={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};e.fn.dmxGMDirections=function(n){function m(h,c){b.debug&&console.log("calcRoute start",h,c);h&&c&&(o=!0,s.route({origin:h,destination:c,provideRouteAlternatives:b.provideRouteAlternatives,travelMode:b.travelMode,unitSystem:b.unitSystem},
function(b,h){switch(h){case google.maps.DirectionsStatus.OK:i.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.!")}}))}function l(){var b;a&&a.markers&&a.markers.length>0&&(b=a.markers[a.markers.length-1].getPosition(),a.markers[a.markers.length-
1].setMap(null),a.markers.pop());return b}function p(b,c,a){var g=c.formatted_address;e("#"+b).val(g);g=c.formatted_address;e("#"+b).trigger("select",[g,g,{data:g,value:g,latLng:c.geometry.location,result:g,obj:c},a])}function t(b,c){c==null&&(c=a.markers.length-1);if(a&&a.markers&&a.markers.length>0)if(a.markers.length==1)j=a.markers[c].getPosition(),m(b,j);else for(var d=a.markers.length-1;d>=0;d--)v(b,d)}function v(h,c){a.markers[c].dmxDistance=-1;var d={origin:h,destination:a.markers[c].getPosition(),
provideRouteAlternatives:!1,travelMode:b.travelMode,unitSystem:b.unitSystem};s.route(d,function(d,e){if(e==google.maps.DirectionsStatus.OK)a.markers[c].dmxDistance=d.routes[0].legs[0].distance.value,b.debug&&console.log("route "+c+" distance "+a.markers[c].dmxDistance);if(c==0){for(var f=a.markers[0].dmxDistance,i=0,k=1;k<a.markers.length;k++)if(f==-1||f>a.markers[k].dmxDistance&&a.markers[k].dmxDistance!=-1)f=a.markers[k].dmxDistance,i=k;b.debug&&console.log("found nearest "+i);j=a.markers[i].getPosition();
m(h,j)}})}function w(a){var c;navigator.geolocation?navigator.geolocation.getCurrentPosition(function(d){b.debug&&console.log("got browser current location ..");c=new google.maps.LatLng(d.coords.latitude,d.coords.longitude);a(c)}):google.gears&&google.gears.factory.create("beta.geolocation").getCurrentPosition(function(b){c=new google.maps.LatLng(b.latitude,b.longitude);a(c)})}var b=e.extend(u,n,{}),i,n=e(this.selector),a=null,q=null,f=null,j=null,r=e("#"+b.useMap);if(r&&r.gMap)a=r.data("gmap"),q=
a.gmap,b.endLoc&&b.endLoc!=""&&e("#"+b.endLoc).bind("result",function(h,c,d,g){b.debug&&(console.log("got AAC end result",g),console.log("end markers",a.markers));if(!b.startLoc||b.startLoc&&e("#"+b.startLoc).val()!="")j=l(),f||(f=l()),m(f,j)});var s=new google.maps.DirectionsService,o=!0;i=new google.maps.DirectionsRenderer({draggable:b.draggable,hideRouteList:!b.provideRouteAlternatives});q&&i.setMap(q);b.directionsDisplay&&i.setPanel(n.get(0));b.draggable&&google.maps.event.addListener(i,"directions_changed",
function(){if(!o){var a=i.getDirections();if(a&&a.routes[0]&&a.routes[0].legs[0]){a=a.routes[0].legs[0];if(b.startLoc){var c=new google.maps.Geocoder;c.geocode({location:a.start_location},function(a,c){c==google.maps.GeocoderStatus.OK&&p(b.startLoc,a[0],!0)})}b.endLoc&&(c=new google.maps.Geocoder,c.geocode({location:a.end_location},function(a,c){c==google.maps.GeocoderStatus.OK&&p(b.endLoc,a[0],!0)}))}}o=!1});b.startLoc&&e("#"+b.startLoc).bind("result",function(h,c,d,g){b.debug&&(console.log("got AAC start result",
g),console.log("start markers",a.markers));b.endLoc?e("#"+b.endLoc).val()!=""&&(f=l(),j||(j=l()),m(f,j)):(f=l(),t(f))});b.useCurrentLocation&&(b.debug&&console.log("use current location"),e(window).load(function(){w(function(a){b.debug&&console.log("got current location ",a);f=a;b.startLoc?(new google.maps.Geocoder).geocode({location:a},function(a,d){d==google.maps.GeocoderStatus.OK&&(b.debug&&console.log("trigger autocomplete ",b.startLoc),p(b.startLoc,a[0]))}):t(f)})}))}})(jQuery);

