/**
 * 
 * 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;
	}
};

jQuery.fn.jqScroll = function(options){
	
  return this.each(function(i){
		
		var trackHeight, buttonHeight, scrollHeight, contentHeight,
				increment = 50,
				scrolling = false,
				scrollTimer = null,
				pagecontent = jQuery(this),
				outer = jQuery('<div class="jqScrollWrap"/>'),
				inner = jQuery('<div class="innerscroll"/>'),
				scroller = jQuery('<div class="scroller" />'),
				scrollbar = jQuery('<div class="scrollbar" />').appendTo(scroller),
				scrollbot = jQuery('<div class="bottom" />').appendTo(scrollbar),
				track = jQuery('<div class="track" />').appendTo(scrollbot),
				button = jQuery('<div class="button" />').appendTo(track),
				up = jQuery('<div class="up" />').appendTo(scrollbot),
				down = jQuery('<div class="down" />').appendTo(scrollbot);
		
		pagecontent.wrap(outer);
		pagecontent.wrapInner(inner);
		pagecontent.after(scroller);

		initvars();
		
		pagecontent.mousewheel(function(objEvent, intDelta){
			if(intDelta > 0) scrollPage('up')
			else if (intDelta < 0) scrollPage('down');
		});
		
		jQuery.each([up,down],function(){
			jQuery(this).mousedown(function(){
				var button = jQuery(this);
				scrolling = true;
				scrollTimer = setInterval(function(){
					button.hasClass('up')? scrollPage('up', 10) : scrollPage('down', 10);
				}, 50);
			}).mouseup(clearScroll).click(clearScroll);
		});
		
		button.draggable({
			axis:'y',
			containment:'parent',
			drag: function(event, ui){ buttonScrollContent(ui.position.top); }
		});
		
		function clearScroll(){
			clearInterval(scrollTimer);
			scrolling = false;
			testheight();
		}
			
		function buttonScrollContent(buttonTop){
			var p = Math.round((buttonTop/(trackHeight-buttonHeight))*100);
			var diff = Math.round(((contentHeight - scrollHeight)*p)/100);
			pagecontent.scrollTop(diff);
		}
		
		function scrollPage(dir, inc){
			var i = inc || increment;
			var t = pagecontent.scrollTop();
			var p = dir=='up'? t-i : t+i;
			pagecontent.scrollTop(p);
			if(p <=0 || p >= contentHeight - scrollHeight) clearScroll();
			updateButton();
		}
		
		function updateButton(){
			var d = contentHeight - scrollHeight;
			var t = pagecontent.scrollTop();
			var p = Math.round((t/d)*100);
			var b = Math.round((trackHeight-buttonHeight)*(p/100));
			button.css({top:b});
		}
		
		function initvars(){
			scrollbar.show();
			var h = pagecontent.outerHeight(true);
			scrollbar.css({height: h});
			trackHeight = track.height();
			buttonHeight = button.height();
			scrollHeight = scrollbar.height();
			contentHeight = pagecontent[0].scrollHeight;
			pagecontent.scrollTop(0);
			if(contentHeight <= scrollHeight) scrollbar.hide();
		}
		
		function testheight(){
			if(contentHeight != pagecontent[0].scrollHeight) initvars();
		}

	});
	
}
