(function(){

function StateMarker(latlng, txt) {
  GMarker.call(this, latlng);
  this.setText(txt);
}

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

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

  GEvent.addListener(this, 'click', function() {
    this.onClick();
  });
  this.setTooltip(map.getTooltip(), this.getText());
};

StateMarker.prototype.onClick = function() {
  // are we already the current topic?
  if (REMOVED_STATE_MARKER === this) {
    return;
  }

  // remove the old markers
  $(map.getOverlays(StationMarker)).each(function(){
    map.removeOverlay(this);
  });
  if (REMOVED_STATE_MARKER) {
    REMOVED_STATE_MARKER.show();
    REMOVED_STATE_MARKER = null;
  }

  if (this._data) {
    this.hide();
    REMOVED_STATE_MARKER = this;
    this.mapData(this._data);
  }
  else {
    om.Set('Loading locations.<br/>Please wait...');
    var state_marker = this;
    var url = 'station_markers.php?node=' + this.getText();
    var callback = GEvent.callback(state_marker, StateMarker.prototype.processStationMarkers);
    GDownloadUrl(url, callback);
  }
}

StateMarker.prototype.mapData = function(data_array) {
  var marker_data = data_array[0];
  var center_data = data_array[1];
  var sw_data = data_array[2];
  var ne_data = data_array[3];
  var center = new GLatLng(center_data[0], center_data[1]);
  var sw = new GLatLng(sw_data[0], sw_data[1]);
  var ne = new GLatLng(ne_data[0], ne_data[1]);

  var bounds = new GLatLngBounds(sw, ne);
  var zoom = map.getBoundsZoomLevel(bounds);
  map.setCenter(center, zoom);

  var opts = {
    progressbar: window.pb,
    mkMarker: window.mkStationMarker,
    ondone: function() {
      window.markers = map.getOverlays(StationMarker);
      $(window.markers).each(function(){
        this.setTooltipHtml(this.getText());
      });
      map.zoomTo(.1, window.markers);
    }
  };
  map.mapArray(marker_data, opts);
};

StateMarker.prototype.processStationMarkers = function(data, status) {
  if (status == 200) {
    om.Clear();

    // remove this state marker from the map
    this.hide();
    REMOVED_STATE_MARKER = this;

    // create the markers to add to the map
    window.markers = [];
    try {
      var data_array = eval(data);
      this._data = data_array;
      this.mapData(data_array);
    }
    catch (e) {
      alert('There was an error.\nPlease reload your browser to continue.');
    }
  }
  else {
    alert('There was an error.\nPlease reload your browser to continue.');
  }
};

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

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

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

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

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

window.StateMarker = StateMarker;
})();