var SkDeroul = new Class({
	
	Implements: [Events, Options],

	options: {
		nbItems : 5,
		itemVisible : 3,
		itemSize : 170,
		deroulWay : 'horizontal',
		deroulSpeed : 600,
		deroulFx : Fx.Transitions.Quad.easeInOut,
		posInfo : null,
		nameInfo : null,
		nameDiv : null,
		infinite : true,
		autoPlay : 0,
		zoom : null,
		contentDisplay : null
	},

	initialize: function(dmain, dprev, dnext, options){
		this.setOptions(options);
		if ( this.options.nbItems == 1 ) this.options.autoPlay = 0;
		this.main = $(dmain);
		this.curItem = 0;
		this.canPlay = true;
		this.margin = (this.options.deroulWay == 'horizontal') ? 'marginLeft' : 'marginTop';
		this.dprev = dprev;
		this.dnext = dnext;
		dprev.addEvent('click', function(){$clear(this.autoPlayDelay);this.options.autoPlay=0;this.moveItem(-1);}.bind(this));
		dnext.addEvent('click', function(){$clear(this.autoPlayDelay);this.options.autoPlay=0;this.moveItem(+1);}.bind(this));
		dprev.onmouseover = function(){
			this.style.backgroundPosition = '0 100%';
		}
		dprev.onmouseout = function(){
			this.style.backgroundPosition = '0 0';
		}
		dnext.onmouseover = function(){
			this.style.backgroundPosition = '0 100%';
		}
		dnext.onmouseout = function(){
			this.style.backgroundPosition = '0 0';
		}
		
		if ( this.options.infinite ){
			var tds = this.main.getElements('td');
			tds[0].clone().inject(tds[0].parentNode);
		} else {
			this.dprev.style.display = 'none';
		}
		
		if ( $(this.options.posInfo) ){
			$(this.options.posInfo).innerHTML = (this.curItem+1) + '/' + this.options.nbItems;
		}
		
		if ( this.options.nameDiv ){
			var tds = this.main.getElements('td');
			this.options.nameDiv.innerHTML = tds[0].getProperty('name') + ' 2010';
		}
		
		if ( this.options.autoPlay > 0 ){
			this.autoPlayDelay = this.moveItem.bind(this).delay( this.options.autoPlay, this, 1 );
		}
		
		if ( this.options.zoom ){
			this.options.zoom.addEvent('click', function(){this.showZoom();}.bind(this));
		}
		
		if ( this.options.contentDisplay ){
			for(var i=0,j=this.options.nbItems;i<j;i++){
				$$('div.'+this.options.contentDisplay+i)[0].style.display = 'none';
			}
			var id = this.curItem;
			if ( id == this.options.nbItems ) id = 0;
			$$('div.'+this.options.contentDisplay+id)[0].style.display = 'block';
		}
		
	},
	
	showZoom: function(){
		var id = this.curItem;
		if ( id == this.options.nbItems ) id = 0;
		var a = this.main.getElements('td')[id].getElement('a');
		a.fireEvent('click', a);
	},
	
	moveItem: function(way){
		//alert(this.canPlay);
		if ( this.canPlay == false ) return false;
		
		/*
		var start_anim = false;
		if ( way < 0 ) start_anim = ( this.curItem > 0 ) ? true : false;
		else start_anim = ( this.curItem < this.options.nbItems - this.options.itemVisible ) ? true : false;
		if ( start_anim == false ) return false;
		
		var start_pos = this.curItem * -this.options.itemSize;
		this.curItem = this.curItem + way;
		var final_pos = this.curItem * -this.options.itemSize;
		*/
		
		var start_pos = 0;
		var final_pos = 0;
		if ( way < 0 && this.curItem == 0){
			this.main.style.marginLeft = -(this.options.nbItems * this.options.itemSize)+'px';
			this.curItem = this.options.nbItems-1;
			start_pos = -this.options.nbItems * this.options.itemSize;
			final_pos = this.curItem * -this.options.itemSize;
		}
		else if ( way > 0 && this.curItem == this.options.nbItems){
			//alert('fin');
			this.main.style.marginLeft = '0px';
			this.curItem = 1;
			start_pos = 0;
			final_pos = this.curItem * -this.options.itemSize;
		}
		else {
			start_pos = this.curItem * -this.options.itemSize;
			this.curItem = this.curItem + way;
			final_pos = this.curItem * -this.options.itemSize;
		}
		//var prevdisp = (this.curItem == 0) ? 'none' : 'block';
		//this.dprev.style.display = prevdisp;
		//alert(this.curItem);
		//var nextdisp = (this.curItem == (this.options.nbItems - this.options.itemVisible)) ? 'none' : 'block';
		//this.dnext.style.display = nextdisp;
		
		if ( $(this.options.posInfo) ){
			$(this.options.posInfo).innerHTML = (this.curItem+1) + '/' + this.options.nbItems;
		}
		
		if ( this.options.nameDiv ){
			var tds = this.main.getElements('td');
			this.options.nameDiv.innerHTML = tds[this.curItem].getProperty('name')+' 2010';
		}
		
		if ( this.options.contentDisplay ){
			for(var i=0,j=this.options.nbItems;i<j;i++){
				$$('div.'+this.options.contentDisplay+i)[0].style.display = 'none';
			}
			var id = this.curItem;
			if ( id == this.options.nbItems ) id = 0;
			$$('div.'+this.options.contentDisplay+id)[0].style.display = 'block';
		}
		
		this.canPlay = false;
		new Fx.Morph(this.main, {
			wait: false,
			duration: this.options.deroulSpeed,
			transition: this.options.deroulFx
		}).start({
			marginLeft: [start_pos,final_pos]
		});
		this.resetAnimation.bind(this).delay(this.options.deroulSpeed+50);
		
		if ( this.options.autoPlay > 0 ){
			this.autoPlayDelay = this.moveItem.bind(this).delay( this.options.autoPlay, this, 1 );
		}
		return true;
	},
	
	resetAnimation: function(){
		this.canPlay = true;
		return true;
	}

});
