//
// Geolocation core javascript
//
// Author:  Andrew Waer
// Date:    March 2009
//
// Requires: Google Ajax API, EI.Cookie
// 
// This namespace is for geolocation functionality. Relies on Google data as canonical location.
// 

if (typeof EI != 'undefined') {

  EI.namespace('Geolocation');

  // Encapsulates location information returned from geocoding request.
  // 
  // Parameters:
  // 
  //   loc: Object with the following properties: latitude, longitude, streetAddress, city, state, postalCode, countryCode
  // 
  EI.Geolocation = function(loc) {

    var self = this;

    var _cookie = new EI.Cookie('_ei_geoloc');
  	var _location = loc || {};

    function storeCookie(loc) {
      if (loc) {
        ['latitude','longitude','city','state','countryCode'].each(function(p){
          if (loc[p]) {
            self[p] = _cookie[p] = loc[p];
          }
        });
      }
      _cookie.store(999,'/');
    }

    // Reset object and cookie with Google geocoding values
    function syncWithGoogle() {
      var loc = {};

      loc['latitude']      = google.loader.ClientLocation.latitude;
      loc['longitude']     = google.loader.ClientLocation.longitude;

      if (google.loader.ClientLocation.address) {
        loc['city']        = google.loader.ClientLocation.address.city;
        loc['state']       = google.loader.ClientLocation.address.region;
        loc['countryCode'] = google.loader.ClientLocation.address.country_code; 
      }
      storeCookie(loc);
    }

    self.latitude      = _location.latitude      || _cookie.latitude  || null;
    self.longitude     = _location.longitude     || _cookie.longitude || null;
    self.streetAddress = _location.streetAddress || null;
    self.city          = _location.city          || null;
    self.state         = _location.state         || null;
    self.postalCode    = _location.postalCode    || null;
    self.countryCode   = _location.countryCode   || null;
    self.accuracy      = _location.accuracy      || 10;
    self.maxAccuracy   = 100;

    // Only sync if values are empty, do not crush custom user location
    if (!self.longitude && !self.latitude && google.loader.ClientLocation) {
      syncWithGoogle();
    }

    // Returns whether this geocoding request successfully recovered a geographic point
    // 
    this.isSuccess = function() {
      return (self.latitude != null && self.longitude != null);
    };

    // Returns whether this geolocation is located in the US
    // 
    this.isUS = function(){
      return /^US$/i.match(self.countryCode);
    };

    // Updates cookie geolocation property values with custom values
    // 
    this.updateLocation = function(loc) {
      storeCookie(loc);
    };

    // Empties cookie of geolocation values and resets to Google geolocation values
    //  
    this.resetLocation = function() {
      ['latitude','longitude','city','state','countryCode'].each(function(p){
        delete _cookie[p];
      });
      syncWithGoogle();
    };

  }
}