Indicator = Class.create();
Indicator.image = "/images/spinner.gif";
Indicator.active = $H({});
Indicator.for_element = function(element) {
  if (element.hasClassName("overlay_to_indicate"))
    return(new OverlayIndicator(element));
  else if (element.hasClassName("hide_to_indicate"))
    return(new HideIndicator(element));
  else if (element.hasClassName("visible_to_indicate"))
    return(new VisibleIndicator(element));
  else
    return(new Indicator(element));
}
Indicator.start = function (element_id) {
  if (Indicator.active.get(element_id)) return false
  var element = $(element_id);
  var indicator = Indicator.for_element(element);
  Indicator.active.set(element_id, indicator);
  indicator.start();
  return true;
}
Indicator.stop = function (element_id) {
  var indicator = Indicator.active.get(element_id)
  if (! indicator) return false
  indicator.stop();
  Indicator.active.unset(element_id);
}
Object.extend(Indicator.prototype, {
  initialize: function(element, options) {
    this.options = $H({image: Indicator.image}).merge(options);
    this.element = element;
    this.indicating = false;
    this.indicator_img = new Element("img", {src: this.options.get("image")});
  }
});
HideIndicator = Class.create(Indicator, {
  start: function($super) {
    this.element.hide();
    this.element.insert({after: this.indicator_img})
    return true;
  },
  stop: function($super) {
    if (this.element.up("body")) this.element.show();
    if (this.indicator_img.up("body")) this.indicator_img.remove();
    return true;
  }
});
OverlayIndicator = Class.create(Indicator, {
  start: function($super) {
    var dimensions = this.element.getDimensions();
    var position = this.element.cumulativeOffset();
    this.indicator_div = new Element("div", {'class': 'overlay'}).update(this.indicator_img).setStyle({width: dimensions.width + "px", height: dimensions.height + "px", lineHeight: dimensions.height + "px", top: position.top + "px", left: position.left + 'px'});
    this.indicator_img.insert({after: new Element("span").update("&nbsp;")});
    document.body.appendChild(this.indicator_div);
    return true;
  },
  stop: function($super) {
    if (this.indicator_div.up("body")) this.indicator_div.remove();
    return true;
  }
});
VisibleIndicator = Class.create(Indicator, {
  start: function($super) { this.element.style.visibility = "visible"; return true; },
  stop:  function($super) { this.element.style.visibility = "hidden";  return true; }
});
