/* 
#####################################################
#####################################################
##												   ##
##		Document JS développé pour le site		   ##		
##			http://www.gueuleton.com/			   ##
##												   ##
##			Propriété de ce document			   ##
##												   ##
##			  Simon Jean-Philippe				   ##
##		document créer le 25 janvier 2009		   ##
##				mis à jour le 					   ##
##												   ##
#####################################################
#####################################################
*/
	
	var Slider = Class.create({
									  
		//INITIALISATION DE L'ASCENSEUR				  
		initialize: function(pBoxSlider,pBoxContent,pWheel) {
			
			this.name = 'bonjour';
			
			this.pBoxSlider = $(pBoxSlider) ; // identifiant du conteneur du slider
			
			this.pBoxContent = $(pBoxContent) ; // identifiant de la box qui contient le texte qui sera scroller
			this.pBoxDownContent = this.pBoxContent.down().id; // identifiant de la box qui sera scroller
			//alert(this.pBoxDownContent);
			var idBoxScrool = this.pBoxDownContent;
			
			this.pDimensionBoxSlider = $(this.pBoxSlider).getDimensions(); // dimensions de la box contenant l'ascenseur
			this.pDimensionBoxContent = $(this.pBoxContent).getDimensions(); //dimension de la box contenant la box scoller
			this.pDimensionDownContent = $(this.pBoxDownContent).getDimensions(); //dimension de la box contenant le texte qui sera scroller
			//alert(this.pDimensionDownContent.height);
			this.pRange = 0 ; // debut du scrool
			this.pEndRange =  this.pDimensionDownContent.height - this.pDimensionBoxContent.height; //fin du scrool
			//alert(this.pBoxDownContent);
			this.pHeightAscenseur = this.pDimensionBoxSlider.height/ (this.pDimensionDownContent.height / this.pDimensionBoxContent.height ); // hauteur de l'ascenseur suivant les dimensions des box
			//this.construct(this.pBoxDownContent,this.pBoxContent);
			var topAscenseurScrool = 0 ;
			
			//#########################################
			//	CONSTRUCTION DE L'ASCENSEUR INTERIEUR/
			//#########################################
			
			if(this.pEndRange>0) {
						
				this.pAscenseur = Builder.node('div', { //construction de l'ascenseur
					id : 'pAscenseur',
					className : 'scroolBar',
					style : ' height:'+ this.pHeightAscenseur+'px; width:'+this.pDimensionBoxSlider.width+'px; background-color:#888888; '
				}) ;
				this.pAscenseur = '<div id="pAscenseur" class="scroolBar" style="height:'+ this.pHeightAscenseur+'px; width:'+this.pDimensionBoxSlider.width+'px; background-color:#888888;"></div>';
				$(this.pBoxSlider).insert(this.pAscenseur); // insertion de l'ascenseur dans la box Slider
				
				$('pAscenseur').observe('mouseover', function() {
					$('pAscenseur').setStyle({background : '#888888'});	   
				});
				$('pAscenseur').observe('mouseout', function() {
					$('pAscenseur').setStyle({background : '#666666' });	   
				});
				
				
				var scroolValue = 0 ;
				
				//construction de l'ascenseur
				new Control.Slider('pAscenseur', this.pBoxSlider, {
					axis : 'vertical',
					range: $R( this.pRange, this.pEndRange),
					sliderValue: 0,
					onSlide: function(value) {
						$(idBoxScrool).setStyle({ top: -(parseInt(value)+1)	 + 'px'});
						scroolValue = parseFloat( $(idBoxScrool).getStyle('top') ) ;
						topAscenseurScrool = parseInt($('pAscenseur').getStyle('top'));
						//alert(topAscenseurScrool);
					},
					onChange: function(value) {
						//alert(this.pBoxDownContent);
						$(idBoxScrool).setStyle({ top: -(parseInt(value)+1)	 + 'px'});
						scroolValue = parseFloat($(idBoxScrool).getStyle('top'));
						topAscenseurScrool = parseInt($('pAscenseur').getStyle('top'));
						//salert(topAscenseurScrool);
					}
				});
	
			}
			
			
			var newHeightDownBoxContent = '' ;
			
			if(pWheel) { 
			
					Object.extend(Event, {
						wheel:function (event){
							Event.stop(event);
							var delta = 0;
							if (!event) event = window.event;
							if (event.wheelDelta) { // on rentre ici si on n'est sous IE et OPERA
								//alert(event.wheelDelta);
								delta = event.wheelDelta/120;
								if (window.opera) delta = -delta;
							} else if (event.detail) {  // on rentre ici si on n'est sous Firefox
								//alert(event.detail);
								delta = -event.detail/3; 
							}
							return Math.round(delta); //Safari Round
							
							
						}
					});
					
					
					//calcul du nombre pixel a monter ou descendre du box scrool
					var pixelTopBottom = (this.pDimensionBoxContent.height-this.pDimensionDownContent.height)/ (parseInt(this.pBoxSlider.getStyle('height'))-parseInt(this.pHeightAscenseur));
					
					//calcul de lespace du bat entre ascenseur et barre defilement
					var pEspaceBottom = parseInt(this.pDimensionBoxSlider.height-this.pHeightAscenseur);
					var topAscenceur = parseInt($('pAscenseur').getStyle('top'));
					if(pEspaceBottom>=300) { // si c'est supérieur à 300px on descende de 60px par 60px
						scroolAscenseur = 60;
					} else if(pEspaceBottom>=200) { //si c'est supérieur à 200px on descend de 50px par 50px
						scroolAscenseur = 40;
					} else if(pEspaceBottom>=100) {
						scroolAscenseur = 25;
					} else if(pEspaceBottom>=50) {
						scroolAscenseur = 25;
					} else if(pEspaceBottom>=1) {
						scroolAscenseur = 10;
					} else {
						scroolAscenseur = 1;
					}
					
					ancienScroolAscenseur = scroolAscenseur ;
					
					$(this.pBoxContent).observe("mousewheel", function(e) {
						gestionScrooler(e);
					});
					
					
					$(this.pBoxContent).observe("DOMMouseScroll", function(e) {
						gestionScrooler(e);
					});
					
					function gestionScrooler(e) {
						if(Event.wheel(e) < 0 ) { // molette vers nous
							//si on est pas arrivé en bas
							if(topAscenseurScrool<(parseInt($(pBoxSlider).getStyle('height'))-parseInt( $('pAscenseur').getStyle('height') ) ) ) {
								
								//verification 
								 var i=0;
								 while(i<=0) {
									 if( (topAscenseurScrool+scroolAscenseur) >= (parseInt($(pBoxSlider).getStyle('height'))-parseInt( $('pAscenseur').getStyle('height') ) )   ) {
										 scroolAscenseur -= 1 ;
									 } else {
										 i = 1 ;
									 }
								 }
								 
								scroolValue += pixelTopBottom*scroolAscenseur;
								  
								$(idBoxScrool).setStyle({ top: (parseInt(scroolValue))+'px'});
								$('pAscenseur').setStyle({ top : (topAscenseurScrool+scroolAscenseur)+'px' });
								topAscenseurScrool = parseInt($('pAscenseur').getStyle('top'));
								
								//remise du scrool de base
								scroolAscenseur = ancienScroolAscenseur ;
							}
							
						} else { //molette extérieur
							//si on est arrivé en haut
							if(topAscenseurScrool>0) {
								
								 //verification 
								 var i=0;
								 while(i<=0) {
									 if( (topAscenseurScrool-scroolAscenseur) < 0 ) {
										 scroolAscenseur -= 1 ;
									 } else {
										 i = 1 ;
									 }
								 }
								 
								scroolValue -= pixelTopBottom*scroolAscenseur;
								
								$(idBoxScrool).setStyle({ top: (parseInt(scroolValue))+'px'});
								$('pAscenseur').setStyle({ top : (topAscenseurScrool-scroolAscenseur)+'px'});
								topAscenseurScrool = parseInt($('pAscenseur').getStyle('top'));
								
								//remise du scrool de base
								scroolAscenseur = ancienScroolAscenseur ;
								
							} 
							
						}
					}//fin de focntion scrooler
					
					
					
					
			} // condition wheel
			
		},
		
		resizeSlider: function($super) {
			//alert(this.name);
			/*var newHeightDownBoxContent = $(this.pBoxDownContent).getDimensions();
			
				//calcul de la hauteur du box scoller
				if(newHeightDownBoxContent.height!=this.pDimensionBoxContent.height) {
				//si différent de l'ancienne hauteur
					alert('ok');
					this.pDimensionBoxContent.height = newHeightDownBoxContent;
					//redimension du slider
					
					//changement de l'interval du scroll
				}*/
			
			
		}
		
		
	});
	
					


