/*** START Primary nav hover function for IE ***/

sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

/*** START Live search ***/

var xmlHttp
	
function showResult(str) {
	// Clear the suggestions box when no chars
	if (str.length<=minchars) {
		document.getElementById("livesearch").innerHTML="";
		document.getElementById("livesearch").style.visibility="hidden";
		return
	}
	if (livesearchon!="true") {
		return
	}
	
	// Place the AJAX loading gif. Replaced with the clear icon after the request is complete (below)
	//document.getElementById("livesearchbutton").src="en_GX/webadmin/assets/image/ajaxload.gif";
	document.getElementById("livesearchbutton").className="";
	
	xmlhttp=null;
	url = suggestionsfile; // Suggestions xml file taken from local.js

	if (window.XMLHttpRequest) {// code for IE7, Firefox, Mozilla, etc.
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {// code for IE5, IE6
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp!=null) {
		xmlhttp.onreadystatechange=processsearch;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	else {
		//alert("Your browser does not support XMLHTTP.");
		clearlivesearch();
		return;
	}
}

function processsearch() { // Check if file is valid & if so do something with its contents

	// Take the current search string
	var str = document.getElementById("searchfield").value.toLowerCase();

	// Break the loop when http call is complete or there's an error
	if(xmlhttp.readyState!=4) return;
	if(xmlhttp.status!=200) {
		//alert("Problem retrieving XML data");
		clearlivesearch();
		return;
	}

	// Locate the link element to traverse
	x=xmlhttp.responseXML.documentElement.getElementsByTagName("link");
	var searchoutput = "";
	var searchoutputheader = "<div id='livesearchheader'>Recommended results</div>";
	var searchoutputfooter = "<div id='livesearchfooter'><a href='#'>View all results >></a></div>"; 
	if (str.length > minchars) {

		// Loop through all link elements
		for (i=0;i<x.length;i++) {

			xtitles=x[i].getElementsByTagName("title");
			xurls=x[i].getElementsByTagName("url"); {
			try {
				// The magic of string matching (must be lowercare)
				var testquery = xtitles[0].firstChild.nodeValue.toLowerCase();
				if (testquery.indexOf(str) !=-1) {
					searchoutput = searchoutput + "<a href='" + xurls[0].firstChild.nodeValue + "'>" + xtitles[0].firstChild.nodeValue + "</a><br />";
				} else {
					searchoutput = searchoutput + "";
				}
			}
				catch (er) { searchoutput = searchoutput + ""; }
			}				
		}

	}

	if (searchoutput=="") { 
		// Collapse the suggestions box when no matches & halt the ajax loading gif
		document.getElementById("livesearch").style.visibility="hidden";
		//document.getElementById("livesearchbutton").src="en_GX/webadmin/assets/image/ajaxclose.gif"; // Show the close button, replacing the ajax gif
		document.getElementById("livesearchbutton").className="livesearchclose";
	} else {
		// Return the response, only when xml http call is done. Show the close button, replacing the ajax gif
		if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete") { 
			document.getElementById("livesearch").style.visibility="visible";
			document.getElementById("livesearch").innerHTML=searchoutputheader + searchoutput + searchoutputfooter;
			//document.getElementById("livesearchbutton").src="en_GX/webadmin/assets/image/ajaxclose.gif";
			document.getElementById("livesearchbutton").className="livesearchclose";
		} 
	}

}

function clearsearch() { //Clear from initial focus
	document.getElementById("searchfield").value="";
}

function clearlivesearch() { // Clear from the button & also hide the button
	document.getElementById("searchfield").value=" Search";
	document.getElementById("livesearch").innerHTML="";
	document.getElementById("livesearch").style.visibility="hidden";
	//document.getElementById("livesearchbutton").src="en_GX/webadmin/assets/image/tran.gif";
	document.getElementById("livesearchbutton").className="";
}

// Hide the livesearch box on a click outside its div
function hidelivesearch(evt) {
    evt = evt || window.event;
    var targ = evt.target || evt.srcElement;

	// If the livesearch box is open, clear it
	if (targ.id=="livesearch" || targ.id=="livesearchheader" || targ.id=="livesearchfooter" || targ.id=="searchsubmit") {
		return;
	} else {
		if (document.getElementById("livesearch").style.visibility=="visible") {
			clearlivesearch();
		}
	}

}


