window.addEvent('domready', function(){

	var Actualites = new Class({
		_timeline : undefined,
		_list : undefined,
		_current : 0,
        _old : 0,
        _auto : true,
        _total : 0,
        _nav : undefined,
        _delay : 8000,
        _effect : Fx.Transitions.linear,
        _duration : 1500,
        
        initialize: function(){
        	
        	if (!$('actu-container')){
        		return;
        	}
        	
        	this._list = $('actu-container');
        	var children = this._list.getElements("div.home-actu");
        	this._total = children.length;
 
        	if (this._total < 2){
        		return;
        	}
        	
        	//console.log(children);
        	
        	var num;
        	for (var i = 0; i < this._total; i++){
				num = i + 1;
				opacity = (i === this._current) ? 1 : 0;
				$("push-actu-home-"+num).setStyles({
					opacity : opacity
				});
			}

        	this._timer = this._autoRun.periodical(this._delay, this);
        },
        _change: function(num){
            if (num != this._current){
                this._old = this._current;
                this._current = num;
                this._hide();
            }
        },
        _show: function(){ 
        	
        	var num = this._current + 1;
            var element = $("push-actu-home-" + num);
            if (element.hasClass('hide')){
            	element.removeClass('hide');            	
            }
            element.setStyles({display : 'block', visibility : 'visible'});
            
            var myFx = new Fx.Tween(element);
            myFx.start('opacity', '0', '1');
        },
        _hide: function(){
        	var num = this._old + 1;
            var element = $("push-actu-home-" + num);
            
            var myFx = new Fx.Tween(element);
            myFx.addEvent("complete", this._onHide.bind(this));
            myFx.start('opacity', '1', '0');
        },
        _onHide : function(){
        	var num = this._old + 1;
            var element = $("push-actu-home-" + num);
        	element.setStyles({display : 'none', visibility : 'hidden'});
            this._show();
        },
		_autoRun : function(){
			if (this._auto){
				var num = this._current + 1;
				if (num >= this._total){
					num = 0;
				}
				this._change(num);
			}
		}
    });
    var actualites = new Actualites;
});
