/**
*	jQuery 1.6+
*	jQuery.mousewheel 3.0.4+
*	jQuery.easing 1.3+
*
**/		
(function($) {
    $.fn.extend({
        initscroll: function(options) {				
		var defaults = {  
			animSpeed: 1000, 
			easeType: "easeOutSine", 
			reloadType: "", 
			scrollBtnsSpeed: 40
		};
		var options = $.extend(defaults, options);
	var scroll = this;
	var ScrollBox= this.find(".scroll_wrap");
	var ScrollBox_container= this.find(".scroll_container");
	var ScrollBox_content= this.find(".scroll_content");
	var Dragger_container= this.find(".dragger_container");
	var Dragger= this.find(".dragger");
	var ScrollUpBtn= this.find(".scrollUpBtn");
	var ScrollDownBtn= this.find(".scrollDownBtn");
	
	//get & store minimum dragger height & width (defined in css)
	if(!ScrollBox.data("minDraggerHeight")){
		ScrollBox.data("minDraggerHeight",Dragger.height());
	}
	if(!ScrollBox.data("minDraggerWidth")){
		ScrollBox.data("minDraggerWidth",Dragger.width());
	}
	
	//get & store original content height & width
	if(!ScrollBox.data("contentHeight")){
		ScrollBox.data("contentHeight",ScrollBox_container.height());
	}
	if(!ScrollBox.data("contentWidth")){
		ScrollBox.data("contentWidth",ScrollBox_container.width());
	}
	
	CustomScroller(options.reloadType);
	
	function CustomScroller(reloadType){
		//vertical scrolling ------------------------------		 
			var visibleHeight=ScrollBox.height();
			if(ScrollBox_container.height()>visibleHeight){ //enable scrollbar if content is long
				scroll.addClass('Scrolled');
				Dragger.css("display","block");
				if(reloadType!="resize" && ScrollBox_container.height()!=ScrollBox.data("contentHeight")){
					Dragger.css("top",0);
					ScrollBox_container.css("top",0);
					ScrollBox.data("contentHeight",ScrollBox_container.height());
				}
				Dragger_container.css("display","block");
				ScrollDownBtn.css("display","inline-block");
				ScrollUpBtn.css("display","inline-block");
				var totalContent=ScrollBox_content.height();
				var minDraggerHeight=ScrollBox.data("minDraggerHeight");
				var draggerContainerHeight=Dragger_container.height();
		
				function AdjustDraggerHeight(){
						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);
					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($) {
						ScrollBox.unbind("mousewheel");
						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 buttons
					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.height()-Dragger.height();
							var scrollSpeed=Math.abs(Dragger.position().top-btnsScrollTo)*(100/options.scrollBtnsSpeed);
							Dragger.stop().animate({top: btnsScrollTo}, scrollSpeed,"linear");
						} else {
							var btnsScrollTo=0;
							var scrollSpeed=Math.abs(Dragger.position().top-btnsScrollTo)*(100/options.scrollBtnsSpeed);
							Dragger.stop().animate({top: -btnsScrollTo}, scrollSpeed,"linear");
						}
						clearInterval(btnsScrollTimer);
						btnsScrollTimer = setInterval( Scroll, 20);
					}
				
					function BtnsScrollStop(){
						clearInterval(btnsScrollTimer);
						Dragger.stop();
					}
				
				//scroll
				var scrollAmount=(totalContent-visibleHeight)/(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}, options.animSpeed, options.easeType);
					
							if(Dragger.position().top<=0){
								ScrollUpBtn.addClass('disable');
								ScrollDownBtn.removeClass('disable');
							}else{ ScrollUpBtn.removeClass('disable'); }
							if(Dragger.position().top>=Dragger_container.height()-Dragger.height()){
								ScrollDownBtn.addClass('disable');
								ScrollUpBtn.removeClass('disable');
							}else{ ScrollDownBtn.removeClass('disable'); }
				}
				
				Scroll();
			} else { //disable scrollbar if content is short
				scroll.removeClass('Scrolled');
				ScrollBox.unbind("mousewheel");
				Dragger.css("top",0).css("display","none"); //reset content scroll
				ScrollBox_container.css("top",0);
				Dragger_container.css("display","none");
				ScrollDownBtn.css("display","none");
				ScrollUpBtn.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(Dragger.position().top>Dragger_container.height()-Dragger.height()){
				Dragger.css("top", Dragger_container.height()-Dragger.height());
			}
		//CustomScroller("resize");
	});
},
        stopscroll: function() {
        	this.unbind("mousewheel");
        }		

		
		
    });
})(jQuery);
