﻿// Research Library tab controls

// Speed
var rl_Speed = 16;  // Lower = Slower

var bIsOpen = "0";
var iLeft = 0;
var iTimer;
var iClosedLeft = -226;
var iOpenLeft = -1;

var LOADINGIMG = "<img src='/images/loading.gif' border='0'>";

// First load - read preference from cookie - assumed closed by default
bIsOpen = (readCookie("rl_tab")!=null) ? readCookie("rl_tab") : "0";
if (bIsOpen=="0") {iLeft = iClosedLeft;rl_SetLeft(iLeft);rl_UpdateCloseButton('none');}

// Tab originally hidden to stop flicker - So, now we can display tab...
rl_SetDisplay('Inline');

function rl_OpenTab() {
    if (bIsOpen == "1") return;
    iTimer = setInterval("rl_open()", 30);
}

function rl_open() {
    iLeft=iLeft+rl_Speed;
    if (iLeft >= iOpenLeft) {
        clearInterval(iTimer);
        iLeft = iOpenLeft;
        bIsOpen = "1";
        rl_UpdateCookie();
        rl_UpdateCloseButton('inline');
    }
    rl_SetLeft(iLeft);
}

function rl_CloseTab() {
    if (bIsOpen == "0") return;
    iTimer = setInterval("rl_close()", 30);
}

function rl_close() {
    iLeft=iLeft-rl_Speed;
    if (iLeft <= iClosedLeft) {
        clearInterval(iTimer);
        iLeft = iClosedLeft;
        bIsOpen = "0";
        rl_UpdateCookie();
        rl_UpdateCloseButton('none');
    }
    rl_SetLeft(iLeft);
}

function rl_UpdateCloseButton(w) {
    var oTab = document.getElementById('rl_image_close');
    if (oTab!=null) oTab.style.display = w;
}

function rl_UpdateCookie() {
    createCookie("rl_tab", bIsOpen, 360);
}

function rl_SetLeft(x) {
    var oTab = document.getElementById('rl_tab');
    if (oTab!=null) oTab.style.left = (x)+"px";
}

function rl_SetDisplay(x) {
    var oTab = document.getElementById('rl_tab');
    if (oTab!=null) oTab.style.display = x;
}

function rl_DoSearch() {
    rl_StartLoad();
    // Ensure tab open...
    rl_OpenTab();
    // Do AJAX request...
    DoAjaxRequest("/rl_handler.aspx", "search="+(encodeURI(document.forms[0].search.value)), "rl_EndRequest")
}
// Version that leaves tab in current position
function rl_DoSearch2(st) {
    rl_StartLoad();
    document.forms[0].search.value = st;
    // Do AJAX request...
    DoAjaxRequest("/rl_handler.aspx", "search="+(encodeURI(document.forms[0].search.value)), "rl_EndRequest")
}
// Repeat last search
function rl_RepeatLastSearch() {
    rl_StartLoad();
    // Ensure tab open...
    rl_OpenTab();
    // Do AJAX request...
    DoAjaxRequest("/rl_handler.aspx?last=1", "", "rl_EndRequest")
}
// Do page code search...
function rl_DoPageCodeSearch(pc) {
    rl_StartLoad();
    // Ensure tab open...
    rl_OpenTab();
    // Do AJAX request...
    DoAjaxRequest("/rl_handler.aspx?pc=1", "search="+(encodeURI(pc)), "rl_EndRequest")
}
// Do page code search...and clear search box
function rl_DoPageCodeSearch2(pc) {
    rl_StartLoad();
    document.forms[0].search.value = "";
    // Do AJAX request...
    DoAjaxRequest("/rl_handler.aspx?pc=1", "search="+(encodeURI(pc)), "rl_EndRequest")
}
// Do page code and keyword search...
function rl_DoPageCodeAndKeywordSearch(pc, st) {
    rl_StartLoad();
    // Ensure tab open...
    rl_OpenTab();
    // Do AJAX request...
    DoAjaxRequest("/rl_handler.aspx?pc=2", "search="+(encodeURI(pc))+"&search2="+(encodeURI(st)), "rl_EndRequest")
}
function rl_StartLoad() {
    var tb = document.getElementById("rl_content");
    if (tb!=null) tb.innerHTML = LOADINGIMG;
}
// End of AJAX request returns here...
function rl_EndRequest(result) {
    var tb = document.getElementById("rl_content");
    if (tb!=null) {
        tb.innerHTML = result;
        // If content differs from last result, assume new list so leave scrollbar at top
        var rl_length = (readCookie("rl_length")!=null) ? readCookie("rl_length") : 0;
        if (rl_length == result.length) {
            setTimeout("rl_UpdateScrollPosition()", 120);
        } else {
            createCookie("rl_length", result.length, 1);
        }
        Ajax_CheckScriptTags(result);
    }
}

// Set timeout that when user stops typing THEn we send single request!
var rl_kuto = null;
function rl_keyup() {
    if (rl_kuto != null)
        clearTimeout(rl_kuto);
    rl_kuto = setTimeout('rl_keyup_send()', 400);
}

function rl_keyup_send() {
    st = new String(document.forms[0].search.value);
    if (st.length >= 0)
        rl_DoSearch();
}

function rl_SetScrollPosition() {
    createCookie("rl_tab_scroll", document.getElementById('rl_content').scrollTop, 1);
}
function rl_InitialiseScrollPosition() {
    createCookie("rl_tab_scroll", "0", 1);
}

function rl_UpdateScrollPosition() {
    document.getElementById('rl_content').scrollTop = (readCookie("rl_tab_scroll")!=null) ? readCookie("rl_tab_scroll") : 0;
}

function rl_scrollIntoView(id) {
    var zz = document.getElementById(id);
    if (zz!=null) {
        // Scroll item into view...
        zz.scrollIntoView(true);
        // ...now offset so roughly centered taking into account bottom of page items...
        var zy = document.getElementById('rl_content');
        if (zy!=null) {
            var os = (zy.offsetHeight/2);
            if ((zy.scrollTop+os+(zy.clientHeight/2)) < zy.scrollHeight)
                zy.scrollTop = zy.scrollTop - os;
        }
    }
}