(function($)
{
	var debut_scroll_Y = false;
	var debut_marginTop = 0;
	var hauteur_scrollbar_auto = 100;
	var scroll_decal = 15;
	$.FixScrollIpad = function ()
	{
		if(navigator.userAgent.match(/iPhone|iPod|iPad/i))
		{
			$(".scroll_auto").each(function()
			{
				var contenu = $(this).html();
				var start_time = 0;
				var last_decal = 0;
				var rapport = 1;
				var hauteur_max = 100;
				var hauteur_boite = 100;
				
				if ($(this).children(".wrapper").width() > 0){}
				else
				{
					$(this).html( "<div class='scrollbar_auto' style='position: absolute; top: 0px; right: 3px; -webkit-box-shadow: 0px 0px 2px rgba(255,255,255, 0.3); width: 5px; height: 100px; -webkit-border-radius: 3px; background-color: rgba(0,0,0,0.5); z-index: 100; display: none; '></div>"
									+ "<div class='wrapper' style='height: auto; -webkit-transition-property: margin-top; -webkit-transition-duration: 0.2s; -webkit-transition-timing-function: ease-out;'>" 
									+ contenu + "<div style='clear: both;'></div></div>"
									+ "<div class='bidon' style='position: absolute; opacity: 0;'></div>"
								)
						.css("position", "relative")
						.css("overflow", "hidden");
						
				}
		
				this.ontouchstart = function(evt)
				{
					debut_scroll_Y = evt.touches[0].pageY;
					var now = new Date().getTime();
				    start_time = parseInt(now, 10);
					last_decal = 0;
					debut_marginTop = parseInt($(this).children(".wrapper").css("marginTop"));
					
					rapport = $(this).outerHeight() / $(this).children(".wrapper").outerHeight();
					
					if (rapport < 1) {
						$(this).children(".scrollbar_auto").fadeIn();
						hauteur_scrollbar_auto = Math.floor(rapport * $(this).outerHeight());
						$(this).children(".scrollbar_auto").height(hauteur_scrollbar_auto);
					}
					hauteur_max = $(this).children(".wrapper").outerHeight() - $(this).outerHeight()+scroll_decal;
					hauteur_max = -1 * hauteur_max;
					hauteur_boite = $(this).outerHeight();
					
				};
				
				this.ontouchmove = function(event)
				{
					if (rapport > 1) return false;
					
					var y = event.touches[0].pageY;
					
					var decal = y - debut_scroll_Y;
					last_decal = decal;
					decal = decal + debut_marginTop;
					
					
					if (decal > 0) decal = 0;
					if (decal < hauteur_max) decal = hauteur_max;
					$(this).children(".wrapper").css("marginTop", decal);
					// Hack bizarre: dans certains cas, il faut ecrire quelque chose quelquepart pour que le scroll fonctionne en direct
					$(this).children(".bidon").html("bidon");
					
					var rapport_pos = (decal / hauteur_max);
					var vide = Math.round( rapport_pos * (hauteur_boite - hauteur_scrollbar_auto));
					
					$(this).children(".scrollbar_auto").css("marginTop", vide);
					
					
					return false;						
				};
				
				this.ontouchend = function(event)
				{
					var now_end = new Date().getTime();
				    var end_time = parseInt(now_end, 10);
				    
				    var duree = end_time - start_time;
				
					var decal = Math.round(last_decal * ( 1 + 200/duree));
					decal = decal + debut_marginTop;
					
					
					if (hauteur_max < 0)
					{
						
						if (decal > 0) decal = 0;
						if (decal < hauteur_max) decal = hauteur_max;
						
						$(this).children(".wrapper").css("marginTop", decal);
						// Hack bizarre: dans certains cas, il faut ecrire quelque chose quelquepart pour que le scroll fonctionne en direct
						$(this).children(".bidon").html("bidon");
					}
					$(".scrollbar_auto").fadeOut();
				}
				
				this.ontouchcancel - function(event)
				{
					$(".scrollbar_auto").fadeOut();
				}
			});
		}
	}
})(jQuery);
