//il faut importer jQuery 1.4 dans la meme page
jQuery(function($)
{
	var reg=/iPhone|iPod|iPad/i;
	var mobile = reg.test(navigator.userAgent);
	
	if(mobile)
	{
		$(".scrollDemi, .scrollQuart, #galerie div").each(function(){
			$(this).jScrollTouch(); 				   
		});
	}
	else
	{
	
		var lastPosY = 0;
			
		var url = document.location.href.split('/');
		var data = url[url.length - 2] == "data" ? "" : "data/";
			
		var scrollables = new Array();
		
		function creerBars()
		{
			var objet = {scrollZone : $(this)};
	
			if(objet.scrollZone.get()[0].scrollHeight > objet.scrollZone.innerHeight())
			{
				objet.scrollZone.children("p:first").css({'float': "left", 'overflow-y': 'hidden', 'padding': '10px 0'});
				objet.scrollZone.children("p:first").width(objet.scrollZone.width());
				objet.scrollZone.children("p:first").height(objet.scrollZone.height());
				objet.scrollZone.children("p:first").scrollTop(0);
				
				objet.scrollZone.css({
									 'overflow-y': 'hidden',
									 'padding': '0 0 0 10px',
									 'width': (objet.scrollZone.width() + 10) + 'px',
									 'height': (objet.scrollZone.height() + 20) + 'px'
									 });
				
				objet.scrollBar = $(document.createElement('P'));
				objet.scrollBar.css({'float': "left", 'padding': '0 1 0 1px', 'position': 'relative','background-color':'#2F2F2F'});
				objet.scrollBar.height(objet.scrollZone.height());
				
				objet.ascenseur = $(document.createElement('IMG'));
				//l'image d'ascenseur, hauteur sans importance (ds les limites de la zone bien sur), essayer de garder une largeur proche (faire essais pour augmenter)
				objet.ascenseur.attr({
						src: data + 'templates/scroll.gif',
						width: '8',
						height: '53'
					});
				objet.ascenseur.css({'float': "left", 'position': 'relative'});
	
				objet.scrollBar.append(objet.ascenseur);
				
				scrollables.push(objet);
				
				objet.scrollZone.append(objet.scrollBar);
				
				objet.scrollZone.bind('mousedown', objet, downHandler);
			}
		}

		function downHandler(e)
		{
			if(e.target == e.data.scrollBar.get()[0])
			{
				var ascenseur = e.data.ascenseur;
				var scrollBar = e.data.scrollBar;
				var scrollZone = e.data.scrollZone.children('p:first');
				
				var posY = e.pageY - scrollBar.offset().top - ascenseur.height() * .5;
				if(posY < 0) posY = 0;
				else if(posY > scrollBar.height() - ascenseur.height()) posY = scrollBar.height() - ascenseur.height();
				
				var scrollValue = Math.round((scrollZone.get()[0].scrollHeight - scrollZone.height()) * posY / (scrollZone.height() - ascenseur.height()));
				
				ascenseur.css('top', posY);
				scrollZone.scrollTop(scrollValue);
			}
			else if(e.target == e.data.ascenseur.get()[0]) 
			{
				$('html').bind('mousemove', e.data, dragAscenseur);
				$('html').bind('mouseup mouseleave', stopDragAscenseur);
			}
			else
			{
				lastPosY = e.pageY;
				$('html').bind('mousemove', e.data, dragContent);
				$('html').bind('mouseup mouseleave', stopDragContent);
			}
	
			e.preventDefault();
		}
		
		function dragAscenseur(e)
		{
			var ascenseur = e.data.ascenseur;
			var scrollBar = e.data.scrollBar;
			var scrollZone = e.data.scrollZone.children('p:first');
			
			var posY = e.pageY - scrollBar.offset().top - ascenseur.height() * .5;
			if(posY < 0) posY = 0;
			else if(posY > scrollBar.height() - ascenseur.height()) posY = scrollBar.height() - ascenseur.height();
			
			var scrollValue = Math.round((scrollZone.get()[0].scrollHeight - scrollZone.height()) * posY / (scrollZone.height()));
				
			ascenseur.css('top', posY);
			scrollZone.scrollTop(scrollValue);
			
			e.preventDefault();
		}
		
		function dragContent(e)
		{
			var ascenseur = e.data.ascenseur;
			var scrollBar = e.data.scrollBar;
			var scrollZone = e.data.scrollZone.children('p:first');
			
			var newScroll = scrollZone.scrollTop() - lastPosY + e.pageY;
			
			lastPosY = e.pageY;
			
			if(newScroll < 0) newScroll = 0;
			else if(newScroll > scrollZone.get()[0].scrollHeight - scrollZone.height()) newScroll = scrollZone.get()[0].scrollHeight - scrollZone.height();
	
			var scrollValue = Math.round((scrollBar.height() - ascenseur.height()) * newScroll / (scrollZone.get()[0].scrollHeight - scrollBar.height()));
			
			ascenseur.css('top', scrollValue);
			scrollZone.scrollTop(newScroll);
			
			e.preventDefault();
		}
		
		function stopDragAscenseur()
		{
			$('html').unbind('mousemove', dragAscenseur);
			$('html').unbind('mouseup mouseleave', stopDragAscenseur);
		}
		
		function stopDragContent()
		{
			$('html').unbind('mousemove', dragContent);
			$('html').unbind('mouseup mouseleave', stopDragContent);
		}
		
		$(".scrollDemi, .scrollQuart, #galerie div").each(creerBars);
	}
});
