(
	function($) 
	{
		$.fn.mobileScroll = function(options) 
		{
			var defaults = {
			position:'vertical',
			blockX:"right: 1px",
			blockY: "top: 0px",
			marginType:'margin-top',
			sizeType:'',
			sizeWidthWrapper:'',
			scrollWidth:'3px',
			scrollHeight:'100px'
				
		};


		var options = $.extend(defaults, options);
			
			return this.each(function() 
			{
				var conteneur = $(this);
				var contenu = $(this).html();
				var start_time = 0;
				var last_decal = 0;

			if(options.position=="horizontal")
			{
				options.blockX = "left: 1px";
				options.blockY = "bottom: 3px";
				options.marginType = "margin-left";
				options.sizeType = "width";
				options.scrollWidth = "100px";
				options.scrollHeight = "3px";
				
			}
			
			//Si le Conteneur a déjà une classe wrapper on l'efface avant d'en recréer une
			
			if ($(conteneur).children(".wrapper").lenght > 0) {$(this).children(".wrapper").remove();} 
				
				$(conteneur).html( "<div class='scrollbar_auto' style='position: absolute;"+options.blockY+";"+options.blockX+"; -webkit-box-shadow: 0px 0px 2px rgba(255,255,255, 0.3); width:"+options.scrollWidth+"; height:"+options.scrollHeight+"; -webkit-border-radius: 3px; background-color: rgba(0,0,0,0.5); z-index: 100; display: none; '></div>"
								+ "<div class='wrapper' style='height:auto;"+options.sizeType+":"+options.sizeWidthWrapper+"; -webkit-transition-property: "+options.marginType+"; -webkit-transition-duration: 0.2s; -webkit-transition-timing-function: ease-out;padding-bottom:5px;'>" 
								+ contenu + "</div>"
							)
					.css("position", "relative");
	
	
			this.ontouchstart = function(evt) 
			{
				var now = new Date().getTime();
			    start_time = parseInt(now, 10);
				last_decal = 0;
			
			
			if(options.position=="horizontal")
			{
				//position sur l'ipad au touché
				debut_scroll = evt.touches[0].pageX;
		
				//On checke s'il y a une marge à gauche
				debut_margin = parseInt($(this).children(".wrapper").css("marginLeft"));
				
				//Le rapport entre la taille du conteneur défini en hauteur et la hauteur réel du wrapper.
				rapport = $(this).outerWidth() / $(this).children(".wrapper").outerWidth();
				
				//ON calcule la taille de la scrollbar
				size_scrollbar_auto = Math.floor(rapport * $(this).outerWidth());
				
				// On attribue la taille à l'élément
				$(conteneur).children(".scrollbar_auto").width(size_scrollbar_auto);
				
				hauteur_max = $(conteneur).children(".wrapper").outerWidth() - $(conteneur).outerWidth();
				
				hauteur_max = -1 * hauteur_max;
				
				hauteur_boite = $(conteneur).outerWidth();

			}
			else
			{
				
				
				debut_scroll = evt.touches[0].pageY;
				debut_margin = parseInt($(this).children(".wrapper").css("marginTop"));
				
				//Le rapport entre la taille du conteneur défini en hauteur et la hauteur réel du wrapper.
				rapport = $(this).outerHeight() / $(this).children(".wrapper").outerHeight();
				
				//ON calcule la taille de la scrollbar
				size_scrollbar_auto = Math.floor(rapport * $(this).outerHeight());
				
				// On attribue la taille à l'élément
				$(conteneur).children(".scrollbar_auto").height(size_scrollbar_auto);
				
				
				hauteur_max = $(this).children(".wrapper").outerHeight() - $(this).outerHeight();
				hauteur_max = -1 * hauteur_max;
				hauteur_boite = $(this).outerHeight();
			}
				
				if (rapport < 1) 
				{
					//On affiche la scrollBar
					$(conteneur).children(".scrollbar_auto").fadeIn();
				}
				

				
			};
			
			this.ontouchmove = function(event) 
			{
				if (rapport > 1) return false;
				
				if(options.position=="horizontal")
				{
					var y = event.touches[0].pageX;
				}
				else
				{
					var y = event.touches[0].pageY;
				}
				
				// On calcule le déplacement du mouvement en fonction de la position de départ
				var decal = y - debut_scroll;
			
				
				//On stocke ce décalage dans la variable last_decal
				last_decal = decal;
				
				//On ajoute si elle existe, la marge à gauche 
				decal = decal + debut_margin;
				
				// On fixe les limites du décalage Minimum
				if (decal > 0) decal = 0;
				
				// On fixe les limites du décalage Maximum
				if (decal < hauteur_max) decal = hauteur_max;
				
				if(options.position=="horizontal")
				{
					$(this).children(".wrapper").css("marginLeft", decal);
				}
				else
				{
					$(this).children(".wrapper").css("marginTop", decal);
				}	
				
				var rapport_pos = (decal / hauteur_max);
				
				//
				var vide = Math.round( rapport_pos * (hauteur_boite - size_scrollbar_auto));

				$(conteneur).children(".scrollbar_auto").css(options.marginType, vide);
				$(conteneur).children(".scrollbar_auto").css(options.marginType, vide);
				return false;
									
			};
			
			$(document).bind("touchend touchcancel", function(){
				$(".scrollbar_auto").fadeOut();
			});
			
		});
		};
	})
(jQuery);


