AnimatedRotators = function(){
    return {
        init : function(){
            $(".AnimatedRotator").each(function(i){
                var rotator = new AnimatedRotator(this);
                rotator.start();
            });
        }
    }
}();

AnimatedRotator = function(el) {
    var self = this;
    var el = el;
    var z = 101;

    this.rotatingBlocks = [];
    this.rotatingBlocks.current = null;
    this.timer;

    this.interval = 4000;
    var seconds = 0 + $(el).attr("interval");
    if (seconds != 0) this.interval = seconds * 1000;


    var blocks = $(el).find(".AnimatedRotatorBlock").get();
    if (blocks && blocks.length) {
        this.start = function() {
            if (this.rotatingBlocks && this.rotatingBlocks.length > 1) {
                this.rotatingBlocks.current = 0;
                this.timer = window.setInterval(function() { self.rotate(); }, this.interval);
            }
        }
        this.rotate = function(next) {
            var current = -1;
            if (this.rotatingBlocks.current != null) {
                current = this.rotatingBlocks.current;
            }
            if (next === undefined) next = current + 1;
            if (next >= this.rotatingBlocks.length) next = 0;

            var c = this.rotatingBlocks[current];
            c.style.zIndex = z;
            z++;

            var n = this.rotatingBlocks[next];
            n.currentOpacity = 0;
            n.style.zIndex = z;

            n.timer = window.setInterval(function() { self.fade(c, n, next); }, 100);
        }

        this.fade = function(c, n, next) {
            var interval = 10;

            if (n.currentOpacity < 100) {
                var newOpacity = n.currentOpacity + interval;
                n.style.opacity = newOpacity / 100;
                n.style.filter = "alpha(opacity=" + newOpacity + ")";
                n.currentOpacity = newOpacity;
            }
            else {
                window.clearInterval(n.timer);
                n.timer = null;
                c.style.opacity = 0;
                c.style.filter = "alpha(opacity=" + 0 + ")";
                c.currentOpacity = 0;
                this.rotatingBlocks.current = next;
                return;
            }
        }

        this.stop = function() {
            window.clearInterval(this.timer);
        }


        for (var i = 0; i < blocks.length; i++) {
            var block = blocks[i];
            this.rotatingBlocks[this.rotatingBlocks.length] = block;
            if (i > 0) {
                block.style.opacity = 0;
                block.style.filter = "alpha(opacity=" + 0 + ")";
                block.currentOpacity = 0;
            }
            else {
                //opacity:1.0;filter:alpha(opacity=100);z-index:100;position:relative;
                block.style.position = "relative";
                block.style.opacity = 1;
                block.style.filter = "alpha(opacity=" + 100 + ")";
                block.currentOpacity = 100;
            }
        }
    }
}

$(document).ready(function(){
    AnimatedRotators.init();
});
