// $Id: Realestate.js,v 1.8.2.1 2006/05/05 11:57:19 killes Exp $

// Global killswitch
if (isJsEnabled()) {
  addLoadEvent(realestateAutoAttach);
}

/**
 * Attaches the Realestate behaviour to the Realestate form.
 */
function realestateAutoAttach() {
  var inputs = document.getElementsByTagName('input');
  for (i = 0; input = inputs[i]; i++) {
    if (input && hasClass(input, 'swlocselect')) {
      var uri = input.value;
      // Extract the button ID based on a substring of the input name: edit[foo][bar] -> foo-bar
      var selector = input.name.replace('-swlocselect', '').replace(/\]\[/g,'-').replace('[','-').replace(']','');
      var locwrapper = 'location-wrapper';
      var propwrapper = 'properties-wrapper';
      var switcher = new jsRealestateSwitch(uri, selector, locwrapper, propwrapper);
    }
    else if (input && hasClass(input, 'swlocbutton')) {
      input.style.display = 'none';
    }
  }
}

function redirectFormChange(uri, element, handler) {
  // (Re)create an iframe to target.
  createIframe();

  // Trap the button
  element.onfocus = function() {
    element.onchange = function() {
      // Prepare variables for use in anonymous function.
      var element = this;
      var action = element.form.action;
      var target = element.form.target;

      // Redirect form submission
      this.form.action = uri;
      this.form.target = 'redirect-target';

      handler.onsubmit();

      // Set iframe handler for later
      window.iframeHandler = function () {
        var iframe = $('redirect-target');
        // Restore form submission
        element.form.action = action;
        element.form.target = target;

        // Get response from iframe body
        try {
          response = (iframe.contentWindow || iframe.contentDocument || iframe).document.body.innerHTML;
          // Firefox 1.0.x hack: Remove (corrupted) control characters
          response = response.replace(/[\f\n\r\t]/g, ' ');
          if (window.opera) {
            // Opera-hack: it returns innerHTML sanitized.
            response = response.replace(/&quot;/g, '"');
          }
        }
        catch (e) {
          response = null;
        }

        $('redirect-target').onload = null;
        $('redirect-target').src = 'about:blank';

        response = parseJson(response);
        // Check response code
        if (response.status == 0) {
          handler.onerror(response.data);
          return;
        }
        handler.oncomplete(response.data);
      }

      return true;
    }
  }
  element.onblur = function() {
    element.onchange = null;
  }
}

/**
 * JS Realestate object.
 */
function jsRealestateSwitch(uri, selector, locwrapper, propwrapper) {
  this.selector    = selector;
  this.locwrapper  = locwrapper;
  this.propwrapper = propwrapper;
  redirectFormChange(uri, $(selector), this);
}

/**
 * Handler for the form redirection submission.
 */
jsRealestateSwitch.prototype.onsubmit = function () {
  $(this.selector).form.submit();
}

/**
 * Handler for the form redirection completion.
 */
jsRealestateSwitch.prototype.oncomplete = function (data) {
  // Replace form and re-attach behaviour

  if (data['location']) {
    $(this.locwrapper).innerHTML = data['location'];
  }
  if (data['property']) {
    $(this.propwrapper).innerHTML = data['property'];
  }
  
  realestateAutoAttach();
  collapseAutoAttach();
}

/**
 * Handler for the form redirection error.
 */
jsRealestateSwitch.prototype.onerror = function (error) {
  alert('An error occurred:\n\n'+ error);
}

