(function($){
	$.fn.extend({
		slider: function(opts){
			opts = $.extend({
				scroll_content: '.scroll_content',
				drag_selector: '.scroller_bar',
				up_button: '.scroller_up',
				down_button: '.scroller_down',
				increment: 50
			}, opts);
			return this.each(function(i){
				// scrolling fn
				var container = $(this);
				var handle = $(opts.drag_selector, container).css({position: 'absolute', padding: '0 5px', left: '-5px'});
				var scroll_content = $(opts.scroll_content, container);
				var down_button = $(opts.down_button, container);
				var up_button = $(opts.up_button, container);
				var scale = 0;
				var limit = handle.parent().height() - handle.height();				
				scale = - ((scroll_content.height() - scroll_content.parent().height()) / limit);
				if (scale < 0) {
					var move = function(x,y) {
						if (typeof(y)=='undefined') {
							y = x;
						}
						if (y >= 0 && scale < 0 && y <= limit) {
							scroll_content.css({"top": y * scale });
						}
					}
					handle.parent().css({cursor: 'pointer', display: 'block'}).bind('click.scroll', function(e){
						var offset = e.pageY - ($(this).offset()).top - handle.height()/2;
						var y = offset < 0 ? 0 : offset > limit ? limit : offset;
						move(y);
						handle.css('top', y);
					});
					down_button.bind('click.scroll', function(e){
						var y = parseInt(handle.css('top'),10) ?  parseInt(handle.css('top'),10) + opts.increment : opts.increment;
						if (y < limit) {
							move(y);
							handle.css('top', y + opts.increment);
						}
						else {
							move(limit);
							handle.css('top', limit);
						}
					});
					up_button.bind('click.scroll', function(e){
						var y = parseInt(handle.css('top'),10) ?  -parseInt(handle.css('top'),10) + opts.increment : -opts.increment;
	
						if (parseInt(handle.css('top'),10) - opts.increment > 0) {
							move(parseInt(handle.css('top'),10) - opts.increment);
							handle.css('top', parseInt(handle.css('top'),10) - opts.increment);
						}
						else {
							move(0);
							handle.css({top: 0});
						}
					});
					var reset_drag = function(){
						handle.DraggableDestroy(); // ie bug fix
						handle.Draggable({
							axis: 'vertically',
							containment: 'parent',
							onDrag: move,
							onStop: function(){
								reset_drag();
							}
						});
					}
					reset_drag();
				}
			});
		}
	});
	
	$(document).ready(function(){
		$(".slider").slider();
	});
	
})(jQuery);
