(function($){  
	$.fn.scrolltastic = function(options) {
		var anchors;
		var timer;
		var mouseWheelActive;
		var defaults = {
			up: null,
			down: null,
			duration: "auto"
		};  
		var options = $.extend(defaults, options);
			
		var elem = this;
		
		var content = $(elem);
		var wrapper = content.parent();
		if(wrapper.css("position") == "static") {
			wrapper.css("position", "relative");
		}
		content.css("position", "relative");
		var wrapperHeight = wrapper.height();
		var contentHeight = content.height();
	
		content.children(":last-child").each(function(){
			if($(this).css("margin-top") != "") {
				var margin = $(this).css("margin-top");
				var marginArr = margin.split("p");
				contentHeight = contentHeight + parseInt(marginArr[0]);
			}
		});

		if((options.up != null && options.down == null) || (options.up == null && options.down != null)) {
			alert('Scrolltastic: If you\'re going to assign anchors, you MUST asign both "up" and "down" anchors. Not just one of them.');
		} else if(options.up != null && options.down != null) {
			$("#" + options.up).mousedown(function() {
				upMouseDown();
			});

			$("#" + options.up).mouseup(function() {
				mouseUp();
			});
		
			$("#" + options.down).mousedown(function() {
				downMouseDown();
			});

			$("#" + options.down).mouseup(function() {
				mouseUp();
			});

			$("#" + options.down + ", #" + options.up).click(function(){
				return false;
			});
			anchors = true;
		} else {
			anchors = false;
		}

		content.mousewheel(function(event, delta, deltaX, deltaY) {
			if(mouseWheelActive == null) {
				$.clear(timer);
				if(delta > 0) {
					upMouseDown(content);
				} else {
					downMouseDown(content);
				}
				$.timeout(mouseUp, 100);
				mouseWheelActive = true;
			}
			return false;
		});

		function upMouseDown() {
			if(options.duration == "auto") {
				var dis = $(content).position().top * -1;
				var dur = dis * 3;
			}
			$(content).stop().animate({
				top: "0px"
			}, dur, "linear");
		}
		
		function downMouseDown() {
			if(options.duration == "auto") {
				var dis = ($(content).position().top + contentHeight) - wrapperHeight;
				var dur = dis * 3;
			}
			$(content).stop().animate({
				top: (contentHeight - wrapperHeight) * -1
			}, dur, "linear");
		}

		function mouseUp() {
			content.stop();
			var top = $(content).position().top;
			if(anchors) {
				if(top * -1 == contentHeight - wrapperHeight) {
					$("#" + options.down).hide();
					$("#" + options.down + "_inactive").show();
				} else {
					$("#" + options.down + "_inactive").hide();
					$("#" + options.down).show();
				}
				if(top == 0) {
					$("#" + options.up).hide();
					$("#" + options.up + "_inactive").show();
				} else {
					$("#" + options.up + "_inactive").hide();
					$("#" + options.up).show();
				}
			}
			mouseWheelActive = null;
		}

	};
})(jQuery);


