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

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

// search form input messages
var searchInputMessage1 = 'LiveSearch';
var searchInputMessage2 = 'LiveSearch Failed';

// id of the search form submit button
var searchSubmitId = 'search-submit';

// 'searching' image vars
var searchingImage = 'searching.gif';
var searchingImageDivId = 'searching-image';
var searchingImageWidth = 13;
var searchingImageHeight = 13;
var searchingImagePosX = -17;
var searchingImagePosY = 0;

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

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

// php file which will produce the results
var processURI = 'livesearch.php?q=';
// 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;


function addEvent(obj, evType, fn)
	{
	if (obj.addEventListener)
		{
		obj.addEventListener(evType, fn, false); 
		return true;
		}
	else if (obj.attachEvent)
		{
		var r = obj.attachEvent('on'+evType, fn);
		return r;
		}
	else
		{
		return false;
		}
	}




// prep functions
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// create the results div _once_ on page load
// (this function is called from the global function running script inside general_scripts.js)
function runLSPrep()
	{
	// set vars to simplify code
	var resultsDivParent = document.getElementById(resultsDivParentId);
	var searchForm = document.getElementById(searchFormId);
	var searchInput = document.getElementById(searchInputId);
	var searchSubmit = document.getElementById(searchSubmitId);

	// hide the submit button for the form
	searchSubmit.style.display = 'none';

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

	// create the div which will hold the results
	resultsDiv = document.createElement('div');
	// set its id
	resultsDiv.id = resultsDivId;
	// append to parent
	resultsDivParent.appendChild(resultsDiv);




	// set initial search field text
	searchInput.value = searchInputMessage1;

	// event listener for clearing the search field text
	addEvent(searchInput, 'focus', clearSearchText, false);

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

	// event listener for the escape key being pressed anywhere
	addEvent(document, 'keydown', escapeReset, false);

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

	// build the 'searching' image and its containing div
	searchForm.style.position = 'relative';
	var newDiv = document.createElement('div');
	newDiv.id = searchingImageDivId;
	newDiv.style.backgroundColor = 'transparent';
	newDiv.style.border = '0';
	newDiv.style.display = 'none';
	newDiv.style.height = searchingImageWidth+'px';
	newDiv.style.width = searchingImageWidth+'px';
	newDiv.style.padding = '0';
	newDiv.style.position = 'absolute';
	newDiv.style.left = searchingImagePosX+'px';
	newDiv.style.top = searchingImagePosY+'px';
	searchForm.appendChild(newDiv);

	var newImage = document.createElement('img');
	newImage.setAttribute('src', searchingImage);
	newImage.setAttribute('height', searchingImageHeight);
	newImage.setAttribute('width', searchingImageWidth);
	newImage.style.border = '0';
	newImage.style.backgroundColor = 'transparent';
	newImage.style.margin = '0';
	newImage.style.padding = '0';
	newDiv.appendChild(newImage);
	}






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

	// set vars to simplify code
	var searchSubmit = document.getElementById(searchSubmitId);
	var searchInput = document.getElementById(searchInputId);

	// hide the submit button for the form
	searchSubmit.style.display = 'none';

	// clear the keyPressDelay if it exists from before
	if (keyPressDelay)
		{
		window.clearTimeout(keyPressDelay);
		}

	if (searchInput.value != '')
		{
		// wait 0.8 seconds after a keypress before running the search
		keyPressDelay = window.setTimeout('liveSearchDoSearch()', 800);
		}
	}

function liveSearchDoSearch()
	{
	// set vars to simplify code
	var searchInput = document.getElementById(searchInputId);
	var searchingImageDiv = document.getElementById(searchingImageDivId);


	searchingImageDiv.style.display = 'block';
	processTimerCount = 0;
	processTimer();

	if (liveSearchLast != searchInput.value)
		{
		if (liveSearchReq && liveSearchReq.readyState < 4)
			{
			liveSearchReq.abort();
			}
		if (window.XMLHttpRequest)
			{
			// branch for IE/Windows ActiveX version
			}
		else if (window.ActiveXObject)
			{
			liveSearchReq = new ActiveXObject('Microsoft.XMLHTTP');
			}
		liveSearchReq.onreadystatechange = liveSearchProcessReqChange;
		liveSearchReq.open('GET', processURI+searchInput.value);
		liveSearchLast = searchInput.value;
		liveSearchReq.send(null);
		}
	}

function liveSearchProcessReqChange()
	{
	// set vars to simplify code
	var searchForm = document.getElementById(searchFormId);
	var searchSubmit = document.getElementById(searchSubmitId);
	var resultsDiv = document.getElementById(resultsDivId);
	var searchingImageDiv = document.getElementById(searchingImageDivId);

	// hide the submit button for the form
	searchSubmit.style.display = 'none';


	if (liveSearchReq.readyState == 4)
		{
		resultsDiv.innerHTML = liveSearchReq.responseText;
		resultsDiv.style.display = 'block';
		searchingImageDiv.style.display = 'none';

		// halt the process timer
		processTimerCount = -1;
		processTimer(0);

		getDDSize();
		setListenLinks();
		}
	}






// timer functions
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// this times how long the whole process takes
// if it takes more that 3 seconds then it kills the xmlhttp request and lets the user submit the form as normal
function processTimer()
	{
	// if process running normally
	if (processTimerCount != -1)
		{
		// loop if process time still under 3 seconds
		if (processTimerCount < 3)
			{
			processTimerCount++;
			setTimeout('processTimer()', 1000);
			}
		// timed out, process taken 3 seconds already
		else
			{
			// set vars to simplify code
			var searchForm = document.getElementById(searchFormId);
			var searchInput = document.getElementById(searchInputId);
			var searchSubmit = document.getElementById(searchSubmitId);
			var searchingImageDiv = document.getElementById(searchingImageDivId);
			
			searchingImageDiv.style.display = 'none';
			searchSubmit.value = searchInputMessage2;
			searchSubmit.style.display = 'block';
			
			processTimerCount = 0;
			
			liveSearchHide();
			liveSearchReq.abort();
			return false;
			}
		}
	else
		{
		processTimerCount = -1;
		}
	}






// reset and clear functions
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function resetEverything()
	{
	// set vars to simplify code
	var searchInput = document.getElementById(searchInputId);
	var searchSubmit = document.getElementById(searchSubmitId);
	var searchingImageDiv = document.getElementById(searchingImageDivId);
	
	searchInput.value = searchInputMessage1;
	searchSubmit.style.display = 'none';
	searchingImageDiv.style.display = 'none';
	
	liveSearchReq.abort();
	liveSearchHide();
	}

function resetOnBlur()
	{
	// set vars to simplify code
	var searchInput = document.getElementById(searchInputId);
	var searchSubmit = document.getElementById(searchSubmitId);
	var searchingImageDiv = document.getElementById(searchingImageDivId);

	if (searchInput.value == '')
		{
		searchInput.value = searchInputMessage1;
		searchSubmit.style.display = 'none';
		searchingImageDiv.style.display = 'none';
		
		liveSearchReq.abort();
		liveSearchHide();
		}
	}

// 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;
		}
	}

function clearSearchText()
	{
	// set vars to simplify code
	var searchInput = document.getElementById(searchInputId);
	
	if (searchInput.value == searchInputMessage1)
		{
		searchInput.value = '';
		}
	}

function liveSearchHide()
	{
	// set vars to simplify code
	var resultsDiv = document.getElementById(resultsDivId);
	
	resultsDiv.style.display = 'none';
	}






// styling for the info bubbles
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// grab the height of all <dd>s and style them
function getDDSize()
	{
	// set vars to simplify code
	var resultsDiv = document.getElementById(resultsDivId);
	
	// get array of all the <dd>s
	dds = resultsDiv.getElementsByTagName('dd');
	
	// loop through them
	for (i = 0; i < dds.length; i++)
		{
		// if their class name matches the one we're interested in
		if (dds[i].className == 'ls-data')
			{
			// style it so it looks the same as it will when live
			// (we must do this or the height will be incorrectly calucalated)
			dds[i].className = 'ls-data-on';
			// grab height
			dds[i].height = dds[i].offsetHeight;
			// set class name back to normal so it's hidden from view
			dds[i].className = 'ls-data';
			// set vertical position
			dds[i].style.top = '-'+(dds[i].height - 21)+'px';
			}
		}
	}

function setListenLinks()
	{
	// set vars to simplify code
	var resultsDiv = document.getElementById(resultsDivId);
	
	// get array of all the <a>s
	ahrefs = resultsDiv.getElementsByTagName('a');
	
	// loop through them
	for (i = 0; i < ahrefs.length; i++)
		{
		addEvent(ahrefs[i], 'focus', setLinkParentId, false);
		addEvent(ahrefs[i], 'blur', resetLinkParentId, false);
		}
	}
	
// give the parent of the <a> a new id so it displays the info bubble
function setLinkParentId(ev)
	{
	var link = (window.event) ? window.event.srcElement : ev.target;
	var linkParent = link.parentNode.parentNode.parentNode;
	linkParent.id = 'faux-hover';
	}

// reset the parent of the <a> so it has no id
function resetLinkParentId(ev)
	{
	var link = (window.event) ? window.event.srcElement : ev.target;
	var linkParent = link.parentNode.parentNode.parentNode;
	linkParent.id = '';
	}
