/**
 * @author landon
 */
jQuery(function(){
    var defaults = {
        deadspace: 20
    };
    jQuery.fn.extend({
        mouseScroll: function(opts){
            opts = jQuery.extend(defaults, opts || {})
            jQuery(this).each(function(){
				if (jQuery.fn._mouseScrollInit) {
					jQuery.fn._mouseScrollInit.apply(this, new Array(opts));
				}
            });
        },
        _mouseScrollInit: function(opts){
            if (jQuery(this).children().filter(".mouseScroll").length == 0) {
                var ch = jQuery(this).css({
                    overflow: 'hidden',
                    position: 'relative'
                }).wrapInner(jQuery("<div class=\"mouseScroll\"></div>")).children(0).css({
                    left: '0',
					position:'relative'
                }).end().css('position', 'absolute');
                
                var containerHeight = jQuery(this).height();
                var dif = this.scrollHeight - containerHeight;
                var ratio = dif / (containerHeight - opts.deadspace * 2);
				jQuery(this).data('lastT',0);
                jQuery(this).mousemove(function(e){
                    var t = -ratio * (e.clientY - jQuery(this).offset().top - opts.deadspace);
                    t = t > 0 ? 0 : (t < -dif ? -dif : t);
					if (jQuery.fn._mouseScrollAnimate) {
						jQuery.fn._mouseScrollAnimate(jQuery(this).find(".mouseScroll"), t);
					}
                });
            }
        },
        _mouseScrollAnimate: function(obj,t,force) {
			var dif = obj.css('top').replace('px','')-t;
			if (obj.not(":animated") || t != obj.data('lastAnimT')) {
				if (Math.abs(dif) > 30) {
					obj.data('lastAnimT', t);
					obj.stop().animate({
						top: t + 'px'
					}, 500, 'swing');
				}
				else {
					if(Math.abs(dif) <10) obj.stop();
					obj.filter(":not(:animated)").css('top', t + 'px');
				}
			}
			obj.data('lastT',t);
        }
    });
});
