
var Name = Class.create();
Name.prototype = {
  initialize: function(base) {
    this.base = base;
  },

  child: function(childName) {
    return this.base+'-'+childName;
  }
}

var ConfirmedAction = Class.create();
ConfirmedAction.prototype = {
  initialize: function(element) {
    this.element = $(element);
    this.name = new Name(this.element.id);
  },

  bind: function() {
    Event.observe(this.name.child('action'), 'click',
                  this.clickAction.bindAsEventListener(this));
    Event.observe(this.name.child('confirmFalse'), 'click',
                  this.clickConfirmFalse.bindAsEventListener(this));
    Event.observe(this.name.child('confirmTrue'), 'click',
                  this.clickConfirmTrue.bindAsEventListener(this));
  },

  clickAction: function(event) {
    Event.stop(event);
    Element.toggle(this.name.child('actionContainer'));
    Element.toggle(this.name.child('confirmationContainer'));
  },

  clickConfirmTrue: function(event) {
    Event.stop(event);
    link = $(this.name.child('action'));
    if (Element.hasClassName(link, 'async')) {
        var href = link.attributes['href'].value;
        var pairs = href.split('?');
        var r = new AjaxClient(pairs[0],
                         {method: "post",
			  parameters: pairs[1] + '&r=' + Math.random(),
			  onFailure: reportError});
    } else {
        window.location = $(this.name.child('action')).href;
    }
  },

  clickConfirmFalse: function(event) {
    Event.stop(event);
    Element.toggle(this.name.child('actionContainer'));
    Element.toggle(this.name.child('confirmationContainer'));
  }
}

function registerActions () {
  elements = document.getElementsByClassName('confirmedAction');
  elements.each(function (e) {var a = new ConfirmedAction(e.id); a.bind();});
}

if (Ajax.getTransport()) {
    new FastInit(registerActions);
}
