//
// Original author/genius :
//
// Dunstan Orchard - 1976design.com/blog/
//
// http://creativecommons.org/licenses/by-nc-sa/1.0/
//

// define all the starting variables
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// id of the search form
var searchFormId = 'contact'; 

// id of the search form input field
var searchInputId = 'b';

// search form input messages
var searchInputMessage1 = '';

// id of the div we wish to create and display the results in
var resultsDivId = 'wibble';

// id of the object we wish to append the results div to
var resultsDivParentId = 'vemail';

// php file which will produce the results
var processURI = '/g/vmail.e';

// set intial vars
var liveSearchReq = false;
var keyPressDelay = null;
var liveSearchLast = '';
var isIE = false;
// on IE we only have to initialize it once
if (window.XMLHttpRequest) {liveSearchReq = new XMLHttpRequest();}

// set the timing vars
var processTimerCount = 0;
var timedOut = 0;



// prep functions
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// create the results div _once_ on page load
// (this function is called from an onload function)
function runLSPrep()
	{
	// set vars to simplify code
	var searchInput = $(searchInputId);
	var resultsDivParent = $(resultsDivParentId);
	
	// event listener for the escape key being pressed anywhere
	addEvent(document, 'keydown', escapeReset, false);

		// create the div which will hold the results
		var resultsDiv = document.createElement('span');
		// set its id
		resultsDiv.className = 'ajaxBox inl joker';
		resultsDiv.id = resultsDivId;
		resultsDiv.style.display = 'none';
		// append to parent
		resultsDivParent.appendChild(resultsDiv);

	// disable autocomplete on the search box
	searchInput.setAttribute('autocomplete', 'off');

	// event listener for hiding the search field result boxen
	addEvent(searchInput, 'focus', liveSearchHide, false);

	// event listener for starting the search
	addEvent(searchInput, 'keyup', liveSearchStart, false);

	// event listener for resetting things on blur
	addEvent(searchInput, 'blur', resetOnBlur, false);

	// event listener for hiding the search field result boxen
	addEvent(searchInput, 'focus', liveSearchHide, false);


	}



// livesearch functions
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function liveSearchStart(e)
	{

var kCignore;
	// set vars to simplify code
	var resultsDiv = $(resultsDivId);

	// if the 'escape' key has been pressed while typing in the search box
	if (e.keyCode == 27)
		{
		resetEverything();
		return false;
		}

if ( e.keyCode == 9 || e.keyCode == 13 ||
    e.keyCode == 16 || e.keyCode == 17 || e.keyCode == 35 || e.keyCode == 36 ||
    e.keyCode == 37 || e.keyCode == 38 || e.keyCode == 39 || e.keyCode == 40)
	kCignore = true;

	// set vars to simplify code
	var searchInput = $(searchInputId);

	// clear the keyPressDelay if it exists from before
	if (keyPressDelay)
		{
		window.clearTimeout(keyPressDelay);
		}
	
	if (searchInput.value != '')
		{

	/*	if(resultsDiv.childNodes.length != 0 &&
		   resultsDiv.childNodes[0].textContent != ''
		) {
		   searchInput.className = 'ajaxInput';
		   resultsDiv.childNodes[0].className = 'ajaxAct';
		} else */
		if(resultsDiv.childNodes.length != 0)
		   resultsDiv.childNodes[0].className = 'ajaxAct';
		   searchInput.className = 'ajaxActInput';

		resultsDiv.style.display = 'inline';

		// wait 0.8 seconds after a keypress before running the search
		keyPressDelay = window.setTimeout('_liveSearchDoSearch()', 800);
		}
	return true;
	}


function _liveSearchDoSearch() {

	var searchInput = $(searchInputId);
	var pars = 'e=' + searchInput.value;
	var uri = processURI;

	var myAjax = new Ajax.Updater(resultsDivId, uri, {method: 'get', parameters: pars});
//	resultsDiv.childNodes[0].className = 'ajaxAct';
	searchInput.className = 'ajaxInput';

}


// reset and clear functions
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function resetEverything()
	{
	// set vars to simplify code
	var searchInput = $(searchInputId);
	searchInput.className = 'ajaxInput';

	liveSearchReq.abort();
	liveSearchHide();
	}

function resetOnBlur() {

	// set vars to simplify code
	var resultsDiv = $(resultsDivId);

}

// if a key has been pressed when focus was outside the search box
function escapeReset(e)
	{
	// if the 'escape' key was been pressed
	if (e.keyCode == 27)
		{
		resetEverything();
		return false;
		}
	}

// [on]focus handler
function clearSearchText(e)
	{
	liveSearchHide();	

	}


function liveSearchHide()
	{
	}

function urchinTracker(page) {}


//window.onload = runLSPrep; // ?