(function($){
	
	var el,el_height,interval,max_height,test_interval;
	
	var current_position=0;
	
	function update_scrollHandler_pos(){	//Mise à jour de la poignée de scroll en fonction de la position du contenu
		$("#slider-handle").clearQueue();
		$("#slider-handle").css('top',-(current_position-50)/($("#content-sliding-area").height()/$("#content-area").height()));
	}
	
	function scroll_down(nRow){		//Scroll descendant par appui sur le bouton et par la roulette
		el_height=el.height();
		
		if(current_position > -el_height+max_height){
			current_position=(current_position-(nRow*30))<(-el_height-max_height)?el_height-max_height:current_position-(nRow*30);
			el.clearQueue();
			el.css("top",current_position);
			update_scrollHandler_pos();
		}
	}
	
	function scroll_up(nRow){	//Scroll montant par appui sur le bouton et par la roulett
		el_height=el.height();
		
		if(current_position<0){
			current_position=current_position+(nRow*30)<0?current_position+(nRow*30):0;
			el.clearQueue();
			el.css("top",current_position);
			update_scrollHandler_pos();
		}
	}
	
	$.fn.customSlider=function(m){
		el=$(this);	
		var parent=$(this).parent();
		max_height=m;
		el_height=el.height();
		var parent_height=parent.height();
		
		if(el_height>parent_height){
			var left=parent.offset().left-$("#main-content").offset().left;
			var top=parent.offset().top-$("#main-content").offset().top+max_height+15;
			var handlerSize = (max_height/(el_height/max_height));
			
			if($("#slider-handle").size()==0){
				$("#content-area-wrapper").append("<div id='slider-handle' style='background-color:#191818; border:solid 1px #606060;border-radius:4px; opacity: 0.7;z-index:30;position:absolute ;top:"+($("#content-area").offset().top-$("#content-area-wrapper").offset().top)+"px;left:"+500+"px;height:"+handlerSize+"px; width:8px'></div>");
				$("#slider-handle").mousedown(function(event){
					var mouseY=Math.floor(event.pageY);
					var handlerOffset = mouseY-$("#slider-handle").offset().top;
					$('body').css('-webkit-user-select', 'none');
					$('body').css('-khtml-user-select', 'none');
					$('body').css('-moz-user-select', 'none');
					$('body').css('-o-user-select', 'none');
					$('body').css('user-select', 'none');
					$('body').attr('unselectable','on',0);
					$('body').bind('mousemove',function(event){
						var mouseY=Math.floor(event.pageY);
						if ((mouseY >= (parent.offset().top+handlerOffset)) && (mouseY <= (parent.offset().top+max_height-handlerSize+handlerOffset+70)))
						{
							$("#slider-handle").css('top', Math.floor(mouseY-handlerOffset-$("#content-area-wrapper").offset().top)+'px');
							current_position =(-$("#slider-handle").position().top+25)*($("#content-sliding-area").height()/$("#content-area").height());
						}
						else if (mouseY < (parent.offset().top+handlerOffset))
						{
							$("#slider-handle").css('top',$("#content-area").offset().top-$("#content-area-wrapper").offset().top);
							
							current_position=0;
						}
						el.css('top' ,Math.floor(current_position)+'px');
					});
					$('body').mouseup(function(){
						$('body').unbind('mouseup');
						$('body').unbind('mousemove');
						$('body').css('-webkit-user-select', 'text');
						$('body').css('-khtml-user-select', 'text');
						$('body').css('-moz-user-select', 'text');
						$('body').css('-o-user-select', 'text');
						$('body').css('user-select', 'text');
						$('body').attr('unselectable','',0);
					});
				});
				$("#content-area-wrapper").bind('mousewheel',function(event,delta){
					if(delta>0) scroll_up(delta);
					else scroll_down(-delta);
					event.stopPropagation();
					event.preventDefault();
				});
			}else{
				$("#slider-handle").css('top',$("#content-area").offset().top-$("#content-area-wrapper").offset().top);
				$("#slider-handle").css('height',handlerSize);
				current_position=0;
			}
		}
		else {	
			$("#slider-handle").remove();
			parent.removeAttr("style");
		}
	}
}
	
)(jQuery)
