﻿var intervalIDH;
var intervalIDV;
var scrollPosH;
var scrollPosV;

scrollPosH=0;
scrollPosV=0;

var TickerH = new Class({
    //implements
    Implements: [Options],

    //options
    options:{
			fps: 35,
			direction: 'vertical',
			onComplete: Class.empty,
			onStart: Class.empty
    },
    
	initialize: function(el,options){
		this.setOptions(options);
		this.el = $(el);
		this.items = this.el.getElements('li');
		var w = 0;
		var h = 0;
        if(this.options.direction.toLowerCase()=='horizontal') {
            h = this.el.getSize().y;
            this.items.each(function(li,index) {
                w += li.getSize().x;
            });
        } else {
            w = this.el.getSize().x;
            this.items.each(function(li,index) {
                h += li.getSize().y;
            });
        }
        
        this.el.setStyles({
            position: 'absolute',
            top: 0,
            left: 0,
            width: w,
            height: h
        });
        
        this.fx = new Fx.Morph(this.el,{
            duration:0,
            onComplete:function() {
                if(this.options.direction.toLowerCase()=='horizontal') {
                    if(scrollPosH<-(this.el.getSize().x/this.items.length)){
                        this.items[this.current].inject(this.el);
                        this.el.setStyles({
                            left:0,
                            top:0
                        });
		                this.current++;
		                scrollPosH=-1;
                    }
                } else {
                    if(scrollPosH<-(this.el.getSize().y/this.items.length)){
                        this.items[this.current].inject(this.el);
                        this.el.setStyles({
                            left:0,
                            top:0
                        });
		                this.current++;
		                scrollPosH=-1;
                    }
                }
            }.bind(this)
        });
        this.current = 0;
    },
    
    scroll: function(num) {
		if (this.current >= this.items.length) this.current = 0;
        if(this.options.direction.toLowerCase()=='horizontal') {
		    this.fx.start({
                left: scrollPosH
		    });
        } else {
		    this.fx.start({
                top: scrollPosH
		    });
        }		
		//document.getElementById("prova").innerText=scrollPos;
		scrollPosH--;
    },
    
    start: function() {
        //alert(this.options.fps);
        intervalIDH = this.scroll.bind(this).periodical(this.options.fps);
    },
    
	pause: function() {
	    $clear(intervalIDH);
	    intervalIDH = null;
	},
		
	resume: function() {
	    if (intervalIDH == null) {
	        this.start();
	    }
	},
	
	fade: function(elm, opa) {
        this.items.each(function(li,index) {
           if(li!=elm){
	            li.morph({
		            opacity: opa
	            });
           }
        });
	}
});


var TickerV = new Class({
    //implements
    Implements: [Options],

    //options
    options:{
			fps: 35,
			direction: 'vertical',
			onComplete: Class.empty,
			onStart: Class.empty
    },
    
	initialize: function(el,options){
		this.setOptions(options);
		this.el = $(el);
		this.items = this.el.getElements('li');
		var w = 0;
		var h = 0;
        if(this.options.direction.toLowerCase()=='horizontal') {
            h = this.el.getSize().y;
            this.items.each(function(li,index) {
                w += li.getSize().x;
            });
        } else {
            w = this.el.getSize().x;
            this.items.each(function(li,index) {
                h += li.getSize().y;
            });
        }
        
        this.el.setStyles({
            position: 'absolute',
            top: 0,
            left: 0,
            width: w,
            height: h
        });
        
        this.fx = new Fx.Morph(this.el,{
            duration:0,
            onComplete:function() {
                if(this.options.direction.toLowerCase()=='horizontal') {
                    if(scrollPosV<-(this.el.getSize().x/this.items.length)){
                        this.items[this.current].inject(this.el);
                        this.el.setStyles({
                            left:0,
                            top:0
                        });
		                this.current++;
		                scrollPosV=-1;
                    }
                } else {
                    if(scrollPosV<-(this.el.getSize().y/this.items.length)){
                        this.items[this.current].inject(this.el);
                        this.el.setStyles({
                            left:0,
                            top:0
                        });
		                this.current++;
		                scrollPosV=-1;
                    }
                }
            }.bind(this)
        });
        this.current = 0;
    },
    
    scroll: function(num) {
		if (this.current >= this.items.length) this.current = 0;
        if(this.options.direction.toLowerCase()=='horizontal') {
		    this.fx.start({
                left: scrollPosV
		    });
        } else {
		    this.fx.start({
                top: scrollPosV
		    });
        }		
		//document.getElementById("prova").innerText=scrollPos;
		scrollPosV--;
    },
    
    start: function() {
        //alert(this.options.fps);
        intervalIDV = this.scroll.bind(this).periodical(this.options.fps);
    },
    
	pause: function() {
	    $clear(intervalIDV);
	    intervalIDV = null;
	},
		
	resume: function() {
	    if (intervalIDV == null) {
	        this.start();
	    }
	},
	
	fade: function(elm, opa) {
        this.items.each(function(li,index) {
           if(li!=elm){
	            li.morph({
		            opacity: opa
	            });
           }
        });
	}
});

var intervalIDH = null;
var intervalIDV = null;
var mytimer = null;

window.addEvent('domready', function() {
    var tHorizontal = new TickerH('TickerHorizontal', { fps : 30, direction : 'horizontal'});
	
    /* setto le azioni del mouse*/
    tHorizontal.items.each(function(li,index) {
        li.addEvents({
            mouseenter: function(){
                tHorizontal.pause();
                tHorizontal.fade(li,0.5);
            },
            mouseleave: function(){
                tHorizontal.resume();
                tHorizontal.fade(li,1);
           }
        });
    });
    
    var tVertical = new TickerV('TickerVertical', { fps : 30, direction : 'vertical'});
	
    /* setto le azioni del mouse*/
    tVertical.items.each(function(li,index) {
        li.addEvents({
            mouseenter: function(){
                tVertical.pause();
                tVertical.fade(li,0.5);
            },
            mouseleave: function(){
                tVertical.resume();
                tVertical.fade(li,1);
           }
        });
    });

    /*avvio lo scroll*/
    tVertical.start();
    tHorizontal.start();
    $('scrollHorizontal').setStyle('visibility', 'visible');    
    $('HiderHorizontal').setStyle('visibility', 'hidden');
    $('scrollVertical').setStyle('visibility', 'visible');    
    $('HiderVertical').setStyle('visibility', 'hidden');
});
