// Simple - InPlaceEditor
IPE = Class.create({
    initialize: function(element, url, options, maxlength) {
      this.element = element = $(element);
      this.maxlength = maxlength;
      this.url = url;
      this.options = options;
      this.element.observe("click", this.displayForm.bindAsEventListener(this));
      this.element.style.cursor = "pointer";
      this.element.className = "editable";
      this.leer = 0;

      if(this.element.innerHTML == "") {
//          this.element.createTextNode("Edit...");
//           Element.update(this.element, '');
//           Element.update(this.element, 'bhhb');
//          $(element).update("asdf");
//          this.element.update("Edit...");
//          this.element.innerHTML = "Edit...";
          this.leer = 1;
      }
      else {
          this.leer = 0;
      }
    },
    checkForEscapeOrReturn: function(e) {
      if (e.ctrlKey || e.altKey || e.shiftKey) return;
      if (27 == e.keyCode) {
        this.handleFormCancellation(e);
      }
      else if (13 == e.keyCode) {
        this.handleFormSubmission(e);
      }
    },
    handleFormCancellation: function(e) {
      this.element.show();
      this.removeForm();
    },
    handleFormSubmission: function(e) {
      this.element.innerHTML = "Saving...";
      this.element.show();
      var options = Object.clone(this.options);
      options.parameters = options.parameters + "&" + Field.serialize(this.editField);
      new Ajax.Updater(this.element, this.url, options);
//      this.form.hide();
      this.removeForm();
      return true;
    },
    displayForm: function(e) {
      this.form = document.createElement("form");
//      this.form.onsubmit = function() {alert("asdf");};
      this.form.onsubmit = this.handleFormSubmission.bind(this);
      this.form.action = "#";
      this.form.id = this.element.id;

      var textField = document.createElement("input");
      textField.obj = this;
      textField.type = "text";
      textField.name = "value";
//      textField.writeAttribute("maxlength",this.maxlength);
      textField.maxlength = "40";
      if(this.leer == 0) {
        textField.value = this.element.innerHTML;
      }
      textField.onblur = this.handleFormCancellation.bind(this);
      this.editField = textField;
      this.form.appendChild(this.editField);
      Element.hide(this.element);
      this.element.parentNode.insertBefore(this.form, this.element);
      this.editField.focus();
      this.form.observe("keydown", this.checkForEscapeOrReturn.bindAsEventListener(this));
    },
    removeForm: function() {
      if(this.form) {
        if (this.form.parentNode) Element.remove(this.form);
        this.form = null;
      }
    }
  }
)

// Simple Busy sign
progress = document.createElement("img");
progress.src = "/images/ajax-loader-1.gif";
progress.className = "progress";
//progress.appendChild(progress_img);
progress.writeAttribute("style",
"filter:alpha(opacity=50);-moz-opacity:0.5;-khtml-opacity: 0.5;opacity: 0.5;border:0px");


function hideControls(ctrlid) {
    $('edit_'+ctrlid).fade({ duration: 0.3 });
    $('title_'+ctrlid).fade({ duration: 0.3 });
}

function showControls(ctrlid) {
    $('edit_'+ctrlid).appear({ duration: 0.3 });
    $('title_'+ctrlid).appear({ duration: 0.3 });
}

