// 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()
  {
    // 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
      $.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 +'" />';
              }              
              
              strOutputHTML += objData.products[intCounter].title;
              
              //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);
          }
        }
      });
    }
  });
}

// Namespae objAutoCompleteNew
var objAutoCompleteNew = {}
/*
* Defines the number of miliseconds after a request before another request can be generated
*/
objAutoCompleteNew.delayRequestFor = 1;
/*
* Initialize and attatch actions to autocomplete elements
*/
objAutoCompleteNew.init = function(strTargetID,intResultWidth,intResultMinChars,blnFade,strAjaxCall)
{
  // 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()
  {
    // 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((objAutoCompleteNew.lastrun+objAutoCompleteNew.delayRequestFor) != NaN && (objAutoCompleteNew.lastrun+objAutoCompleteNew.delayRequestFor) > strDateTime)
      {
        return false;
      }
      // mark the lastrun time
      objAutoCompleteNew.lastrun = strDateTime;
      // add the loading class while we're loading
      $("div.ac_results").addClass('ac_loading');
      // now constuct and send the request
      $.ajax({
        type: "GET",
        url: strAjaxCall+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.entries)
          {
            for(intCounter = 0; intCounter < objData.entries.length; intCounter++)
            {
              strClassExtra = '';
              if(objData.entries[intCounter].id == 0)
              {
                strClassExtra = ' ac_fullsearch autocomplete_search';
              }
              strOutputView = objData.entries[intCounter].name + ' ('+ objData.entries[intCounter].email_address + ')';
              strOutputHTML += '<li class="ac_entries' + strClassExtra + '"> <a href="" onclick="return selectSubmission(\''
                            + objData.entries[intCounter].id + '\', \''
                            + strOutputView + '\');">'+strOutputView+'</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);
          }
        }
      });
    }
  });
}

function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;

	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";

	if(typeof(arr) == 'object') { //Array/Hashes/Objects
		for(var item in arr) {
			var value = arr[item];

			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}
