/* code for search box, uses jQuery.
 * 
 * @AUTHOR Matt Pearson <mpearson@lizearle.com> 
 */
jQuery(document).ready(function(){
	
	var classNormal = "";
	var classHighlight = "highlight";
	var activeInput = null;
	
	
	

	
	
	function arrowKeys(keyCode,enclosure) {
		if(keyCode == 40 || keyCode == 38){
			var d=(keyCode == 38)?-1:1;
			var suggListPointer = (enclosure.data('pointer') != undefined)?suggListPointer = enclosure.data('pointer'):-1;
			var listLength = (enclosure.data('listLength')!= undefined)?listLength = enclosure.data('listLength'):0;
			suggListPointer = (suggListPointer > listLength-1 || suggListPointer < 0)?0:suggListPointer+(1*d);
			enclosure.data('pointer',suggListPointer);
			highlightTerm(enclosure,suggListPointer,1);
			return true;
		} else {
			//reset
			
			return false;
		}
	}
	
	
	
	function highlightTerm(a,b,c){
		
		var list = a.find('ul');
		
		//loop through each result div applying the correct style
		list.children().each(function(i,item){
			if(i == b){
				if(c == 1)activeInput.val(jQuery(item).text());
					item.className = classHighlight;
			} else {
					item.className = classNormal;
			}
		});
	}
	
	
	function populateSuggestionList(str,enclosure){
		
			if(str.length>0){
				
				jQuery.ajaxSetup({headers: {"X-Requested-With":"Ajax"}});
				jQuery.ajax({type:'GET',
					 url:"/pagesearch/index/suggest",
					 processData:true,
					 data:{str:str},
					 dataType:"json",
					 success:function(json){
						 
						 enclosure.children().remove();
						 var divTerms = jQuery("<ul></ul>").appendTo(enclosure);
						 if(null !==json.items && json.items.length>0){
							 
							    jQuery.each(json.items, function(i,item){
							    	//alert(item.query);
							    	l = jQuery('<a href="/catalogsearch/result?q=' + item.url_query + '">' + item.query + '</a>');
							    	l.bind('click',{enclosure:enclosure,sugg_index:i},function(e){
								        highlightTerm(e.data.enclosure,e.data.sugg_index,1);
								        e.preventDefault;
									});
							    	l.bind('mouseover',{enclosure:enclosure,sugg_index:i},function(e){
								        highlightTerm(e.data.enclosure,e.data.sugg_index,0);
									});
							    	jQuery("<li></li>").append(l).appendTo(divTerms);
							    
							    	if ( i == 12 ) return false;
						        });
							    enclosure.data('listLength',json.items.length);
							    enclosure.data('pointer',-1);
							    
							    enclosure.fadeIn();
							    
						 }else{
							 enclosure.fadeOut();
						 }
	
					 },
					 error:function(x,y,z){
						 enclosure.fadeOut();
					 }}); 
		}else{
			enclosure.fadeOut();
		}
	}
	
	jQuery("#search").keyup(function(e){
		
		var divOutput =jQuery("#search_autocomplete");
		
		/*get keyCode (window.event is for IE)*/
		var keyCode = e.keyCode || window.event.keyCode;
		if(arrowKeys(keyCode,divOutput))return;
		if(keyCode == 13 || keyCode == 27){
			divOutput.fadeOut();
			return;
		}
		activeInput = jQuery(this);
		populateSuggestionList(jQuery(this).val(),divOutput);
	});
	
	jQuery("#search").blur(function(){ 
		
		var divOutput =jQuery("#search_autocomplete");
		
		var enclosure = jQuery(this);
		if(enclosure.data('timeoutref') != undefined)clearTimeout(enclosure.data('timeoutref'));
		enclosure.data('timeoutref',setTimeout(function(){
	    	divOutput.fadeOut()
	    		}, 200));
	});
	
	jQuery("#search").click(function(){
		if(jQuery(this).data('instruction') == undefined)jQuery(this).data('instruction',jQuery(this).val().toLowerCase());
		if(jQuery(this).val().toLowerCase() == jQuery(this).data('instruction'))jQuery(this).val("");
	});
	
	jQuery("#search-btn").click(function(){
		jQuery(this).parents('form').submit();
	});
	
jQuery("#LE_ftSearch").keyup(function(e){
		
	   var divOutput =jQuery("#LE_autocomplete2");
		
		/*get keyCode (window.event is for IE)*/
		var keyCode = e.keyCode || window.event.keyCode;
		if(arrowKeys(keyCode,divOutput))return;
		if(keyCode == 13 || keyCode == 27){
			divOutput.fadeOut();
			return;
		}
		activeInput = jQuery(this);
		populateSuggestionList(jQuery(this).val(),divOutput);
	});
	
	jQuery("#LE_ftSearch").blur(function(){ 
		
		var divOutput =jQuery("#LE_autocomplete2");
		
		var enclosure = jQuery(this);
		if(enclosure.data('timeoutref') != undefined)clearTimeout(enclosure.data('timeoutref'));
		enclosure.data('timeoutref',setTimeout(function(){
	    	divOutput.fadeOut()
	    		}, 200));
	});
	
	jQuery("#LE_ftSearch").click(function(){
		if(jQuery(this).data('instruction') == undefined)jQuery(this).data('instruction',jQuery(this).val().toLowerCase());
		if(jQuery(this).val().toLowerCase() == jQuery(this).data('instruction'))jQuery(this).val("");
	});
	
	jQuery("#LE_ftSearch_btn").click(function(){
		jQuery(this).parents('form').submit();
	});
	
	
});
