/*****************************

 VHS - Vyre HiJAX Search
 Version: 2.1
 Author: Phil Nash
 Purpose: Make search pages accessible to users without JavaScript and remove JavaScript from the page level, insead performing functions on the page load.
 Requires: AJAX Search Framework
 HiJAX? http://domscripting.com/blog/display/41
 
*****************************/

VHS = function(settings) {
  this.searchDiv = 'searchResults'; // id of the div that search results will be pumped into
  this.before = null; // comma separated list of ids of divs to be created and inserted before the search results div
  this.after = null; // comma separated list of ids of divs to be created and inserted after the search results div
  this.results = null; // id of the div that contains your results
  this.form = null; // id of the form used to search over the results
  this.buttons = [{'type':'input','attributes':{'type':'button','className':'portlet-form-button','id':'ajaxSearchButton','value':'Search'}},{'type':'input','attributes':{'type':'button','className':'portlet-form-button','id':'ajaxResetButton','value':'Reset'}}];
  
  // this.buttons = '<input type="button" class="portlet-form-button" id="ajaxSearchButton" value="Search"/><input id="ajaxResetButton" class="portlet-form-button" value="Reset" type="button" />';
  this.search = null; // requires an object of type VYRE.AjaxSearch
  this.callback = null; // an optional function to be performed after the hijacking and searching, for registering taxonomies and other such things.
  this.debugging = false; // boolean: true displays error messages, false fails silently, any error leaves you with the original search intact.
  if( typeof settings != "undefined" ) {
    for( var property in this) {
      if(typeof settings[property] != "undefined") {
        this[property] = settings[property];
      }
    }
  }
}

VHS.prototype = {
  init:function() {
    var error = '';
    if (this.results) { if (!document.getElementById(this.results)) { error += 'Could not select "'+this.results+'" to hijack the results.\n'; } } else { error+= 'No results div to hijack.\n'; }
    if (this.form) { if (!document.getElementById(this.form)) { error += 'Could not select "'+this.form+'" to hijack the form.\n'; } }// else { error+= 'No search form to hijack.\n'; }
    if (!this.search || typeof this.search != 'object') { error += 'No search to perform.\n' }
    if (error == '') {
      this.hijack();
      this.search.search();
      if (this.callback) { this.callback(); }
    }
    else if (this.debugging) {
      error += 'AJAX Search will not be performed. Falling back to regular search.'; alert(error);
    }
  },
  hijack:function() { // hijack wraps the results div in a div of your choice to receive the ajax search results, prepending and appending the any divs supplied in this.before and this.after
    VHSUtils.wrapDiv(this.searchDiv,this.results);
    if (this.before) {
      var b = this.before.split(',');
      for (var i=0;i<b.length;i++) {
        VHSUtils.insertDivBefore(b[i],this.searchDiv);
      }
    }
    if (this.after) {
      var a = this.after.split(',');
      for (var i=0;i<a.length;i++) {
        VHSUtils.insertDivAfter(a[i],this.searchDiv);
      }
    }
    if (this.form) {
      var inputs = document.getElementById(this.form).getElementsByTagName('input');
      var submits = [];
      for (var i =0;i<inputs.length;i++) {
        if(inputs[i].type=='submit') { submits.push(inputs[i]); }
      }
      var newButtons = [];
      for (var i=0;i<this.buttons.length;i++) {
        newButtons[i] = VHSUtils.makeElement(this.buttons[i]);
      }
      if (submits.length > 0) {
        VHSUtils.replaceButton(newButtons,submits[0]);
      }
    }
  }
}

VHSUtils = { // provides utility functions for the hijacker (basically removing the reliance on jQuery to create elements and add them to the DOM whereever you want.
  wrapDiv: function(wrapperId, targetId) {
    if (!document.getElementById(targetId)) { return; }
    var target = document.getElementById(targetId);
    var wrapper = document.createElement('div');
    wrapper.id = wrapperId;
    target.parentNode.insertBefore(wrapper, target);
    wrapper.appendChild(target);
  },
  insertDivBefore: function (insertId, targetId) {
    if (!document.getElementById(targetId)) { return; }
    var target = document.getElementById(targetId);
    var insert = document.createElement('div');
    insert.id = insertId;
    target.parentNode.insertBefore(insert, target);
  },
  insertDivAfter: function(insertId, targetId) {
    if (!document.getElementById(targetId)) { return; }
    var target = document.getElementById(targetId);
    var insert = document.createElement('div');
    insert.id = insertId;
    target.parentNode.insertBefore(insert,target.nextSibling);
  },
  makeElement: function(elementSettings) { // takes an object of the following form {'type':elementType, 'attributes': { name-value pairs of attributes } }
    var elt = document.createElement(elementSettings['type']);
        for (var att in elementSettings['attributes']) {
      elt[att] = elementSettings['attributes'][att];
    }
    return elt;
  },
  replaceButton: function(newButtons, oldButton) {
    for (var i=0;i<newButtons.length;i++) {
      oldButton.parentNode.insertBefore(newButtons[i],oldButton);
    }
    oldButton.parentNode.removeChild(oldButton);
  }
}