/*
SearchNow v1.1

Copyright (c) 2004 by Nick Leeper (aka lucky33)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation 
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, 
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var searchNowReq = null;
var searchNowPath;
var searchNowLast;
var searchTimeout = null;
var searchResultsCurrPage = 1;
var searchResultsTotalPages = 1;
var searchResultsPerPage = 10;
var searchBoxBlur = false;

function initSearchNow(path) {
	var searchBox = document.getElementById('searchNowBox');
	var searchButton = document.getElementById('searchNowSubmit');
	var searchForm = document.forms.frmSearch;
	
	if (window.ActiveXObject || window.XMLHttpRequest) {
		if (document.all)
			addEvent(searchBox, 'keydown', function(e){handleKeyPress(e);});
		else {
			addEvent(searchBox, 'keypress', function(e){handleKeyPress(e);});
			addEvent(document, 'click', function(e){handleSearchClick(e);});
		}	
		
		addEvent(searchBox, 'blur', function(e){handleSearchBlur();});
		addEvent(searchForm, 'submit', function(e){return false;});
		searchButton.style.display = 'none';
		searchBox.autocomplete = 'off';
		searchNowPath = path;
	}
}
function addEvent(obj, evtType, fxn) {
	if (obj.addEventListener) {
		obj.addEventListener(evtType, fxn, false);
		return true;
	}
	else if (obj.attachEvent) {
		var ret = obj.attachEvent('on' + evtType, fxn);
		return ret;
	}
	else
		return false;
}
function handleKeyPress(e) {
	var searched = false;
	var searchSelected = document.getElementById('searchNowSelected');
	var searchResults = document.getElementById('searchNowResults');
	
	if (document.all)
		e = window.event;
	
	//Arrow-left key pressed
	if (e.keyCode == 37)
		handlePageChange(-1);
	//Arrow-right key pressed
	else if (e.keyCode == 39)
		handlePageChange(1);
	//Arrow-down key pressed
	else if (e.keyCode == 40) {
		if (searchResults.firstChild) {
			if (!searchSelected)
				searchSelected = searchResults.firstChild.nextSibling.firstChild;
			else {
				searchSelected.removeAttribute('id');
				searchSelected = searchSelected.nextSibling;
				if (!searchSelected)
					searchSelected = searchResults.firstChild.nextSibling.firstChild;
			}
			if (searchSelected.firstChild.nextSibling.nodeName.toUpperCase() == 'A')
				searchSelected.setAttribute('id', 'searchNowSelected');
		}
	}
	//Arrow-up key pressed
	else if (e.keyCode == 38) {
		if (searchResults.firstChild) {
			if (!searchSelected)
				searchSelected = searchResults.firstChild.nexSibling.lastChild;
			else {
				searchSelected.removeAttribute('id');
				searchSelected = searchSelected.previousSibling;
				if (!searchSelected)
					searchSelected = searchResults.firstChild.nextSibling.lastChild;
			}
			if (searchSelected.firstChild.nextSibling.nodeName.toUpperCase() == 'A')
				searchSelected.setAttribute('id', 'searchNowSelected');
		}
	}
	//Enter key pressed
	else if (e.keyCode == 13) {
		if (searchResults.firstChild) {
			if (searchSelected)
				window.location = searchSelected.firstChild.nextSibling.href;
		}
	}
	//Esc key pressed
	else if (e.keyCode == 27) {
		if (searchResults.firstChild) {
			if (searchSelected)
				searchSelected.removeAttribute('id');
			hideSearchNow();
		}
	}
	else if (e.keyCode != 9 && e.keyCode != 32) {
		searched = true;
		if (searchTimeout)
			window.clearTimeout(searchTimeout);
		searchTimeout = window.setTimeout('doSearchNow()', 200);
	}
	
	if (!searched && !document.all)
		e.preventDefault();
}
function handleStateChange() {
	if (searchNowReq.readyState == 4) {
		var searchBox = document.getElementById('searchNowBox');
		var searchResults = document.getElementById('searchNowResults');
		if (searchNowReq.responseText != "" && searchBox.value != "") {
			//Reset the paging mechanisms
			searchResultsCurrPage = 1;
			searchResultsTotalPages = 1;
			searchResults.innerHTML = handleResultsPaging();
			searchResults.style.display = 'block';
		}
	}
}
function handleResultsPaging() {
	var x = 0, start = 0, end = 0, numResults = 0;
	var results = "";
	var rootNode;
	
	rootNode = searchNowReq.responseXML.documentElement;
	numResults = rootNode.childNodes.length;
	searchResultsTotalPages = Math.ceil(numResults/searchResultsPerPage);
	
	start = searchResultsPerPage * (searchResultsCurrPage - 1);
	end = searchResultsPerPage * searchResultsCurrPage;
	if (end > numResults)
		end = numResults;
		
	results += '<div id="searchNowDetails">';
	if (numResults > 0 && rootNode.firstChild.firstChild.nextSibling)
		results += 'Results ' + (start + 1) + '-' + end + ' of ' + numResults;
	results += '   <a href="javascript:hideSearchNow();" title="Close search results">(close)</a></div>';
	if (numResults > searchResultsPerPage) {
		results += '<' + rootNode.nodeName;
		for (x = 0;x < rootNode.attributes.length;x++) {
			if (rootNode.attributes[x].specified)
				results += ' ' + child.attributes[x].name + '="' + child.attributes[x].value + '"';
		}
		results += '>';
		
		for (x = start;x < end;x++)
			results += childNodeToText(rootNode.childNodes[x]);
			
		results += '</' + rootNode.nodeName + '>';
		
		results += '<div id="searchNowPages">';
		if (searchResultsCurrPage > 1)
			results += '<a href="javascript:handlePageChange(-1);" title="Previous Page">Previous Page</a>';
		if (searchResultsCurrPage < searchResultsTotalPages)
			results += '<a href="javascript:handlePageChange(1);" title="Next Page">Next Page</a>';
		results += '</div>';
	}
	else
		results += searchNowReq.responseText;

	return results;
}
function handlePageChange(dir) {
	if ((searchResultsCurrPage + dir) > 0 && (searchResultsCurrPage + dir) <= searchResultsTotalPages) {
		searchResultsCurrPage += dir;
		document.getElementById('searchNowResults').innerHTML = handleResultsPaging();
	}
}
function childNodeToText(child) {
	var x = 0;
	var response = "";
	
	if (child.nodeType == 1) {
		response += '<' + child.nodeName;
		for (x = 0;x < child.attributes.length;x++) {
			if (child.attributes[x].specified)
				response += ' ' + child.attributes[x].name + '="' + child.attributes[x].value + '"';
		}
		response += '>';
		
		if (child.childNodes.length > 0) {
			for (x = 0;x < child.childNodes.length;x++)
				response += childNodeToText(child.childNodes[x]);
		}
		response += '</' + child.nodeName + '>';
	}
	else if (child.nodeType == 3)
		response += child.nodeValue;

	return response;
}
function doSearchNow() {
	var searchBox = document.getElementById('searchNowBox');
	if (searchBox.value != "") {
		if (searchNowLast != searchBox.value) {
			if (searchNowReq && searchNowReq.readyState < 4)
				searchNowReq.abort();
			if (window.ActiveXObject)
				searchNowReq = new ActiveXObject('Microsoft.XMLHTTP');
			else {
				if (!searchNowReq)
					searchNowReq = new XMLHttpRequest();
			}
			searchNowReq.onreadystatechange = handleStateChange;
			searchNowReq.open('GET', searchNowPath + '/searchnow.php?s=' + encodeURI(searchBox.value));
			searchNowReq.send(null);
		}
	}
	else
		hideSearchNow();
}
function handleSearchBlur() {
	//This method has to be here because in Mozilla, the click event is fired after the blur event, so we can't detect if the user has clicked
	//in the search results till after the blur event fires
	if (document.all)
		checkHideSearchNow(window.event.clientX + document.documentElement.scrollLeft, window.event.clientY + document.documentElement.scrollTop);
	else
		searchBoxBlur = true;
}
function handleSearchClick(e) {
	if (searchBoxBlur) {
		checkHideSearchNow(e.pageX, e.pageY);
		searchBoxBlur = false;
	}
}
function checkHideSearchNow(x, y) {
	//If the user clicked in the search results, we don't want to close the display
	var results = document.getElementById('searchNowResults');
	if (x >= results.offsetLeft && x <= (results.offsetLeft + results.offsetWidth) && y >= results.offsetTop && y <= (results.offsetTop + results.offsetHeight))
		document.getElementById('searchNowBox').focus();
	else
		hideSearchNow();
}
function hideSearchNow() {
	document.getElementById('searchNowResults').style.display = 'none';
}