imedo.FeedbackWidget = Class.create(thc2.Widget,
/** @scope FeedbackWidget.prototype */
{

  /**
   * Constructor.
   * @param {HTMLElement} element The element to which this widget is connected.
   */

  formTransition: 390,

  toggleForm: function() {
    var _this = this
    new Effect.Move("questions-or-problems", {
      duration: 0.25,
      x: _this.formTransition,
      transition: Effect.Transitions.sinoidal
    });
    _this.formTransition *= -1
  },

  initialize: function(element) {
    thc2.Widget.prototype.initialize.apply(this, arguments);
    var _this = this
    this.element.writeAttribute("action", $("question_url").readAttribute("value"))
    this.hideTextLabels()
    this.textInputs().each(function(input) {
      _this.setDefaultValue(input)
      _this.addObservers(input)
    })
    Event.observe("feedback-link", "click", function(e) {
      _this.toggleForm()
      e.stop()
    })
    Event.observe(this.element, 'submit', function(e) {
      if (_this.valid()) {
        $(_this.element).request({
          onSuccess: function() {
            alert("Vielen Dank für Ihre Anfrage!")
            _this.textInputs().each(function(input) {
              _this.setDefaultValue(input, true)
            })
            _this.toggleForm()
          },
          onFailure: function() {alert("Oops!")}
        })
      } else {
        alert("Bitte geben Sie eine E-Mail-Adresse und Ihr Anliegen an.")
      }
      e.stop()
      return false
    })
  },

  valid: function() {
    var _this = this
    var error_count = 0
    this.textInputs().each(function(input) {
      if (_this.labelFor(input).innerHTML == $(input).value) {
        error_count += 1
      }
      if ($(input).readAttribute("name").match(/email/)) {
        if (!$(input).value.match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)) {
          error_count += 1
        }
      }
    })
    return (error_count == 0)
  },

  labelFor: function(input) {
    var name = $(input).readAttribute("id");
    result = this.element.select("label[for=" + name + "]")
    // ie8 won't select 'for' attribute
    return this.element.select("label." + name).first()
  },

  hideTextLabels: function(input) {
    var _this = this
    this.textInputs().each(function(input) {
      _this.labelFor(input).hide()
    })
  },

  setDefaultValue: function(input, reset) {
    if (reset ||$(input).getValue() == "") {
      $(input).setValue(this.labelFor(input).innerHTML)
      $(input).addClassName("default_value")
    }
  },

  addObservers: function(input) {
    var _this = this
    var value = this.labelFor(input).innerHTML

    Event.observe(input, 'focus', function(event) {
      if ($(input).getValue() == "" || $(input).getValue() == value) {
        $(input).setValue("")
        $(input).removeClassName("default_value")
      }
    });
    Event.observe(input, 'blur', function(event) {
      $(input).setValue(_this.setDefaultValue(input))
    });
  },
  labels: function() {
    return this.element.select('label');
  },

  textInputs: function() {
    return this.element.select("input[type='text']", "textarea");
  }
});

thc2.CurrentPage.registerBehaviour("thc2-feedback", imedo.FeedbackWidget);

