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 : 5000,
		zoom : null,
		contentDisplay : null,
		selectors : null
	},

	initialize: function(d, options){
		this.setOptions(options);
		this.main = $(d);
		this.curItem = 0;
		this.canPlay = true;
		this.as = this.main.getElements('.slideimg');
		this.as.setStyle('display','none');
		this.as[0].setStyle('display','block');
		this.i = 10;
		
		this.select_off = this.main.getElements('.slideselectoff').get('src');
		this.select_on = this.main.getElements('.slideselecton').get('src');
		
		
		
		this.createSelectors();
		/*
		for(var i=0,j=this.as.length;i<j;i++){
			new Element('img', {'src':
		}
		*/
		
		if ( this.options.autoPlay > 0 ){
			this.autoPlayDelay = this.moveItem.bind(this).delay( this.options.autoPlay, this, 1 );
		}
		
		return;
		
		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';
		}
		
	},
	
	createSelectors: function(){
		var p = this.main.getElement('.slideselect');
		p.empty();
		for(var i=0,j=this.as.length;i<j;i++){
			var src = (i==this.curItem) ? this.select_on : this.select_off;
			//continue;
			var img = new Element('img', {'src':src}).inject(p);
			img.src = src;
			if(i!=this.curItem){
				img.addEvent('click', this.goToImg.bind(this, i));
				img.addClass('clickable');
			}
		}
		
	},
	
	goToImg: function(nb){
		//alert(nb);
		if ( this.canPlay == false ) return false;
		$clear(this.autoPlayDelay);
		this.curItem = nb;
		this.showItem(1);
	},
	
	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;
		this.curItem++;
		
		if ( this.curItem == this.as.length){
			this.curItem = 0;
		}
		this.showItem(1);
	},
	showItem: function(way){
		this.i++;
		var a = this.as[this.curItem];
		a.style.zIndex = this.i;
		a.setStyle('opacity', '0');
		a.setStyle('display', 'block');
		
		this.canPlay = false;
		new Fx.Morph(a, {
			wait: false,
			duration: this.options.deroulSpeed,
			transition: this.options.deroulFx
		}).start({
			opacity: 1
		});
		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 );
		}
		this.createSelectors();
		return true;
	},
	
	resetAnimation: function(){
		this.canPlay = true;
		return true;
	}

});
