﻿// JScript File

//2D array of divs in scrollers
var scrollernodes = new Object();
//Array of curent pages on scroller
var curentscroll = new Object();

var Sroller = new Class({
    // inicialization of slider
    initialize: function(id,moveTime,stopTime,height){
        this.id = id;
        this.moveTime = moveTime;
        this.stopTime = stopTime;
        this.height   = height;        
        scrollernodes[this.id] = [];
        curentscroll[this.id] = 1;
        var scroll = $(this.id);
        var alldivs = $$("div");
        for (var i=0; i<alldivs.length; i++){
            if ((alldivs[i].className == "scrollerContent")&&(alldivs[i]).parentNode.id == this.id)
            scrollernodes[this.id].push(alldivs[i])
        }
        if (scrollernodes[this.id].length != 0) {
            //inicialize divs for active and next
            this.active = scrollernodes[this.id][0];
            if (scrollernodes[this.id].length > 1 ) {
                this.next   = scrollernodes[this.id][1];
            }
            else {
                // if only one then next = active.clone
                this.next   = this.active.clone().injectInside(this.id);
                scrollernodes[this.id][1] = this.next;
            }
            
            //show first
            this.show(curentscroll[this.id],this.height);
        }
    },
    //show start page and move to top and hide others
    show:function(page,height){  
        scrollernodes[this.id].each(function(item, index){
            item.setOpacity(0);
            if (index == 0){
                var top = 0;
            }
            else{
                var top = (-(index-1) * height);
            }
            item.setStyle('top', top);
        });
        // show active and next div
        this.active.setOpacity(1);
        this.next.setOpacity(1);
    },
    //move div
    move:function(){
        var tmp = this;
        var fxActive = new Fx.Styles(this.active, {duration:this.moveTime, wait:false});
        var fxNext   = new Fx.Styles(this.next,   {duration:this.moveTime, wait:false, onComplete:function(){tmp.swap();}});
        
        //active top
        if (curentscroll[this.id] == 0){
            var top1 = (-scrollernodes[this.id].length*this.height);
        }else {
            var top1 = (-curentscroll[this.id]*this.height);
        }
        //next top
        var top2 = (-curentscroll[this.id]*this.height);
        
        fxActive.start({
            'top': top1
        });
		fxNext.start({
            'top': top2
        });
    },
    //swap divs
    swap:function(){
        //hide active div
        this.active.setOpacity(0);
        //move div to bottom
        if (curentscroll[this.id] == 0){
            this.active.setStyle('top',(-(scrollernodes[this.id].length-2)*this.height));
        }else {
            this.active.setStyle('top',(-(curentscroll[this.id]-2)*this.height));
        }
        
        curentscroll[this.id]++;
        if (curentscroll[this.id] >= scrollernodes[this.id].length) {
            curentscroll[this.id] = 0;
        }
        //swap divs
        this.active = this.next;
        this.next = scrollernodes[this.id][curentscroll[this.id]];
        //show next div
        this.next.setOpacity(1);        
    }
});

function startScroller(scrollingText,time, move){
    if(move)
    {
        scrollingText.move();
    }
    window[scrollingText.id+"timer"]=setTimeout(function(){startScroller(scrollingText,time, true)},time);
}