var g_requestQueue = new RequestQueue();

function call() {
  var args = $A(arguments);
  var callback = args.shift();
  var xmlRpcPost = xrpcBuild.apply(null, args);
  var request = new Request('POST', '/ajax', xmlRpcPost, callback);
  Global.g_requestQueue.sendRequest(request);
}

var application = {
  contextMenu: null
}

function registerContextMenu(menu) {
  if (application.contextMenu != null)
    application.contextMenu.destroy();
  application.contextMenu = menu;
}

function closeContextMenu(evt) {
  if (application.contextMenu != null)
    application.contextMenu.destroy();
  application.contextMenu = null;
}

function main() {
  g_components.each(function (f) { f(); });
  // Event.observe(document, 'click',
  //               closeContextMenu.bindAsEventListener(Global));
  Event.observe(document, 'click',
                closeContextMenu.bindAsEventListener(Global));
}

function showElement(element) {
  var e = $(element);
  e.style.visibility = 'visible';
  e.style.display = '';
}

function hideElement(element) {
  var e = $(element);
  e.style.visibility = 'hidden';
  e.style.display = 'none';
}

var RemovalPowerText = klass.create();
Object.extend(RemovalPowerText.prototype, {
  initialize: function(parent, container, callback) {
    this.parent = parent;
    this.container = container;
    this.callback = callback;

    // remove any placeholders
    // placeholders will allow the
    // dynamic spans to build without
    // the reflow flicker if the container
    // was empty
    removeChildren(this.container);

    this.element = $E('span');
    this.container.appendChild(this.element);

    this.removePowerText = $E('span');
    this.removePowerText.className = 'smallactionpromote';
    this.element.appendChild(this.removePowerText);
    var text = $T('remove');
    this.removePowerText.appendChild(text);

    this.promptSpan = $E('span');
    Element.setStyle(this.promptSpan,
      {fontFamily: 'Tahoma', fontSize: '8pt'});
    text = $T('really? ');
    this.promptSpan.appendChild(text);
    this.promptSpan.style.color = 'red';
    this.promptYes = $E('span');
    this.promptYes.className = 'smallactionpromote';
    this.promptSpan.appendChild(this.promptYes);
    text = $T('yes');
    this.promptYes.appendChild(text);
    text = $T(' / ');
    this.promptSpan.appendChild(text);
    this.promptNo = $E('span');
    this.promptNo.className = 'smallactionpromote';
    this.promptSpan.appendChild(this.promptNo);
    text = $T('no');
    this.promptNo.appendChild(text);

    Event.observe(this.removePowerText, 'click',
                  this.startPrompt.bindAsEventListener(this));
    Event.observe(this.promptYes, 'click',
                  this.commit.bindAsEventListener(this));
    Event.observe(this.promptNo, 'click',
                  this.cancel.bindAsEventListener(this));

  },

  destroy: function() {
    this.callback = Prototype.emptyFunction;
    Event.stopObservingElement(this.removePowerText);
    Event.stopObservingElement(this.promptYes);
    Event.stopObservingElement(this.promptNo);
    this.container.removeChild(this.element);
  },

  startPrompt: function() {
    this.element.removeChild(this.removePowerText);
    this.element.appendChild(this.promptSpan);
  },

  commit: function() {
    this.element.removeChild(this.promptSpan);
    this.element.appendChild(this.removePowerText);
    this.callback();
  },

  cancel: function() {
    this.element.removeChild(this.promptSpan);
    this.element.appendChild(this.removePowerText);
  }
});

//Event.observe(this, 'load', this.main.bind(this));
document.observe('dom:loaded', this.main.bind(this))

/////
///// TESTING
/////

function userMessagePopup(name) {
  // var template = '<div>Hello there ' + name + '</div><div>And again</div>'
  //                + '<img src="/captcha">';
  // var overlay = new Overlay({backgroundColor: 'black',
  //                            opacityMax: 30,
  //                            color: 'white',
  //                            data: template});
  // overlay.fadeIn();
  new MessageComponent(name);
}


// vim: set sts=2 sw=2 expandtab:
