(function($) {

var methods = {
	init: function(options) {
		var settings = {
			'margin-right': '0'
		}
		
		return this.each(function() {
			var target = $(this);
			
			if (options) {
				$.extend(settings, options);
			}

			// remove the previous scrollbar
			target.removeClass('scrollbar');
			$('.scrollbar-guide', target).remove();
			$('.scrollbar-content :eq(0)', target).unwrap();
			
			target.addClass('scrollbar');
			target.wrapInner('<div class="scrollbar-content" />');
			var content = $('.scrollbar-content', this);
			if (content.height() < target.height()) {
				content.removeClass('scrollbar-content');
				target.removeClass('scrollbar');
				target.css('overflow-y', 'hidden');
				return;
			}
			
			target.append('<div class="scrollbar-guide"><div class="scrollbar-cursor"><div class="scrollbar-cursor-top" /><div class="scrollbar-cursor-middle" /><div class="scrollbar-cursor-bottom" /></div></div>');

			
			var guide = $('.scrollbar-guide', this);
			var cursor = $('.scrollbar-cursor', this);

			guide.css(settings);
			
			content.css('padding-right', Math.max(parseInt(content.css('padding-right')), Math.max(0, guide.width() - parseInt(target.css('padding-right')) - parseInt(content.css('margin-right')) - parseInt(content.css('border-right-width')))));
			
			var scrollHeight = target.innerHeight() - parseInt(target.css('padding-top')) - parseInt(target.css('padding-bottom'));
			var contentHeight = content.outerHeight() + parseInt(content.css('margin-top')) + parseInt(content.css('margin-bottom'));
			
			guide.css('top', parseInt(target.css('padding-top')));
			guide.height(scrollHeight);
			var guideHeight = guide.height();
			var guideWidth = guide.width();

			var cursorTopHeight = $('.scrollbar-cursor-top', this).height();
			var cursorBottomHeight = $('.scrollbar-cursor-bottom', this).height();
			cursor.height(Math.max(cursorTopHeight + cursorBottomHeight, (scrollHeight / contentHeight) * guideHeight));
			$('.scrollbar-cursor-middle', this).height(cursor.height() - cursorTopHeight - cursorBottomHeight);
			
			var cursorWidth = cursor.width();
			var cursorHeight = cursor.height();

			var marginTop = parseInt(content.css('margin-top'));
			var rect = guide.offset();

			cursor.draggable({containment: [rect.left, rect.top, rect.left + guideWidth - cursorWidth, rect.top + guideHeight - cursorHeight]});

			cursor.bind('drag', function() {
				var pos = cursor.position();
				content.css('margin-top', marginTop - ((contentHeight - scrollHeight) / (guideHeight - cursorHeight)) * pos.top);
			});

			var top = parseInt(content.css('margin-top'));
			target.mousewheel(function(event, delta) {
				// set content position
				top += 30 * delta;
				top = Math.max(scrollHeight - contentHeight + marginTop, Math.min(marginTop, top));
				content.css('margin-top', top);

				// set cursor position
				var cursorTop = ((guideHeight - cursorHeight) / (contentHeight - scrollHeight)) * (marginTop - top);
				cursorTop = Math.min(guideHeight - cursorHeight, Math.max(0, cursorTop));
				cursor.css('top', cursorTop);
			});
		});
	}
}

$.fn.scrollbar = function(method) {
	for (var i in arguments) {
		alert(i);
	}
	
	// Method calling logic
	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 on jQuery.tooltip');
	}
};

})(jQuery);
