if (!Array.prototype.forEach) {
    Array.prototype.forEach = function(fun /*, thisp*/) {
        var len = this.length;
        if (typeof fun != "function")
            throw new TypeError();

        var thisp = arguments[1];
        for (var i = 0; i < len; i++) {
            if (i in this)
                fun.call(thisp, this[i], i, this);
        }
    };
}

function rotateNews() {
    rotateList(function() {
        var header = byId('newsHeader');
        if (header) {
            return header.getElementsByTagName('span');
        } else
            return [];
    }, {count:1});
}

function rotateQuotes() {
    rotateList(function() {
        var quotes = byId('quotes');
        if (quotes) {
            return quotes.getElementsByTagName('div');
        } else
            return [];
    }, {count:1});
}

function rotateList(listGetter, counter) {
    var elements = listGetter();
    var eltToHide = counter.count == 0 ? elements[elements.length - 1] : elements[counter.count - 1];
    var eltToShow = counter.count == elements.length ? elements[0] : elements[counter.count];
    hide(eltToHide, function(){show(eltToShow);});
    counter.count++;
    if (counter.count >= elements.length)
        counter.count = 0;
    setTimeout(function(){rotateList(listGetter, counter);},10000);
}

function show(elt, callback) {
    if ('undefined' != typeof jQuery && jQuery != null) {
        var wrapped = $(elt);
        wrapped.fadeIn("slow", callback);
    } else {
        elt.style['display']  = '';
        if (callback)
            callback();
    }
}

function hide(elt, callback) {
    if ('undefined' != typeof jQuery && jQuery != null) {
        var wrapped = $(elt);
        wrapped.fadeOut("slow", callback);
    } else {
        elt.style['display']  = 'none';
        if (callback)
            callback();
    }
}

function byId(id) {
    return document.getElementById(id);
}
