/////////////// Code for search box suggestion box /////////////

$(document).ready(function(){
	
	$("#crit").attr('value', 'Type keyword(s) here for suggestions');
      
	$("#crit").keyup(
		function() { 
			var inputString = $(this).val();
			acompl( inputString );
		}
	);
	
	$("#suggestions").hide();
	
//	$("#helptosection").append( write_helpme() );
//	$("#contactssection").append( write_contacts() );
//	$("#iamasection").append( write_iama() );
   
});    

suggestionfilter = "all";

var oldInputString = "supercalifragilisticexpialidocious";
var helpitems = new Array;

function acompl( inputString ) {
	
	
	if ( inputString.length > 1 ) {
		
		
		if ( oldInputString != inputString.substr(0, oldInputString.length ) ) { 

			// old input string is not a substring of input string, so we will 
			// need to fetch a new set of hits from the server

//			alert("getting new data");

			$.getJSON( "/cgi-bin/cls/ajax/autocompleter.pl", { "crit": inputString }, function(json){
				helpitems = json;
//				alert(helpitems);
				makeSuggestions( inputString, helpitems );
			} );
			
		} else {
			
			// old input string is a subset of input string, helpitems
			// will already contain hits matching input string, no need to
			// go and fetch a new data set from the server

			makeSuggestions( inputString, helpitems );
			
		}
		
		oldInputString = inputString;

				
	} else {
	
		$("#suggestiontext").html( "" );	
		$("#suggestions").hide();
		
	}
	
	
}


function makeSuggestions(inputString, helpitems) {

	// set up the leading text for each section
	var suggestions = "";
	var helpto = "<p>Help me to ...</p><ul>";
	var iama = "<p>I am a ...</p><ul>";
	var contact = "<p>Contact ...</p><ul>";

	// store original string length to see if anything has been added later on
	// This allows us to change the leading text without having to change the 
	// comparison conditional below
	var hlength = helpto.length;
	var ilength = iama.length;
	var clength = contact.length;
			
	// set up regexp to match inputString
	var re = new RegExp(inputString, "i");
		
	$("#suggestions").hide();

	for (var item in helpitems) {

		if ( helpitems[item][suggestionfilter] == 1 ) {

			if ( ( helpitems[item].text.match(re) ) || ( helpitems[item].keywords.match(re) ) || ( helpitems[item].title.match(re) ) ) {
					
				var strongtext = helpitems[item].text.replace(re, "<strong>"+inputString+"</strong>");
					
				if ( helpitems[item].type == "Help me to ..." ) {
						
					helpto += "<li><a href='"+helpitems[item].url+"' title='"+helpitems[item].title+"'>";
					helpto += strongtext+"</a></li>";	
						
				}
				if ( helpitems[item].type == "I am a ..." ) {
					
					iama += "<li><a href='"+helpitems[item].url+"' title='"+helpitems[item].title+"'>";
					iama += strongtext+"</a></li>";	
						
				}
				if ( helpitems[item].type == "Contact ..." ) {
					
					contact += "<li><a href='"+helpitems[item].url+"' title='"+helpitems[item].title+"'>";
					contact += strongtext+"</a></li>";	
						
				}
				
			}
				
		}
			
	}
	
	helpto += "</ul>";
	iama += "</ul>";
	contact += "</ul>";
	
	
	if ( helpto.length > hlength+5 ) { suggestions += helpto; }
	if ( iama.length > ilength+5 ) { suggestions += iama; }
	if ( contact.length > clength+5 ) { suggestions += contact; }
	
	if ( suggestions != "" ) {
		$("#suggestiontext").html( suggestions );
		$("#suggestions").show();
//		alert("Results!");
	} else {
//		alert("No results, staying hidden");
		$("#suggestions").hide();	
	}
	
	// finally, check to see if inputString matches the value of #crit
	// if not then typing has probably outpaced the script: re-run acompl
	
	var currcrit = $("#crit").val();
	if ( currcrit != inputString ) {
		acompl(	currcrit );
	}
		
}


function pickHelpItem( helpText ) {

	$("#crit").val( helpText );
	$("#suggestions").hide();
	
}



