/**
 * 
 * credits for this plugin go to brandonaaron.net
 * 	
 * unfortunately his site is down
 * 
 * @param {Object} up
 * @param {Object} down
 * @param {Object} preventDefault
 */
jQuery.fn.extend({
	mousewheel: function(up, down, preventDefault) {
		return this.hover(
			function() {
				jQuery.event.mousewheel.giveFocus(this, up, down, preventDefault);
			},
			function() {
				jQuery.event.mousewheel.removeFocus(this);
			}
		);
	},
	mousewheeldown: function(fn, preventDefault) {
		return this.mousewheel(function(){}, fn, preventDefault);
	},
	mousewheelup: function(fn, preventDefault) {
		return this.mousewheel(fn, function(){}, preventDefault);
	},
	unmousewheel: function() {
		return this.each(function() {
			jQuery(this).unmouseover().unmouseout();
			jQuery.event.mousewheel.removeFocus(this);
		});
	},
	unmousewheeldown: jQuery.fn.unmousewheel,
	unmousewheelup: jQuery.fn.unmousewheel
});


jQuery.event.mousewheel = {
	giveFocus: function(el, up, down, preventDefault) {
		if (el._handleMousewheel) jQuery(el).unmousewheel();
		
		if (preventDefault == window.undefined && down && down.constructor != Function) {
			preventDefault = down;
			down = null;
		}
		
		el._handleMousewheel = function(event) {
			if (!event) event = window.event;
			if (preventDefault)
				if (event.preventDefault) event.preventDefault();
				else event.returnValue = false;
			var delta = 0;
			if (event.wheelDelta) {
				delta = event.wheelDelta/120;
				if (window.opera) delta = -delta;
			} else if (event.detail) {
				delta = -event.detail/3;
			}
			if (up && (delta > 0 || !down))
				up.apply(el, [event, delta]);
			else if (down && delta < 0)
				down.apply(el, [event, delta]);
		};
		
		if (window.addEventListener)
			window.addEventListener('DOMMouseScroll', el._handleMousewheel, false);
		window.onmousewheel = document.onmousewheel = el._handleMousewheel;
	},
	
	removeFocus: function(el) {
		if (!el._handleMousewheel) return;
		
		if (window.removeEventListener)
			window.removeEventListener('DOMMouseScroll', el._handleMousewheel, false);
		window.onmousewheel = document.onmousewheel = null;
		el._handleMousewheel = null;
	}
};
(function($) {
	$.fn.jScrollbar= function(op) {
        var defaults = {
			scrollStep : 10,
			allowMouseWheel : true
        };
		
		if(this.length>0)
		return this.each(function() {
			
			/*
			// Vars
			*/
			var 
				$this = $(this),
				opts = $.extend(defaults, op),
				js_mask = $this.find('.jScrollbar_mask'),
				js_drag = $this.find('.jScrollbar_draggable a.draggable'),
				js_Parentdrag = $this.find('.jScrollbar_draggable'),
				diff = parseInt(js_mask.innerHeight()) - parseInt($this.height());
			
			/** if mask container is heighter than the main container **/
			if(diff > 0)
			{
				js_Parentdrag.show();
				var pxDraggable = parseInt(js_Parentdrag.height()) - parseInt(js_drag.height());;
				var pxUpWhenScrollMove = opts.scrollStep;
				var pxUpWhenMaskMove = pxUpWhenScrollMove * (diff/pxDraggable);
				
				js_drag
				.click(function(e){e.preventDefault();})
				.draggable({
					axis:'y',
					containment: js_Parentdrag,
					scroll: false,
					drag: function(event, ui){
						js_mask.css('top','-'+(ui.position.top * (diff/pxDraggable))+'px');
					}
				});
				
				
				/** if mousewheel allowed **/
				if(opts.allowMouseWheel)
				$this.mousewheel(function(objEvent, intDelta) {
					// mousewheel up (first if)  and mousewheel down (second if)
					if (intDelta > 0 && parseInt(js_mask.css('top')) < 0){
						js_drag.stop(true, true).animate({top:'-='+pxUpWhenScrollMove+'px'}, 100);
						js_mask.stop(true, true).animate({top:'+='+pxUpWhenMaskMove+'px'},100,function(){
							RelativeTop = parseInt(js_mask.css('top'));
							if(RelativeTop > 0 ) {
								js_drag.animate({top:'0px'},150);
								js_mask.css({top:0});
							}
						});
					}
					else if (intDelta < 00 && parseInt(js_mask.css('top')) > -diff) {
						js_drag.stop(true, true).animate({top:'+='+pxUpWhenScrollMove+'px'}, 100);
						js_mask.stop(true, true).animate({top:'-='+pxUpWhenMaskMove+'px'},100,function(){
							RelativeTop = parseInt(js_mask.css('top'));
							if(RelativeTop < -diff)
							{
								js_mask.css({top:-diff});
								js_drag.animate({top:pxDraggable},150);
							}
						});
					}
				});
			}
		});

	}
})(jQuery);
