/**
 * U.Va. A-Z Index Live Search
 * Observes a form, parses JSON, displays results 
 * Based on a plugin by http://www.fromvega.com
 * @steve.johnson@virginia.edu 1/23/2008
 */

// global variables
var acListTotal   =  0;
var acDelay		  = 50;
var acURL		  = null;
var acSearchId	  = null;
var acResultsId	  = null;
var acSearchField = null;
var acResultsDiv  = null;

var acResultsServer = null;

function setAutoComplete(field_id, results_id, category){

	// register mostly used vars
	acSearchField	= $('#searchField');
	acResultsDiv	= $('#results');


	// on key up listener
	acSearchField.keyup(function (e) {

		// get keyCode (window.event is for IE)
		var keyCode = e.keyCode || window.event.keyCode;
		var lastVal = acSearchField.val();
	

		// check for an ENTER or ESC
		if(keyCode == 13 || keyCode == 27 || lastVal.length < 1){
			clearAutoComplete();
			return;
		}

		if (category && category.length > 0) {

			// if is text, call
			if(lastVal.length > 1) {
				setTimeout(function () {autoComplete(lastVal, 'parametric', category)}, acDelay);
			}
			
			if(lastVal.length == 1) {
				setTimeout(function() {autoComplete(lastVal, 'parametric', category)}, acDelay);
			}

		} else {
		
			// if is text, call
			if(lastVal.length > 1) {
				setTimeout(function () {autoComplete(lastVal, 'keyword')}, acDelay);
			}
			
			if(lastVal.length == 1) {
				setTimeout(function() {autoComplete(lastVal, 'alpha')}, acDelay);
			}

		}
		
	});
}

// treat the auto-complete action 
function autoComplete(lastValue, searchType, categoryFilter)
{
		

	// get the field value
	var part = acSearchField.val() != null ? acSearchField.val() : '';

if(searchType != "recent") {

	// if it's empty clear the resuts box and return
	if(part == ''){
		clearAutoComplete();
		return;
	}

		// if it's equal the value from the time of the call, allow
		if(lastValue != part){
			//alert (lastValue);
			//alert(part);
			return;
		}
	
	}
	
	// Replace spaces with a plus symbol
	var alphaURL 		= '/atoz/alpha/'+ part.replace(/\s/g, "+");
	var keywordURL		= '/atoz/keyword/'+ part.replace(/\s/g, "+");
	var categoryURL		= '/atoz/cat/'+ part.replace(/\s/g, "+");
	var recentURL = '/atoz/recent/';
	
	switch(searchType) {
		case "alpha":
			var jsonURL = alphaURL;
			var searchMessage = '<span class="message">Sites beginning with <strong>"'+ lastValue + '"</strong></span>';
			break;
		case "keyword":
			var jsonURL = keywordURL;
			var searchMessage = '<span class="message">Sites containing <strong>"'+ lastValue + '"</strong></span>';
			break;
		case "category":
			var jsonURL = categoryURL;
			var searchMessage = '<span class="message">Sites belonging to category <strong>"'+ lastValue + '"</strong></span>';
			break;
		case "parametric":
			var parametricURL		= '/atoz/parametric/' + categoryFilter.replace(/\s/g, "+") + '/' + part.replace(/\s/g, "+");
			var jsonURL = parametricURL;
			if (lastValue.length == 1)
				var searchMessage = '<span class="message">Sites belonging to category <strong>"'+ categoryFilter + '"</strong> and beginning with <strong>"' + lastValue + '"</strong></span>';
			else
				var searchMessage = '<span class="message">Sites belonging to category <strong>"'+ categoryFilter + '"</strong> and containing <strong>"' + lastValue + '"</strong></span>';
			break;
		case "recent":
		  var jsonURL = recentURL;
		  var searchMessage = '<span class="message">Recently added or updated sites</span>';
			acResultsDiv.addClass("recent");
		  break;
		default: 
			var jsonURL = keywordURL;
			var searchMessage = '<span class="message">Sites containing <strong>"'+ lastValue + '"</strong></span>';
			break;
		}	
	

	// get remote data as JSON
	$.getJSON(jsonURL, function(json){


		// get the total of results
		var ansLength = acListTotal = json.length;
		// if there are results populate the results div
		if(json.length > 0){
			
		
			acResultsDiv.show();
			
			var html = '<h3>' + searchMessage + '</h3><a id="clear" href="#">clear [x]</a><ul>';
			
			// create a li for each result
			for(i=0; i < ansLength; i++) {
				html += '<li><a href="' + json[i]["url"] + '">' + json[i]["title"] + '</a>'
				
				if(!json[i]["updated"]) {
					html +=	'<br /><small>'+ json[i]["url"] + '</small></li>';
				}
				
			}
			
			html += '</ul>';

			// update the results div
			acResultsDiv.html(html);

		} else {
			clearAutoComplete();
			acResultsDiv.show();
			
			var html = '<h3>No results found.</h3> <a id="clear" href="#">clear [x]</a>';
			html += '<p><a href="http://www.virginia.edu/search/results.html?cx=017593750330628318309%3Algfaqmeyqba&q=';
			html += part + '&sa=Search&cof=FORID%3A11#999">';
			html += 'Try your search on the U.Va. web &gt;&gt;</a></p>';
			acResultsDiv.html(html);
			
		}
	});
}

// clear auto complete box
function clearAutoComplete()
{
	$('#results').html('');
	$('#results').hide();
}




