/* DOM
--------------------------------------------------------------------------------------------------*/

//
// Обертка вокруг getElementById.
// Возвращает узел с заданным ID.
//

function $(id) {
    var obj;
    try      {obj = document.getElementById(id);}
    catch(e) {obj = null;}
    return obj;
}


//
// Обертка вокруг getElementsByTagName.
// Возвращает либо список узлов, либо один узел (если в переменной num задан его номер в списке).
// Параметры parent и num - необязательные.
//

function $$(tag, parent, num) {
    var objs, obj;

    if (parent == null) parent = document;

    try      {objs = parent.getElementsByTagName(tag);}
    catch(e) {objs = null;}

    if (num != null && objs != null) {
        obj = objs.item(num);
        return obj;
    }
    else return objs;
}


//
// Создает новый элемент
//

function createEl(name, parent, text) {
    var el = document.createElement(name);
    if (text != null) {
        var text_el = document.createTextNode(text);
        el.appendChild(text_el);
    }
    if (parent != null) parent.appendChild(el);

    return el;
}


//
// Добавляет класс
//

function addClass(el, newClass) {
    if (el == null) return;
    
    el.className += ' ' + newClass;
}


//
// Удаляет класс
//

function removeClass(el, oldClass) {
    if (el == null) return;
    
    var re = new RegExp('(^| +)' + oldClass, 'g');
    el.className = el.className.replace(re, '');
}


//
// Проверяет, есть ли у элемента заданный класс
//

function hasClass(el, soughtClass) {
    if (el == null) return;
    
    if (el.className.indexOf(soughtClass) != -1) return true;
    else return false;
}


/* Эффекты
--------------------------------------------------------------------------------------------------*/

//
// Показывает/скрывает элемент
//

function toggleEl(el) {
    var obj, curStatus;

    // Определяем тип переданного параметра (объект или строка)
    var vartype = typeof(el);
    switch (vartype) {
        case 'object':
            obj = el;
            break;
        case 'string':
            obj = $(el);
            break;
        default:
            obj = null;
    }

    if (obj != null) {
        // Показываем
        if (hasClass(obj, 'hidden')) {
            removeClass(obj, 'hidden');
            curStatus = 'visible';
        }
        // Прячем
        else {
            addClass(obj, 'hidden');
            curStatus = 'hidden';
        }
    }

    return curStatus;
}

