
var suggest = '';
// Namespae objAutoComplete
var objAutoComplete = {}
/*
* Defines the number of miliseconds after a request before another request can be generated
*/
objAutoComplete.delayRequestFor = 1;
/*
* Initialize and attatch actions to autocomplete elements
*/
objAutoComplete.init = function(strTargetID,intResultWidth,intResultMinChars,blnFade)
{
  // catch missing target
  if(strTargetID==null)
  {
    return false;
  }
  // animation selector passed in args
  if(blnFade==null)
  {
    blnFade=true;
  }
  // do initial results div formatting
  $("div.ac_results").css('position','absolute');
  $("div.ac_results").css('z-index',1000);
  // if 0 is passed use auto width
  if(intResultWidth>0)
  {
    $("div.ac_results").css('width',intResultWidth+'px');
  }
  // hide the results div
  $("div.ac_results").hide();
  // disble browser based autocomplete's
  $("input#" + strTargetID).attr('autocomplete','off');
  // bind the results close to searchbox blur action
  $("input#" + strTargetID).blur(function(){
      if(blnFade)
      {
        $("div.ac_results").fadeOut(200);
      }
      else
      {
        $("div.ac_results").slideUp(200);
      }
  });
  // catch key presses on input source
  $("input#" + strTargetID).keyup(function()
  {
    if(typeof(suggest) != 'string')
    {
      suggest.abort();
    }
    // no search, best hide the quicksearch DIV...
    if(this.value.length<intResultMinChars){
      if(blnFade)
      {
        $("div.ac_results").fadeOut(200);
      }
      else
      {
        $("div.ac_results").slideUp(200);
      }
      return false;
    }
    else
    {
      // use date astring to get arround caching in IE, this will be included in the request url
      var objDate = new Date;
      var strDateTime = objDate.getTime();
      // ceckthat this is not the first request and that there jave been no other requests inthe last second
      if((objAutoComplete.lastrun+objAutoComplete.delayRequestFor) != NaN && (objAutoComplete.lastrun+objAutoComplete.delayRequestFor) > strDateTime)
      {
        return false;
      }
      // mark the lastrun time
      objAutoComplete.lastrun = strDateTime;
      // add the loading class while we're loading
      $("div.ac_results").addClass('ac_loading');
      // now constuct and send the request
      suggest = $.ajax({
        type: "GET",
        url: "/ajax/autocomplete/json?" + strDateTime + "&q="+this.value,
        dataType: "json",
        success: function(objData)
        {
          // get rid of the loading class
          $("div.ac_results").removeClass('ac_loading');
          // we've got results, so build the html
          var strOutputHTML = '<ul class="ac_list">';
          // add any error string passed
          if(objData.error)
          {
            strOutputHTML += '<li class="ac_error"><a href="#">'
                          + objData.error + '</a></li>';
          }
          // add product results
          if(objData.products)
          {
            for(intCounter = 0; intCounter < objData.products.length; intCounter++)
            {
              strClassExtra = '';
              if(objData.products[intCounter].id == 0)
              {
                strClassExtra = ' ac_fullsearch autocomplete_search';
              }

              /*strOutputHTML += '<li class="ac_product' + strClassExtra + '"><a href="'
                            + objData.products[intCounter].url + '">'
                            + objData.products[intCounter].title + '</a></li>';
             */

              strOutputHTML += '<li class="ac_product' + strClassExtra + '"><a href="'
                            + objData.products[intCounter].url + '">';

              //include image
              if(objData.products[intCounter].show_image == 'Y'){
              	strOutputHTML += '<img src="' + objData.products[intCounter].image + '"'
              	              + ' alt="' + objData.products[intCounter].title +'" />';
              }

              if(typeof objData.products[intCounter].brand !=='undefined')
              {
                strOutputHTML += '<span class="product_brand">'+objData.products[intCounter].brand+'</span>';
              }
              strOutputHTML += '<span class="product_title">'+objData.products[intCounter].title+'</span>';

              //include price
              if(objData.products[intCounter].show_price == 'Y'){
              	strOutputHTML += objData.products[intCounter].price;
              }

              strOutputHTML += '</a></li>';
            }
          }
          // add category results
          if(objData.categories)
          {
            for(intCounter = 0; intCounter < objData.categories.length; intCounter++)
            {
              strOutputHTML += '<li class="ac_category"><a href="'
                            + objData.categories[intCounter].url + '">'
                            + objData.categories[intCounter].title
                            + ' <span class="num_products">' + objData.categories[intCounter].count
                            + ' Products </span></a></li>';
            }
          }
          // add manufacturer results
          if(objData.manufacturers)
          {
            for(intCounter = 0; intCounter < objData.manufacturers.length; intCounter++)
            {
              strOutputHTML += '<li class="ac_manufacturer"><a href="'
                            + objData.manufacturers[intCounter].url + '">'
                            + objData.manufacturers[intCounter].title
                            + ' <span class="num_products">' + objData.manufacturers[intCounter].count
                            + ' Products </span></a></li>';
            }
          }
          // add tag results
          if(objData.tags)
          {
            for(intCounter = 0; intCounter < objData.tags.length; intCounter++)
            {
              strOutputHTML += '<li class="ac_tag"><a href="'
                            + objData.tags[intCounter].url + '">'
                            + objData.tags[intCounter].title
                            + ' <span class="num_products">' + objData.tags[intCounter].count
                            + ' Products </span></a></li>';
            }
          }
          strOutputHTML += '</ul>';
          // paste the output html into the results div and slide the div down to show results
          $("div.ac_results").html(strOutputHTML);
          if(blnFade)
          {
            $("div.ac_results").fadeIn(200);
          }
          else
          {
            $("div.ac_results").slideDown(200);
          }
        }
      });
    }
  });
}
