
var quickSearchReq = false;
var t = null;
var quickSearchLast = "";	
var isIE = false;


function AsyncUpdateEvent(status, statusText, responseText, responseXML, url) 
{ 
		var  res = document.getElementById("QuickResult");
		res.style.display = "block";
		var  sh = document.getElementById("QuickResultArea");
		
		sh.innerHTML= responseText;
		cleanWhitespace(sh);
}	
function AsyncLoadingEvent() { 
	quickSearchText("<ul><li class='QuickRes'>Loading . . .</li></ul>");
}

var asynchronous = new Asynchronous(); 
asynchronous.loading = AsyncLoadingEvent; 
asynchronous.complete = AsyncUpdateEvent; 


function quickSearchInit()
{
	
	if (navigator.userAgent.indexOf("Safari") > 0) 
	{
		document.getElementById('quicksearch').addEventListener("keydown",quickSearchKeyPress,false);
	} 
	else if (navigator.product == "Gecko") 
	{
		document.getElementById('quicksearch').addEventListener("keypress",quickSearchKeyPress,false);
		document.getElementById('quicksearch').addEventListener("blur",quickSearchHideDelayed,false);
	} 
	else 
	{
		document.getElementById('quicksearch').attachEvent('onkeydown',quickSearchKeyPress);
		isIE = true;
	}
	
	document.getElementById('quicksearch').setAttribute("autocomplete","off");
}

function quickSearchHideDelayed() 
{
	window.setTimeout("closeResults()",400);
}
	
function closeResults() 
{
    highlight = document.getElementById("QuickHighlight");
	if (highlight) {
		highlight.removeAttribute("id");
	}
	document.getElementById("QuickResult").style.display = "none";
}



function quickSearchStart() 
{
	if (t) 
	{
		window.clearTimeout(t);
	}
	t = window.setTimeout("quickSearchDoSearch()",300); //Control server requests. The higher the number here, the longer the delay, but the fewer server queries.
}

function quickSearchDoSearch() {
	
	if (quickSearchLast != document.forms.searchform.formKeyword.value) 
	{		
		if ( document.forms.searchform.formKeyword.value == "" || (document.forms.searchform.formKeyword.value).length <4 || ( (document.forms.searchform.formKeyword.value).substr(0,2) == "97" && (document.forms.searchform.formKeyword.value).length <10 )) 
		{
			closeResults();
			quickSearchLast = "";
			return false;
		}
		else
		{
			quickSearchText("<ul><li class='QuickRes'>Loading . . .</li></ul>");
			asynchronous.call('services/quick_search_results.cfm?q=' + document.forms.searchform.formKeyword.value + '&link=http://www.penguin.com.au/lookinside/spotlight.cfm')
			quickSearchLast = document.forms.searchform.formKeyword.value;
		}
	}
}




function quickSearchText(theText)
{
	var  res = document.getElementById("QuickResult");
	res.style.display = "block";
	var  sh = document.getElementById("QuickResultArea");
	sh.innerHTML  = theText;
}

function mouseHighlight(block)
{
	highlight = document.getElementById("QuickHighlight");
	if (!highlight) {
		block.setAttribute("id","QuickHighlight");
	} 
	else 
	{
		highlight.removeAttribute("id");
		block.setAttribute("id","QuickHighlight");
	}	
}
function mouseHighlightRemove()
{
	highlight = document.getElementById("QuickHighlight");
	if (highlight) {
		highlight.removeAttribute("id");
	}	
}

function quickSearchKeyPress(event) 
{
	if (event.keyCode == 40 )
	// Key press DOWN
	{
		highlight = document.getElementById("QuickHighlight");
		if (!highlight) {
			highlight = document.getElementById("QuickResultArea").firstChild.firstChild;
		} else {
			highlight.removeAttribute("id");
			highlight = highlight.nextSibling;
		}
		if (highlight) {
			highlight.setAttribute("id","QuickHighlight");
		} 
		if (!isIE) { event.preventDefault(); }
	} 
	// Key Press UP
	else if (event.keyCode == 38 ) {
		highlight = document.getElementById("QuickHighlight");
		if (!highlight) {
			highlight = document.getElementById("QuickResult").firstChild.lastChild;
		} 
		else {
			highlight.removeAttribute("id");
			highlight = highlight.previousSibling;
		}
		if (highlight) {
				highlight.setAttribute("id","QuickHighlight");
		}
		if (!isIE) { event.preventDefault(); }
	} 
	// Key Press ESC
	else if (event.keyCode == 27) {
		closeResults();
	} 
	//Backspace Key is needed for IE
	else if (event.keyCode == 8 && isIE) {
		quickSearchStart();
	}
}

function quickSearchSubmit() {
	var highlight = document.getElementById("QuickHighlight");
	if (highlight && highlight.firstChild) {
		document.searchform.formKeyword.value = "Loading details . . ."
		parent.location = "http://www.penguin.com.au/lookinside/spotlight.cfm?SBN=" + highlight.firstChild.getAttribute("id");
		return false;
	} 
	else {
		parent.location = "http://www.penguin.com.au/search/search-results.cfm?formKeyword=" + document.searchform.formKeyword.value;		
		return false;
	}
}



function cleanWhitespace(node) 
{
     var notWhitespace = /\S/;
     
	 for (var i=0; i < node.childNodes.length; i++) 
	 {
         var childNode = node.childNodes[i];
         if ((childNode.nodeType ==3)&&(!notWhitespace.test(childNode.nodeValue))) 
		 {
             // if this is a whitespace text node
             node.removeChild(node.childNodes[i]);
             i--;
         }

         if (childNode.nodeType == 1) 
		 {
             // elements can have text child nodes of their own
             cleanWhitespace(childNode);
         }
     }
}



