/**
* Simple jQuery modal window plugin.
*
* @name Simple Modal
* @version 1.0
* @author Petr Komárek
* @requires jQuery http://jquery.com/
*/

/*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/

;(function($) {
    $.fn.komodal = function(options) {

        // DEFAULTS
        var defaults = {
            background: "#000",
			opacity: "0.8",
			width: "600",
			height: "400",
			padding: "20px",
			textAlign: "left",
			closeBackground: "#000"			
        };


		// VARS
		var
			options = $.extend({}, defaults, options),
			loadingMarginTop = $(window).height()/2,
			loadingMarginLeft = $(window).width()/2,
			finalMarginTop = ($(window).height()/2)-(options.height/2),
			finalMarginLeft = ($(window).width()/2)-(options.width/2),
			modalHolder = "#modal-holder",
			modalWindow = "#modal-window";


		// FUNCTIONS
		function closeModal(){
			$(modalHolder).fadeOut('fast', function(){
				$(modalHolder).remove();
			});
			$(modalWindow).fadeOut('fast', function(){
				$(modalWindow).remove();
			});
		}


		// MAIN
        return this.each(function() {

			var modalHolder = "#modal-holder";

			if($.browser.msie && $.browser.version==6){
				//$('body').css('position','static');
				$("body").prepend('<div id="modal-holder"></div>');
			}else{
				$("body").prepend('<div id="modal-holder"></div>');
			}
			$(modalHolder).css({
				'display' : 'none',
				'position' : 'absolute',
				'top' : '0',
				'left' : '0',
				'z-index' : '9999',
				'width': $(window).width(),
				'height': $(document).height(),
				'opacity' : options.opacity,
				'background': options.background
			});

			// FX
			$(modalHolder).fadeIn('slow', function(){
				$('body').prepend('<div id="modal-window"></div>');

				$('#modal-window')
				.css({
					'background' : '#FFF',
					'position' : 'absolute',
					'top' : '0',
					'left' : '0',
					'z-index' : '10000',
					'width' : '24px',
					'height' : '24px',
					'text-align' : options.textAlign,
					'padding' : options.padding,
					'margin-top' : loadingMarginTop+'px',
					'margin-left' : loadingMarginLeft+'px'
				})
				.animate({
					width: options.width+"px",
					height: options.height+"px",
					marginTop: finalMarginTop+'px',
					marginLeft: finalMarginLeft+'px'					
				}, {duration: 500, complete: function(){
					$('#modal-window')
					.load(options.load + '?' + Math.floor(Math.random()*11), function(){
						$('#modal-window').append('<div id="close-modal">x</div>');
						$('#close-modal')
						.css({
							'position' : 'absolute',
							'right' : '0',
							'top' : '0',
							'background' : options.closeBackground,
							'color' : '#FFF',
							'padding' : '0px 5px 2px',
							'font-size' : '20px',
							'font-weight' : 'bold',
							'cursor' : 'pointer'
						})
						.click(function(){
							closeModal();
						});

						$(modalHolder).click(function(){
							closeModal();
						});


					})
					}
				});

			});


		});

	};

})(jQuery);