
/* set up our namespace */
if (typeof(window.gsf) === 'undefined') gsf = {};

gsf.deliverymap = {

  /* constants */
  kml_url: 'http://www.goldenstatefirewood.com/media/kml/delivery-map.kmz',
  center_lat: 37.75785,
  center_lng: -122.16807,
  mapDomId: '#mapWrap',

  /* GMap handles */
  googleGeocoder: null,
  googleMap: null,

  mapCenterIs: function(addr, callback_onsuccess, callback_onerror) {
    var evt_this = this;
    this.googleGeocoder.geocode(
      { 'address': addr },
      function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          var geo = results[0].geometry;
          evt_this.googleMap.fitBounds(geo.viewport);

          var marker = new google.maps.Marker({
            map: this.googleMap,
            position: geo.location,
          });
          marker.setMap(evt_this.googleMap);

          var infowindow = new google.maps.InfoWindow();
          infowindow.setContent(results[0].formatted_address);
          infowindow.open(evt_this.googleMap, marker);

          google.maps.event.addListener(
            marker, 'click', function(evt) {
              infowindow.open(evt_this.googleMap, marker);
            }
          );
          callback_onsuccess();
        }
        else {
          callback_onerror();
        }
      }
    );
  },

  bodyOnLoad: function() {
    /* init the map */
    var map_wrapper = $(this.mapDomId)[0];
    var map_center =
      new google.maps.LatLng(this.center_lat, this.center_lng);
    var map_opts = {
      zoom: 9,
      center: map_center,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      mapTypeControl: false,
    }
    this.googleMap = new google.maps.Map(map_wrapper, map_opts);
    this.googleGeocoder = new google.maps.Geocoder();

    /* init the kml */
    var kml_opts = {
      preserveViewport: true,
      suppressInfoWindows: true,
    }
    var kml = new google.maps.KmlLayer(this.kml_url, kml_opts);
    kml.setMap(this.googleMap);
  },
};

gsf.addressfinder = {

  /* constants */
  formDomId: '#delivery-finder-form',
  inputDomId: '#inputAddr',
  resultsDomId: '#delivery-finder-results',

  /* DOM handles */
  formNode: null,
  inputNode: null,
  resultsNode: null,

  /* state */
  _inputHasFocused: false,
  _address: '',

  address: function() { return this._address; },
  addressIs: function(val) {
    if (val == this._address) return;
    this._address = val;
    if (val != '') {
      var evt_this = this;
      gsf.deliverymap.mapCenterIs(
        val,
        function() { evt_this.centerMapOnSuccess(); },
        function() { evt_this.centerMapOnError(); }
      );
    }
  },

  results: function() {
    return this.resultsNode.html();
  },
  resultsIs: function(val) {
    this.resultsNode.html(val);
  },

  inputOnFocus: function() {
    if (this._inputHasFocused) return;
    this.inputNode.val('');
    this.inputNode.addClass('hasFocused');
    this._inputHasFocused = true;
  },

  formOnSubmit: function(evt) {
    this.addressIs(this.inputNode.val());
    evt.preventDefault();
  },

  centerMapOnSuccess: function() {
    this.resultsIs('');
    this.addressIs('');
  },

  centerMapOnError: function() {
    var mes =
      "Sorry, the address: '" + this.address() + "' could not be found."
    this.resultsIs(mes);
    this.addressIs('');
  },

  bodyOnLoad: function() {
    this.formNode = $(this.formDomId);
    this.inputNode = $(this.inputDomId);
    this.resultsNode = $(this.resultsDomId);

    var evt_this = this;
    this.formNode.submit(function(evt) { evt_this.formOnSubmit(evt); });
    this.inputNode.focus(function(evt) { evt_this.inputOnFocus(evt); });
  },
};

