var isset = function(x){
    return typeof(x) != 'undefined';
}

var isBorderRadiusSupported = function () {
    var d = document.documentElement.style;
    return isset(d.borderRadius) ||
           isset(d.WebkitBorderRadius) ||
           isset(d.KhtmlBorderRadius) ||
           isset(d.MozBorderRadius);
}

document.documentElement.className += isBorderRadiusSupported() ? 'border-radius' : ' no-border-radius';
document.createElement('ABBR');

var setOpacity = function(e, value) {
    if (!e.setOpacity)
        if (isset(e.style.filter)) {
            e.setOpacity = function(value) { e.style.filter = "Alpha(Opacity=" + (value * 100) + ")"; };
        } else
        if (isset(e.style.KhtmlOpacity))
            e.setOpacity = function(value) { e.style.KhtmlOpacity = value };
        else
        if (isset(e.style.MozOpacity))
            e.setOpacity = function(value) { e.style.MozOpacity = value };
        else
        if (isset(e.style.opacity))
            e.setOpacity = function(value) { e.style.opacity = value };
        else
            e.setOpacity = function(value) {};

    e.setOpacity(value);
}

function rotate(container, heap, partLength, fadeDuration, changeDuration) {
    var items = [], i = heap.length, item;
    while (i--) {
        item = document.createElement('LI');
        item.innerHTML = '<a href="' + heap[i][1] + '" title="' + heap[i][2] + '" target="_blank">' +
                           '<img src="' + heap[i][0] + '" alt="' + heap[i][2] + '" />' +
                         '</a>';
        items.push(item);
    }

    var itemsLength = items.length,

        fade = function(e, d, callback) {
            var start = (new Date).getTime(),
                finish = start + fadeDuration,
                value = (d > 0) ? 0 : 1;

            fading = window.setInterval(function() {
                var now = (new Date).getTime(),
                    k = now > finish ? 1 : (now - start) / fadeDuration;
                    setOpacity(e, value + d * k);
                if (now > finish) {
                    window.clearInterval(fading);
                    if (callback)
                        callback.call(e);
                }
            }, 10);
        },

        list = null,
        change = function() {

            if (list != null)
                return fade(list, -1, function() {
                    container.removeChild(container.getElementsByTagName('UL')[0]);
                    list = null;
                    change();
                });

            list = container.appendChild(document.createElement('UL'));
            var itemsCopy = items.slice(0), i = Math.min(partLength, itemsCopy.length);
            setOpacity(list, 0);
            while (i--)
                list.appendChild(itemsCopy.splice(Math.round(Math.random() * (itemsCopy.length - 1)), 1)[0].cloneNode(1));


            fade(list, 1, function() {
                window.setTimeout(change, changeDuration);
            });
        };

    change();
}
