
/* ----- livesearchccam.js ----- */
/*
// +----------------------------------------------------------------------+
// | 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 liveSearchReqCCAM = false;
var t = null;
var liveSearchLastCCAM = "";
var queryTargetCCAM = "livelightsearchccam_reply?q=";

var searchForm = null;
//var searchInput = null; 
var _searchesInput = new Object()

var isIE = false;


var _cacheCCAM = new Object();

var widthOffset=1;

function calculateWidth(){
}


function getElementDimensionsCCAM(elemID,searchInput) {
    var base = document.getElementById(elemID);
    var offsetTrail = base;
    var offsetLeft = 0;
    var offsetTop = 0;
    var width = 0;
    
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 &&
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }

    if(!isIE){
	width =  searchInput.offsetWidth-widthOffset*2;
    }
    else {
	width = searchInput.offsetWidth;
    }

    return { left:offsetLeft, 
	     top:offsetTop, 
	     width: width, 
             height: base.offsetHeight,
	     bottom: offsetTop + base.offsetHeight, 
	     right : offsetLeft + width};
}

function liveLightSearchInitCCAM(id) {    
    var searchInput = document.getElementById('searchGadget_'+id);
    if (searchInput == null || searchInput == undefined) return
    _searchesInput[id]=searchInput 

}
function liveSearchInitCCAM(id) {    
    var searchInput = document.getElementById('searchGadget_'+id);
    if (searchInput == null || searchInput == undefined) return
    _searchesInput[id]=searchInput 
//  Only keypress catches repeats in moz/FF but keydown is needed for
//  khtml based browsers.
    if (navigator.userAgent.indexOf("KHTML") > 0) {
        searchInput.addEventListener("keydown",function (e) {liveSearchKeyPressCCAM(e,id);},false);
        searchInput.addEventListener("focus",function () {liveSearchDoSearchCCAM(id);},false);
        searchInput.addEventListener("keydown",function (e) {liveSearchStartCCAM(e,id);}, false);
    } else if (searchInput.addEventListener) {
        searchInput.addEventListener("keypress",function (e) {liveSearchKeyPressCCAM(e,id);},false);
        searchInput.addEventListener("blur",function () {liveSearchHideDelayedCCAM(id);},false);
        searchInput.addEventListener("keypress",function (e) {liveSearchStartCCAM(e,id);}, false);
    } else {
        searchInput.attachEvent("onkeydown",function (e) {liveSearchKeyPressCCAM(e,id);});
        searchInput.attachEvent("onkeydown",function (e) {liveSearchStartCCAM(e,id);});
//      searchInput.attachEvent("onblur",function (){liveSearchHideCCAM(id);});
        isIE = true;
    }

//  Why doesn't this work in konq, setting it inline does.
    searchInput.setAttribute("autocomplete","off");

	var pos = getElementDimensionsCCAM('searchGadget_'+id,searchInput);	
	result = document.getElementById('LSResult_'+id);
	pos.left = pos.left - result.offsetParent.offsetLeft + pos.width;
	result.style.display='none';
}


function liveSearchHideDelayedCCAM(id) {
	window.setTimeout("liveSearchHide("+id+")",400);
}
	
function liveSearchHideCCAM(id) { 
	document.getElementById("LSResult_"+id).style.display = "none";
	var highlight = document.getElementById("LSHighlight_"+id);
	if (highlight) {
		highlight.removeAttribute("id");
	}
}

function getFirstHighlightCCAM(id) {
    var set = getHits(id);
    return set[0];
}

function getLastHighlightCCAM(id) {
    var set = getHits(id);
    return set[set.length-1];
}

function getHitsCCAM(id) {
    var res = document.getElementById("LSShadow_"+id);
    var set = res.getElementsByTagName('li');
    return set
}

function findChild(object, specifier) {
    var cur = object.firstChild;
    try {
	while (cur != undefined) {
	    cur = cur.nextSibling;
	    if (specifier(cur) == true) return cur;
	}
    } catch(e) {};
    return null;
    
}

function findNext(object, specifier) {
 var cur = object;
 try {
 while (cur != undefined) {

	cur = cur.nextSibling;
	if (cur.nodeType==3) cur=cur.nextSibling;
	
	if (cur != undefined) {
	    if (specifier(cur) == true) return cur;
    } else { break }
 }
 } catch(e) {};
 return null;
}

function findPrev(object, specifier) {
 var cur = object;
 try {
        cur = cur.previousSibling;
        if (cur.nodeType==3) cur=cur.previousSibling;
        if (cur!=undefined) {
            if (specifier(cur) == true) 
                return cur;
        } 
 } catch(e) {};
 return null;
}


function liveSearchKeyPressCCAM(event,id) {
	if (event.keyCode == 40 )
	//KEY DOWN
	{
		highlight = document.getElementById("LSHighlight_"+id);
		if (!highlight) {
		    highlight = getFirstHighlightCCAM(id);
		} else {
			highlight.removeAttribute("id");
			highlight = findNext(highlight, function (o) {return o.className =="LSRow";});

		}
		if (highlight) {
			highlight.setAttribute("id","LSHighlight_"+id);
		} 
		if (!isIE) { event.preventDefault(); }
	} 
	//KEY UP
	else if (event.keyCode == 38 ) {
		highlight = document.getElementById("LSHighlight_"+id);
		if (!highlight) {
		    highlight = getLastHighlightCCAM(id);
		} 
		else {
			highlight.removeAttribute("id");
			highlight = findPrev(highlight, function (o) {return o.className=='LSRow';});
		}
		if (highlight) {
				highlight.setAttribute("id","LSHighlight_"+id);
		}
		if (!isIE) { event.preventDefault(); }
	} 
	//ESC
	else if (event.keyCode == 27) {
		highlight = document.getElementById("LSHighlight_"+id);
		if (highlight) {
			highlight.removeAttribute("id");
		}
		document.getElementById("LSResult_"+id).style.display = "none";
	} 
}
function liveSearchStartCCAM(event,id) {
	if (t) {
		window.clearTimeout(t);
	}
	code = event.keyCode;
	if (code!=40 && code!=38 && code!=27 && code!=37 && code!=39) {
		t = window.setTimeout("liveSearchDoSearchCCAM("+id+")",200);
	} 
}

function liveSearchDoSearchCCAM(id) {

        var searchInput = _searchesInput[id];

	if (typeof liveSearchRootCCAM == "undefined") {
		liveSearchRootCCAM = "";
	}
	if (typeof liveSearchRootSubDirCCAM == "undefined") {
		liveSearchRootSubDirCCAM = "";
	}

	if (liveSearchLastCCAM != searchInput.value) {
	if (liveSearchReqCCAM && liveSearchReqCCAM.readyState < 4) {
		liveSearchReqCCAM.abort();
	}
	if ( searchInput.value == "") {
		liveSearchHideCCAM(id);
		return false;
	}

	// Do we have cached results
	var result = _cacheCCAM[searchInput.value];
	if (result) {
		showResultCCAM(result,id);	
		return;
	}
	liveSearchReqCCAM = new XMLHttpRequest();
	liveSearchReqCCAM.onreadystatechange= function () {liveSearchProcessReqChangeCCAM(id);};
	liveSearchReqCCAM.open("GET", liveSearchRootCCAM + queryTargetCCAM+ searchInput.value+"&id="+id );
	liveSearchLastCCAM = searchInput.value;
	liveSearchReqCCAM.send(null);
	}
}


function liveLightSearchDoSearchCCAM(id) {

        var searchInput = _searchesInput[id];

	if (typeof liveSearchRootCCAM == "undefined") {
		liveSearchRootCCAM = "";
	}
	if (typeof liveSearchRootSubDirCCAM == "undefined") {
		liveSearchRootSubDirCCAM = "";
	}

	if (liveSearchReqCCAM && liveSearchReqCCAM.readyState < 4) {
		liveSearchReqCCAM.abort();
	}
	if ( searchInput.value == "") {
		liveSearchHideCCAM(id);
		return false;
	}

	// Do we have cached results
	var result = _cacheCCAM[searchInput.value];
	if (result) {
		showResultCCAM2(result,id);	
		return;
	}
	liveSearchReqCCAM = new XMLHttpRequest();
	liveSearchReqCCAM.onreadystatechange= function () {liveSearchProcessReqChangeCCAM(id);};
	liveSearchReqCCAM.open("GET", liveSearchRootCCAM + queryTargetCCAM+ searchInput.value+"&id="+id );
	liveSearchLastCCAM = searchInput.value;
	liveSearchReqCCAM.send(null);
	
}


function showResultCCAM(result,id) {
  var  res = document.getElementById("LSResult_"+id);
  res.style.display = "block";
  var  sh = document.getElementById("LSShadow_"+id);
  sh.innerHTML = result;
}

function showResultCCAM2(result,id) {
  var  res = document.getElementById("LSResult2_"+id);
  res.innerHTML = result;
}

function liveSearchProcessReqChangeCCAM(id) {
	if (liveSearchReqCCAM.readyState == 4) {
		if (liveSearchReqCCAM.status > 299 || liveSearchReqCCAM.status < 200  ||
			liveSearchReqCCAM.responseText.length < 10) return;	
	showResultCCAM2(liveSearchReqCCAM.responseText,id);
	_cacheCCAM[liveSearchLastCCAM] = liveSearchReqCCAM.responseText;
	}
}

function liveSearchSubmitCCAM(id) {
	var highlight = document.getElementById("LSHighlight_"+id);
	
	if (highlight){
		target = highlight.getElementsByTagName('a')[0];
		window.location = liveSearchRootCCAM + liveSearchRootSubDirCCAM + target;
		return false;
	} 
	else {
		return true;
	}
}



if (window.addEventListener) 
     window.addEventListener("load",function () {liveLightSearchInitCCAM(1);liveLightSearchInitCCAM(2);liveLightSearchInitCCAM(3);},
                                     false);

//if (window.addEventListener) 
//     window.addEventListener("load",function () {liveSearchInitCCAM(1);liveSearchInitCCAM(2);liveSearchInitCCAM(3);},
//                                     false);
//else if (window.attachEvent) window.attachEvent("onload",
//                                     function () {liveSearchInitCCAM(1);liveSearchInitCCAM(2);liveSearchInitCCAM(3);});


