(function(window) {
var logger = getLogger('03-dom.js')

window.ge = function ge(id) {
    return document.getElementById(id)
}

window.re = function re(el) {
    if (typeof el === 'string')
        el = ge(el);
    if (el && el.parentNode) {
        el.parentNode.removeChild(el)
    }
}

window.ce = function ce(tagName, attr, style) {
    if (arguments.length === 1 && typeof tagName == 'string') {
        var div = document.createElement('div');
        div.innerHTML = tagName;
        return div.firstChild;
    }

    var el = document.createElement(tagName);
    if (attr) {
        extend(el, attr);
    }
    if (style) {
        setStyle(el, style);
    }
    return el
}

window.insertAfter = function insertAfter(elem, refElem) {
    return refElem.parentNode.insertBefore(elem, refElem.nextSibling);
}

window.show = function show(el, dsp) {
    if (typeof el === 'string')
        el = ge(el);
    if (!el)
        return logger.local('show').error('invalid element:', el)
    if (!dsp)
        dsp = el.tagName === 'SPAN' || el.tagName === 'A' ? 'inline' : 'block';
    el.style.display = dsp;
}

window.hide = function hide(el) {
    if (typeof el === 'string')
        el = ge(el);
    if (!el)
        return logger.local('hide').error('invalid element:', el)
    el.style.display = 'none';
}

window.visible = function visible(el) {
    return getStyle(el, 'display') !== 'none';
}

window.toggle = function toggle(el) {
    visible(el) ? hide(el) : show(el)
}

window.hasClass = function hasClass(el, name) {
    if (typeof el === 'string')
        el = ge(el);
    return el && el.nodeType === 1 && (" " + el.className + " ").replace(/[\t\r\n\f]/g, " ").indexOf(" " + name + " ") >= 0
}

window.addClass = function addClass(el, name) {
    if (typeof el === 'string')
        el = ge(el);
    if (!el)
        return logger.local('addClass').warn('el is', el)
    if (!hasClass(el, name))
        el.className = (el.className ? el.className + ' ' : '') + name
}

window.removeClass = function removeClass(el, name) {
    if (typeof el === 'string')
        el = ge(el);
    if (!el)
        return logger.local('removeClass').warn('el is', el)
    if (isArray(name)) {
        for (var i = 0; i < name.length; i++)
            removeClass(el, name[i]);
        return;
    }
    el.className = ((el.className || '').replace((new RegExp('(\\s|^)' + name + '(\\s|$)')), ' ')).trim()
}

window.cancelEvent = function cancelEvent(evt) {
    if (!evt)
        return logger.local('cancelEvent').warn('cancelEvent: event is', evt)

    if (evt.preventDefault) evt.preventDefault();
    if (evt.stopPropagation) evt.stopPropagation();

    evt.cancelBubble = true;
    evt.returnValue = false;

    return false;
}

window.setStyle = function setStyle(el, name, value) {
    if (typeof el === 'string')
        el = ge(el);

    if (Array.isArray(el)) {
        return el.forEach(function(el, i) {
            setStyle(el, name, value);
        });
    }

    if (isObject(name)) {
        for (var k in name) {
            var v = name[k];
            setStyle(el, k, v);
        }
        return;
    }

    if (typeof value === 'number'
        && name !== 'z-index'
        && ! /acit|ex(?:s|g|n|p|$)|rph|ows|mnc|ntw|ine[ch]|zoo|^ord/i.test(name))
        value += 'px';

    el.style[toCamelCase(name)] = value;
}

/**
 * @param {HTMLElement} el
 * @param {String|Array} css
 * @return
 */
window.getStyle = function getStyle(el, css) {
    if (typeof el === 'string')
        el = ge(el);

    if (Array.isArray(css)) {
        var result = {}
        for (var i = 0; i < css.length; i++) {
            var key = css[i]
            result[key] = getStyle(el, key)
        }
        return result
    }

    if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(el, '')
        return compStyle.getPropertyValue(css)
    } else if (el.currentStyle) {
        return el.currentStyle[camelcase(css)]
    }
}
})(window);
