Allocate.search = {
  initialise : function(opt) {
    jQuery.extend(this,opt||{});
    var src = this;
    this.default_text = jQuery(this.search).attr('placeholder');
    this.can_placeholder = 'placeholder' in document.createElement('input') 
      ? true : false;
    if(!this.can_placeholder) {
      jQuery(this.search).val(this.default_text);
      jQuery(this.search).focus(function() {
        if(jQuery(this).val() == src.default_text) {
          jQuery(this).val("");
        } else {
          
        }
      });
    }
    jQuery(this.fm).bind('submit', function() {
      var res1 = jQuery(src.target + " li:first").get(0);
      if(jQuery(res1).attr('href')) {
        window.location = jQuery(res1).attr('href');
        return false;
      } else {
        src.commit.apply(src);
        return false;
      }
    });

    jQuery(this.search).data('timeout',null).keyup(
      function() {
        if(jQuery(this).data('timeout')) {
          clearTimeout(jQuery(this).data('timeout'));
        }
        var val = jQuery(this).val();
        jQuery(this).data('timeout', setTimeout(
        function() {
          Allocate.search.commit.apply(src);
        },800));
    });
    jQuery(this.target + ' li').live('click',function() {
      var tar = jQuery(this).attr('href');
      if (tar) window.location = jQuery(this).attr('href');
    });
    initialised = true;
  },
  uri : '/en/search',
  commit : function() {
    var val = jQuery(this.search).val();
    if(val.length < this.min_search_length || val == this.default_text) return;
    var src = this;
    jQuery.getJSON(
      this.uri, { s : val, wht : "hl" },
      function(data) {
        src.render(data);
      }
    );
  },
  render : function(data) {
    var list = [];
    if(data.length == 0 ) {
      jQuery(this.target).empty();
      jQuery(this.target).appendDom([{tagName : 'li', className: 'strong', innerHTML : "Sorry, No Results"}]);
    } else {
      jQuery.each(data, function(id,wht) {
        list.push({ 
          tagName : 'li',
          href : wht.uri,
          innerHTML : (id+1) + ". " + wht.name,
          className : 'a-rs-' + wht.type
        });
      });
      jQuery(this.target).empty();
      jQuery(this.target).appendDom(list);
    }
  },
  default_text : '',
  min_search_length : 3,
  fm : '#a-search-fm',
  search : '#a-search-box',
  target : '#a-search-results',
  initialised : false
};
