
function SearchBox(resultUrl, wordsMode, catalogue, textId, btnId) {
    this.ResultUrl = resultUrl;
    this.WordsMode = wordsMode;
    this.Catalogue = catalogue;
    this.TextId = textId;
    this.BtnId = btnId;
    this.TextCtrl = null;
    this.BtnCtrl = null;
    
    var instance = this;  
	instance.InitializeHandler = function() {instance.Initialize();};
	if (document.all && window.attachEvent) {	
		window.attachEvent("onload", this.InitializeHandler);
	} 
	else if (document.addEventListener) {
        window.addEventListener("load", this.InitializeHandler, false);
	}
}

SearchBox.Create = function (resultUrl, wordsMode, catalogue, textId, btnId) {
    if (typeof(searchBox) == "undefined") {
        searchBox = new Array();
    }
    searchBox[textId] = new SearchBox(resultUrl, wordsMode, catalogue, textId, btnId);
}

SearchBox.prototype.GotoResult = function () {
	window.location = this.GetLocation();
}

SearchBox.prototype.GetLocation = function () {
    var query = this.TextCtrl.value;
    var join = (this.ResultUrl.indexOf("?") == -1) ? "?" : "&";
    var mode = (this.WordsMode != "AllWords") ? "&WordsMode=" + this.WordsMode : "";
    return this.ResultUrl + join + "IndexCatalogue=" 
	    + Url.Encode(this.Catalogue) + "&SearchQuery=" + Url.Encode(query) + mode;
}

SearchBox.prototype.KeyPress = function (e) {
    if (!e) {
	    var e = event;
    }
    if (e.keyCode == 13) {
        if (e.stopPropagation) 
            e.stopPropagation(); 
        else
            e.cancelBubble = true;  
        if (e.preventDefault) 
            e.preventDefault(); 
        else
            e.returnValue = false;
        this.GotoResult();
    }
}

SearchBox.prototype.KeyUp = function (e) {
    this.BtnCtrl.href = this.GetLocation();
}

SearchBox.prototype.Initialize = function () {
    this.TextCtrl = document.getElementById(this.TextId);
    this.BtnCtrl = document.getElementById(this.BtnId);
    var instance = this;
	instance.KeyPressHandler = function(e) {instance.KeyPress(e);};
	instance.KeyUpHandler = function(e) {instance.KeyUp(e);};
	if (document.all && this.TextCtrl.attachEvent) {	
		this.TextCtrl.attachEvent("onkeypress", this.KeyPressHandler);
		if (this.BtnCtrl.href) {
	        this.TextCtrl.attachEvent("onkeyup", this.KeyUpHandler);
	    }
	} 
	else if (this.TextCtrl.addEventListener) {
        this.TextCtrl.addEventListener("keypress", this.KeyPressHandler, false);
        if (this.BtnCtrl.href) {
	        this.TextCtrl.addEventListener("keyup", this.KeyUpHandler, false);
	    }
	}
}

function indexingStart() {

    var windowH = getPageSize();
    document.getElementById('searchIndexingInProcess').getElementsByTagName('div')[0].style.top = ((windowH[3])/2)+'px';
    //document.getElementById('searchIndexingInProcess').getElementsByTagName('div')[0].style.left = ((windowH[2])/2)+'px';
    document.getElementById('searchIndexingInProcess').style.display = 'block';
    document.getElementsByTagName('body')[0].className = 'searchIndexingBodyCrop';
}
function getPageSize(){

    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {    
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;

    //      console.log(self.innerWidth);
    //      console.log(document.documentElement.clientWidth);

    if (self.innerHeight) {   // all except Explorer
    if(document.documentElement.clientWidth){
          windowWidth = document.documentElement.clientWidth; 
    } else {
          windowWidth = self.innerWidth;
    }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    } 
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else { 
        pageHeight = yScroll;
    }

    //      console.log("xScroll " + xScroll)
    //      console.log("windowWidth " + windowWidth)

    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){      
        pageWidth = xScroll;        
    } else {
        pageWidth = windowWidth;
    }
    //      console.log("pageWidth " + pageWidth)
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
    return arrayPageSize;
}

