/**
 * In-page search
 */

var _is_ie		= (navigator.appName.indexOf('Microsoft') != -1) ? true : false;
var sbVisible	= false; // Searchbar visibility

var InpageSearch = function(element) {
    
    this._container = (typeof element != 'undefined') ? element : document.body;
    
    this.isFound = false;
    
    this._lDelim             = '{{';
    this._rDelim             = '}}';
    this._sText              = '';
    this._highlightHTMLBegin = '<span class="search_term_highlight">';
    this._highlightHTMLEnd   = '</span>';
    
    this._markOccurrences = function(haystack) {
        var query = this._sText.replace(/([.*+?|(){}\[\]\\&])/g, '\\$1');
        query = query.replace(/\s+/g, '\\s+');
        
        if (eval('/' + query + '/ig').test(haystack)) {
            this.isFound = true;
            return haystack.replace(eval('/(' + query + ')/ig'), this._lDelim + '$1' + this._rDelim);
        } else {
            return haystack;
        } 
    }
    
    this._highlightOccurrences = function() {
        var query = this._sText.replace(/([.*+?|(){}\[\]\\])/g, '\\$1');
        
        if (eval('/&/g').test(query)) {
            query = query.replace(/(\s)&(\s)/g, '$1(&|&amp;)$2');
        }
        query = query.replace(/\s+/g, '\\s+');
        
        this._container.innerHTML = this._container.innerHTML.replace(
            eval('/' + this._lDelim + '(' + query + ')' + this._rDelim + '/ig'),
            this._highlightHTMLBegin + '$1' + this._highlightHTMLEnd
        );
    }
    
    this._textNodeWalker = function(nd) {
        if (nd.nodeType == 1) { // ELEMENT_NODE
            for (var i in nd.childNodes) {
                this._textNodeWalker(nd.childNodes[i]);
            }
        } else if (nd.nodeType == 3 && nd.nodeValue.replace(/[\s]*/, '') != '') { // TEXT_NODE
            nd.nodeValue = this._markOccurrences(nd.nodeValue);
            return true;
        }
        return false;
    }
    
    this._regexEscape = function(str) {
        // TODO: Extend
        str = str.replace('/', '\\/');
        str = str.replace('[', '\\[');
        str = str.replace(']', '\\]');
        return str;
    }
    
    this.search = function(needle, bOmitPrevResults) {
        if (!bOmitPrevResults) {
            this.flushSearchResults();
            if (typeof dishighlightMatchedTopics == 'function') {
                dishighlightMatchedTopics();
            }
        }
            
        if (needle) {
            this._sText = needle.toString();
            this._textNodeWalker(this._container);
            this._highlightOccurrences();
        }
    }
    
    this.flushSearchResults = function() {
        this._container.innerHTML = this._container.innerHTML.replace(
            eval('/' + this._regexEscape(this._highlightHTMLBegin.replace(/"/ig, '"?')) + '([^>]+)' + this._regexEscape(this._highlightHTMLEnd) + '/ig'),
            '$1'
        );
        this.isFound = false;
    }
    
    return this;
}

function $(elId) {
    return document.getElementById(elId);
}

function switchSearchbarStatus() {
	if (sbVisible) {
		$('searchbar').style.display = 'none';
        switchFoundStatus(true);
		$('sText').value = '';
        //new InpageSearch($('main')).flushSearchResults();
        saMain.flushSearchResults();
        dishighlightMatchedTopics();
		sbVisible = false;
	} else {
		$('searchbar').style.display = 'block';
        switchFoundStatus(true);
		$('sText').focus();
		sbVisible = true;
	}
	
	return false;
}

function switchFoundStatus(isFound) {
    if (!isFound) {
        $('sNotFound').style.visibility = 'visible';
        $('sText').className = 's_not_found';
    } else {
        $('sNotFound').style.visibility = 'hidden';
        $('sText').className = '';
    }
}

function blockCtrlChar(e, chr) {
    if (!e) {
        e = window.event;
    }
    
    if(e.keyCode == chr.charCodeAt(0)) {
        if(e.ctrlKey) { //if press ctrl
            switchSearchbarStatus();
            
            if (e.cancelBubble != null) {
                e.cancelBubble = true;
            }
            
            if (e.stopPropagation) {
                e.stopPropagation();
            }
            
            if (e.preventDefault) {
                e.preventDefault();
            }
            
            if (_is_ie) {
                e.keyCode = 0;
            }

            e.returnValue = false;
            return false;
        }
    }
}

function blockRightClick(e) {
    return true;
    /*
    if (!e) {
        e = window.event;
    }
    
    if(e.button == 2) { //right mouse click
        if (e.cancelBubble != null) {
            e.cancelBubble = true;
        }
        
        if (e.stopPropagation) {
            e.stopPropagation();
        }
        
        if (e.preventDefault) {
            e.preventDefault();
        }
        
        if (_is_ie) {
            e.button = 0;
        }

        e.returnValue = false;
        return false;
    }
    */
}

function blockCopy(e) {
    if (!e) {
        e = window.event;
    }

    return $('searchbar') ? blockCtrlChar(e, 'F') : true;
}