

(function($){
		
	jQuery.fn.scroll_me = function(options){
	
		var settings = {
			'left_image':false,
			'right_image':false,
			'orientation':'vertical',
			'number_displayed':false,
			'width':false,
			'height':200,
			'scrollbar':true,
			'scrollbar_position':'right',
			'scrollbar_width':12
		};
		
		this.each(function(){
			if(options){
				$.extend(settings,options);
			}
		
			if(settings['number_displayed'])
				var num_items = $(this).children().length;
			var total_height = $(this).height()+40;
			$(this).height(settings['height']);
			var total_width = $(this).width();
			var handle_length = 30;
			var top_position = $(this).position().top;
			//console.log(total_height+' - '+settings['height']);
			if(settings['orientation']=='vertical' && total_height>settings['height']){
				var content = $(this).html();
				$(this).html('<div class="scroller"><div class="handle"></div></div><div class="scroll_container">'+content+'</div>');
				if(settings['height'])
					$(this).css({'height':settings['height']});
				if(settings['scrollbar_position']=='left' || settings['scrollbar_position']=='right'){
					scale=1;
					if(total_height>settings['height'])
						scale = total_height/settings['height'];
					handle_length = Math.floor(settings['height']-(total_height-settings['height'])/scale);
					
					//console.log(scale);
					$(this).children('.scroll_container').width(total_width-settings['scrollbar_width']).css({'position':'relative','z-index':90,'float':settings['scrollbar_position']});
					$(this).children('.scroller').width(settings['scrollbar_width']).height(settings['height']).css({'position':'relative','z-index':100,'float':settings['scrollbar_position']});
					$(this).children('.scroller').children('.handle').width(settings['scrollbar_width']).height(handle_length).css({'position':'relative','z-index':100,'float':settings['scrollbar_position']}).draggable(
						{
							axis:'y',
							containment:'parent',
							
							drag: function(){
								
								scale = ($(this).parent().siblings('.scroll_container').height()+40)/settings['height'];
								var margin_top = -$(this).position().top*scale;
								//console.log(margin_top);
								$(this).parent('.scroller').siblings('.scroll_container').css({'margin-top':margin_top});
							}
						});
				}
			}
		});
		
	}
})(jQuery);
