﻿(function($) {

    $.fn.smoothmouseweel = function(method) {
		
        var methods = {

            init : function(options) {
                $(this).smoothmouseweel.settings = $.extend({}, $(this).smoothmouseweel.defaults, options);
				
				return $(this).bind('mousewheel', function(event, delta) {
					if( $(event.target).parents('.scroll').length == 0) {
						var currentY =  $(this).scrollTop();
						var height = helpers.getHeight() - $(window).height();
						var targetY = currentY + -delta * $(this).smoothmouseweel.settings.range;
						targetY = (targetY < 0) ? 0 : (targetY > height) ? height : targetY;
						$(this).stop(true).animate( { scrollTop : targetY }, $(this).smoothmouseweel.settings.duration, $(this).smoothmouseweel.settings.easing);
						event.preventDefault();
						event.stopPropagation();
						return false;
					}
				});

            },

            // a public method. for demonstration purposes only - remove it!
            stop: function(options) {
                // code goes here
				$(this).stop(true);
            }

        }
		
        var helpers = {
            getHeight: function() {
				var D = document;
				return Math.max(Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), Math.max(D.body.clientHeight, D.documentElement.clientHeight));
            }
        }
		
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error( 'Method "' +  method + '" does not exist in smoothmouseweel plugin!');
        }

    }

    $.fn.smoothmouseweel.defaults = {
		range    : 180,
		duration : 680,
		//easing   : 'easeOutCirc'
		easing   : 'easeOutCubic'
    }

    $.fn.smoothmouseweel.settings = {}

})(jQuery);
