(function($){
	var methods = {
		create : function(options) {
			var settings = {'html': '<p>NONE</p>', 'text': 'TEXT', 'boxWidth': '400', 'boxHeight': '250', 'maskBG': 'black', 'closeButton': true};
			if(options){
				$.extend(settings, options);
			}
			var windowHeight = $(window).height();
			var windowWidth = $(window).width();
			var documentHeight = $(document).height();
			var documentWidth = $(document).width();
			var boxWidth = +options['boxWidth'] > 0 ? options['boxWidth'] : 400;
			var boxHeight = +options['boxHeight'] > 0 ? options['boxHeight'] : 250;
			maskHeight = documentHeight;
			if(maskHeight < windowHeight)
				maskHeight = windowHeight;
			maskWidth = documentWidth;
			if(maskWidth < documentWidth)
				maskWidth = documentWidth;
			var	boxOffsetLeft = windowWidth / 2 - boxWidth / 2;
			var	boxOffsetTop = windowHeight / 2 - boxHeight / 2 + $(window).scrollTop();
			if(boxHeight > windowHeight)
				boxOffsetTop = $(window).scrollTop();
			if(boxWidth > windowWidth)
				boxOffsetLeft = 0;
			var maskBG = options['maskBG'] ? options['maskBG'] : 'black';
			var closeButton = options['closeButton'] === false ? false : true;
			var $mask = $('<div>', {id: 'MASK'})
			.css({textAlign: 'center', 
				position: 'absolute', 
				cursor: 'crosshair', 
				width: '100%', 
				height: '100%', 
				left: '0', 
				top: '0', 
				zIndex: '1000', 
				backgroundColor: maskBG, 
				position: 'fixed'})
			.hide()
			.fadeTo(200, .5);

			var $box = $('<div>', {id: 'MODAL'})
			.css({width: boxWidth, 
				height: boxHeight, 
				left: boxOffsetLeft, 
				top: boxOffsetTop, 
				backgroundColor: 'white',
				zIndex: '1000', 
				position: 'absolute'})
			.hide()
			.fadeIn(1000)
			.append(
				$('<div>', {id: 'MODAL-CONT'}).css({padding: 24})
				.html(this.modal('makeAsContent', {text: options['text'], html: options['html'], boxWidth: boxWidth, boxHeight: boxHeight}))
			);
			if(closeButton) {
				$box.prepend(
					$('<div>', {id: 'MODAL-CLOSE'})
					.css({position: 'absolute', 
						top: '.5em', 
						right: '0', 
						cursor: 'pointer', 
						color: 'red'})
					.text('Χ')
					.click(function(){$('body').modal('remove');})
				);
			}
			$('body').append($mask);
			$('body').append($box);
		},
		makeAsContent : function(options) {
			var settings = {'html': '<p>NONE</p>', 'text': 'TEXT', 'boxWidth': '400', 'boxHeight': '250'};
			if(options){
				$.extend(settings, options);
			}
			var $content = '';
			if(options['html']) {
				$content = options['html'];
			}
			else if(options['text']) {
				var lines = options['text'].split('\n');
				for(i = 0; i < lines.length; i++) {
					$content += lines[i]+'<br/>';
				}
			}
			return $('<div>', {id: 'MODAL-CONTENT'})
					.css({overflow: 'auto'})
					.html($content);
		},
		remove : function() {
			$('a#UPDATING').attr('id', '');
			return $('#MASK, #MODAL').remove();
		}
	};
	$.fn.modal = function(method) {
		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.modal');
		}
	};
})( jQuery );

