var ARROW_DOWN = 40;
var ARROW_UP = 38;
var ENTER = 13;


	function toggleSelectBoxes(toggle){
		//Hide select boxes for IE6 and below only
		var browser = detectBrowser()[0]
		var browserVersion = parseInt(detectBrowser()[1]);
		if(browser == 'Microsoft Internet Explorer' && browserVersion <= 6){
			var selectBoxes = document.getElementsByTagName("select");
			for(i = 0; i<selectBoxes.length; i++){
				if($(selectBoxes[i]).id != "searchArea"){
					if(toggle=="hide"){
						$(selectBoxes[i]).style.display = "none";
					}
					if(toggle=="show"){
						$(selectBoxes[i]).style.display = "inline";
					}
				}
			}		
		}
	}
	

	function showPredictiveSearches(){	
		//Select boxes get in the way in some browsers to hide them out of the way
		toggleSelectBoxes('hide')		
		//Get the x position of the start of the search box, thus predictive box will always display in same relative x position.
		var searchElemOffsetX = cumulativeOffset('productSearchText')[0];
		return overlib('<div id=\'predictiveSearchResults\' onmouseleave=\'toggleSelectBoxes("show")\'>&nbsp;</div>', WIDTH,350,STICKY,MOUSEOFF, FIXX, searchElemOffsetX, FIXY, 135);		
 	}
 	
 	function choosePredictedSearch(predictedText){
 		$('productsearchForm').productSearchText.value = predictedText; 
 		$('productsearchForm').submit(); 
 		showSearchProgressBar('Searching', 'Searching for ' + predictedText);
 	}
 	
 	function unHighLightSingleRow(rowId){  	
 		$(rowId).className = $(rowId).className.replace('on','off')
 	}
 	
 	function highLightSingleRow(rowId){   		
 		$(rowId).className = $(rowId).className.replace('off','on')
 		$('productsearchForm').productSearchText.value = $(rowId).getElementsByTagName("div")[0].innerHTML;
 	}
 	
 	function highLightRow(rowId){
 		//Loop through all childnodes and unhighlight any that are currently highlighted
 		var numChildNodes = $('predictiveSearchContainer').childNodes.length;
 		for(c=0; c<numChildNodes; c++){
 			if($('predictiveSearchContainer').childNodes[c].id){
	 			if($('predictiveSearchContainer').childNodes[c].id.indexOf('predictiveSearch_') >= 0){
	 				if($('predictiveSearchContainer').childNodes[c].className.indexOf('row_on') >= 0){
	 					unHighLightSingleRow($('predictiveSearchContainer').childNodes[c].id);
	 				}
	 			}
 			}
 		} 
 		highLightSingleRow(rowId)	
 	}
 	
 	function monitorSearchKeys(e){
 		
 		//IE Bodge
 		if(window.event){
			ke = window.event.keyCode; // IE
		}else{
			ke = e.which; // Firefox and everthing else decent
		}

 		//Don't do anything if enter is hit
 		if(ke != ENTER){
 
	 		//Test to see if arrows are used, if so move between predictive fields, otherwise do ajax update
	 		if(ke == ARROW_DOWN || ke == ARROW_UP){
	 			//Test to ensure the predictive box is present before moving through
	 			if($('predictiveSearchResults')){
		 			moveBetweenSearches(ke); 
		 		}
	 		}else{
	 			showPredictiveSearches();
		 		params='searchText='+$('productSearchText').value+'&topLevelCategoryId='+$('searchArea').value;
		 		new Ajax.Updater('predictiveSearchResults', 'ajaxPredictiveSearch', {
		 			method:'get',
		 			parameters:params
		 		});
		 	}
	 	}
 				
 	}
 	
 	function moveBetweenSearches(ke){
 		
 		//Firefox treats white space between elements as text nodes, so be careful to check that each node property exists first
 			
 		//Find all child nodes that are predictive search lines
 		var numChildNodes = $('predictiveSearchContainer').childNodes.length;
 		var numValidChildNodes = 0;
 		validChildNodesArr = new Array;
 		
 		for(c=0; c<numChildNodes; c++){ 			
 			if($('predictiveSearchContainer').childNodes[c].id){;
	 			if($('predictiveSearchContainer').childNodes[c].id.indexOf('predictiveSearch_') >= 0){
	 				numValidChildNodes ++;
	 				validChildNodesArr[validChildNodesArr.length] = c;
	 			}
 			}
 		} 	
 		 		
 		var validNodeCounter = 0;
 		if(ke == ARROW_DOWN){
 			var newRowSet = false;
 			//Loop through child nodes finding which is highlighted and moving onto the next
 			for(c=0; c<numChildNodes; c++){
 				if($('predictiveSearchContainer').childNodes[c].id){ 							
	 				if($('predictiveSearchContainer').childNodes[c].id.indexOf('predictiveSearch_') >= 0){	 					
		 				if($('predictiveSearchContainer').childNodes[c].className.indexOf('row_on') >= 0){		 					
		 					unHighLightSingleRow($('predictiveSearchContainer').childNodes[c].id);
		 					if(validNodeCounter + 1 < numValidChildNodes){	 						
		 						highLightSingleRow($('predictiveSearchContainer').childNodes[validChildNodesArr[validNodeCounter + 1]].id);
		 					}
		 					newRowSet = true;
		 					break;
		 				}
	 				}
	 				validNodeCounter ++;
 				}
 			}
 			//If no rows set, highlight the first child node
 			if(!newRowSet){
 				highLightSingleRow($('predictiveSearchContainer').childNodes[validChildNodesArr[0]].id);
 			}
 		} 
 		
 		if(ke == ARROW_UP){
 			var newRowSet = false;
 			//Loop through child nodes finding which is highlighted and moving onto the next
 			for(c=0; c<numChildNodes; c++){
 				if($('predictiveSearchContainer').childNodes[c].id){ 					
	 				if($('predictiveSearchContainer').childNodes[c].id.indexOf('predictiveSearch_') >= 0){
		 				if($('predictiveSearchContainer').childNodes[c].className.indexOf('row_on') >= 0){
		 					unHighLightSingleRow($('predictiveSearchContainer').childNodes[c].id);
		 					if(validNodeCounter - 1 >= 0){	 						
		 						highLightSingleRow($('predictiveSearchContainer').childNodes[validChildNodesArr[validNodeCounter - 1]].id);
		 					}
		 					newRowSet = true;
		 					break;
		 				}
	 				}
	 				validNodeCounter ++;
	 			}
 			}
 			//If no rows set, highlight the last child node
 			if(!newRowSet){ 				
 				highLightSingleRow($('predictiveSearchContainer').childNodes[validChildNodesArr[numValidChildNodes - 1]].id);
 			}
 		}  		
 		
 	}
 	
 	function closePredictiveSearchBox(){
 		cClick();
 		toggleSelectBoxes('show');
 	}

