function $(id) {
    return document.getElementById(id);
}

function getXOffset(e) {
    var x = 0;
    do {
        if (e.offsetLeft > 0)
            x += e.offsetLeft;
    } while (e = e.offsetParent);

    return x;
}

function setOpacity(e, o) {
	if(!e) return;
    if (typeof (e.style.KhtmlOpacity) != 'undefined') {
        e.style.KhtmlOpacity = o;
    } else
    if (typeof (e.style.MozOpacity) != 'undefined') {
        e.style.MozOpacity = o;
    } else
    if (typeof (e.style.opacity) != 'undefined') {
        e.style.opacity = o;
    } else
    if (typeof (e.filters) != 'undefined') {
        e.style.filter = 'Alpha(Opacity=' + Math.round(o * 100) + ');';
    }
}

function clearSelection() {

    if (window.getSelection) {
        try {
             window.getSelection().collapse();
        } catch(e) {
             window.getSelection().removeAllRanges();
        }
    } else
    if (document.selection) {
        try {
             document.selection.clear();
        } catch (e) {}
    }
}

function fade(e, from, to, time, steps, onend) {

    if (e.fadin)
        window.clearInterval(e.fadin);

    var d = to - from;
    var h = d / steps;
    var t = Math.ceil(time / steps);

    e.o = from;
    e.fadin = window.setInterval(
        function() {
            if (Math.abs((Math.abs(to) - Math.abs(e.o))) <= Math.abs(h)) {
                setOpacity(e, to);
                window.clearInterval(e.fadin);
                e.fadin = null;

                if (onend)
                    onend();
            } else
                setOpacity(e, (e.o += h));
        }, t
    );
}

function query(URI, callback) {

    var head = document.getElementsByTagName('HEAD')[0];
    if (!head)
        return;

    var scr = document.createElement('SCRIPT');
        scr.type = 'text/javascript';

    head.appendChild(scr);
    scr.src  = URI;

    scr.onload = callback;
    scr.onreadystatechange = function() {
        if ((this.readyState == 'loaded') || (this.readyState == 'complete')) {
            this.onreadystatechange = null;
            callback();
        }
    }
}