// This script requires prototype.js v1.6.
// http://www.prototypejs.org

// required variables to make things happen
var GBrowserIsCompatible;
var map;
var baseIcon;

// useful Google map utility functions
var map_utils = {
  loading_indicator_element: 'loading_indicator',  
  
  //containers for map elements
  map_locations: '',

  loadMapWithDefaults: function() {
    map = new GMap2($("map"));
    map.enableContinuousZoom();
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map_utils.resetAndCenter();
    
    baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    baseIcon.iconSize = new GSize(20, 34);
    baseIcon.shadowSize = new GSize(37, 34);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);
  },
  resetAndCenter: function() {
    map.setCenter(new GLatLng(38.40302528453207, -122.574462890625), 10);
  },
  setData: function(json) {
    this.map_locations = eval(json);
  },
  displayLocations: function() {
    $$('div.supplemental')[0].insert({ bottom: "<div id='locations_list'></div>" })
    var bounds = new GLatLngBounds;
    for (var i = 0; i < this.map_locations.length; i++) {
      if(this.map_locations[i].lat != '') {
        var latlng=new GLatLng(this.map_locations[i].lat,this.map_locations[i].lng)
        bounds.extend(latlng);
        map.addOverlay(this.createMarker(latlng, this.map_locations[i], i));          
      }
    }
    map.setCenter(bounds.getCenter(),map.getBoundsZoomLevel(bounds));
  },
  createMarker: function(latlng, producer, index) {
    var letter = String.fromCharCode("A".charCodeAt(0) + index);
    var letteredIcon = new GIcon(baseIcon);
    letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

    markerOptions = { icon:letteredIcon };
    var marker = new GMarker(latlng, markerOptions);

    var html="<strong>"+producer.company_name+"</strong>";
    if(producer.street) html += "<br/>" + producer.street;
    if(producer.street2) html += "<br/>" + producer.street2;
    html += "<br/>" + producer.city + ", " + producer.state + " " + producer.postal;
    if(producer.phone) html += "<br/>" + producer.phone;
    
    var image_tag = "<img src='" + letteredIcon.image + "' />";
    var sidebar_entry = "<div class='map_location'>" + image_tag + html + "</div>";
    
    $('locations_list').insert({ bottom: sidebar_entry });
    GEvent.addListener(marker, "click", function() {
      map.openInfoWindowHtml(latlng, html);
    });
    return marker;
  },
  loadMap: function () {
    if($('map').hasClassName('off')) {
      if($('profile-sub-links')) $('profile-sub-links').hide();
      if($('profile_assets')) $('profile_assets').hide();
      if($$('.blurb')) $$('.blurb').invoke('hide');
      $('map').removeClassName('off');
      Effect.BlindDown('map', {
        afterFinish: this.revealMapAndDisplayData
      });
    } else {
      Effect.SlideUp('map', {
        afterFinish: this.resetVisibilityOfKeyElements
      });
      return false; 
    }
  },
  revealMapAndDisplayData: function () {
    $('map').show();
    map_utils.loadMapWithDefaults();
    map_utils.displayLocations();
  },
  resetVisibilityOfKeyElements: function () {
    $('map').addClassName('off');
    $('locations_list').remove();
    if($('profile-sub-links')) $('profile-sub-links').show();
    if($('profile_assets')) $('profile_assets').show();
    if($$('.blurb')) $$('.blurb').invoke('show');
  }
}