﻿////////////////////
// tabs functions
////////////////////


// closes all open tabs and remove shortcuts
function removeAllTabs(all) {
    while (tabcount > -1) {
        removeSelectedTab();
    }
    if (all) {
        //$$('#acme_buttons img').each(function(e) { e.remove(); });
        //$$('.tooltip').each(function(e) { e.hide(); });
        //$('div_search').hide();
        Effect.SlideUp('topmenu');
    }
}


function previousTab(nextitem) {
    var cont = false;

    if ($('t_' + currtab + '_prevTab')) {
        var prevTab = $('t_' + currtab + '_prevTab').value;

        if (tabExists(prevTab) != -1) {
            selectTab(prevTab);
            //return prevTab;
        }
        else {
            cont = true;
        }
    }
    else {
        cont = true;
    }


    if (cont) {
        if (nextitem != "" && nextitem != undefined) {   // if another tab is still open, make that active, this will be the last other tab in the list 
            selectTab(nextitem);
            //return nextitem;
        }
    }
}


// adds a new tab at the end of the tab list and make it active
// 2009-02-09 change this to add to start of list
// 2009-03-01 changed to use unsorted lists instead of tables
function addTab(title, name, closescript, verbsInterestedIn) {
    var ul;

    if (tabExists(name) == -1) {                // add a new tab if the tab doesn't exist
        tabcount++;

        for (var i = tabcount; i > 0; i--) {    // move current tabs one forward
            tabs[i] = tabs[i - 1];
            ctScripts[i] = ctScripts[i - 1];
            verbsInt[i] = verbsInt[i - 1];
        }
        tabs[0] = name;                         // add new tab name to start
        ctScripts[0] = closescript;
        verbsInt[0] = verbsInterestedIn;        // maintain verbs the tab is interested in

        if (!$('tabs_ul')) {            // add the unordered list if it does not exist yet
            var parent = $('acme_tabs');
            ul = document.createElement('ul');
            ul.id = "tabs_ul";
            parent.appendChild(ul);
        }

        ul = $('tabs_ul');
        var li = document.createElement('li');
        li.id = 't_' + name;
        li.className = 'tab_selected';                       

        // add title for tab
        var sp = document.createElement('span');
        sp.className = 'acme_tabs_span';                
        sp.appendChild(document.createTextNode(title));
        li.appendChild(sp);

        var anc = document.createElement('span');        
        anc.className = 'tabclose';
        anc.id = 't_' + name + '_close';
        li.appendChild(anc);

        var prevT = document.createElement( 'input');
        prevT.type = 'hidden';
        prevT.id = 't_' + name + '_prevTab';
        prevT.value = currtab;
        li.appendChild(prevT);

        ul.insert( { top: li });

        // TODO: deregister this event on deletion of tab
        Event.observe('t_' + name, 'click', (function(link) { selectTab(name); }));
        Event.observe('t_' + name + '_close', 'click', (function(link) { removeTab(name); link.stop(); }));
    }
    selectTab(name);
    return true;
}


// check if a tab with the given name exists
function tabExists(name) {
    for (var i = 0; i <= tabs.length; i++) { if (tabs[i] == name) { return i; } }
    return -1;
}


// remove the tab and div for the currently selected tab, make the tab after the removed one active
function removeSelectedTab() {
    removeTab(currtab);
}


// remove the tab and div for the currently selected tab, make the tab after the removed one active
function removeTab(tbName) {
    if (tabcount >= 0) {
        var row = $('tabs_ul');    // reference to the table row containing the tab images
        var d = $('obj_body');  // reference obj_body where each tab's data is stored in a div
        var d2 = $(tbName);    // refs the div containing the tab's data
        var nextitem = '';
        var found = false; // will indicate that the selected tab has been found and tabs that follow will be moved on position left
        var prevTab = '';

        for (var i = 0; i < tabs.length; i++) { // iterate through every tab
            if (tabs[i] == tbName) {
                found = true;

                if ($('t_' + currtab + '_prevTab')) {
                    prevTab = $('t_' + currtab + '_prevTab').value;
                }

                // check if there is a script to perform on close for this tab
                eval(ctScripts[i]);

                $('t_' + tbName).remove();
                if (d2 != null) {
                    d.removeChild(d2);  // remove the div containg the tab's data
                }
                tabcount--;
            }
            else {
                if (nextitem == '') {       // mark the next tab as active
                    nextitem = tabs[i];
                };
                if (found) {
                    // after the selected tab is found, move all tabs that follow one position forward and update cookie info
                    tabs[i - 1] = tabs[i];
                    ctScripts[i - 1] = ctScripts[i];
                    verbsInt[i - 1] = verbsInt[i];
                }
            }
        }

        tabs.length = tabs.length - 1;  // remove the last element from the tabs array

        if (tbName == currtab) {
            if (prevTab == '') {
                selectTab(nextitem);
            }
            else {
                selectTab(prevTab);
            }
        }
    }

    if (tabcount == -1) {
        showDiv('div_default', 'block');
        showDiv('tab_menu', 'none');
        showDiv('tab_close', 'none');
    }
}


function selectTab(name) {
    setCurrTab(name);
    showDiv(name, 'block');

    $$('#tabs_ul li').each(function(e) {
        if (e.id != 'tab_close' && e.id != 'tab_menu') {
            if (e.id == 't_' + name) {
                e.className = 'tab_selected';
            }
            else {
                e.className = '';
            }
        }
    });
}


// handle clicks when tab menu is displayed
// clicking anywhere outside tab menu hides it
// click on close all tabs will close all open tabs and display welcome screen again and hide tab menu
// click on tab name will select that tab and hide tab menu
// click on delete icon next to tab name will close that tab and remove from tab menu
function clickTabMenu(e) {
    var tid = e.target.id.gsub('tm_', '');
    var tdiv = e.target.up('div');

    var closeMenu = true;

    if (tdiv) {
        if (tid == 'tab_menu') {    // click was on tab menu
            closeMenu = false;
        }

        if (tdiv.id == 'tablist') {
            var row = e.target.up('tr');
            var rowid = row.id;

            closeMenu = false;

            if (e.target.hasClassName('select')) {  // a tab's description was clicked
                selectTab(tid);
                closeMenu = true;
            }

            if (e.target.hasClassName('close')) {   // a tab's close icon was clicked
                tid = e.target.up('td').previous('td').id.gsub('tm_', '');
                removeTab(tid);
                if (tabcount == -1) {
                    closeMenu = true;
                    getObjectResponse(0, 42, '', '', '', '');       // show welcome screen 
                }
                showTabMenu();  // refresh tab menu (effectively removing the closed tab)
            }

            if (e.target.hasClassName('closeall')) {    // close all open tabs was clicked
                removeAllTabs(false);
                closeMenu = true;
                getObjectResponse(0, 42, '', '', '', '');       // show welcome screen                
            }
        }
    }

    if (closeMenu) {
        hideTabMenu();
    }
}


function incLoadingStatus() {
    busyCount += 1;
    updateLoadingStatus();
}


function decLoadingStatus() {
    busyCount = busyCount - 1;
    updateLoadingStatus();
}


// display the number of request currently running
function updateLoadingStatus() {
    if (busyCount < 1) {
        $('loading').hide();
    }
    else {
        if (busyCount == 1) {
            $('loading').innerHTML = "<img src=\"theme/spinner_small.gif\" alt=\"\" />Loading 1 item";
        }
        else {
            $('loading').innerHTML = "<img src=\"theme/spinner_small.gif\" alt=\"\" />Loading " + busyCount + " items";
        }
        $('loading').show();
    }
}


function setCurrTab(divname) {
    if (currtab != "") { showDiv(currtab, "none"); }
    currtab = divname;
}


function hideTabMenu() {
    Effect.SlideUp('tablist', { duration: 0.3 });
    Event.stopObserving(document.body, 'click');
}


// display a menu with links to all open tabs
function showTabMenu() {
    var bod = '<table>';
    var t;
    for (var i = 0; i < tabs.length; i++) { // iterate through every tab
        t = tabs[i];
        bod += '<tr>';
        bod += '<td id=\'tm_' + t + '\' class=\'select\'>' + $('t_' + t).down( 'span').innerHTML + '</td>';
        bod += '<td class=\'tbl_row_check\'><img src=\'icons/cancel.png\' alt=\'\' title=\'Close this tab\' class=\'close\'></td>';
        bod += '</tr>';
    }
    bod += '<tr><td class=\'closeall\'>Close all open tabs</td><td class=\'closeall\'></td></tr>';
    bod += '</table>';

    $('tablist').innerHTML = bod;
    Effect.SlideDown('tablist', { duration: 0.3 });
    Event.observe(document.body, 'click', clickTabMenu); // hides tabs menu when clicking anywhere else
    SetTooltips('#tablist img');
}


// hides or displays the given <div>
function showDiv(div, state) {
    if (state == "block") {
        if ($(div) != null) {
            $(div).show();
        }
    }
    else {
        if ($(div) != null) {
            $(div).hide();
        }
    }
}
