var LocationManager=Class.define(function(a){a.CITIES={LOS_ANGELES:{id:"LOS_ANGELES",name:"Los Angeles",state:"CA",country:"US",boundaries:[-118.549144306805,33.57565485544509,-117.7597403753311,34.1540820654869]}};this.initialize=function(){this.buildUserLocations();this._id=Marshal.uniqueId()};this.id=function(){return this._id};this.getDropDownLocations=function(b){if(b.forDelivery){return this.deliveryDropdownLocations}else{return this.dropdownLocations}};this.createPopup=function(b){this.map=null;this.errorMsg=null;this.mapIdx=0;this.popupEnabled=true;this.popupMode=b.mode;this.popupOptions=b;this.buildUserLocations();if(b.mode=="create_new"){this.page="enter_address";b.buttonLabel="search";this.cancelCallback=b.callback;this.chooseBackCallback=function(){this.page="enter_address"}.bind(this);this.enterCallback=function(d,c){if(d==a.STATUS_FOUND){this.chosenLocation=this.createLocation(c[0]);if(Location.getAccuracy(this.chosenLocation)<8){this.chosenLocation=this.addLocation(this.chosenLocation);this.buildUserLocations();b.callback(a.STATUS_FINISHED,this.chosenLocation)}else{this.page="enter_details"}}};this.detailsBackCallback=function(){this.page="enter_address"};this.detailsCallback=function(c){if(c==a.STATUS_FINISHED){var d=this.addLocation(this.chosenLocation);this.buildUserLocations();b.callback(a.STATUS_FINISHED,d)}else{b.callback(arguments)}}}else{if(b.mode=="edit_existing"){this.page="enter_address";b.buttonLabel="search";this.cancelCallback=b.callback;this.chosenLocation=Marshal.deepCopy(b.loc);this.chooseBackCallback=function(){this.page="enter_address"}.bind(this);this.detailsCallback=function(c){if(c==a.STATUS_FINISHED){$replaceMerge(b.loc,this.chosenLocation);b.loc.histIndex=UserPrefs.current().getNewLocationHistoricalIndex();this.buildUserLocations();b.callback(a.STATUS_FINISHED,b.loc)}else{b.callback(arguments)}};this.detailsBackCallback=function(){this.page="enter_address"};this.enterCallback=function(d,c){if(d==a.STATUS_FOUND){Location.applyGoogleAddress(this.chosenLocation,c[0]);if(Location.getAccuracy(this.chosenLocation)<8){$replaceMerge(b.loc,this.chosenLocation);b.loc.histIndex=UserPrefs.current().getNewLocationHistoricalIndex();this.buildUserLocations();b.callback(a.STATUS_FINISHED,b.loc)}else{this.page="enter_details"}}}}else{if(b.mode=="choose_address"){this.page="choose_address";this.chooseCallback=b.callback;this.foundLocs=b.foundLocs;this.cancelCallback=b.callback;this.chooseBackCallback=function(){b.callback(a.CANCEL)}}else{if(b.mode=="edit_details"){this.page="enter_details";this.cancelCallback=b.callback;this.detailsCallback=b.callback;this.chosenLocation=b.loc}}}}};this.dismissPopup=function(){this.popupEnabled=false;delete this.chosenLocation;delete this.popupOptions;delete this.popupMode;delete this.map;delete this.foundLocs;delete this.detailsBackCallback;delete this.cancelCallback;delete this.chosenLocation;delete this.chooseBackCallback;delete this.enterCallback;Lightview.hide()};this.mapVisible=function(){return true};this.updateMapCoords=function(b,c){GoogleMaps.execute(function(){if(!this.map){this.map=new GMap2($(c));this.map.addControl(new GSmallMapControl())}this.map.clearOverlays();if(b){var d=new GLatLng(b[1],b[0]);this.map.setCenter(d,15);this.map.addOverlay(new GMarker(d,{clickable:false}))}}.bind(this))};this.setPage=function(b){this.page=b;this.errorMsg=null};this.matchGoogleAddress=function(d,b){if(!d.google_point){return(-1)}for(var c=0;c<b.length;c++){if(d.google_point.coordinates[0]==b[c].Point.coordinates[0]&&d.google_point.coordinates[1]==b[c].Point.coordinates[1]){return(c)}}return(-1)};this.getUserLocations=function(b){if(b&&b.forDelivery){return this.deliveryUserLocations}else{return this.userLocations}};this.buildUserLocations=function(){this.userLocations=UserPrefs.current().locations||[];this.deliveryUserLocations=[];this.dropdownLocations=[];this.deliveryDropdownLocations=[];for(var b=0;b<this.userLocations.length;b++){this.dropdownLocations.push({name:this.userLocations[b].address1||this.userLocations[b].city||this.userLocations[b].zip,value:this.userLocations[b].uuid});if(Location.getAccuracy(this.userLocations[b])>=8){this.deliveryUserLocations.push(this.userLocations[b]);this.deliveryDropdownLocations.push(this.dropdownLocations.last())}}};this.addNewLocation=function(f){var b=this.createLocation(f);var d=UserPrefs.current();d.locations=d.locations||[];for(var c=0;c<d.locations.length;c++){var e=d.locations[c];if(Location.compare(e,b)){d.setDefaultLocation(e);return e}}return this.addLocation(b)};this.addLocation=function(b){var d=UserPrefs.current();d.locations=d.locations||[];var c=d.locations.detect(function(e){return(e.point.coordinates[0]==b.point.coordinates[0]&&e.point.coordinates[1]==b.point.coordinates[1])});if(c){b=c}else{b.histIndex=d.getNewLocationHistoricalIndex();if(d.locations.length>=a.MAX_LOCATIONS){d.pruneOlderLocation()}d.locations.push(b)}d.setDefaultLocation(b);this.buildUserLocations();return b};this.createLocation=function(c){var b=Location.create();Location.applyGoogleAddress(b,c);return b};a.MAX_LOCATIONS=8;a.STATUS_FINISHED="finish";a.CANCEL="cancel";a.STATUS_NOT_FOUND="not_found";a.STATUS_FOUND="found";a.STATUS_FOUND_MULTIPLE="found_multiple";this.search=function(d,c,b){GoogleMaps.execute(function(){GoogleMaps.geocoder().getLocations(d,function(e){var g=a.STATUS_NOT_FOUND;if(e&&(e.Status.code==200)){var f=[];for(var h=0;h<e.Placemark.length;h++){var j=e.Placemark[h];if(j.AddressDetails.Accuracy<(b&&b.forDelivery?8:4)){continue}if(j.AddressDetails.Country.CountryNameCode!="US"){continue}f.push(j);if(f.length==6){break}}if(f.length==1){g=a.STATUS_FOUND}else{if(f.length>1){g=a.STATUS_FOUND_MULTIPLE}else{g=a.STATUS_NOT_FOUND;if(e.Placemark.length>0){f=e.Placemark}}}}c(g,f)}.bind(this))}.bind(this))}});