/* malihu custom scrollbar plugin - http://manos.malihu.gr */

	var id;
	var $customScrollBox;
	var $customScrollBox_container;
	var $customScrollBox_content;
	var $dragger_container;
	var $dragger;
	var $customScrollBox_horWrapper;
	
	var animSpeed_;
	var easeType_;
	var bottomSpace_;
	var draggerDimType_;
	var mouseWheelSupport_;
	var scrollBtnsSupport_;
	var scrollBtnsSpeed_;
	
	var scrollAmount;
	

(function ($) {
$.fn.mCustomScrollbar = function (scrollType,animSpeed,easeType,bottomSpace,draggerDimType,mouseWheelSupport,scrollBtnsSupport,scrollBtnsSpeed){
	
	id = $(this).attr("id");
	$customScrollBox=$("#"+id+" .customScrollBox");
	$customScrollBox_container=$("#"+id+" .customScrollBox .container");
	$customScrollBox_content=$("#"+id+" .customScrollBox .content");
	$dragger_container=$("#"+id+" .dragger_container");
	$dragger=$("#"+id+" .dragger");
	$customScrollBox_horWrapper=$("#"+id+" .customScrollBox .horWrapper");
	
	animSpeed_ = animSpeed;
	easeType_ = easeType;
	bottomSpace_ = bottomSpace;
	draggerDimType_ = draggerDimType;
	mouseWheelSupport_ = mouseWheelSupport;
	scrollBtnsSupport_ = scrollBtnsSupport;
	scrollBtnsSpeed_ = scrollBtnsSpeed;
	
	//get & store minimum dragger height & width (defined in css)
	if(!$customScrollBox.data("minDraggerHeight")){
		$customScrollBox.data("minDraggerHeight",$dragger.height());
	}
	if(!$customScrollBox.data("minDraggerWidth")){
		$customScrollBox.data("minDraggerWidth",$dragger.width());
	}
	
	//get & store original content height & width
	if(!$customScrollBox.data("contentHeight")){
		$customScrollBox.data("contentHeight",$customScrollBox_container.height());
	}
	if(!$customScrollBox.data("contentWidth")){
		$customScrollBox.data("contentWidth",$customScrollBox_container.width());
	}
	
	CustomScroller();
	
	$dragger.mouseup(function(){
		DraggerRelease();
	}).mousedown(function(){
		DraggerPress();
	});
	
	$(window).resize(function() {
		if($dragger.position().top>$dragger_container.height()-$dragger.height()){
			$dragger.css("top", $dragger_container.height()-$dragger.height());
		}
		CustomScroller("resize");
	});
};  
})(jQuery);


	function CustomScroller(reloadType){
		
		//vertical scrolling ------------------------------
		
		var visibleHeight=$customScrollBox.height();
		if($customScrollBox_container.height()>visibleHeight){ //enable scrollbar if content is long
			$dragger.css("display","block");
			if(reloadType!="resize" && $customScrollBox_container.height()!=$customScrollBox.data("contentHeight")){
				$dragger.css("top",0);
				$customScrollBox_container.css("top",0);
				$customScrollBox.data("contentHeight",$customScrollBox_container.height());
			}
			$dragger_container.css("display","block");
			var totalContent=$customScrollBox_content.height();
			var minDraggerHeight=$customScrollBox.data("minDraggerHeight");
			var draggerContainerHeight=$dragger_container.height();
	
			
			AdjustDraggerHeight(totalContent, visibleHeight, minDraggerHeight, draggerContainerHeight);
	
			var targY=0;
			var draggerHeight=$dragger.height();
			$dragger.draggable({ 
				axis: "y", 
				containment: "parent", 
				drag: function(event, ui) {
					Scroll();
				}, 
				stop: function(event, ui) {
					DraggerRelease();
				}
			});
			
			$dragger_container.click(function(e) {
				var $this=$(this);
				var mouseCoord=(e.pageY - $this.offset().top);
				if(mouseCoord<$dragger.position().top || mouseCoord>($dragger.position().top+$dragger.height())){
					var targetPos=mouseCoord+$dragger.height();
					if(targetPos<$dragger_container.height()){
						$dragger.css("top",mouseCoord);
						Scroll();
					} else {
						$dragger.css("top",$dragger_container.height()-$dragger.height());
						Scroll();
					}
				}
			});
				//mousewheel
			$(function($) {
				if(mouseWheelSupport_=="yes"){
					$customScrollBox.unbind("mousewheel");
					$customScrollBox.bind("mousewheel", function(event, delta) {
						var vel = Math.abs(delta*10);
						$dragger.css("top", $dragger.position().top-(delta*vel));
						Scroll();
						if($dragger.position().top<0){
							$dragger.css("top", 0);
							$customScrollBox_container.stop();
							Scroll();
						}
						if($dragger.position().top>$dragger_container.height()-$dragger.height()){
							$dragger.css("top", $dragger_container.height()-$dragger.height());
							$customScrollBox_container.stop();
							Scroll();
						}
						return false;
					});
				}
			});
				//scroll buttons
			if(scrollBtnsSupport_=="yes"){
				$scrollDownBtn.mouseup(function(){
					BtnsScrollStop();
				}).mousedown(function(){
					BtnsScroll("down");
				}).mouseout(function(){
					BtnsScrollStop();
				});
			
				$scrollUpBtn.mouseup(function(){
					BtnsScrollStop();
				}).mousedown(function(){
					BtnsScroll("up");
				}).mouseout(function(){
					BtnsScrollStop();
				});
			
				$scrollDownBtn.click(function(e) {
					e.preventDefault();
				});
				$scrollUpBtn.click(function(e) {
					e.preventDefault();
				});
			
				btnsScrollTimer=0;
			
				
			}
			
			//scroll
			if(bottomSpace_<1){
				bottomSpace_=1; //minimum bottomSpace value is 1
			}
			scrollAmount=(totalContent-(visibleHeight/bottomSpace_))/(draggerContainerHeight-draggerHeight);
			
		} else { //disable scrollbar if content is short
			$dragger.css("top",0).css("display","none"); //reset content scroll
			$customScrollBox_container.css("top",0);
			$dragger_container.css("display","none");
		}
	}
	
	function AdjustDraggerHeight(totalContent, visibleHeight, minDraggerHeight, draggerContainerHeight){
				if(draggerDimType_=="auto"){
					var adjDraggerHeight=Math.round(totalContent-((totalContent-visibleHeight)*1.3)); //adjust dragger height analogous to content
					if(adjDraggerHeight<=minDraggerHeight){ //minimum dragger height
						$dragger.css("height",minDraggerHeight+"px").css("line-height",minDraggerHeight+"px");
					} else if(adjDraggerHeight>=draggerContainerHeight){
						$dragger.css("height",draggerContainerHeight-10+"px").css("line-height",draggerContainerHeight-10+"px");
					} else {
						$dragger.css("height",adjDraggerHeight+"px").css("line-height",adjDraggerHeight+"px");
					}
				}
			}
	
	function BtnsScroll(dir){
					if(dir=="down"){
						var btnsScrollTo=$dragger_container.height()-$dragger.height();
						var scrollSpeed=Math.abs($dragger.position().top-btnsScrollTo)*(100/scrollBtnsSpeed_);
						$dragger.stop().animate({top: btnsScrollTo}, scrollSpeed,"linear");
					} else {
						var btnsScrollTo=0;
						var scrollSpeed=Math.abs($dragger.position().top-btnsScrollTo)*(100/scrollBtnsSpeed_);
						$dragger.stop().animate({top: -btnsScrollTo}, scrollSpeed,"linear");
					}
					clearInterval(btnsScrollTimer);
					btnsScrollTimer = setInterval( Scroll, 20);
				}
			
				function BtnsScrollStop(){
					clearInterval(btnsScrollTimer);
					$dragger.stop();
				}

	function Scroll(){
				var draggerY=$dragger.position().top;
				var targY=-draggerY*scrollAmount;
				var thePos=$customScrollBox_container.position().top-targY;
				$customScrollBox_container.stop().animate({top: "-="+thePos}, animSpeed_, easeType_);
	}
	
	function DraggerPress(){
		$dragger.addClass("dragger_pressed");
	}

	function DraggerRelease(){
		$dragger.removeClass("dragger_pressed");
	}
