/*
// +----------------------------------------------------------------------+
// | Copyright (c) 2004-2008 Liip AG                                      |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the "License");      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an "AS IS" BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: Liip AG                                                      |
// | Improvements: Niall King, IMG                                        |                                                   |
// +----------------------------------------------------------------------+
*/
var playersearchForm = null;
var livesearchInput='livesearch'
var liveSearchReq = false;
var t = null;
var liveSearchLast = "";
var inputBlurred=false;
var resultsBlurred=true;
	
var isIE = false;
// on !IE we only have to initialize it once
if (window.XMLHttpRequest) {
	liveSearchReq = new XMLHttpRequest();
}
function tracer(stri){$('helptext').innerHTML = '<p>'+stri+'<p>';}
function liveSearchKeyPress(event) {
    inputBlurred=false;
var resd = $("LSResult");
var lssh = $("LSShadow");
	if (event.keyCode == 40 )
	//KEY DOWN
	{
		highlight = $("LSHighlight");
		if (!highlight && lssh.getElementsByTagName('li').length > 0) {
			highlight = lssh.getElementsByTagName('li')[0];
			resd.scrollTop = 0;
		} else {
			highlight.removeAttribute("id");
			highlight = highlight.nextSibling;
			while (highlight && highlight.tagName != 'LI') {highlight = highlight.nextSibling;}
		}
		if (highlight) {
			highlight.setAttribute("id","LSHighlight");
		    if (highlight.offsetTop + resd.scrollTop > 180 && highlight.offsetTop > resd.scrollTop+180) {
		        resd.scrollTop += 18;
		    }
		} 
		
		
		if (!isIE) { event.preventDefault(); }
	} 
	//KEY UP
	
	else if (event.keyCode == 38 ) {
		highlight = $("LSHighlight");
		if (!highlight && lssh.getElementsByTagName('li').length > 0) {
			highlight = lssh.getElementsByTagName('li')[lssh.getElementsByTagName('li').length-1];
			resd.scrollTop = lssh.offsetHeight;
		} 
		else {
			highlight.removeAttribute("id");
			highlight = highlight.previousSibling;
			while (highlight && highlight.tagName != 'LI') {highlight = highlight.previousSibling;}
		}
		if (highlight) {
			highlight.setAttribute("id","LSHighlight");
            if (resd.scrollTop > 0 && highlight.offsetTop < resd.scrollTop) {
		        resd.scrollTop -= 18;
		    }
		}
		if (!isIE) { event.preventDefault(); }
	} 
	//ESC
	else if (event.keyCode == 27) {
		highlight = $("LSHighlight");
		if (highlight) {
			highlight.removeAttribute("id");
		}
		resd.style.display = "none";
	} 
	//BACKSPACE - required for IE
	else if (event.keyCode == 8 && isIE) {
		liveSearchStart();
	}
	//RETURN
	else if (event.keyCode == 13 ) {
		return liveSearchSubmit();
	}
}

function liveSearchInit() {
	var ls = $(livesearchInput);
	if (ls) {
	    var yPos = ls.offsetTop;
	    var xPos = ls.offsetLeft;
	    var tempEl = ls.offsetParent;
	    while (tempEl && tempEl.id != 'centerdiv') {
		    yPos += tempEl.offsetTop;
		    xPos += tempEl.offsetLeft;
  		    tempEl = tempEl.offsetParent;
	    }
	    var t = 21+yPos;
	    var l = xPos;
	    var LSResult=$("LSResult");
	    LSResult.style.top = t+"px";
        LSResult.style.left = l+"px";
    	
	    if (ls&&!ls.eventsDone) {
	        ls.eventsDone=1;
	        if (ls.addEventListener) {
	            ls.addEventListener("keydown",liveSearchKeyPress,false);
		        ls.addEventListener("blur",inputLostFocus,false);	        
		        ls.addEventListener("focus",inputHasFocus,false);
		        LSResult.addEventListener("scroll",inputHasFocus,false); //for google chrome
                LSResult.addEventListener("blur",resultsLostFocus,false);
                LSResult.addEventListener("focus",resultsHasFocus,false);
	        } else {
	            ls.attachEvent("onkeydown",liveSearchKeyPress);
	            ls.attachEvent("onfocus",inputHasFocus);
                ls.attachEvent("onblur",inputLostFocus);
                LSResult.attachEvent("onblur",resultsLostFocus);
                LSResult.attachEvent("onfocus",resultsHasFocus);
		        isIE = true;
	        }
	        ls.setAttribute("autocomplete","off");
        }
    }
}

function resultsHasFocus() {  
    inputBlurred=true;
    resultsBlurred=false;    
}

function inputHasFocus() {  
    inputBlurred=false;
    resultsBlurred=true;
}

/* results blurred */
function resultsLostFocus() {
    resultsBlurred=true;
	window.setTimeout("liveSearchHide()",500);
}

/* input blurred */
function inputLostFocus() {
    inputBlurred=true;
    window.setTimeout("liveSearchHide()",500);	
}

	
function liveSearchHide() {
    if(inputBlurred&&resultsBlurred)
    {
	    $("LSResult").style.display = "none";
	    var highlight = $("LSHighlight");
	    if (highlight) {
		    highlight.removeAttribute("id");
	    }
	}
}

function liveSearchStart(playersearchID,inputControl) {
    if(playersearchID) playersearchForm=$(playersearchID);
    if(inputControl) livesearchInput=inputControl;

	if (t) {
		window.clearTimeout(t);
	}
	t = window.setTimeout("liveSearchDoSearch()",200);
	
	liveSearchInit();
}

function liveSearchDoSearch() {

	if (typeof liveSearchRoot == "undefined") {
		liveSearchRoot = "";
	}
	if (typeof liveSearchRootSubDir == "undefined") {
		liveSearchRootSubDir = "";
	}
	if (typeof liveSearchParams2 == "undefined") {
		liveSearchParams2 = "";
	} else {
		liveSearchParams2 = "&" + liveSearchParams2;
	}
	if (liveSearchLast != playersearchForm.q.value) {
	
	if (liveSearchReq && liveSearchReq.readyState < 4) {
	
		liveSearchReq.abort();
	}
	if ( playersearchForm.q.value == "") {
		liveSearchHide();
		liveSearchLast = "";
		return false;
	}
	if (window.XMLHttpRequest && window.ActiveXObject) {
	   // need a new one each time for IE7 because it's just IE6 in a frock
	   liveSearchReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
	    // and for IE6 because it's IE6 not in a frock
		liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	liveSearchReq.onreadystatechange= liveSearchProcessReqChange;
	//liveSearchReq.open("GET", liveSearchRoot + "/livesearch.php?q=" + document.forms.searchform.q.value + liveSearchParams2);
	//liveSearchLast = document.forms.searchform.q.value;
	liveSearchReq.open("GET", liveSearchRoot + "/PresentationServices/PlayerSearchQuery.aspx?q=" + playersearchForm.q.value + liveSearchParams2);
	liveSearchLast = playersearchForm.q.value;
	liveSearchReq.send(null);
	}
}

function liveSearchProcessReqChange() {
	
	if (liveSearchReq.readyState == 4) {
		var  res = $("LSResult");
		res.style.display = "block";
		res.scrollTop = 0;
		var  sh = $("LSShadow");
		sh.innerHTML = liveSearchReq.responseText;	 
	}
}

function liveSearchSubmit() {
	var highlight = $("LSHighlight");
	if (highlight) {
		window.location = liveSearchRoot + liveSearchRootSubDir + highlight.getElementsByTagName('A')[0].getAttribute("href");
		return false;
	} 
	else {
		return true;
	}
}

addDOMLoadEvent(liveSearchInit);