// For Additional Product Images Scroll Bar 
var speed = 5;
var grap  = 175;

$(function() {

$(".search_box").attr("autocomplete","off");
	$('#search .search_box').keyup(function(e) {
	        var $this = $(this);
	        
	        // Sometimes IE dont know the event
	        var code = (e.keyCode ? e.keyCode : e.which);
	           
	        // The Enter Button
	        if (e.keyCode == '13') {
	            e.preventDefault();
	        }   
            
            clearTimeout($.data(this, 'timer'));

            // Only do the ajax call then a centin amount of letters have been called.

            if ($this.val().length <= 3) {
               $("#predictive-content").html("<p>More than 3 characters required.</p>");
               return;
            }  else {            	
            	$("#predictive-content").html("");            	
            } 
            
            var wait = setTimeout(function() {productSearch($this)}, 500); 
            
    });


});


function productSearch($this)
{
   $.ajax({
          	type: "POST", 
           	url:  "ajax.php", 
           	data: "type=product&action=predictiveSearch&l=5&size=medium&searchfor="+$this.val(), 
           	dataType: "json",
           	success: function(options){ 
               	    var max    = options.length;
                    var string = '';
					string = '<div id="predictive-top"></div><div id="predictive-content"><table cellspacing="0" cellpadding="0">';
               	    if (max > 1) {
	        	           for (var i = 0; i < max; i++) {	
   	                            string += '<tr><td class="search_image_td"><a class="search_image" href="' + options[i].url + '">' + options[i].image + '</a></td><td><a class="search_name" href="' + options[i].url + '">' + options[i].name + '</a><div class="search_description">' + options[i].description + '</div></td></tr>\n';
	                	   } 
					string = string + '</table></div><div id="predictive-bot"></div>';	   
	             	      $('#predictiveSearch').html(string).show();
	            	} else {
	            	       $('#predictiveSearch').html('').hide();
	            	}              
       	}
   });
}

