var toc, main, div, dbtn, TOCPrevWidth;

var toolbarHeight   = 74;
var dividerWidth    = 8;
var TOCBaseWidth    = 200;
var TOCCollapsed    = false;

document.tocTopics      = new Array();
document.foundTopics    = new Array();

function getBodyWidth() {
    return parseInt(document.body.clientWidth) - dividerWidth;
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    
    if (typeof expiredays == 'undefined' || expiredays == null) {
        expiredays = null;
        exdate.setDate(exdate.getDate() + expiredays);
    }
    
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/";
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) { 
            c_start = c_start + c_name.length + 1; 
            c_end = document.cookie.indexOf(";",c_start);
            if (c_end == -1) {
                c_end = document.cookie.length;
            }
                
            return unescape(document.cookie.substring(c_start, c_end));
        } 
    }
    return "";
}

function getTOCWidth() {
    return parseInt(toc.style.width);
}

function getStoredTOCWidth() {
    var tocWidth;
    
    if ((tocWidth = getCookie('tocWidth')) == '') {
        tocWidth = TOCBaseWidth;
    }
    
    setCookie('tocWidth', tocWidth);
    
    return tocWidth;
}
function getStoredTOCPrevWidth() {
    var tocWidth;
    
    if ((tocWidth = getCookie('tocPrevWidth')) == '') {
        tocWidth = TOCBaseWidth;
    }
    
    return tocWidth;
}

function storeTOCWidth() {
    setCookie('tocWidth', getTOCWidth());
}

function setDivBtnClassName() {
    dbtn.className = TOCCollapsed ? 'coll' : '';
}

function resizeTOC(w) {
    w = typeof w != 'undefined' && !isNaN(parseInt(w)) ? parseInt(w) : parseInt(div.style.left);
    toc.style.width = (w > 0 ? w : 0) + 'px';
    main.style.left = w + dividerWidth + 'px';
    
    storeTOCWidth();
}

function collapse() {
    if (TOCCollapsed) {
        resizeTOC(TOCPrevWidth);
        div.style.left = TOCPrevWidth + 'px';
        TOCCollapsed = false;
        setDivBtnClassName();
    } else {
        TOCPrevWidth = getTOCWidth();
        setCookie('tocPrevWidth', TOCPrevWidth);
        resizeTOC(0);
        div.style.left = 0 + 'px';
        TOCCollapsed = true;
        setDivBtnClassName();
    }
}

function initTOC() {
    div = document.getElementById('div');
    main = document.getElementById('main');
    storedTOCWidth = getStoredTOCWidth();
    if (storedTOCWidth == 0) {
        TOCCollapsed = true;
    }
    
    if (typeof div != 'undefined' && div != null) {
        div.style.top = '0px';
        div.style.left = storedTOCWidth + 'px';
        div.ondblclick = collapse;
        
        toc = document.getElementById('toc');
        toc.style.width = storedTOCWidth + 'px';
        TOCPrevWidth = getStoredTOCPrevWidth();
        
        dbtn = document.getElementById('dbtn');
        setDivBtnClassName();
        dbtn.onclick = collapse;
        
        Drag.init(div, null, 0, getBodyWidth(), 0, 0);
        div.onDragEnd = resizeTOC;
        
        resizeTOC(storedTOCWidth);
    } else {
        main.style.left = 0;
    }
}

function showDefinition(term, obj, e) {
    term = term.toUpperCase();
    var def = terms[term];
    
    if (def) {
        def = def.replace(/(\[\[|\]\])/g, '');
        showhint(def, obj, e, '200px');
    } else {
        showhint('<span style="color:#f00;">Error. Term is not defined</span>', obj, e, '150px');
    }
}

function putDefinition(def) {
    def = def.replace(/\[\[(.*?)\]\]/g, '<a onmouseover="showDefinition(\'$1\', this, event)" href="#">$1</a>');
    document.write(def);
}

function listenEnter(e, url) {
    if (!e) {
        e = window.event;
    }
    
    if(e.keyCode == 13) { // "Enter" key
        if (typeof(forceRedirect) != 'undefined' && forceRedirect != '') {
            alert("Sorry, the search functionality is not available without authorization.");
            return false;
        }
        
        saMain.search(_GID('sText').value);
        
        var sText = _GID('sText').value.replace(/[\s]+/, '+');
        url = url.replace('{SEARCH_TEXT}', sText);
        
        if (url.match('\\?')) {
            url += '&template_type=xml';
        } else {
            url += '?template_type=xml';
        }
        
        return false;
    }
}

function inpageInitalSearch(text, url) {
    saMain.search(text);
    
    var sText = text.replace(/[\s]+/, '+');
    url = url.replace('{SEARCH_TEXT}', sText);
    
    if (url.match('\\?')) {
        url += '&template_type=xml';
    } else {
        url += '?template_type=xml';
    }
    
    var ajaxRequest = new AjaxRequest(url);
    
    ajaxRequest.setPostRequest(highlightMatchedTopics);
    ajaxRequest.sendRequest();    
    
    return false;
}

function registerTopic(path, objId) {
    if (_GID(objId)) {
        var i = document.tocTopics.length;
        document.tocTopics[i] = new Array();
        document.tocTopics[i].id = path;
        document.tocTopics[i].obj = _GID(objId);
    }
}

function highlightMatchedTopics() {
    var i, j;
    for (i in document.tocTopics) {
        for (j in document.foundTopics) {
            if (document.tocTopics[i].id.replace(/^.*&path=(.*)$/, '$1') == document.foundTopics[j]) {
                document.tocTopics[i].obj.className = (document.tocTopics[i].obj.className == '') ? 'search_term_highlight' : document.tocTopics[i].obj.className + ' search_term_highlight';
                if (saMain != undefined)
                    document.tocTopics[i].obj.href += "&highlight=" + saMain._sText;
            }
        }
    }
    
    switchSearchbarStatus();
    _GID('sText').value = saMain._sText;
    
    switchFoundStatus(document.foundTopics.length > 0 || saMain.isFound);
    
    document.foundTopics = new Array();
}

function dishighlightMatchedTopics() {
    for (j in document.tocTopics) {
        document.tocTopics[j].obj.className = (document.tocTopics[j].obj.className.match(' ')) ? document.tocTopics[j].obj.className.replace(' search_term_highlight', '') : '';
        
        document.tocTopics[j].obj.href = document.tocTopics[j].obj.href.replace(/\&highlight=.*$/, '');
    }
    document.foundTopics = new Array();
}

function scrollTOCTo(id)
{
    var pageElement    = document.getElementById(id);
    var tocElement     = document.getElementById('toc');
    var toolbarElement = document.getElementById('toolbar');
    
    if (pageElement != null) {
        if (typeof document.getBoxObjectFor == 'function') {
            offY = document.getBoxObjectFor(pageElement).y
        } else if (typeof pageElement.getBoundingClientRect == 'function') {
            offY = pageElement.getBoundingClientRect().top;
        } else {
            offY = pageElement.offsetTop;
        }
    }
    
    if (pageElement && tocElement && offY > tocElement.clientHeight) {
        tocElement.scrollTop = offY - toolbarElement.clientHeight - 200;
    }
}
