/**
 * @author Barraq
 */

(function($){
 
$.fn.simpleScroll = function(options) {  
	
	// Default parameters
	var defaults = {  
		mouseWheelSpeed: 10,
		up: "",
		down: ""
	};  
		
	// Append custom parameters
	var options = $.extend(defaults, options);  
				
	return this.each(function() {
		
		// declare var		
		var $this = $(this);
		var $currentScrollPos = 0;
		var $currentScrollInc = 0;
		var $currentScrollButton;		// #up, #down
		var $currentScrollInterval;		// setInterval, clearInterval
		var $currentScrollDirection;	// -1:up, 1:down
		
		// declare function
		var whileScrollButtonDown = function() {
			if( $currentScrollInc > 4 || $currentScrollInc%4==0 ) {
				$currentScrollPos = $this.scrollTop()+(options.mouseWheelSpeed*$currentScrollDirection);
				$currentScrollPos = $currentScrollPos <= 0 ? 0 : Math.min( $currentScrollPos, $this[0].scrollHeight );
				$this.scrollTo(eval(Math.ceil($currentScrollPos)));
			}
			$currentScrollInc++;
		};
		
		var onArrowMouseUp = function(event) {
			clearInterval($currentScrollInterval);
		};
		
		var onArrowMouseDown = function() {
			$currentScrollInc = 0;
			whileScrollButtonDown();
			$currentScrollInterval = setInterval(whileScrollButtonDown, 100);
		};
		
		// Switch the element's overflow to hidden to ensure we get the size of the element without the scrollbars [http://plugins.jquery.com/node/1208]
		$this.css('overflow', 'hidden');
		
		// Create container
		$this.wrap("<div></div>");
		var $container = $this.parent();
		$container.append(
			$('<div></div>')
				.attr({'className':'scroll'})
				.append(
					$("<ul></ul>")
						.append(
							$('<li></li>')
								.append(
									$('<a></a>')
										.attr({'href':'javascript:;', 'id':'up'})
										.append(options.up)
										.mouseup(function() {
												onArrowMouseUp();
										})
										.mousedown(function(){
												$currentScrollButton = $(this);
												$currentScrollDirection = -1;
												onArrowMouseDown();
										})
								)
							,
							$('<li></li>')
								.append(
									$('<a></a>')
										.attr({'href':'javascript:;', 'id':'down'})
										.append(options.down)
										.mouseup(function() {
												onArrowMouseUp();
										})
										.mousedown(function(){
												$currentScrollButton = $(this);
												$currentScrollDirection =1;
												onArrowMouseDown();
										})
								)
						)
				)
		);
		$container.attr({'className':'simpleScroll'});
		
		// set up mousewheel
		//// method 1
		$this.mousewheel(function(event, delta) {
			//$currentScrollInc = 0;
			$currentScrollDirection = -delta;
			whileScrollButtonDown();
		});
		
		//// method 2
		/*
		$this.mousewheel(function(event, delta) {
			$currentScrollPos = $this.scrollTop()+(options.mouseWheelSpeed*(-delta));
			$currentScrollPos = $currentScrollPos <= 0 ? 0 : Math.min( $currentScrollPos, $this[0].scrollHeight );
			$this.scrollTo(eval(Math.ceil($currentScrollPos)));	
		});
		*/
		//$('#content').mousewheel(function(event, delta) {
		//	if (delta > 0) 
		//		$("#content").scrollTo("-="+eval(Math.ceil(Math.abs(delta*10))));
		//	else if (delta < 0) 
		//		$("#content").scrollTo("+="+eval(Math.ceil(Math.abs(delta*10))));
		//});
				
		//alert($container.html());
	});
};
    
})(jQuery); 

