var tabs       = new Array();
var names      = new Array();

var tab        = 'tab';
var tab_hover  = 'tab-hover';
var tab_active = 'tab-active';

function addTab(id, name)
{
    tabs[tabs.length]      = id;
    names[tabs.length - 1] = name;
    return true;
}

function _id(id)
{
    return document.getElementById(id);
}

function _showTab(id, cookie)
{
    _id(id).style.display      = '';
    _id('link' + id).className = tab_active;
    _id('link' + id).blur();
    if(cookie) {_setCookie(id);}
    return true;
}

function _hideTab(id)
{
    _id(id).style.display      = 'none';
    _id('link' + id).className = tab;
    return true;
}

function _setCookie(id)
{
    var today  = new Date();
    var expire = new Date();
    expire.setTime(today.getTime() + 3600000*24);
    document.cookie = "tabs=" + id + ";expires=" + expire.toGMTString();
}

function _getCookie()
{
    var theCookie = "" + document.cookie;
    var ind  = theCookie.indexOf('tabs');
    // nie ma cookie
    if(ind == -1) { return false;}
    var ind1 = theCookie.indexOf(';', ind);
    if (ind1 == -1) ind1 = theCookie.length;
    theCookie.substring(ind + 4 + 1 , ind1);
    return theCookie.substring( ind + 4 + 1, ind1)
}

function hideAllButOne()
{
    var show = _getCookie();
    var app = navigator.userAgent;
    if (app.indexOf('MSIEZZZZ') == -1) {
        for(i=0; i < tabs.length; i++) {
            i == 1 ? _showTab(tabs[i], false) : _hideTab(tabs[i]);
        }
        setTimeout("showByCookie()", 500);
    } else {
        showByCookie();
    }
}

function showByCookie()
{
    var show = _getCookie();
    if(!show) {
        for(i=0; i < tabs.length; i++) {
            i == 0 ? _showTab(tabs[i], true) : _hideTab(tabs[i]);
        }
    } else {
        for(i=0; i < tabs.length; i++) {
            show == tabs[i] ? _showTab(tabs[i], false) : _hideTab(tabs[i]);
        }
    }
}

function tabOnClick(id)
{
    for(i = 0; i < tabs.length; i++) {
        tabs[i] == id ? _showTab(tabs[i], true) : _hideTab(tabs[i]);
    }
    return true;
}

function drawLinks()
{
    for(i=0; i < tabs.length; i++) {
        document.write('<div id="tabs">');
        document.write('<a href="javascript:void(0)" id="link' + tabs[i] + '" class="tab" onclick="javascript:return tabOnClick(\'' + tabs[i] + '\');">' + names[i] + '</a>');
        document.write('</div>');
    }
}