/* malihu custom scrollbar plugin - http://manos.malihu.gr */
(function ($) {
$.fn.mCustomScrollbar = function (scrollType,animSpeed,easeType,bottomSpace,draggerDimType,mouseWheelSupport,scrollBtnsSupport,scrollBtnsSpeed){
    var id = $(this).attr("id");
    var $customScrollBox=$("#"+id);
    var $customScrollBox_container=$("#"+id);
    var $customScrollBox_content=$("#"+id+"_content");
    var $dragger_container=$("#"+id+"_scrollContainer");
    var $dragger=$("#"+id+"_dragger");
    var $scrollUpBtn=$("#"+id+"_scrollUpBtn");
    var $scrollDownBtn=$("#"+id+"_scrollDownBtn");
    var $customScrollBox_horWrapper=$("#"+id+" .customScrollBox .horWrapper");
    
    //get & store minimum dragger height & width (defined in css)
    if(!$customScrollBox.data("minDraggerHeight")){
        $customScrollBox.data("minDraggerHeight",$dragger.outerHeight());
    }
    if(!$customScrollBox.data("minDraggerWidth")){
        $customScrollBox.data("minDraggerWidth",$dragger.width());
    }
    
    //get & store original content height & width
    if(!$customScrollBox.data("contentHeight")){
        $customScrollBox.data("contentHeight",$customScrollBox_container.outerHeight());
    }
    if(!$customScrollBox.data("contentWidth")){
        $customScrollBox.data("contentWidth",$customScrollBox_container.width());
    }
    
    CustomScroller();
    //alert($customScrollBox.attr("id"));
    function CustomScroller(reloadType){
        var visibleHeight=$customScrollBox.outerHeight();//alert(visibleHeight);
        
        if($customScrollBox_container.outerHeight()<$customScrollBox_content.outerHeight()){ //enable scrollbar if content is long
            $dragger.css("display","block");
            if(reloadType!="resize" && $customScrollBox_container.outerHeight()!=$customScrollBox.data("contentHeight")){
                $dragger.css("top",0);
                $customScrollBox_container.css("top",0);
                $customScrollBox.data("contentHeight",$customScrollBox_container.outerHeight());
            }
            $dragger_container.css("display","block");
            //$scrollDownBtn.css("display","inline-block");
            //$scrollUpBtn.css("display","inline-block");
            var totalContent=$customScrollBox_content.outerHeight();
            var minDraggerHeight=$customScrollBox.data("minDraggerHeight");
            var draggerContainerHeight=$dragger_container.outerHeight();
            var draggerHeight=$dragger.outerHeight();
            
            function AdjustDraggerHeight(){
                if(draggerDimType=="auto"){
                    //var adjDraggerHeight=Math.round(totalContent-((totalContent-visibleHeight)*1.3)); //adjust dragger height analogous to content
                    var perHeight = draggerContainerHeight / visibleHeight;
                    var marginContentHeight = (totalContent - visibleHeight) * perHeight;
                    var adjDraggerHeight=Math.round(draggerContainerHeight - marginContentHeight);
                    if(adjDraggerHeight<=minDraggerHeight){ //minimum dragger height
                       // $dragger.css("line-height",minDraggerHeight+"px");
                        var adjustHeight = minDraggerHeight;
                    } else if(adjDraggerHeight>=draggerContainerHeight){
                        //$dragger.css("line-height",draggerContainerHeight-10+"px");
                        var adjustHeight = draggerContainerHeight-10;
                    } else {
                       // $dragger.css("line-height",adjDraggerHeight+"px");
                        var adjustHeight = adjDraggerHeight;
                    }
                    draggerHeight=adjustHeight;
                    $dragger.animate({
	            		    height:adjustHeight
	          		  }, 1000, function() {
	          			
	          		  });
                }
            }
            AdjustDraggerHeight();
            //alert(draggerHeight);
            var targY=0;
            
            $dragger.draggable({ 
                axis: "y", 
                containment: "parent", 
                drag: function(event, ui) {
                    Scroll();
                    
                    if(parseInt($dragger.css("top"))<0){
                        $dragger.css("top", 0);
                        $customScrollBox_container.stop();
                        Scroll();
                    }
                    if(parseInt($dragger.css("top"))>$dragger_container.outerHeight()-$dragger.outerHeight()){
                        $dragger.css("top", $dragger_container.outerHeight()-$dragger.outerHeight());
                        $customScrollBox_container.stop();
                        Scroll();
                    }
                }
            });
            
            $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.outerHeight())){
                    var targetPos=mouseCoord+$dragger.outerHeight();
                    if(targetPos<$dragger_container.outerHeight()){
                        $dragger.css("top",mouseCoord);
                        Scroll();
                    } else {
                        $dragger.css("top",$dragger_container.outerHeight()-$dragger.outerHeight());
                        Scroll();
                    }
                }
            });

            //mousewheel
            $(function($) {
                if(mouseWheelSupport=="yes"){
                    $customScrollBox.unbind("mousewheel");
                    $customScrollBox.bind("mousewheel", function(event, delta) {
                    	var vel = delta*10;
                    	var nowTop = parseInt($dragger.css("top"));
                    	$dragger.css("top", nowTop-vel);
                    	Scroll();
                        if(parseInt($dragger.css("top"))<0){
                            $dragger.css("top", 0);
                            $customScrollBox_container.stop();
                            Scroll();
                        }
                        if(parseInt($dragger.css("top"))>$dragger_container.outerHeight()-$dragger.outerHeight()){
                            $dragger.css("top", $dragger_container.outerHeight()-$dragger.outerHeight());
                            $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;
            
                function BtnsScroll(dir){
                    if(dir=="down"){
                        var btnsScrollTo=$dragger_container.outerHeight()-$dragger.outerHeight();
                        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();
                }
            }
            
            //scroll
            //if(bottomSpace<1){
                bottomSpace=1; //minimum bottomSpace value is 1
            //}
            
            var scrollAmount=(totalContent-(visibleHeight/bottomSpace))/(draggerContainerHeight-draggerHeight);
            
            function Scroll(){
                var draggerY=parseInt($dragger.css("top"));
                var targY=0-(draggerY*scrollAmount);
                //var thePos=$customScrollBox_container.position().top-targY;
                $customScrollBox_content.stop().animate({top: targY}, animSpeed, easeType);
            }
        } 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");
            $scrollDownBtn.css("display","none");
            $scrollUpBtn.css("display","none");
        }
    }
};  
})(jQuery);
