﻿function LocationWindow(target){this.target=target;this.callbacks=[];this.outerDiv=pyd$("divLocations");this.tabNew=pyd$("divNewLocTabLocation");this.tabFav=pyd$("divNewLocTabFavorites");this.bodyNew=pyd$("divNewLocBodyLocation");this.bodyFav=pyd$("divNewLocBodyFavorites");this.newAddrBox=pyd$("txtNewLocAddress");this.newCityStateBox=pyd$("txtNewLocCityState");this.favoriteNameBox=pyd$("txtNewFavoriteName");this.saveNewFavoriteCheckBox=pyd$("chkSaveNewFavorite");this.makeDefaultCheckBox=pyd$("chkNewLocationDefault");this.newLocationAddressDiv=pyd$("divNewLocAddress");this.newLocationGpsDiv=pyd$("divNewLocGPS");this.favoritesList=pyd$("selFavoriteLocations");this.gpsDecimalBox=pyd$("txtNewLocGpsDecimal");this.gpsDegreesBox=pyd$("txtNewLocGpsDegrees");this.width=0;this.preSaveName="";this.autoRefreshFavoritesOnSet=true;this.loadFavorites();};LocationWindow.prototype.toString=function(){return "LocationWindow";};LocationWindow.prototype.addListener=function(func){this.callbacks[this.callbacks.length]=func;};LocationWindow.prototype.notify=function(latlng,name){var i;if(0<this.callbacks.length){for(i=0;i<this.callbacks.length;i++){this.callbacks[i].call(window,latlng,name);}}};LocationWindow.prototype.getGPS=function(){var coords=null;var latlng=null;var ok,lat,lng;var latString,lngString;try{if(0<this.gpsDecimalBox.value.length){coords=this.gpsDecimalBox.value.trim().split(",");lat=parseFloat(coords[0].trim());lng=parseFloat(coords[1].trim());ok=true;}else if(0<this.gpsDegreesBox.value.length){coords=this.gpsDegreesBox.value.trim().split(",");latString=coords[0].trim().split(" ");lat=parseFloat(latString[0])+(parseFloat(latString[1])/60);if("s"==latString[2].toLowerCase()){lat=-lat;}lngString=coords[1].trim().split(" ");lng=parseFloat(lngString[0])+(parseFloat(lngString[1])/60);if("w"==lngString[2].toLowerCase()){lng=-lng;}ok=true;}if(ok){if(-90<=lat&&lat<=90&&-180<=lng&&lng<=180){latlng=new GLatLng(lat,lng);}}}catch(e){latlng=null;}return latlng;};LocationWindow.prototype.getNewAddress=function(){return(this.newAddrBox.value.trim()+" "+this.newCityStateBox.value.trim()).trim();};LocationWindow.prototype.init=function(){this.outerDiv.style.top="-1000px";this.outerDiv.style.display="";this.width=this.outerDiv.offsetWidth;this.outerDiv.style.display="none";};LocationWindow.prototype.windowResizeHandler=function(){if(this.isVisible()){this.position();}};LocationWindow.prototype.position=function(){try{var pos=getAbsolutePos(this.target);this.outerDiv.style.left=(pos.x+this.target.offsetWidth-this.width)+"px";this.outerDiv.style.top=(pos.y+this.target.offsetHeight)+"px";}catch(e){showError(e);}};LocationWindow.prototype.isVisible=function(){if(""==this.outerDiv.style.display){return true;}else{return false;}};LocationWindow.prototype.isNewLocationFormVisible=function(){if(""==this.bodyNew.style.display){return true;}else{return false;}};LocationWindow.prototype.isGpsFormVisible=function(){if(""==this.newLocationGpsDiv.style.display){return true;}else{return false;}};LocationWindow.prototype.show=function(){try{this.position();if(!this.isVisible()){this.setNewLocationTab();this.newAddrBox.value="";this.newCityStateBox.value="";this.favoriteNameBox.value="";this.saveNewFavoriteCheckBox.checked=false;this.makeDefaultCheckBox.checked=false;this.favoritesList.selectedIndex=-1;this.gpsDecimalBox.value="";this.gpsDegreesBox.value="";this.outerDiv.style.display="";}}catch(e){showError(e);}};LocationWindow.prototype.hide=function(){this.outerDiv.style.display="none";};LocationWindow.prototype.setNewLocationTab=function(){this.bodyNew.style.display="";this.bodyFav.style.display="none";this.tabNew.style.backgroundColor="#ffffff";this.tabFav.style.backgroundColor="#bee7fe";};LocationWindow.prototype.setFavoritesTab=function(){this.bodyFav.style.display="";this.bodyNew.style.display="none";this.tabFav.style.backgroundColor="#ffffff";this.tabNew.style.backgroundColor="#bee7fe";};LocationWindow.prototype.setNewLocationToAddress=function(){this.newLocationAddressDiv.style.display="";this.newLocationGpsDiv.style.display="none";};LocationWindow.prototype.setNewLocationToGPS=function(){this.newLocationAddressDiv.style.display="none";this.newLocationGpsDiv.style.display="";};LocationWindow.prototype.geocode=function(input){try{var geocoder=new GClientGeocoder();geocoder.getLatLng(input,PyDEvent.createCallback(this,this.geocodeCallback));}catch(e){showError(e);}};LocationWindow.prototype.geocodeCallback=function(point){try{if(!point){throw new CustomError("No location could be determined using your input.\n\n"+"Check your input and try again.");}this.saveNewLocation(point,false);}catch(e){showError(e);}};LocationWindow.prototype.deleteFavorite=function(){try{if(0==this.favoritesList.selectedIndex){throw new CustomError("You cannot delete your default location.");}var name=getListText(this.favoritesList);if(null==name){throw new CustomError("Select one of your favorites to delete first.");}else if(""==getListValue(this.favoritesList)){throw new CustomError("Wait until the list is finished loading.");}var params=[];params[0]="deletefavoritelocation";params[1]=name;var name=new PyDajax(params).send();this.loadFavorites();}catch(e){showError(e);}};LocationWindow.prototype.getSelectedFavoriteName=function(){var name=getListText(this.favoritesList);if(null!=name){name=name.replace(" (default)","").trim();}return name;};LocationWindow.prototype.renameFavorite=function(){try{var name=this.getSelectedFavoriteName();if(null==name){throw new CustomError("Select one of your favorites to rename first.");}else if(""==getListValue(this.favoritesList)){throw new CustomError("Wait until the list is finished loading.");}ae_prompt(this.renameFavoriteCallback,"Enter a new name for this favorite.",name,this);}catch(e){showError(e);}};LocationWindow.prototype.renameFavoriteCallback=function(oldVal,newVal,locwin){try{if(null!=newVal){if(-1<newVal.toLowerCase().indexOf("default")){throw new CustomError("The name cannot include the word 'default'.");}var params=[];params[0]="renamefavoritelocation";params[1]=oldVal;params[2]=newVal;new PyDajax(params).send();locwin.loadFavorites();if(pyd$("spnCurrentLocation").innerHTML==oldVal){pyd$("spnCurrentLocation").innerHTML=newVal;}}}catch(e){showError(e);}};LocationWindow.prototype.saveNewLocation=function(point,isFavorite){try{var params=[];params[0]="setlocation";params[1]=point.lat();params[2]=point.lng();params[3]=this.preSaveName;if(__isSignedIn){params[4]=this.favoriteNameBox.value.trim();params[5]=this.saveNewFavoriteCheckBox.checked;params[6]=this.makeDefaultCheckBox.checked;}else{params[4]="";params[5]=false;params[6]=false;}params[7]=isFavorite;var name=new PyDajax(params).send();this.hide();this.notify(point,name);if(this.autoRefreshFavoritesOnSet){this.loadFavorites();}}catch(e){showError(e);}};LocationWindow.prototype.update=function(){var latlng,gps,coords;this.preSaveName="";try{if(this.isNewLocationFormVisible()){if(this.isGpsFormVisible()){latlng=this.getGPS();if(!latlng){throw CustomError("The GPS coordinates you entered do not appear to be valid.\n\n"+"Use either decimal format (e.g. 47.123, -122.456) or degrees/minutes format (e.g. 47 12.3 N, 122 5.6 W).");}this.saveNewLocation(latlng,false);}else{this.preSaveName=this.newCityStateBox.value.trim();if(0==this.preSaveName.length){throw new CustomError("Enter a city/state, region name or ZIP code.");}this.geocode(this.getNewAddress());}}else{gps=getListValue(this.favoritesList);if(null==gps){throw new CustomError("Select one of your favorite locations to switch to.");}else if(0==gps.length){throw new CustomError("Wait until the list is finished loading.");}coords=gps.split(",");latlng=new GLatLng(coords[0],coords[1]);this.saveNewLocation(latlng,true);}}catch(e){showError(e);}};LocationWindow.prototype.loadFavorites=function(){try{if(__isSignedIn){emptyList(this.favoritesList);addListItem(this.favoritesList,"","Loading...");new PyDajax("getfavoritelocations",true,this,this.loadFavoritesCallback).send();}}catch(e){showError(e);}};LocationWindow.prototype.loadFavoritesCallback=function(xml){var node,name,i;try{if(xml){emptyList(this.favoritesList);for(i=0;i<xml.documentElement.childNodes.length;i++){node=xml.documentElement.childNodes[i];name=node.getAttribute("Name");if(0==i){name+=" (default)";}addListItem(this.favoritesList,node.getAttribute("Lat")+","+node.getAttribute("Lng"),name);}}}catch(e){showError(e);}};