function GAddCopyright(a,b,c,d,e,f,g,h,i){
  return true;
}

Effect.ResizeTo = Class.create();
Object.extend(Object.extend(Effect.ResizeTo.prototype, Effect.Base.prototype), {
  initialize: function(element, toWidth, toHeight) {

    this.element      = $(element);
    this.toWidth      = toWidth;
    this.toHeight     = toHeight;


    this.originalWidth  = parseFloat(Element.getStyle(this.element,'width')  || 0);
    this.originalHeight = parseFloat(Element.getStyle(this.element,'height') || 0);

    
    this.effectiveWidth = this.toWidth 
                        - parseFloat(Element.getStyle(this.element,'margin-left') || 0) 
                        - parseFloat(Element.getStyle(this.element,'margin-right') || 0) 
                        - (document.compatMode == 'BackCompat' ? 0 : // height includes padding & border in IE BackCompat mode
                            parseFloat(Element.getStyle(this.element,'padding-left') || 0) 
                            + parseFloat(Element.getStyle(this.element,'padding-right') || 0) 
                            + parseFloat(Element.getStyle(this.element,'border-left-width') || 0)
                            + parseFloat(Element.getStyle(this.element,'border-right-width') || 0));


    this.effectiveHeight = this.toHeight
                        - parseFloat(Element.getStyle(this.element,'margin-top') || 0) 
                        - parseFloat(Element.getStyle(this.element,'margin-bottom') || 0) 
                        - (document.compatMode == 'BackCompat' ? 0 : // height includes padding & border in IE BackCompat mode
                            parseFloat(Element.getStyle(this.element,'padding-top') || 0) 
                            + parseFloat(Element.getStyle(this.element,'padding-bottom') || 0) 
                            + parseFloat(Element.getStyle(this.element,'border-top-width') || 0)
                            + parseFloat(Element.getStyle(this.element,'border-bottom-width') || 0));

    this.options = arguments[3] || {};

    if (this.effectiveWidth < 0) this.effectiveWidth = 0;
    if (this.effectiveHeight < 0) this.effectiveHeight = 0;

    if (this.originalWidth == this.effectiveWidth &&
        this.originalHeight == this.effectiveHeight) {

      // no need to start!
      return;
    }

    this.start(this.options);

  },
  update: function(position) {
    widthd  = this.effectiveWidth * (position) + this.originalWidth * (1 - position);
    heightd = this.effectiveHeight * (position) + this.originalHeight * (1 - position);
    this.setSize(widthd, heightd);
  },
  setSize: function(widthd, heightd) {
    this.element.style.width = widthd+'px';
    this.element.style.height = heightd+'px';
  }
  
});

SMALL = true;
LARGE = false;
__TSM_GLOBAL_MAPS__ = []
__TSM_FILTER_MAP__ = null;


function getFishIcon(icon_path, type){
  if (typeof(icon_path) != 'undefined' && typeof(type) != 'undefined'){
    var icon = new GIcon();
    // Create the icon for the new type
    icon.image = icon_path + type.replace(/\s/g, '_').toLowerCase() + '.png';
    icon.shadow = icon_path + 'shadow.png';
    icon.size = new GSize(23, 60);
    icon.iconSize = new GSize(23, 60);
    icon.iconAnchor = new GPoint(4, 58);
    icon.infoWindowAnchor = new GPoint(12,4);
    return icon;
  }
  else{
    return;
  }
  
}


FilterMap = Class.create();
Object.extend(FilterMap.prototype, {
  initialize: function(options) {
    __TSM_FILTER_MAP__ = this;
    this.options = options;
    this.div = $(options['element_name']);
    this.gmap = new GMap2(this.div);
    this.reset();
    this.data = [];
    this.types = [];
    this.gmap.addControl(new GLargeMapControl());
    if(typeof this.options['icon_path'] == 'undefined'){
      alert("\"icon_path\" was not defined in map options hash.");
    }
    
  },
  reset: function(){
    this.gmap.setCenter(new GLatLng(this.options['center'][0], this.options['center'][1]), this.options['zoom'], G_HYBRID_MAP);
  },
  createMarker: function(item, icon){
    var marker = new GMarker(new GPoint(item['longitude'], item['latitude']), icon);
    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml("<div class=\"InfoWindow\"><h3><a href=\"" + item['url'] + "\">" + item['name'] + "</a></h3><p>" + item['summary'] + "</p></div>");
    });
    return marker;
  },
  filter: function(){
    for(var i=0; i < this.data.length; i++){
      this.gmap.removeOverlay(this.data[i].marker);
    }
    for(var type in this.types){
      
      if ($('filter_checkbox_' + type).checked){
        item_ids = this.types[type];
        for (var i=0; i < item_ids.length; i++){
          this.gmap.addOverlay(this.data[item_ids[i]].marker);
        }
      }
    }
  },  
  setData: function(new_data){
    for(var i=0; i<this.data.length; i++){
      this.gmap.removeOverlay(this.data[i].marker);
    }
    this.data = [];
    this.types = {};
    this.icons = {};
    
    for(var i=0; i < new_data.length; i++){
      var item = new_data[i];
      var type = item['type'];
      
      if (!this.types[type]){
        this.types[type] = [];
        
        this.icons[type] = getFishIcon(this.options['icon_path'], type);
        
      }
      
      //Put the index of the current item into the type's array
      this.types[item['type']].push(i);
      
      var marker = this.createMarker(item, this.icons[type]);
      this.gmap.addOverlay(marker);
      
      this.data.push({
        name: item['name'], 
        url: item['url'], 
        summary: item['summary'],
        type: item['type'],
        marker: marker
      });
      
    }
    for (var x in this.types){
      $(this.options['filter_list_element_name']).appendChild(this.buildFilterNode(x));
    }
  },
  buildFilterNode: function(type){
    return Builder.node(
      'li',
      {className:'Filter'}, 
      [
        Builder.node(
          'input',
          {type: 'checkbox', id: 'filter_checkbox_' + type, checked: 'checked', onclick: "__TSM_FILTER_MAP__.filter()"}
        ),
        Builder.node(
          'label',
          {For: 'filter_checkbox_' + type, id: 'filter_label' + type},
          'Show ' + type
        )
      ]
      );
  }
});

SinglePointMap = Class.create();
Object.extend(SinglePointMap.prototype, {
  baseInit: function(options){
    this.options = options;
    this.current_size = SMALL;
    this.wrapper = $(options['wrapper']);
    this.div = $(options['element_name']);
    this.gmap = new GMap2(this.div);
    this.reset();
    this.control = new GSmallMapControl();
    this.marker = null;
    this.gmap.think_salmon_object = this;
    
    
    this.icon = getFishIcon(this.options['icon_path'], this.options['type']);
    
    //A hack that ensures there is always an icon.
    if (typeof(this.icon) != 'object'){
      this.icon = new GMarker(new GPoint(0,0)).icon;
    }
    
    
    if(this.options['marker']){
      this.marker_latlng = new GLatLng(this.options['marker'][0], this.options['marker'][1]);
      
      this.marker = new GMarker(this.marker_latlng, this.icon);
      
      this.gmap.addOverlay(this.marker);
      this.gmap.panTo(this.marker_latlng);
    }
    this.resizeCallback = this.options['resize_callback'];
    
    
    __TSM_GLOBAL_MAPS__[__TSM_GLOBAL_MAPS__.length] = this;
    
  },
  initialize: function(options) {
    this.baseInit(options);
  },
  beforeResize: function(){
    this.gmap.savePosition()
  },
  afterResize: function(){
    this.gmap.checkResize();
    this.gmap.returnToSavedPosition();
    if(typeof this.resizeCallback == 'function'){
      this.resizeCallback(this.current_size);
    }
  },
  reset: function(){
    this.gmap.setCenter(new GLatLng(this.options['center'][0], this.options['center'][1]), this.options['zoom'], G_HYBRID_MAP);
  },
  toggleSize: function(){
    if(this.current_size == SMALL){
      if(navigator.userAgent.indexOf('MSIE') > 0){
        this.beforeResize();
        this.div.style.width = this.options['large_size'][0] + 'px';
        this.div.style.height = this.options['large_size'][1] + 'px';
        this.wrapper.style.width = this.options['large_size'][0] + 'px';
        this.wrapper.style.height = this.options['large_size'][1] + 20 + 'px'; 
        this.afterResize();
      }
      else{
        new Effect.ResizeTo(this.wrapper, this.options['large_size'][0], this.options['large_size'][1] + 20);
        new Effect.ResizeTo(
          this.div, 
          this.options['large_size'][0], 
          this.options['large_size'][1],
          {
            beforeStart: function(){
              __TSM_GLOBAL_MAPS__.each(function(map){
                map.beforeResize();
              })
          },
            afterFinish: function(){
              __TSM_GLOBAL_MAPS__.each(function(map){
                map.afterResize();
            })
            }
          }
          );
      }
      this.gmap.addControl(this.control);
      this.current_size = LARGE;
    }
    else{
      this.gmap.removeControl(this.control);
      if(navigator.userAgent.indexOf('MSIE') > 0){
        this.beforeResize();
        this.div.style.width = this.options['size'][0] + 'px';
        this.div.style.height = this.options['size'][1] + 'px';
        this.wrapper.style.width = this.options['size'][0] + 'px';
        this.wrapper.style.height = this.options['size'][1] + 20 + 'px'; 
        this.afterResize();
      }
      else{
        new Effect.ResizeTo(
          this.div, 
          this.options['size'][0], 
          this.options['size'][1], 
          {
            beforeStart: function(){
              __TSM_GLOBAL_MAPS__.each(function(map){
              map.beforeResize();
            })
          },
            afterFinish: function(){
              __TSM_GLOBAL_MAPS__.each(function(map){
                map.afterResize();
              })
          }
          });
        new Effect.ResizeTo(this.wrapper, this.options['size'][0], this.options['size'][1] + 20);
      }
      this.current_size = SMALL;
    }
  }
  
});

PointPickerMap = Class.create();
Object.extend(PointPickerMap.prototype, SinglePointMap.prototype);
Object.extend(PointPickerMap.prototype, {
  // This event is fired when the map is clicked with the mouse. If the click was on a marker, 
  // then the marker is passed to the event handler in the overlay argument, and a click event 
  // is also fired on the marker. Otherwise, the geographical coordinates of the point that was 
  // clicked are passed in the point argument.
  handleClick: function(marker, point) {
    if(this.think_salmon_object.current_size == LARGE){
      if(marker){
        //do nothing
      }
      else{
        if(this.think_salmon_object.marker){
          this.think_salmon_object.gmap.removeOverlay(this.think_salmon_object.marker);
        }
        this.think_salmon_object.marker = new GMarker(point);
        this.think_salmon_object.gmap.addOverlay(this.think_salmon_object.marker);
        $(this.think_salmon_object.options['lat_position_field_id']).value = point.y;
        $(this.think_salmon_object.options['lng_position_field_id']).value = point.x;
        
        this.think_salmon_object.gmap.panTo(point);
        
        this.handleZoom('', '');   
      }
    }
    else{
      this.think_salmon_object.toggleSize();
    }
  },
  handleZoom: function(oldZoom, newZoom) {
  
    if(this.think_salmon_object.options['zoom_field_id']){
      $(this.think_salmon_object.options['zoom_field_id']).value = this.getZoom();
    }
  },
  initialize: function(options) {
    this.baseInit(options);
    GEvent.addListener(this.gmap, "click", this.handleClick);
    GEvent.addListener(this.gmap, "zoomend", this.handleZoom);
    if(this.options['zoom_field_id']){
      $(this.options['zoom_field_id']).value = this.gmap.getZoom();
    }
  }
  
});

