/*
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 Bitflux GmbH                                      |
// +----------------------------------------------------------------------+
// | 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: Bitflux GmbH <devel@bitflux.ch>                              |
// +----------------------------------------------------------------------+
*/
var liveSearchReq = false;
var t = null;
var liveSearchLast = "";
var isIE = false;
// on !IE we only have to initialize it once
if (window.XMLHttpRequest) {
	liveSearchReq = new XMLHttpRequest();
}
function liveSearchInit() {

	// Turns off autocomplete on the input element
	var searchInput = document.getElementById('livesearch');
	if( searchInput == null ) return;
	searchInput.setAttribute('autocomplete', 'off');

	if (navigator.userAgent.indexOf("Safari") > 0) {
		document.getElementById('livesearch').addEventListener("keydown",liveSearchKeyPress,false);
	} else if (navigator.product == "Gecko") {
		document.getElementById('livesearch').addEventListener("keypress",liveSearchKeyPress,false);
	} else {
		document.getElementById('livesearch').attachEvent('onkeydown',liveSearchKeyPress);
		//document.getElementById('livesearch').attachEvent("onblur",liveSearchHide,false);
		isIE = true;
	}
}

function liveSearchKeyPress(event) {
	if (event.keyCode == 40 )
	//KEY DOWN
	{
		highlight = document.getElementById("LSHighlight");
		if (!highlight) {
			try {
			highlight = document.getElementById("LSRes").firstChild;
			} catch (exception) { }
		} else {
			highlight.removeAttribute("id");
			highlight = highlight.nextSibling;
		}
		if (highlight) {
			highlight.setAttribute("id","LSHighlight");
		}
		if (!isIE) { event.preventDefault(); }
	}
	//KEY UP
	else if (event.keyCode == 38 ) {
		highlight = document.getElementById("LSHighlight");
		if (!highlight) {
			try {
			highlight = document.getElementById("LSRes").lastChild;
			} catch (exception) {}
		}
		else {
			highlight.removeAttribute("id");
			highlight = highlight.previousSibling;
		}
		if (highlight) {
				highlight.setAttribute("id","LSHighlight");
		}
		if (!isIE) { event.preventDefault(); }
	}
	//ESC
	else if (event.keyCode == 27) {
		highlight = document.getElementById("LSHighlight");
		if (highlight) {
			highlight.removeAttribute("id");
		}
		document.getElementById("LSResult").style.display = "none";
		document.forms.rex_search.q.value = '';
	}
	//ENTER
	else if (event.keyCode == 13) {
		liveSearchSubmit();
	}
	//BACKSPACE - required for IE
	else if (event.keyCode == 8 && isIE) {
		liveSearchStart();
	}

}
function closeLiveSearch() {
	highlight = document.getElementById("LSHighlight");
	if (highlight) {
		highlight.removeAttribute("id");
	}
	document.getElementById("LSResult").style.display = "none";
	document.forms.rex_search.q.value = '';
}

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

		/*var inp = document.getElementById("q");
		inp.className = "searching";*/
	
	if (liveSearchLast != document.forms.rex_search.q.value) {
	if (liveSearchReq && liveSearchReq.readyState < 4) {
		liveSearchReq.abort();
	}
	if ( searchTrimAll(document.forms.rex_search.q.value) == "") {
		document.getElementById("LSResult").style.display = "none";
		highlight = document.getElementById("LSHighlight");
		if (highlight) {
			highlight.removeAttribute("id");
		}
		return false;
	}
	if (window.XMLHttpRequest) {
	// branch for IE/Windows ActiveX version
		liveSearchReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	liveSearchReq.onreadystatechange= liveSearchProcessReqChange;
	liveSearchReq.open("GET", "index.php?article_id=80&q=" + document.forms.rex_search.q.value);
	liveSearchLast = document.forms.rex_search.q.value;
	liveSearchReq.send(null);
	}
}
function liveSearchProcessReqChange() {

	/*var inp = document.getElementById("q");
		inp.className = "";*/

	if (liveSearchReq.readyState == 4) {
		var res = document.getElementById("LSResult");
		res.style.display = "block";
					
		var  sh = document.getElementById("LSShadow");

		sh.innerHTML = liveSearchReq.responseText;
	}
}
function liveSearchSubmit() {
	var highlight = document.getElementById("LSHighlight");
	if (highlight && highlight.firstChild) {
		window.location = highlight.firstChild.getAttribute("href");
		return false;
	} else {
		var searchInput = document.getElementById('rex_search');
	  searchInput.submit();
		return false;
	}
}

function liveSearchByContext(thelink,theText) {
var searchTerm = theText;
	if (liveSearchLast != searchTerm) {
	if (liveSearchReq && liveSearchReq.readyState < 4) {
		liveSearchReq.abort();
	}

	if (window.XMLHttpRequest) {
	// branch for IE/Windows ActiveX version
		liveSearchReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	liveSearchReq.onreadystatechange=function() {
	if (liveSearchReq.readyState == 4) {
		var newDiv = document.createElement('div');
		newDiv.id = 'liveLinkRes';
		newDiv.style.display = 'none';
		newDiv.innerHTML = liveSearchReq.responseText;//;
		newDiv.style.display = 'block';

		myRes=document.getElementById(theText+"-livelink");
		myRes.appendChild(newDiv);
		myBlank=document.createTextNode("");
		myRes.insertBefore(myBlank,newDiv);
	}
 }
	liveSearchReq.open("GET", liveSearchRoot + "index.php?article_id=80&q=" + searchTerm);
	liveSearchLast = searchTerm;
	liveSearchReq.send(null);
	} else {
		closeLiveLink();
	}
}


function searchTrimAll(sString) {
	while (sString.substring(0,1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ') {
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

/*function closeLiveLink() {
	liveSearchLast = "";
	highlight = document.getElementById("LSHighlight");
	if (highlight) {
		highlight.removeAttribute("id");
	}
	document.getElementById("liveLinkRes").style.display = "none";
}*/

/*
 * (c)2006 Dean Edwards/Matthias Miller/John Resig
 * Special thanks to Dan Webb's domready.js Prototype extension
 * and Simon Willison's addLoadEvent
 *
 * For more info, see:
 * http://dean.edwards.name/weblog/2006/06/again/
 * http://www.vivabit.com/bollocks/2006/06/21/a-dom-ready-extension-for-prototype
 * http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 *
 * Thrown together by Jesse Skinner (http://www.thefutureoftheweb.com/)
 *
 *
 * To use: call addDOMLoadEvent one or more times with functions, ie:
 *
 *    function something() {
 *       // do something
 *    }
 *    addDOMLoadEvent(something);
 *
 *    addDOMLoadEvent(function() {
 *        // do other stuff
 *    });
 *
 */

function addDOMLoadEvent(func) {
   if (!window.__load_events) {
      var init = function () {
          // quit if this function has already been called
          if (arguments.callee.done) return;

          // flag this function so we don't do the same thing twice
          arguments.callee.done = true;

          // kill the timer
          if (window.__load_timer) {
              clearInterval(window.__load_timer);
              window.__load_timer = null;
          }

          // execute each function in the stack in the order they were added
          for (var i=0;i < window.__load_events.length;i++) {
              window.__load_events[i]();
          }
          window.__load_events = null;
      };

      // for Mozilla/Opera9
      if (document.addEventListener) {
          document.addEventListener("DOMContentLoaded", init, false);
      }

      // for Internet Explorer
      /*@cc_on @*/
      /*@if (@_win32)
          document.write("<scr"+"ipt id=__ie_onload defer src=//0><\/scr"+"ipt>");
          var script = document.getElementById("__ie_onload");
          script.onreadystatechange = function() {
              if (this.readyState == "complete") {
                  init(); // call the onload handler
              }
          };
      /*@end @*/

      // for Safari
      if (/WebKit/i.test(navigator.userAgent)) { // sniff
          window.__load_timer = setInterval(function() {
              if (/loaded|complete/.test(document.readyState)) {
                  init(); // call the onload handler
              }
          }, 10);
      }

      // for other browsers
      window.onload = init;

      // create event function stack
      window.__load_events = [];
   }

   // add function to event stack
   window.__load_events.push(func);
}

addDOMLoadEvent(liveSearchInit);

