//* Convenience DOM-Functions

function $(n) {
    if(typeof(n) == 'object')
        return n;

    if(typeof(n) != 'string')
        return null;

    return document.getElementById(n);
}

function $Element(n, classname) {
    var e = document.createElement(n);

    if(classname)
        e.className = classname;

    return e;
}

function $Text(n, style) {
    if(style != undefined) {
        var span = $Span();

        for(s in style)
            span.style[s] = style[s];

        span.appendChild(document.createTextNode(n));

        return span;
    }
    else
        return document.createTextNode(n);
}

function $Div(classname, width) {
    var e = $Element('div', classname);

    if(width != undefined)
        e.style.width = width + 'px';

    return e;
}

function $Span(classname, width) {
    var e = $Element('span', classname);

    if(width != undefined)
        e.style.width = width + 'px';

    return e;
}

function $Img(src, classname, width, height) {
    var e = $Element('img', classname);

    e.src = src;

    if(width)
        e.style.width = width + 'px';

    if(height)
        e.style.height = height + 'px';

    return e;
}

function $Table(classname) {
    return $Element('table', classname);
}

function $Tr(classname) {
    return $Element('tr', classname);
}

function $Td(classname, colspan, width) {
    var e = $Element('td', classname);

    if(colspan)
        e.colSpan = colspan;

    if(width)
        e.style.width = width + 'px';

    return e;
}

function $Link(url, content, target, classname) {
    var a = $Element('a', classname);
    a.href = url;

    if(target)
        a.target = target;

    if(typeof(content) == 'object')
        a.appendChild(content);
    else
        a.appendChild($Text(content));

    return a;
}

function setClass(e, classname, exclusive) {
    e = $(e);

    if(!e)
        return;

    exclusive = (exclusive == undefined) ? false : exclusive;

    if(exclusive == true) {
        e.className = classname;

        return;
    }

    var cn = e.className.split(' ');

    if(cn.indexOf(classname) != -1)
        return;

    cn.push(classname);

    e.className = cn.join(' ');
}

function unsetClass(e, classname) {
    e = $(e);

    if(!e)
        return;

    var cn = e.className.split(' ');

    var i = cn.indexOf(classname);

    if(i == -1)
        return;

    cn.splice(i, 1);

    e.className = cn.join(' ');
}

function display(node, type) {
    var n = $(node);

    if(!n)
        return;

    n.style.display = type;
}

function toggle_display(node, type_on, type_off) {
    var n = $(node);

    if(!n)
        return;

    if(n.style.display == type_on)
        n.style.display = type_off;
    
    else if(n.style.display == type_off)
        n.style.display = type_on;
}

function clear(node) {
    var n = $(node);

    if(!n)
        return;

    while(n.firstChild)
        n.removeChild(n.firstChild)
}

function setText(node, s, style) {
    clear(node);

    if(typeof(s) != 'string')
        return;

    $(node).appendChild($Text(s, style));
}

function resetForm(node) {
    var n = $(node);

    if(!n)
        return;

    n.reset();
}

function getText(node) {
    var n = $(node);

    if(!n)
        return '';

    if(node.innerText)
        return node.innerText;

    if(node.textContent)
        return node.textContent;

    return '';
}

function nextNode(node, name) {
    if(node == null)
        return null;

    var x = node.nextSibling;

    while(x != null) {
        if(x.nodeName.toLowerCase() == name)
            return x;

        x = x.nextSibling;
    }

    return null;
}

function getMaxHeight(node) {
    var n = $(node);
    if(!n)
        return 0;
    
    var posy = n.offsetTop;
    
    while((n = n.offsetParent) != null)
        posy += n.offsetTop;
    
    var innerHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
    
    return (innerHeight - posy);
}

function maximizeElement(name, offset) {
    var height = getMaxHeight(name) + offset;

    $(name).style.height = height + 'px';
}

function swap_visibility(hide, show) {
    display(hide, 'none');
    display(show, '');
}

function getAbsolutePosition(node) {
    var n = $(node);
    var pos = {x: 0, y: 0};

    pos.x = n.offsetLeft;
    pos.y = n.offsetTop;

    while((n = n.offsetParent) != null) {
        pos.x += n.offsetLeft;
        pos.y += n.offsetTop;
    }

    return pos;
}

function getDimension(node) {
    var dim = { topleft: {x: 0, y: 0}, bottomright: {x: 0, y: 0}};

    dim.topleft = getAbsolutePosition(node);

    dim.bottomright.x = dim.topleft.x + node.offsetWidth;
    dim.bottomright.y = dim.topleft.y + node.offsetHeight;

    return dim;
}

function getRelativePosition(child, parent) {
    var cpos = this.getAbsolutePosition(child);
    var ppos = this.getAbsolutePosition(parent);

    var pos = {x: 0, y: 0};

    pos.x = cpos.x - ppos.x;
    pos.y = cpos.y - ppos.y;

    return pos;
}

function getPageDimension() {
    var dim = {x: 0, y: 0};

    if (document.body.scrollHeight > document.body.offsetHeight) {
        dim.x = document.body.scrollWidth;
        dim.y = document.body.scrollHeight;
    }
    else {
        dim.x = document.body.offsetWidth;
        dim.y = document.body.offsetHeight;
    }

    return dim;
}

function getInnerDimension() {
    var dim = {x: 0, y: 0};

    if (self.innerHeight) {
        dim.x = self.innerWidth;
        dim.y = self.innerHeight;
    }
    else if(document.documentElement && document.documentElement.clientHeight) {
        dim.x = document.documentElement.clientWidth;
        dim.y = document.documentElement.clientHeight;
    }
    else if(document.body) {
        dim.x = document.body.clientWidth;
        dim.y = document.body.clientHeight;
    }

    return dim;
}
