/*--- function custom scroll ---*/
jQuery.fn.customScroll = function(_options) {
var _options = jQuery.extend({
	lineWidth: 48
}, _options);

return this.each(function(){
	var scroll = jQuery(this);
	if(scroll.is(':visible')){
		var t_scroll = this;
		if(!scroll.hasClass('changed')){
			var line_w = _options.lineWidth;
			t_scroll._h = scroll.height();
			var _w = scroll.width();
			var scrollHolder = jQuery('<div class="scroll-container"></div>');
			var scrollLine = jQuery('<div class="vscroll-bar"></div>');
			var scrollSlider = jQuery('<div class="vscroll-slider"></div>');
			scrollHolder.css({
				height: t_scroll._h,
				width: _w,
				overflow: 'hidden',
				position: 'relative'
			});
			scrollLine.css({
				position: 'absolute',
				top: 0,
				right: 0,
				width: line_w,
				height: t_scroll._h,
				overflow: 'hidden',
				cursor: 'pointer'
			});
			scrollSlider.css({
				position: 'absolute',
				top: 0,
				left: 0,
				width: line_w,
				height: 0,
				overflow:'hidden'
			});
			scroll.css({
				position: 'absolute',
				height: 'auto',
				overflow: 'hidden',
				width: _w - line_w - 5,
				top:0,
				left:0
			});
			scrollLine.append(scrollSlider);
			scroll.after(scrollHolder);
			scrollHolder.append(scroll).append(scrollLine);
			t_scroll.ssH = t_scroll._h/scroll.height()*t_scroll._h;
			if(t_scroll.ssH < t_scroll._h){
				scrollSlider.height(t_scroll.ssH);
				t_scroll._factor = (scroll.height() - t_scroll._h)/(t_scroll._h - t_scroll.ssH);
				t_scroll._step1 = (t_scroll._h - t_scroll.ssH)/20;
				t_scroll._step2 = (t_scroll._h - t_scroll.ssH)/2;
				if(t_scroll._step1 < 4) t_scroll._step1 = 4;
				var _f1 = false;
				var _f2 = true;
				t_scroll._t = 0;
				t_scroll._param = 0;
				scrollSlider.mousedown(function(e){
					_f1 = true;
					t_scroll._t = e.pageY - $(this).position().top;
				}).mouseup(function(){
					_f1 = false;
				});
				scrollLine.mousemove(function(e){
					if(_f1){
						t_scroll._param = e.pageY - t_scroll._t;
						scrollBox();
						_f2 = false;
					}
				}).click(function(e){
					if(_f2){
						if(scrollSlider.offset().top + t_scroll.ssH < e.pageY){
							t_scroll._param += t_scroll._step2;
						}
						else if(scrollSlider.offset().top > e.pageY){
							t_scroll._param -= t_scroll._step2;
						}
						scrollBox();
					}
					else{
						_f2 = true;
					}
					
				}).mouseleave(function(){
					_f1 = false;
					_f2 = true;
				});
				scrollHolder.bind('mousewheel', function(event, delta){
					t_scroll._param -=delta*t_scroll._step1;
					scrollBox();
					if((t_scroll._param > 0) && (t_scroll._param+t_scroll.ssH < t_scroll._h)) return false;
				});
			}
			scroll.addClass('changed');
			function scrollBox(){
				if(t_scroll._param < 0) t_scroll._param = 0;
				else if(t_scroll._param+t_scroll.ssH > t_scroll._h) t_scroll._param = t_scroll._h - t_scroll.ssH;
				scrollSlider.css('top', t_scroll._param);
				scroll.css('top', -t_scroll._param*t_scroll._factor);
			}
		}
		else{
			if(scroll.outerHeight() != t_scroll._h){
				
			}
		}
	}
});
}
