// (c)2007 Mapstraction Team - http://mapstraction.com

function MapstractionRouter(callback,api,error_callback){this.api=api;this.callback=callback;this.routers=new Object();this.geocoders=new Object();if(error_callback==null){this.error_callback=this.route_error}else{this.error_callback=error_callback;}
this.svn_revision_string='$Revision: 107 $';this.addAPI(api);}
MapstractionRouter.prototype.addAPI=function(api){me=this;switch(api){case'mapquest':var proxyServerName="";var proxyServerPort="";var ProxyServerPath="mapquest_proxy/JSReqHandler.php";var geocoderServerName="geocode.access.mapquest.com";var routerServerName="route.access.mapquest.com";var serverPort="80";var serverPath="mq";this.geocoders[api]=new MQExec(geocoderServerName,serverPath,serverPort,proxyServerName,ProxyServerPath,proxyServerPort);this.routers[api]=new MQExec(routerServerName,serverPath,serverPort,proxyServerName,ProxyServerPath,proxyServerPort);break;default:alert(api+' not supported by mapstraction-router');}}
MapstractionRouter.prototype.swap=function(api){if(this.api==api){return;}
this.api=api;if(this.routers[this.api]==undefined){this.addAPI($(element),api);}}
MapstractionRouter.prototype.route_error=function(response){alert("Sorry, we were unable to route that address");}
MapstractionRouter.prototype.route_callback=function(response,mapstraction_router){switch(mapstraction_router.api){case'mapquest':break;}}
MapstractionRouter.prototype.route=function(addresses){var api=this.api;switch(api){case'mapquest':var waypoints=new MQLocationCollection();var mapstraction_points=Array();var gaCollection=new MQLocationCollection("MQGeoAddress");var routeOptions=new MQRouteOptions();for(var i=0;i<addresses.length;i++){var mqaddress=new MQAddress();mqaddress.setStreet(addresses[i].street);mqaddress.setCity(addresses[i].locality);mqaddress.setState(addresses[i].region);mqaddress.setPostalCode(addresses[i].postalcode);mqaddress.setCountry(addresses[i].country);this.geocoders[api].geocode(mqaddress,gaCollection);var geoAddr=gaCollection.get(0);waypoints.add(geoAddr);var mapstraction_point=new Object();mapstraction_point.street=geoAddr.getStreet();mapstraction_point.locality=geoAddr.getCity();mapstraction_point.region=geoAddr.getState();mapstraction_point.country=geoAddr.getCountry();var mqpoint=geoAddr.getMQLatLng();mapstraction_point.point=new LatLonPoint(mqpoint.getLatitude(),mqpoint.getLongitude());mapstraction_points.push(mapstraction_point);}
var session=new MQSession();var routeResults=new MQRouteResults();var routeBoundingBox=new MQRectLL(new MQLatLng(),new MQLatLng());var sessId=this.routers[api].createSessionEx(session);this.routers[api].doRoute(waypoints,routeOptions,routeResults,sessId,routeBoundingBox);var routeParameters=new Array();routeParameters['results']=routeResults;routeParameters['bounding_box']=routeBoundingBox;routeParameters['session_id']=sessId;this.callback(mapstraction_points,routeParameters);break;default:alert(api+' not supported by mapstraction-router');break;}}