/* malihu custom scrollbar plugin */
(function ($) {
$.fn.mCustomScrollbar = function (scrollType,animSpeed,easeType,bottomSpace,draggerDimType,mouseWheelSupport){
	var id = $(this).attr("id");
	var $scrollbox=$("#"+id+" .scrollbox");
	var $scrollbox_container=$("#"+id+" .scrollbox .container");
	var $scrollbox_content=$("#"+id+" .scrollbox .content");
	var $dragger_container=$("#"+id+" .dragger_container");
	var $dragger=$("#"+id+" .dragger");
	
	CustomScroller();
	
	function CustomScroller(){
		//horizontal scrolling ------------------------------
		if(scrollType=="horizontal"){
			var visibleWidth=$scrollbox.width();
			if($scrollbox_container.width()>visibleWidth){ //enable scrollbar if content is long
				$dragger.css("display","block");
				$dragger_container.css("display","block");
				var totalContent=$scrollbox_content.width();
				var minDraggerWidth=$dragger.width();
				var draggerContainerWidth=$dragger_container.width();
		
				function AdjustDraggerWidth(){
					if(draggerDimType=="auto"){
						var adjDraggerWidth=Math.round(totalContent-((totalContent-visibleWidth)*1.3)); //adjust dragger width analogous to content
						if(adjDraggerWidth<=minDraggerWidth){ //minimum dragger width
							$dragger.css("width",minDraggerWidth+"px");
						} else if(adjDraggerWidth>=draggerContainerWidth){
							$dragger.css("width",draggerContainerWidth-10+"px");
						} else {
							$dragger.css("width",adjDraggerWidth+"px");
						}
					}
				}
				AdjustDraggerWidth();
		
				var targX=0;
				var draggerWidth=$dragger.width();
				$dragger.draggable({ 
					axis: "x", 
					containment: "parent", 
					drag: function(event, ui) {
						ScrollX();
					}, 
					stop: function(event, ui) {
						DraggerRelease();
					}
				});
			
				$dragger_container.click(function(e) {
					var $this=$(this);
					var mouseCoord=(e.pageX - $this.offset().left);
					var targetPos=mouseCoord+$dragger.width();
					if(targetPos<$dragger_container.width()){
						$dragger.css("left",mouseCoord);
						ScrollX();
					} else {
						$dragger.css("left",$dragger_container.width()-$dragger.width());
						ScrollX();
					}
				});

				//mousewheel
				$(function($) {
					if(mouseWheelSupport=="yes"){
						$scrollbox.bind("mousewheel", function(event, delta) {
							var vel = Math.abs(delta*10);
							$dragger.css("left", $dragger.position().left-(delta*vel));
							ScrollX();
							if($dragger.position().left<0){
								$dragger.css("left", 0);
								$scrollbox_container.stop();
								ScrollX();
							}
							if($dragger.position().left>$dragger_container.width()-$dragger.width()){
								$dragger.css("left", $dragger_container.width()-$dragger.width());
								$scrollbox_container.stop();
								ScrollX();
							}
							return false;
						});
					}
				});

				//scroll
				var scrollAmount=(totalContent-visibleWidth)/(draggerContainerWidth-draggerWidth);
				function ScrollX(){
					var draggerX=$dragger.position().left;
					var targX=-draggerX*scrollAmount;
					var thePos=$scrollbox_container.position().left-targX;
					$scrollbox_container.stop().animate({left: "-="+thePos}, animSpeed, easeType);
				}
			} else { //disable scrollbar if content is short
				$dragger.css("left",0).css("display","none"); //reset content scroll
				$scrollbox_container.css("left",0);
				$dragger_container.css("display","none");
			}
		//vertical scrolling ------------------------------
		} else {
			var visibleHeight=$scrollbox.height();
			if($scrollbox_container.height()>visibleHeight){ //enable scrollbar if content is long
				$dragger.css("display","block");
				$dragger_container.css("display","block");
				var totalContent=$scrollbox_content.height();
				var minDraggerHeight=$dragger.height();
				var draggerContainerHeight=$dragger_container.height();
		
				function AdjustDraggerHeight(){
					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");
						}
					}
				}
				AdjustDraggerHeight();
		
				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);
					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"){
						$scrollbox.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);
								$scrollbox_container.stop();
								Scroll();
							}
							if($dragger.position().top>$dragger_container.height()-$dragger.height()){
								$dragger.css("top", $dragger_container.height()-$dragger.height());
								$scrollbox_container.stop();
								Scroll();
							}
							return false;
						});
					}
				});

				//scroll
				var scrollAmount=(totalContent-(visibleHeight/bottomSpace))/(draggerContainerHeight-draggerHeight);
				function Scroll(){
					var draggerY=$dragger.position().top;
					var targY=-draggerY*scrollAmount;
					var thePos=$scrollbox_container.position().top-targY;
					$scrollbox_container.stop().animate({top: "-="+thePos}, animSpeed, easeType);
				}
			} else { //disable scrollbar if content is short
				$dragger.css("top",0).css("display","none"); //reset content scroll
				$scrollbox_container.css("top",0);
				$dragger_container.css("display","none");
			}
		}
		
		$dragger.mouseup(function(){
			DraggerRelease();
		}).mousedown(function(){
			DraggerPress();
		});

		function DraggerPress(){
			$dragger.addClass("dragger_pressed");
		}

		function DraggerRelease(){
			$dragger.removeClass("dragger_pressed");
		}
	}
	
	$(window).resize(function() {
		if(scrollType=="horizontal"){
			if($dragger.position().left>$dragger_container.width()-$dragger.width()){
				$dragger.css("left", $dragger_container.width()-$dragger.width());
			}
		} else {
			if($dragger.position().top>$dragger_container.height()-$dragger.height()){
				$dragger.css("top", $dragger_container.height()-$dragger.height());
			}
		}
		if(mouseWheelSupport=="yes"){
			$scrollbox.unbind("mousewheel");
		}
		CustomScroller();
	});
};  
})(jQuery);
