(function(){

var icon = new GIcon();
icon.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";
icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
icon.iconSize = new GSize(12, 20);
icon.shadowSize = new GSize(22, 20);
icon.iconAnchor = new GPoint(6, 20);
icon.infoWindowAnchor = new GPoint(5, 1);

window.mkStationMarker = function(d) {
  var latlng = new GLatLng(parseFloat(d.lat), parseFloat(d.lng));
  var m = new StationMarker(latlng, d.state, d.text);
  m._data = d;
  return m;
};

function StationMarker(latlng, state, txt) {
  GMarker.call(this, latlng, {icon: icon});
  this.setState(state);
  this.setText(txt);
}

StationMarker.prototype = new GMarker(new GLatLng(0,0));

StationMarker.prototype.initialize = function(map){
  this._map = map;
  GMarker.prototype.initialize.call(this, map);

  GEvent.addListener(this, 'click', this.openInfoWindow);
  this.setTooltip(map.getTooltip());
  this.setTooltipHtml(this._text);
};

//http://www.protides.com/"master:state"/"master:station_id"

StationMarker.prototype.openInfoWindow = function(){
  var d    = this._data;
  var html = '<div><h2>Tides for ' + d.text + '</h2>' + 
    d.state + (d.group != '' ? ' &gt; ' + d.group : '') + ' &gt; ' + d.text + '<br/>' + 
    '<a href="http://www.protides.com/' + d.state.replace(/ /g,'').toLowerCase() + '/' + d.station_id + '">http://www.protides.com/' + d.state.replace(/ /g,'').toLowerCase() + '/' + d.station_id + '</a>' + 
    '</div>';
  this.openInfoWindowHtml(html);
};

StationMarker.prototype.setText = function(txt){
  this._text = txt;
};

StationMarker.prototype.getText = function(){
  return this._text;
};

StationMarker.prototype.setState = function(state){
  this._state = state;
};

StationMarker.prototype.getState = function(){
  return this._state;
};

StationMarker.prototype.remove = function(){
  delete this._map;
  GMarker.prototype.remove.call(this);

  var tt = this.getTooltip();
  if (tt.getMarker() === this)
    tt.hide();
};

StationMarker.prototype.isMapped = function(){
  return this._map ? true : false;
};

window.StationMarker = StationMarker;
})();