var tmr;
var t;
var obj;

function sFa(which) {

	obj = gObj(which);
	sLft();
	shw(true);
	t = 0;
	sTmr();
}

function hFa() {
	t = -100;
	sTmr();
	return false;
}

function sTmr() {
	tmr = setInterval("fd()",20);
}

function fd() {
	var amt = Math.abs(t+=10);
	if(amt == 0 || amt == 100) clearInterval(tmr);
	amt = (amt == 100)?99.999:amt;
  	
	obj.style.filter = "alpha(opacity:"+amt+")";
	obj.style.KHTMLOpacity = amt/100;
	obj.style.MozOpacity = amt/100;
	obj.style.opacity = amt/100;
	
	if(amt == 0) shw(false);
}

function sLft() {
	var w = 260;	// set this to 1/2 the width of the fa div defined in the style sheet 
			// there's not a reliable way to retrieve an element's width via javascript!!
					
	var l = (document.body.innerWidth)? document.body.innerWidth / 2:document.body.offsetWidth / 2;

	obj.style.left = (l - w)+"px";
}

function gObj(which) {
	return document.getElementById(which);
}

function shw(b) {
	(b)? obj.className = 'show':obj.className = '';	
}

	
var isIE;
var req;

function initRequest(url) {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        isIE = true;
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
}
			
function initShopSearch(query) {
	var theShopDiv = document.getElementById("shopSearchResults");
	var url = "/CmsWebApplication/SearchRepeater.aspx?query="+query;
    var req = initRequest(url);
	req.onreadystatechange = function() {
			if (req.readyState == 4) {
                if (req.status == 200) {
		   			updateSearchResults(req.responseXML,theShopDiv,query);
                }
            }
	};
	req.open("GET", url, true);
	req.send(null);
}

function updateSearchResults(responseXML,theDiv,query) {
	var results = responseXML.getElementsByTagName("search-results")[0];
	var numResults = results.getElementsByTagName("number-results")[0];
	if (numResults == null) {
		return;
	}
	theDiv.style.display = "block";
	if (numResults.childNodes[0].nodeValue > 0) {
		var MAXRESULTS = 4;
		var pElement = document.createElement("p");
		pElement.setAttribute("class","otherSearchHeading");
		pElement.setAttribute("className","otherSearchHeading");
		pElement.appendChild(document.createTextNode("Top results from our product catalogue:"));
		theDiv.appendChild(pElement);
		for (loop = 0; loop < results.getElementsByTagName("search-result").length && loop < MAXRESULTS; loop++) {
			var item = results.getElementsByTagName("search-result")[loop];
			//item is a <search-result>
			var ean = item.getElementsByTagName("ean")[0];
			var title = item.getElementsByTagName("name")[0];
			var titleValue;
			if (title.childNodes[0] == null) titleValue = "No description available";
			else titleValue = title.childNodes[0].nodeValue;
			var id = item.getAttribute("id");
			updateSearchDiv(theDiv, id, ean.childNodes[0].nodeValue, titleValue);
		}
		var divElement = document.createElement("div");
		var linkElement = document.createElement("a");
	
		divElement.setAttribute("class", "otherSearchResult");
		divElement.setAttribute("className", "otherSearchResult");
		linkElement.setAttribute("href", "http://www.standards.co.nz/web-shop/?action=basicShopSearch&mod=search&selStatus=Select%20a%20Status&catalog=NZ&SearchBox1_txtShopName="+query);
		linkElement.setAttribute("title", "See all product results");
		linkElement.appendChild(document.createTextNode("See all product results"));
		divElement.appendChild(linkElement);
		theDiv.appendChild(divElement);
	} else {
		resetSearchDiv(theDiv, "");
	}
}

function resetSearchDiv(theDiv, text) {
	if (theDiv && theDiv.childNodes) {
		for (loop = theDiv.childNodes.length -1; loop >= 0 ; loop--) {
    		theDiv.removeChild(theDiv.childNodes[loop]);
    	}
    }
	if (text!=("")) {
		var pElement = document.createElement("p");
		pElement.setAttribute("class", "error");
    	pElement.appendChild(document.createTextNode(text));
		theDiv.appendChild(pElement);
    } else {
		theDiv.style.display = "none";
    }
}

function updateSearchDiv(theDiv, id, ean, title) {
	var divElement = document.createElement("div");
	var linkElement = document.createElement("a");
	//var brElement = document.createElement("br");
	
	divElement.setAttribute("class", "otherSearchResult");
	divElement.setAttribute("className", "otherSearchResult");
	linkElement.setAttribute("href", "http://www.standards.co.nz/web-shop/?action=touchstoneRef&mod=catalog&pid="+id);
	linkElement.setAttribute("title", title);
	linkElement.appendChild(document.createTextNode(ean));
	
	divElement.appendChild(linkElement);
	//divElement.appendChild(brElement);
	//divElement.appendChild(document.createTextNode(title));

	theDiv.appendChild(divElement);
}
