// AnySlide 0.1a
// © Alexandre koch - 2011
// tous droits réservés

	// classe
	var anySlide = new Class({
	
		//implements
		Implements: [Options],
		
		//options
		options: {
			childs: '.travelers', 
			trans: Fx.Transitions.Quint.easeOut, 
			duree: 1000, 
			voyagerSize: 200, 
			nbVoyCabine: 3, 
			leftCtrlText: '', 
			rightCtrlText: '', 
			autoPlay: false, 
			autoWait: 2
		},
		
		//initialization
		initialize: function(station, options) {
			//définis les options
			this.setOptions(options);
			
			// affectations
			this.station = $(station);
			this.container = this.station.getParent('div');
			this.childs = this.station.getChildren(this.options.childs); 
			this.voyagerSize = this.options.voyagerSize;
			this.nbVoyCabine = this.options.nbVoyCabine;
			this.lTxt = this.options.leftCtrlText;
			this.rTxt = this.options.rightCtrlText;
			this.autoPlay = this.options.autoPlay;
			this.autoWait = this.options.autoWait;
			
			this.construct();
			
			$(station).dispose();
		},
		
		
		// CONSTRUCTION DU SLIDE
		construct: function() {
			// voirie
			this.cb = new Element('div', { styles:{ 'clear': 'both' } });
			this.container.setStyles({ 'position': 'relative', 'width': (this.nbVoyCabine * this.voyagerSize) });
			var elements = 0;
			this.childs.each(function(child) { elements++; });	//alert(elements);
			//if(elements < this.nbVoyCabine) { return; } // si y'a pas suffisament de voyageurs le train part pas
			
			// gare (station)
			this.station = new Element('div', {
				'class': 'anyStation', 
				'styles': {
					'width': this.container.getSize().x, 
					'height': this.container.getSize().y, 
					'position': 'relative', 
					'overflow': 'hidden',
					'margin': '0px auto' 
				}
			}).inject(this.container, 'top');
			
			// train
			this.train = new Element('div', {
				'class': 'anyTrain', 
				'styles': {
					'width': (elements * this.voyagerSize), 
					'height': this.container.getSize().y, 
					'position':'absolute', 
					'top':0, 
					'left':0
				}
			}).inject(this.station, 'top');
			
			// voyageurs
			this.childs.inject(this.train);
			this.oldClass = this.childs[0].get('class');
			this.childs.setStyles({
				'width': this.voyagerSize, 
				'height': this.container.getSize().y,
				'margin': '0px ' + this.realMargin + 'px'
			}).removeClass(this.oldClass).addClass('pdtcat1');
			
			this.cb.clone().inject(this.train, 'bottom');

			// controles
			//this.controls(this.container, this.station, this.train, elements);
			this.controls(elements, this);
			
			// utilisation de la roulette de la souris
			this.container.addEvent('mousewheel', function(event){
				event.stop();
				if (event.wheel > 0) this.leftControl.fireEvent('click');		/* Mousewheel UP */
				else if (event.wheel < 0) this.rightControl.fireEvent('click');	/* Mousewheel DOWN*/
			}.bind(this));
			
			// auto play
			if(this.autoPlay) this.setAutoPlay(elements, this);
			
			
		
		},


		animateLeft: function(elements) {
			var left = this.train.getPosition(this.station);
			left.x = (Browser.ie)? Math.round(left.x*0.1)*10 : left.x;
			left.x = (Browser.firefox)? Math.round(left.x*0.1)*10 : left.x;
console.log(left.x);			
			var goLeft = left.x - this.station.getSize().x;
			  var trainLength = elements * this.voyagerSize;
			  var wagon = this.voyagerSize * this.nbVoyCabine;
			if( Math.abs(goLeft)<trainLength && Math.abs(left.x)%wagon==0) this.goThere.start({'left': goLeft});
			if( Math.abs(goLeft)>=trainLength) { this.train.setStyles({'left': wagon}); this.goThere.start({'left': 0}) }
		},
		
		
		animateRight: function(elements) {
			var left = this.train.getPosition(this.station);
			left.x = (Browser.ie)? Math.round(left.x*0.1)*10 : left.x;
			left.x = (Browser.firefox)? Math.round(left.x*0.1)*10 : left.x;

			var goRight = left.x + this.station.getSize().x;
			goRight = (Browser.ie)? Math.round(goRight*0.1)*10 : goRight;
			  var wagon = this.voyagerSize * this.nbVoyCabine;
			if( goRight<=0 && Math.abs(left.x)%wagon==0 ) this.goThere.start({'left': goRight});
			if( goRight>0 ) this.goThere.start({'left': 0});
		}, 
		
		
		controls: function(elements) {
			// animer
			this.goThere = new Fx.Morph(this.train, { 
				link: 'cancel', 
				'duration': this.options.duree, 
				'transition': this.options.trans, 
				onComplete: function(e) {
				}.bind(this)
			});
			
			// dsp controls
			this.rightControl = new Element('div',{'class':'anyRightControl', 'html': this.rTxt, 'styles': {'position': 'absolute', 'z-index': '99'}}).inject(this.container, 'top').addEvent('click', function(e) { this.animateLeft(elements, this) }.bind(this));
			
			this.leftControl  = new Element('div',{'class':'anyLeftControl', 'html': this.lTxt, 'styles': {'position': 'absolute', 'z-index': '99'}}).inject(this.container, 'top').addEvent('click', function(e) { this.animateRight(elements, this) }.bind(this));

		}, 
		
		
		setAutoPlay: function(elements) {
			this.autoPlaySatus = true;
			
			this.autoSlide = function() {
				this.animateLeft(elements, this);
				
			}.bind(this);
			
			this.timer = this.autoSlide.periodical((this.autoWait*1000)+this.options.duree);
			
			this.container.addEvents({
				// autoSlide.periodical((this.autoWait*1000)+this.options.duree)
				// clearInterval(autoSlide)
				'mouseenter': function() {
					clearInterval(this.timer);
					this.autoPlaySatus = false;
									$('debug').set('html', this.autoPlaySatus);
				}.bind(this),
				'mouseleave': function() {
					this.timer = this.autoSlide.periodical((this.autoWait*1000)+this.options.duree);
					this.autoPlaySatus = true;
									$('debug').set('html', this.autoPlaySatus);
				}.bind(this)
			});
			
		}
		
		
	});
