(function($){
	$.fn.modal = function( options ) {
		var settings = {
			'width'					: 400,
			'height'				: 300,
			'background-opacity'	: .6,
			'background-color'		: '#000',
			'fade-time'				: 500
		};
		
		return this.each(function(){
			// Merge options
			if ( options ) {
				$.extend( settings, options );
			}
			
			// Do voodoo...
			$(this).click(function(e){
				e.preventDefault();
				// Get the target for later
				var targetUrl = $(this).attr('href');
				// Create background div
				var $background = $('<div></div>');
				// Apply CSS
				$background.css({
					'position': 'fixed',
					'width': $(window).width(),
					'height': $(window).height(),
					'background-color': settings['background-color'],
					'opacity': 0,
					'z-index': 9999
				});
				// Add div to the DOM
				$('body').prepend($background);
				// Show div with effects and continue the work
				$background.animate({opacity: settings['background-opacity']}, settings['fade-time'], function(){
					// Create the modal box
					var $modalBox = $('<div id="modal"></div');
					// Apply CSS
					$modalBox.css({
						'position' : 'absolute',
						'left': ($(window).width() / 2),
						'top': ($(window).height() / 2),
						'z-index': 99999
					});
					// Add to DOM
					$('body').prepend($modalBox);
					// Show
					$modalBox.animate({
							'left': ($(window).width() / 2) - (settings.width / 2),
							'top': ($(window).height() / 2) - (settings.height / 2),
							'width': settings.width,
							'height': settings.height
						}, settings['fade-time'], function(){
							$.get(targetUrl, function(data){
								$modalBox.html(data);
								$modalBox.tinyscrollbar();
							}, 'html');
					});
					// Close on click
					$background.click(function(e){
						e.preventDefault();
						$modalBox.remove();
						$background.remove();
					});
				});
			});
		});
	}
})(jQuery);
