/**
 * Functions for MLS search, contact, and listings
 */
document.observe('dom:loaded', function () {
  // Elements to check for
  var search = $('mainSearchForm');
  var contact = $('contactForm');
  var details = $('details');

  // Things to do on the contact form
  if (contact) {
    /**
     * Try to populate the address on the contact form if a hidden ID field is 
     * present.
     */
// TODO : make this invoke once we have the form
    function populateContactFormAddress() {
      var field = "id"; // Hidden field
      var textArea = "address"; // Address field
      var url = "/details"; // Address service
      var id; // Contents of field
      if ($(field)) {
        id = Number($F(field));
        if (id > 0) { // Check if id is a valid number
          if ($(textArea)) {
            // Call service for address
            new Ajax.Request(url, {
              method : "GET",
              parameters: { id: id, part : "address" },
              onComplete: function (transport) { 
                // Update element with response and remove HTML
                $(textArea).update(transport.responseText.stripTags()); 
              }
            });
          }
        }
      }
    }
  }

  // Things to do on search page
  if (search) {
    /**
     * Set the values of the main search fields to match what was searched 
     * for in the query string.
     */
    (function () {
      var searchForm = 'mainSearchForm'; // Id of search form.
      var query = {}; // Query string
      if ($(searchForm)) {
        // Convert query string to object  
        query = window.location.search.toQueryParams(); 
        // Iterate over form elements
        $(searchForm).getElements().each(function (field) {
          // Iterate over options
          if(field.type === "select-one") {
            $A(field.options).each(function (option) {
              // Set the option to selected if it matched the query string
              if (option.value != "") {
                if (option.value === unescape(query[field.name])) {
                  option.selected = true;
                }
              } else if (option.innerHTML != "") {
                if (option.innerHTML === unescape(query[field.name])) {
                  option.selected = true;
                }
              }
            });
          }
        }); 
      }
    })();
  }  

  // Things to do on details pages
  if (details) {

    // Observe the print link elements
    $$('a.printLink').invoke('observe', 'click', function (evt) { 
      evt.stop();
      window.print(); 
    });

    /**
     * Image swapper for thumbnails
     */
    (function () {
      var src;
      var primary = 'primaryImage';
      var links = $$('#details .images ul li a');
      if ($(primary) && links.length > 0) {
        links.each(function (a) {
          a.observe("click", function (evt) {
            evt.stop();
            src = a.href;
            a.href = "";
            $(primary).src = src;  
            a.href = src;
          });
        });
      }
    })();
  }
});
