var AllStores = $H({
  "Independent Stores" : "NH,MA,RI",
  "Heinens" : "OH",
  "Stew Leonard's" : "CT,NY",
  "Food Emporium" : "NY,NJ",
  "A&P" : "CT,NY,NJ",
  "Waldbaum's" : "NY", 
  "Super Fresh" : "DC,DE,MD,NJ,PA",
  "United Supermarkets" : "TX",
  "HEB" : "TX",
  "Giant Eagle" : "PA,OH,MD,WV",
  "Kroger" : "OH",
  "BJ's" : "CT,GA,NJ,NC,DE,MA,OH,MD,FL,PA,NY,MA,RI,SC,NH,VA",
  "Wegmans" : "MD,NJ,NY,PA,VA",
  "Highland Farms" : "ON",
  "JH Harvery" : "GA,FL,SC",
  "Loblaws" : "ON",
  "Real Canadian Superstore" : "ON",                            
  "Zehrs" : "ON",
  "Fortinos" : "ON",
  "Independent Grocers" : "ON",                                                                                                                          
  "Atlantic Superstore" : "PE,NS,NB",
  "Provigo" : "QC",
  "Lowes Food Stores" : "NC,SC,VA",
  "The Fresh Markets" : "AL,AR,FL,GA,IL,IN,KY,LA,MD,MI,MS,OH,NC,PA,SC,TN,VA,WI",
  "Fresh and Easy" : "NV,CA,AZ",
  "Woodmans" : "WI,IL",
  "Walts" : "IL",                                                                  
  "Lunds" : "MN"   
});


/**
**
*/
var StateAbbreviations = {
'AL' : 'Alabama',
'AK' : 'Alaska',
'AS' : 'American Samoa',
'AZ' : 'Arizona',
'AR' : 'Arkansas',
'CA' : 'California',                                                                                                                                    
'CO' : 'Colorado',
'CT' : 'Connecticut',                
'OH' : 'Ohio',
'DE' : 'Delaware',
'DC' : 'District of Columbia',
'FL' : 'Florida',
'PR' : 'Puerto Rico',
'GA' : 'Georgia',
'GU' : 'Guam',
'HI' : 'Hawaii',
'SD' : 'South Dakota',
'ID' : 'Idaho',
'IL' : 'Illinois',
'IN' : 'Indiana',                                                                                                                                                                     
'IA' : 'Iowa',
'KS' : 'Kansas',                              
'KY' : 'Kentucky',  
'Vi' : 'Virgin Islands',
'LA' : 'Louisiana',
'ME' : 'Maine',
'MD' : 'Maryland',
'MA' : 'Massachusetts',
'MI' : 'Michigan',   
'MN' : 'Minnesota',
'MS' : 'Mississippi',
'MO' : 'Missouri',                                                                                                                                  
'MT' : 'Montana',    
'NE' : 'Nebraska',
'NV' : 'Nevada',
'NH' : 'New Hampshire',
'NJ' : 'New Jersey',
'NM' : 'New Mexico',
'NY' : 'New York',
'NC' : 'North Carolina',
'ND' : 'North Dakota',
'OK' : 'Oklahoma',                                          
'OR' : 'Oregon',                                                                                                                                            
'PA' : 'Pennsylvania',
'RI' : 'Rhode Island',
'SC' : 'South Carolina',                                                                                                                                    
'TN' : 'Tennessee',
'TX' : 'Texas',
'UT' : 'Utah',
'VT' : 'Vermont',
'VA' : 'Virginia',
'WA' : 'Washington',
'WV' : 'West Virginia',
'WI' : 'Wisconsin',                                                                                                                                                               
'WY' : 'Wyoming',            

'AB' : 'Alberta',
'BC' : 'British Columbia',
'MB' : 'Manitoba',
'NB' : 'New Brunswick',
'NL' : 'Newfoundland/Labrador',
'NT' : 'Northwest Territories',
'NS' : 'Nova Scotia',
'NU' : 'Nunavut',
'ON' : 'Ontario',
'PE' : 'Prince Edward Island ',
'QC' : 'Quebec',
'SK' : 'Saskatchewan',
'YT' : 'Yukon'                                                                                                                     
}

/*
**
*/
var LocationObject = Class.create({
    initialize: function(state, store) {
      this.stores = new Array(store);
      this.abbreviation = state;
      this.state = StateAbbreviations[state];
    },
    add: function(store) {
      this.stores[this.stores.size()] = store;
      this.stores = this.stores.uniq();
    }
});


/*
**
*/
var LocationsHash = Class.create(Hash, {
    add: function(state, store) {
      var a = this.get(state);
      if (!a) {
        this.set(state, new LocationObject(state, store));
      } else {
        a.add(store);
      }
    },
    
    addMany: function(states, store) {
      states.each(function(s) {                                                                                                                                
        this.add(s, store);
      }, this);
    },
    
    stores: function(state) {
      this.get(state);
    },
    
    states: function() {
      return this.values().collect(function(x) { return x.state; }).sort();
    },
                                                                                                                                                     
    locations: function() {                                                                                                                            
      return this.values().sortBy(function(x) { return x.state; });
    }
    
});


function findLocationsForState(state) {
  var a = storeLocations.get(state);
  $('locatorResults').innerHTML = "";
  if (state != "0") {
  $('locatorResults').insert({'bottom':"<li class='state'>"+a.state+"</li>"});
  a.stores.sort().each(function(x) {
    $('locatorResults').insert({'bottom':"<li class='store'>"+x+"</li>"});
  });
  }               
}

function fillSelect() {
  storeLocations.locations().each(function(x) {
      $('locatorSelect').insert({'bottom':"<option value='"+x.abbreviation+"'>"+x.state+"</option>"});
  });
  $('defaultLocation').selected = true;
  $('locatorSelect').selectedIndex = 0;
}


var storeLocations = new LocationsHash();
AllStores.each(function(h) {
    this.addMany(h.value.split(","), h.key); 
}, storeLocations);


//alert(storeLocations.get("ON").state);

//alert(storeLocations.for_select());

