function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		return null;
	}
}

var searchReq = getXmlHttpRequestObject();
var oldQuery = null;

function guess(e) {
    var key = keyHit(e);
    if (key == 13 || key == 40 || key == 38) {
        return;
    }
    performGuess();
}

function performGuess() {

    if (searchReq && (searchReq.readyState == 4 || searchReq.readyState == 0)) {
		var str = escape(document.getElementById('searchstr').value);
//        if (str != oldQuery) {
//            oldQuery = str;
//            searchReq.open("GET", 'do/qsearch?' + str, true);
//            searchReq.onreadystatechange = handleSearchSuggest;
//    	    popSupp.display("<table border='0' class='wikitable'><tr><td> Searching... "+str+"</td></tr></table>");
//            searchReq.send(null);
//        }
	}

}


function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

var popSupp={
    shown: false,
    inited: false,
    obj: null,
    listpos: 0,
    list: null,

    show: function() {
        if (!this.shown) {
            this.init();
            this.obj.style.display = '';
            this.shown = true;
        }
    },

    display: function(str) {
        this.list = null;
        this.show();
        this.obj.innerHTML=str;
        this.list = this.obj.getElementsByTagName('tr');
        this.select(0);
    },

    hide: function() {
        this.obj.style.display = 'none';
        this.shown = false;
    },

    select: function(newPos) {
        if (this.list) {
            oldRow = this.list[this.listpos];
            if (oldRow) {
                oldRow.style.background='white';
            }
            this.listpos = newPos;
            this.list[this.listpos].style.background='#eeeeee';
        }
    },

    down: function() {
        if (!this.shown) {
            this.select(0);
            this.show();
        } else if (this.list) {
            if (this.listpos < this.list.length - 1) {
                this.select(this.listpos+1);
            } else {
                this.select(0);
            }
        }

    },
    up: function() {
        if (this.list) {
            if (this.listpos > 0) {
                this.select(this.listpos-1);
            } else {
                this.select(this.list.length - 1);
            }
        }
    },
    currentPage: function() {
        return this.list ? this.list[this.listpos].getElementsByTagName('a')[0].innerHTML : null;
    },
    noList: function() {
        return this.list && this.listpos == 0 || !this.shown || !this.list;
    },
    init: function() {
        if (!this.inited) {
            var sb = document.getElementById("searchstr")
            this.obj = parent.document.getElementById("searchpopup")
            this.obj.style.left=''+(leftPosition(sb)-2)+'px';
            this.inited = true;

            if (sb.addEventListener) {
                sb.addEventListener("keydown", window.checkpress, false);
            } else if (sb.attachEvent) {
                sb.attachEvent("onkeydown", window.checkpress);
            } else {
                sb.onkeydown = window.checkpress;
            }
        }
    }
};

function handleSearchSuggest() {
	if (searchReq.readyState == 4) {
		if (searchReq.status == 200) {
			popSupp.display(searchReq.responseText);
			performGuess();
		}
	}
}


function doSearch() {
    popSupp.hide();
//    pgWnd = parent.document.getElementById("kbpage").contentWindow
//    pgWnd.location.href='do/search?'+document.getElementById('searchstr').value;
    parent.location.href='?page=search&query=' + document.getElementById('searchstr').value;
}

function doOpen(pageName) {
    popSupp.hide();
    if (pageName) {
        parent.location.href='?page=knowledgeBase.' + escape(pageName);
//        pgWnd = parent.document.getElementById("kbpage").contentWindow;
//        pgWnd.location.href='do/show?'+escape(pageName);
    }

}

function  enterHit(e) {

    if(window.event) {// IE
        keynum = e.keyCode
    } else if(e.which) { // Netscape/Firefox/Opera
        keynum = e.which
    }

    return keynum == 13;
}

function keyHit(e) {
    return e.which || e.keyCode || 0;
/*    var keynum

    if (window.event) { //IE
        keynum = e.keyCode
    } else if (e.which) { //IE
        keynum = e.which
    } else if(e) {// IE
        keynum = e.keyCode
    } else {
        keynum = -1;
    }

    return keynum;*/
}

function checkpress(e) {

    var key = keyHit(e)

    if (key == 13) {
        if (popSupp.noList()) {
            doSearch();
        } else {
            doOpen(popSupp.currentPage());
        }
    } else if (key == 27) { // Escape
        popSupp.hide();
    // down = 40, up = 38;
    } else if (key == 40) {
        popSupp.down();
        e.returnValue = false;
    } else if (key == 38) {
        popSupp.up();
        e.returnValue = false;
    }
}

