﻿YAHOO.namespace("SitNSleep.Widget");

(function() {
  
  YAHOO.SitNSleep.Widget.LocationMap = function(container, stores, cfg){
    var cfg = (null == cfg) ? {} : cfg;
    this.stores = stores;
    this.container = YAHOO.util.Dom.get(container);
    this.geocoder = new google.maps.Geocoder();
    this.zipCode = this.updateZipCode();
    var myLatlng = new google.maps.LatLng(33.904616,-118.138733);//LA Basin
    this.mapOptions = {
      zoom: 8,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    this.map = new google.maps.Map(this.container, this.mapOptions);
    
    if(this.stores){
      for(var i=0;i<this.stores.length;i++){
        this.addMarkerForStore(this.stores[i]);
      }
    }
    
    if(this.zipCode){
      this.addUserZipCodeMarker(this.zipCode);
    }
  };

  YAHOO.SitNSleep.Widget.LocationMap.prototype = {
    
    setZipCode: function(zipCode){
      var expirationDate = new Date();
      expirationDate.setDate(expirationDate.getDate()+365); //expires one year from now
      YAHOO.util.Cookie.set("zipCode", zipCode, { 
        expires: expirationDate
      }); 
    },
    
    updateZipCode: function(){
      var zipReg = new RegExp("[\\?&]zipCode=([^&#]*)");
      var query = zipReg.exec(window.location.href);
      var zipCode = null;

      if(query){
        zipCode = query[1];
      }
      
      if(!zipCode || "" == zipCode){
        zipCode = YAHOO.util.Cookie.get("zipCode");
      } else {
        this.setZipCode(zipCode);
      }
      
      return zipCode;
    },
    
    addMarkerForStore: function(store){
      this.geocoder.geocode( { 'address': store.streetAddress + ", " + store.city + ", " + store.state + " " + store.zipCode},
        this.addMarker(store.name,{
          icon:'http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png',
          infoWindow: new google.maps.InfoWindow({
            content: "<div><strong>Sit &lsquo;n Sleep " 
            + store.name + "</strong></div>" 
            + store.streetAddress + "<br/>" 
            + store.city + ", " + store.state + " " 
            + store.zipCode + "<br/><strong>" 
            + ((store.phone) ? store.phone + "</strong>" : "") })
          }
        )
      );
    },
    
    //Creates a closure around the addMapMarker function with the necessary vars
    addMarker: function(title, cfg){
      var cfg = (null == cfg) ? {} : cfg;
      var title = title;
      var map = this.map;
      function addMapMarker(results, status){
        if (status == google.maps.GeocoderStatus.OK) {
          if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
            map.set_center(results[0].geometry.location);
            var options = {
                map: map,
                title: title,
                position: results[0].geometry.location
            };
            if(cfg.icon){
              options.icon = cfg.icon;
            }
            var marker = new google.maps.Marker(options);
            if(cfg.infoWindow){
              google.maps.event.addListener(marker, 'click', function() {
                cfg.infoWindow.open(map,marker);
              });
            }
          } else {
            alert("Zip code not found");
          }
        } else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT){
          //do nothing, it seems to call the callback again when the query limit is reset (flood)
        }
        else {
          alert("Geocode was not successful for the following reason: " + status);
        }
      }
      return addMapMarker;
    },
    
    addUserZipCodeMarker: function(zipCode){
      this.geocoder.geocode( { 'address': zipCode}, this.addMarker(zipCode));
    }
    
  };
  
})();





