/* malihu custom scrollbar plugin - http://manos.malihu.gr */
(function (jQuery) {
jQuery.fn.mCustomScrollbar = function (scrollType,animSpeed,easeType,bottomSpace,draggerDimType,mouseWheelSupport,scrollBtnsSupport,scrollBtnsSpeed){
	var id = jQuery(this).attr("id");
	var jQuerycustomScrollBox=jQuery("#"+id+" .customScrollBox");
	var jQuerycustomScrollBox_container=jQuery("#"+id+" .customScrollBox .container");
	var jQuerycustomScrollBox_content=jQuery("#"+id+" .customScrollBox .content");
	var jQuerydragger_container=jQuery("#"+id+" .dragger_container");
	var jQuerydragger=jQuery("#"+id+" .dragger");
	var jQueryscrollUpBtn=jQuery("#"+id+" .scrollUpBtn");
	var jQueryscrollDownBtn=jQuery("#"+id+" .scrollDownBtn");
	var jQuerycustomScrollBox_horWrapper=jQuery("#"+id+" .customScrollBox .horWrapper");
	
	//get & store minimum dragger height & width (defined in css)
	if(!jQuerycustomScrollBox.data("minDraggerHeight")){
		jQuerycustomScrollBox.data("minDraggerHeight",jQuerydragger.height());
	}
	if(!jQuerycustomScrollBox.data("minDraggerWidth")){
		jQuerycustomScrollBox.data("minDraggerWidth",jQuerydragger.width());
	}
	
	//get & store original content height & width
	if(!jQuerycustomScrollBox.data("contentHeight")){
		jQuerycustomScrollBox.data("contentHeight",jQuerycustomScrollBox_container.height());
	}
	if(!jQuerycustomScrollBox.data("contentWidth")){
		jQuerycustomScrollBox.data("contentWidth",jQuerycustomScrollBox_container.width());
	}
	
	CustomScroller();
	
	function CustomScroller(reloadType){
		//horizontal scrolling ------------------------------
		if(scrollType=="horizontal"){
			var visibleWidth=jQuerycustomScrollBox.width();
			//set content width automatically
			jQuerycustomScrollBox_horWrapper.css("width",999999); //set a rediculously high width value ;)
			jQuerycustomScrollBox.data("totalContent",jQuerycustomScrollBox_container.width()); //get inline div width
			jQuerycustomScrollBox_horWrapper.css("width",jQuerycustomScrollBox.data("totalContent")); //set back the proper content width value
			
			if(jQuerycustomScrollBox_container.width()>visibleWidth){ //enable scrollbar if content is long
				jQuerydragger.css("display","block");
				if(reloadType!="resize" && jQuerycustomScrollBox_container.width()!=jQuerycustomScrollBox.data("contentWidth")){
					jQuerydragger.css("left",0);
					jQuerycustomScrollBox_container.css("left",0);
					jQuerycustomScrollBox.data("contentWidth",jQuerycustomScrollBox_container.width());
				}
				jQuerydragger_container.css("display","block");
				jQueryscrollDownBtn.css("display","inline-block");
				jQueryscrollUpBtn.css("display","inline-block");
				var totalContent=jQuerycustomScrollBox_content.width();
				var minDraggerWidth=jQuerycustomScrollBox.data("minDraggerWidth");
				var draggerContainerWidth=jQuerydragger_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
							jQuerydragger.css("width",minDraggerWidth+"px");
						} else if(adjDraggerWidth>=draggerContainerWidth){
							jQuerydragger.css("width",draggerContainerWidth-10+"px");
						} else {
							jQuerydragger.css("width",adjDraggerWidth+"px");
						}
					}
				}
				AdjustDraggerWidth();
		
				var targX=0;
				var draggerWidth=jQuerydragger.width();
				jQuerydragger.draggable({ 
					axis: "x", 
					containment: "parent", 
					drag: function(event, ui) {
						ScrollX();
					}, 
					stop: function(event, ui) {
						DraggerRelease();
					}
				});
			
				jQuerydragger_container.click(function(e) {
					var jQuerythis=jQuery(this);
					var mouseCoord=(e.pageX - jQuerythis.offset().left);
					if(mouseCoord<jQuerydragger.position().left || mouseCoord>(jQuerydragger.position().left+jQuerydragger.width())){
						var targetPos=mouseCoord+jQuerydragger.width();
						if(targetPos<jQuerydragger_container.width()){
							jQuerydragger.css("left",mouseCoord);
							ScrollX();
						} else {
							jQuerydragger.css("left",jQuerydragger_container.width()-jQuerydragger.width());
							ScrollX();
						}
					}
				});

	
				


				//scroll
				var scrollAmount=(totalContent-visibleWidth)/(draggerContainerWidth-draggerWidth);
				function ScrollX(){
					var draggerX=jQuerydragger.position().left;
					var targX=-draggerX*scrollAmount;
					var thePos=jQuerycustomScrollBox_container.position().left-targX;
					jQuerycustomScrollBox_container.stop().animate({left: "-="+thePos}, animSpeed, easeType);
				}
			} else { //disable scrollbar if content is short
				jQuerydragger.css("left",0).css("display","none"); //reset content scroll
				jQuerycustomScrollBox_container.css("left",0);
				jQuerydragger_container.css("display","none");
				jQueryscrollDownBtn.css("display","none");
				jQueryscrollUpBtn.css("display","none");
			}
		//vertical scrolling ------------------------------
		} else {
			var visibleHeight=jQuerycustomScrollBox.height();
			if(jQuerycustomScrollBox_container.height()>visibleHeight){ //enable scrollbar if content is long
				jQuerydragger.css("display","block");
				if(reloadType!="resize" && jQuerycustomScrollBox_container.height()!=jQuerycustomScrollBox.data("contentHeight")){
					jQuerydragger.css("top",0);
					jQuerycustomScrollBox_container.css("top",0);
					jQuerycustomScrollBox.data("contentHeight",jQuerycustomScrollBox_container.height());
				}
				jQuerydragger_container.css("display","block");
				jQueryscrollDownBtn.css("display","inline-block");
				jQueryscrollUpBtn.css("display","inline-block");
				var totalContent=jQuerycustomScrollBox_content.height();
				var minDraggerHeight=jQuerycustomScrollBox.data("minDraggerHeight");
				var draggerContainerHeight=jQuerydragger_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
							jQuerydragger.css("height",minDraggerHeight+"px").css("line-height",minDraggerHeight+"px");
						} else if(adjDraggerHeight>=draggerContainerHeight){
							jQuerydragger.css("height",draggerContainerHeight-10+"px").css("line-height",draggerContainerHeight-10+"px");
						} else {
							jQuerydragger.css("height",adjDraggerHeight+"px").css("line-height",adjDraggerHeight+"px");
						}
					}
				}
				AdjustDraggerHeight();
		
				var targY=0;
				var draggerHeight=jQuerydragger.height();
				jQuerydragger.draggable({ 
					axis: "y", 
					containment: "parent", 
					drag: function(event, ui) {
						Scroll();
					}, 
					stop: function(event, ui) {
						DraggerRelease();
					}
				});
				
				jQuerydragger_container.click(function(e) {
					var jQuerythis=jQuery(this);
					var mouseCoord=(e.pageY - jQuerythis.offset().top);
					if(mouseCoord<jQuerydragger.position().top || mouseCoord>(jQuerydragger.position().top+jQuerydragger.height())){
						var targetPos=mouseCoord+jQuerydragger.height();
						if(targetPos<jQuerydragger_container.height()){
							jQuerydragger.css("top",mouseCoord);
							Scroll();
						} else {
							jQuerydragger.css("top",jQuerydragger_container.height()-jQuerydragger.height());
							Scroll();
						}
					}
				});

				//mousewheel
				jQuery(function(jQuery) {
					if(mouseWheelSupport=="yes"){
						jQuerycustomScrollBox.unbind("mousewheel");
						jQuerycustomScrollBox.bind("mousewheel", function(event, delta) {
							var vel = Math.abs(delta*10);
							jQuerydragger.css("top", jQuerydragger.position().top-(delta*vel));
							Scroll();
							if(jQuerydragger.position().top<0){
								jQuerydragger.css("top", 0);
								jQuerycustomScrollBox_container.stop();
								Scroll();
							}
							if(jQuerydragger.position().top>jQuerydragger_container.height()-jQuerydragger.height()){
								jQuerydragger.css("top", jQuerydragger_container.height()-jQuerydragger.height());
								jQuerycustomScrollBox_container.stop();
								Scroll();
							}
							return false;
						});
					}
				});

	
				
				//scroll
				if(bottomSpace<1){
					bottomSpace=1; //minimum bottomSpace value is 1
				}
				var scrollAmount=(totalContent-(visibleHeight/bottomSpace))/(draggerContainerHeight-draggerHeight);
				function Scroll(){
					var draggerY=jQuerydragger.position().top;
					var targY=-draggerY*scrollAmount;
					var thePos=jQuerycustomScrollBox_container.position().top-targY;
					jQuerycustomScrollBox_container.stop().animate({top: "-="+thePos}, animSpeed, easeType);
				}
			} else { //disable scrollbar if content is short
				jQuerydragger.css("top",0).css("display","none"); //reset content scroll
				jQuerycustomScrollBox_container.css("top",0);
				jQuerydragger_container.css("display","none");
				jQueryscrollDownBtn.css("display","none");
				jQueryscrollUpBtn.css("display","none");
			}
		}
		
		jQuerydragger.mouseup(function(){
			DraggerRelease();
		}).mousedown(function(){
			DraggerPress();
		});

		function DraggerPress(){
			jQuerydragger.addClass("dragger_pressed");
		}

		function DraggerRelease(){
			jQuerydragger.removeClass("dragger_pressed");
		}
	}
	
	jQuery(window).resize(function() {
		if(scrollType=="horizontal"){
			if(jQuerydragger.position().left>jQuerydragger_container.width()-jQuerydragger.width()){
				jQuerydragger.css("left", jQuerydragger_container.width()-jQuerydragger.width());
			}
		} else {
			if(jQuerydragger.position().top>jQuerydragger_container.height()-jQuerydragger.height()){
				jQuerydragger.css("top", jQuerydragger_container.height()-jQuerydragger.height());
			}
		}
		CustomScroller("resize");
	});
};  
})(jQuery);
