// ============================================================================
// Developed by Kernel Team.
// http://kernel-team.com
// ============================================================================

// ============================================================================
// | Common functions                                                         |
// ============================================================================

function stub() {
}

function addCl(el, cl) {
    if (!hasCl(el, cl)) {
        if (el.className.length == 0) {
            el.className = cl;
        } else {
            el.className += ' ' + cl;
        }
    }
}

function hasCl(el, cl) {
    var classes = el.className.split(' ');
    for (var i = 0; i < classes.length; i++) {
        if (classes[i] == cl) {
            return true;
        }
    }
    return false;
}

function removeCl(el, cl) {
    var classes = el.className.split(' ');
    var newClass = '';
    for (var i = 0; i < classes.length; i++) {
        if (classes[i] != cl) {
            newClass += ' ' + classes[i];
        }
    }
    el.className = newClass;
}

function getXmlr() {
    var res = null;
    if (window.XMLHttpRequest) {
        res = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        res = new ActiveXObject('Microsoft.XMLHTTP');
    }
    return res;
}

// ============================================================================
// | Blocks visibility switcher                                               |
// ============================================================================

function KTBlockSwitcher(globalId, blockId, swictherId, switcherSelectedStyle) {
    var block = document.getElementById(blockId);
    var switcher = document.getElementById(swictherId);

    if (switcher) {
        switcher.onclick = function() {
            KTBlockSwitcher._switch(globalId, blockId, swictherId, switcherSelectedStyle);
        };
        if (hasCl(switcher, switcherSelectedStyle)) {
            KTBlockSwitcher._cb[globalId] = block;
            KTBlockSwitcher._cs[globalId] = switcher;
            KTBlockSwitcher._csss[globalId] = switcherSelectedStyle;
        }
    }
}

KTBlockSwitcher._cb = new Object();
KTBlockSwitcher._cs = new Object();
KTBlockSwitcher._csss = new Object();

KTBlockSwitcher._switch = function(gid, blid, swid, cl) {
    var bl = document.getElementById(blid);
    var sw = document.getElementById(swid);
    if ((!bl) || (bl == KTBlockSwitcher._cb[gid])) {
        return;
    }
    if (KTBlockSwitcher._cb[gid]) {
        KTBlockSwitcher._cb[gid].style.display = 'none';
    }
    if (KTBlockSwitcher._cs[gid]) {
        removeCl(KTBlockSwitcher._cs[gid], KTBlockSwitcher._csss[gid]);
    }

    bl.style.display = 'block';
    if (bl.style.visibility = 'hidden') {
        bl.style.visibility = 'visible';
    }
    KTBlockSwitcher._cb[gid] = bl;

    if (sw && cl) {
        addCl(sw, cl);
        KTBlockSwitcher._cs[gid] = sw;
        KTBlockSwitcher._csss[gid] = cl;
    }
}