// 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: 'LEFT' 
			,rightCtrlText: 'RIGHT' 
			,autoPlay: false 
			,autoWait: 2 
			,captionClass: '' 
			,captionOpacity: 1 
			,method: 'cycle' 
			,favo : false
		},
		
		//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.captionClass = this.options.captionClass;
			this.captionOpacity = this.options.captionOpacity;
			this.favo = this.options.favo;
			this.position = 0;
			
			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, index) {
				elements++;
				child.set('rev', index);
				//if(child.getElement('div').get('rel') != null) {
					//console.log('ref = ' + child.getElement('div').get('rel'));
					//ref = child.getElement('div').get('rel');
					//child.set('rel', ref);
					//var captionText = child.getElement(this.captionClass).get('html');
				//}
			});
			
			
			//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('anyTravelers');
			
			this.cb.clone().inject(this.train, 'bottom');

			
			// controles
			this.controls(elements, this);
			
			// caption
			//this.firstCaptionText = (this.childs[0].getElement('div.legende'))? this.childs[0].getElement('div.legende').get('text') : '';
			if(this.captionClass.length != 0) {
				this.firstCaptionText = this.childs[0].getElement('div.legende').get('text');
				$$(this.captionClass).setStyles({'display':'none'});
				$$('div.legende').setStyles({'display':'none'});
				
				if(this.favo.length != 0) {
					var ref = this.childs[0].getElement('div.legende').get('rel');
					var dad = this.childs[0].getElement('div.legende').get('rev');
					var txt = this.firstCaptionText;
					//console.log(ccoeur);
					if(ref != null) {
						this.firstCaptionText = this.firstCaptionText + '<br /><div id="favrep"><img src="assets/images/common/heart.png" title="ajouter à votre sélection" alt="add"  style="cursor:pointer;" class="favoris" onclick="var req = new Request.HTML({ method: \'post\', url: \'assets/js/ajax/addfavoris.php\', evalScripts: true, data: {\'ref\': \''+ref+'\', \'txt\': \'' + txt + '\', \'dad\': \'' + dad + '\'}, onComplete: function(){ $(\'favrep\').highlight(\'#459FA0\'); }, update: $(\'favrep\')}).send();" /></div>';
					}
				}
				
				this.caption = new Element('div', {
					'class': this.captionClass, 
					'styles': {
						'opacity': this.captionOpacity,
						'z-index': 999
					}
				}).inject(this.container, 'top').set('html', this.firstCaptionText);
				

			}
			
			
			// 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;
			var goLeft = left.x - this.station.getSize().x;
			var trainLength = elements * this.voyagerSize;
			var wagon = this.voyagerSize * this.nbVoyCabine;
			
			// déplacement
			if( Math.abs(goLeft)<trainLength && Math.abs(left.x)%wagon==0) {
				this.position++;
				this.goThere.start({'left': goLeft});
			}
			
			// retour au début
			if( Math.abs(goLeft)>=trainLength) {
				if (this.options.method != 'rewind') {
					this.firstWagon = this.childs[0].clone().inject(this.container, 'top').setStyles({'position':'absolute', 'top':0, 'left':0 });
					this.position=0;
					this.goThere.start({'left': goLeft}).chain(
						function() {
							this.position = 0;
							this.train.setStyles({'left': 0});
							this.firstWagon.dispose();
						}.bind(this)
					);
				}
				else this.goThere.start({'left': 0/*, 'opacity':1*/});			// rewind
			}
			
			if(this.captionClass.length != 0) {
				var activeChild = this.train.getElement('div[rev="'+(this.position)+'"]');
				var nextChild = this.train.getElement('div[rev="'+(this.position+1)+'"]');
				this.captionText = activeChild.getElement('div.legende').get('text');
				//console.log('Légende : ' + this.captionText);
				
				if(this.favo.length != 0) {
					var ref = activeChild.getElement('div.legende').get('rel');
					var dad = activeChild.getElement('div.legende').get('rev');
					var txt = this.captionText;
					//console.log(ccoeur);
					if(ref != null) {
						this.captionText = this.captionText + '<br /><div id="favrep"><img src="assets/images/common/heart.png" title="ajouter à votre sélection" alt="add"  style="cursor:pointer;" class="favoris" onclick="var req = new Request.HTML({ method: \'post\', url: \'assets/js/ajax/addfavoris.php\', evalScripts: true, data: {\'ref\': \''+ref+'\', \'txt\': \'' + txt + '\', \'dad\': \'' + dad + '\'}, onComplete: function(){ $(\'favrep\').highlight(\'#459FA0\'); }, update: $(\'favrep\')}).send();" /></div>';
						
						/*
						this.captionText = this.captionText + '<br /><div id="favrep"></div>';
						var imgfav = new Element('img', {
							style: {
								 cursor: 'pointer'
								,width: 14 
								,height: 14 
							}
							,alt: 'add' 
							,title: 'ajouter aux favoris' 
							,rel: ccoeur 
							,events : {
								click: function() {
									var req = new Request.HTML({
										 method: 'post' 
										,url: 'assets/js/ajax/addfavoris.php' 
										,evalScripts: true 
										,data: { 
											'ref': ccoeur 
										} 
										,oncomplete: function(){ 
											alert('Click !'); 
										} 
										,update: $('favrep') 
									}).send();
								}
							}
						});
						imgfav.inject($('slide_collection').getFirst('div.legende'));
						*/
					}
				}
			}
			
			
		},
		
		
		animateRight: function(elements) {
			var left = this.train.getPosition(this.station);
			left.x = (Browser.ie)? 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 trainLength = elements * this.voyagerSize;
			var wagon = this.voyagerSize * this.nbVoyCabine;
			
			// déplacement
			if( goRight<=0 && Math.abs(left.x)%wagon==0 ) {
				this.position--;
				this.goThere.start({'left': goRight});
			}
			
			// fin du diapo
			if( goRight>0 ) {
				if (this.options.method != 'rewind') {
					this.lastWagon = this.childs[elements-1].clone().inject(this.container, 'top').setStyles({'position':'absolute', 'top':0, 'left':0 });
					this.position = elements-1;
					this.goThere.start({'left': goRight}).chain(
						function() {
							this.position = elements-1;
							this.train.setStyles({'left': -(trainLength-wagon)});
							this.lastWagon.dispose();
						}.bind(this)
					);
				}
				else {
					var trainLength = elements * this.voyagerSize;
					var nbW = Math.ceil(elements/this.nbVoyCabine);
					var lastPos = (nbW-1) * this.nbVoyCabine * this.voyagerSize;
					this.goThere.start({'left': -lastPos});
					this.position = elements-1;
				}
			}
			
			if(this.captionClass.length != 0) {
				var activeChild = this.train.getElement('div[rev="'+this.position+'"]');
				this.captionText = activeChild.getElement('div.legende').get('text');
				//console.log('Légende : ' + this.captionText);
				if(this.favo.length != 0) {
					var ref = activeChild.getElement('div.legende').get('rel');
					var dad = activeChild.getElement('div.legende').get('rev');
					var txt = this.captionText;
					//console.log(ccoeur);
					if(ref != null) {
						this.captionText = this.captionText + '<br /><div id="favrep"><img src="assets/images/common/heart.png" title="ajouter à votre sélection" alt="add"  style="cursor:pointer;" class="favoris" onclick="var req = new Request.HTML({ method: \'post\', url: \'assets/js/ajax/addfavoris.php\', evalScripts: true, data: {\'ref\': \''+ref+'\', \'txt\': \'' + txt + '\', \'dad\': \'' + dad + '\'}, onComplete: function(){ $(\'favrep\').highlight(\'#459FA0\'); }, update: $(\'favrep\')}).send();" /></div>';
					}
				}
			}
			
			
			
		}, 
		
		
		controls: function(elements) {
			// animer
			this.goThere = new Fx.Morph(this.train, { 
				link: 'cancel', 
				'duration': this.options.duree, 
				'transition': this.options.trans, 
				onStart: function(s) {
					if(this.captionClass.length != 0) this.caption.fade('out');
					//if(this.captionClass.length != 0) this.caption.setStyle('opacity', 0);
				}.bind(this),
				onComplete: function(c) {
					if(this.captionClass.length != 0) this.caption.set('html', this.captionText);
					//if(this.captionClass.length != 0) this.caption.fade(this.captionOpacity);
					if(this.captionClass.length != 0) this.caption.setStyle('opacity', this.captionOpacity);
				}.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({
				'mouseenter': function() {
					clearInterval(this.timer);
					this.autoPlaySatus = false;
				}.bind(this),
				'mouseleave': function() {
					this.timer = this.autoSlide.periodical((this.autoWait*1000)+this.options.duree);
					this.autoPlaySatus = true;
				}.bind(this)
			});
		}
	});
