var search_str = '';

$(document).ready(function(){
	fnInitializeDropdowns();
	var inputDelayer=delayTimer(500);
	$('#searchfield').keyup(function(e){
		var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
		
		switch(key) {
				case 37: 
					break;
				case 38: 
					
					break;
				case 39: 
					break;
				case 40: 
					
					break;
				case 13: 
					//window.location.href=$('li.keynav_focusbox a').attr('href');
					break;
				default:
					$("#suggestions-loading").css("background-position","0px -20px");
					$("#suggestions-loading").show();
					inputDelayer(function(){
						lookup($('#searchfield').val());
					});
					break;
		}	
	});

	$('#searchfield').focus(function(){
		if( $(this).val() == 'Bike Search') {
			$(this).val('');
		}
	});
	
	// Fade out the suggestions box when not active
	$("#searchfield").blur(function(){
		//var inputDelayer=delayTimer(500);
		inputDelayer(function(){
			$('#searchfield').val('Bike Search');
			$("#suggestions-loading").hide();
			$('#suggestions').fadeOut();
		});
	});
	
});

function fnInitializeDropdowns() {
	var config = {
		sensitivity: 3,
		interval: 150,
		over: function(event){
			array_id = (this.id).split("-");
			$("#" + array_id[0] + "-dropdown").slideDown('fast');
		},
		timeout: 0,
		out: function(event){
			array_id = (this.id).split("-");
			$("#" + array_id[0] + "-dropdown").fadeOut('fast');
		}
	};
	$(".dropdown").hoverIntent( config );
}

function delayTimer(delay){
     var timer;
     return function(fn){
          timer=clearTimeout(timer);
          if(fn)
               timer=setTimeout(function(){
               fn();
               },delay);
          return timer;
     }
}

function lookup(inputString) {
	if(inputString.length == 0) {
		$("#suggestions").empty();
		$('#suggestions').fadeOut();
	} else {
		// Do an AJAX call
		$.getJSON("/ajax/search?format=json&s=" + inputString + "&callback=?",
		function(data){
			$("#suggestions").empty();
			var return_text = '<p id="searchresults">';
			if( data.models.length > 0 ) {
				return_text += '<span class="category">Bike Models</span>';
				$.each(data.models, function(i,item){
					return_text += '<a href=\''+item.url+'\'>';
					return_text += '<img alt="" src="'+item.filename+'"/>';
					return_text += '<span>'+item.title+'<span class="red">&nbsp;&raquo;</span></span><br />';
					return_text += '<span class="smalltype">'+item.msrp+'</span></a>';
				});
			}
			if( data.series.length > 0 ) {
				return_text += '<span class="category">Bike Series</span>';
				$.each(data.series, function(i,item){
					return_text += '<a class="bike-series" href=\''+item.url+'\'><span class="searchheading">'+item.title+'</span></a>';
				});
			}
			return_text += '</p>';
			
			if( data.series.length > 0 || data.models.length > 0) {
				$('#suggestions').fadeIn(); // Show the suggestions box
				$('#suggestions').html(return_text); // Fill the suggestions box
				// Initialize jQuery keyboard navigation
			    // $('#suggestions li').keynav('keynav_focusbox','keynav_box');
			    // Set the first div as the one with focus, this is optional
			    // $('#suggestions li:first').removeClass().addClass('keynav_focusbox');
				$("#suggestions-loading").css("background-position","0px 0px");
				$("#suggestions-loading").click(function () { 
					$('#suggestions').fadeOut();
					$(this).css("background-position","0px 0px");
					$(this).hide();
				});
			} else {
				$('#suggestions').fadeOut();
				$("#suggestions-loading").css("background-position","0px 0px");
				$("#suggestions-loading").hide();
			}
      });
   }
}


