var fmcActualites = new Class({
	list:[],
	options: {
		container:'actus_slider',
		wrapper:'actus_wrapper',
		startIndex: 0,
		liste:'actus-item',
		actusNumerosId: 'actus_numeros',
		nbListe:6 // nb d'items affichés.
		//slides: [],
		//wrap: true
	},
	initialize: function(options){
		this.setOptions(options);
		
		this.list = $$('#'+this.options.container+' .'+this.options.liste + ', .news-list-item');
		if(this.list.length>1)
		{
			//alert($(this.options.wrapper) + ' - '+this.list[0].getCoordinates().width*this.list.length +'px')
			$(this.options.wrapper).setStyle('width', this.list[0].getCoordinates().width*this.list.length);
		}
		this.now = this.options.startIndex;
		this.scroller = new fmcScrollTo({
			slides: this.list,
			container: this.options.container,
			duration:800
		});
		this.createButtons();
		this.scrollIt(this.now);
	},
	scrollIt: function(iToScrollTo){
		this.scroller.scrollToEl(iToScrollTo);
		this.changeActif(iToScrollTo);
		this.checkArrows(iToScrollTo);
	},
	changeActif: function(iToScrollTo)
	{
		this.numeros.each(function(el, id)
		{
			el.removeClass('act');
		}, this);
		this.numeros[iToScrollTo].addClass('act');
	},
	checkArrows: function(iToScrollTo)
	{
		switch(parseInt(this.now))
		{
			case 0 :
				this.prec.setStyle('display','none');
				this.suiv.setStyle('display','block');
				this.numerosContainer.fx.start({'padding-left':5});
				break;
			case this.list.length-1 :
				this.suiv.setStyle('display','none');
				this.prec.setStyle('display','block');
				this.numerosContainer.fx.start({'padding-left':20});
				break;
			default :
				this.suiv.setStyle('display','block');
				this.prec.setStyle('display','block');
				this.numerosContainer.fx.start({'padding-left':20});
				break;
		}
		if(this.list.length <2 || !this.list)
		{
			this.suiv.setStyle('display','none');
			this.prec.setStyle('display','none');
		}
	},
	getCategorie: function(el){
		classes = el.getProperty("class").split(' ');
		return classes[1];
	},
	createButtons: function()
	{
		/*
		Liste des numéros */
		var numerosContainer = new Element('div', {'id':this.options.actusNumerosId}).injectAfter($(this.options.container));
		this.numerosContainer = numerosContainer;
		this.numerosContainer.fx = new Fx.Styles(this.numerosContainer, {duration:500, wait:false});
		var numeros = new Array();
		this.list.each(function(el,id)
		{
			var bt = new Element('a', {'title':el.getFirst().getText(), 'class':'numero'}).setHTML(id+1).injectInside(numerosContainer);
			bt.id = id;
			bt.categorie = this.getCategorie(el);
			if(bt.categorie != 'undefined')
			{
				bt.addClass(bt.categorie);
			}
			numeros[id] = bt;
			bt.addEvent('click', function(e)
			{
				//alert(this.innerHTML+' - ' +this.id);
				if(e){
					var e = new Event(e);
					e.stop();
					$clear(obj.per);
					obj.per = obj.execNext.periodical(obj.options.time, obj);
				}
				if(obj.now!= this.id)
				{
					obj.now = this.id;
					obj.scrollIt(obj.now);
				}
				this.blur();
				e.stop();
			});
		}, this);
		this.numeros = numeros;
		/*
		Boutons précédent - suivant*/
		
		this.suiv = new Element('a', {'id':'suiv', 'href':'#'}).injectInside(this.numerosContainer);
		this.prec = new Element('a', {'id':'prec', 'href':'#'}).injectInside(this.numerosContainer);
		var obj = this;
		this.suiv.addEvent('click', function(e)
		{
			if(e){
				var e = new Event(e);
				e.stop();
				$clear(obj.per);
				obj.per = obj.execNext.periodical(obj.options.time, obj);
			}
			if(obj.now < obj.list.length-1)
			{
				obj.now++;
				obj.scrollIt(obj.now);
			}else{
				obj.now=0;
				obj.scrollIt(obj.now);
			}
			this.blur();
		});
		this.prec.addEvent('click', function(e)
		{
			if(e){
				var e = new Event(e);
				e.stop();
				$clear(obj.per);
				obj.per = obj.execNext.periodical(obj.options.time, obj);
			}
			if(obj.now > 0)
			{
				obj.now--;
				obj.scrollIt(obj.now);
			}
			this.blur();
			e.stop();
		});
		if(this.options.autoPlay && this.options.time && this.suiv)
		{
			this.per = this.execNext.periodical(this.options.time, this);
		}
	},
	execNext: function()
	{
		this.suiv.fireEvent('click');
	}
	
});
fmcActualites.implement(new Options, new Events);

window.addEvent('domready', function(){
	/*	slide show demo	*/
	/*new fmcSlideshow({
		slides: $$('#realisations .real .real-images ul li')
	});*/
	if($('actus_slider') && $$('.news-list-container').length < 1){
		var makeIt = new fmcActualites(
		{
			container:'actus_slider',
			startIndex: 0,
			liste:'actus-item',
			actusNumerosId: 'actus_numeros',
			nbListe:1,
			autoPlay:true,
			time:6000
		});
	}
	/*
	var myTips = new Tips($$('#actus_numeros a.numero'), {
		maxTitleChars: 50   //I like my captions a little long
	});
	*/
});

