// jQuery Simple Modal Plugin
//
// Version 1.0
//
// Justin Wright
// Dokodemodesign (http://dokodemodesign.com/)
// 14 May 2009
//
//
// Usage:
//		jsimplemodal = function($url, $close, $loader) {
//		$.simplemodal.showmodal($url, $close, $loader);
//      };
//
// History:
//
//		1.00 - Public Release (tba)
//
// License:
// 
// This plugin is dual-licensed under the GNU General Public License and the MIT License and
// is copyright 2010 Dokodemodesign.com.

(function($) {
		  
		  
		  
	$.simplemodal =  {	  
		
		// These properties can be read/written by accessing $.simplemodal.propertyName from your scripts at any time
		verticalOffset: -75,   // vertical offset of the dialog from center screen, in pixels             
		horizontalOffset: 0,  // horizontal offset of the dialog from center screen, in pixels/
		maskOpacity: .7,      //opacity of mask layer
		modalWidth: '500px',  //width of the modal
		modalMinHeight: '200px', //minHeight of the modal 
		modalBgColor: 'red', //Background color for the #modal_container
		loaderWidth: '500px', //width for loader
		loaderMinHeight: '200px', //minHeight of the loader
		loaderBgColor: 'transparent', //Background color of the loader
		loaderContentHeight: 24, //loader content height usually an image or loader GIF
		ajaxData: 'action=modal',
		modalPos: 'fixed', //moadal position (usually absolute or fixed
		
		
		// Public methods
		
		showmodal: function($url, $close, $loader, callback) {
			if( callback == null ) callback = 'Alert';
			$.simplemodal._show($url, $close, $loader, callback);
		},
		
		
		
		// Private methods 
		
		
		_show: function($url, $close, $loader, callback) {
			$.simplemodal._hide();
			$.simplemodal._mask('show');
			
			$('body').append(
			  '<div id="modal_loader">'+'</div>'+'<div id="modal_container">'+'</div>');

			
			// IE6 Fix
			var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : $.simplemodal.modalPos;
			
			$("#modal_loader").css({
				position: pos,
				zIndex: 99999,
				padding: 0,
				width: $.simplemodal.loaderWidth,
				minHeight: $.simplemodal.loaderMinHeight,
				display: 'none',
				background: $.simplemodal.loaderBgColor,
				margin: 0,
			});
			$("#modal_container").css({
				position: pos,
				zIndex: 99999,
				padding: 0,
				width: $.simplemodal.modalWidth,
				minHeight: $.simplemodal.modalMinHeight,
				display: 'none',
				background: $.simplemodal.modalBgColor,
				margin: 0
			});
			
			
			$.simplemodal._reposition();
			$.simplemodal._maintainPosition(true);
			
			var loaderHght = (($("#modal_loader").outerHeight() / 2) - ($.simplemodal.loaderContentHeight/2));
			
			$("#modal_loader").ajaxStart(function() {
			$("#modal_loader").append('<span style="display: block; width:'+$.simplemodal.loaderContentHeight+'px; margin-right: auto; margin-left: auto; margin-top:'+loaderHght+'px;">'+$loader+'</span>').show(); }).ajaxStop(function() { $("#modal_loader").remove();});
			
			$.ajax({
				type: "POST",
				url: $url,
				data: $.simplemodal.ajaxData,
				dataType: 'json',
				success: function(data, textStatus) {
						if (data.good) {
						$("#modal_container").append(data.good);
						if (typeof callback == "function") callback(); //else alert('meh');
						$("#modal_container").fadeIn("slow");
						}else{
						//display errors
						$("#modal_container").append('<p>There was an error</p>');
						$("#modal_container").fadeIn("slow");
						}
					}
				});
			 
			

			
			
			$.simplemodal._removemodal($close);
			
			
		},//close _show
		
		_hide: function() {
			$("#modal_container").remove();
			$.simplemodal._mask('hide');
			$.simplemodal._maintainPosition(false);
		}, //close_hide
		
		 _mask: function(status) {
			switch( status ) {
				case 'show':
					$.simplemodal._mask('hide');
					$('body').append('<div id="modal_mask"></div>');
					$("#modal_mask").css({
						position: 'absolute',
						zIndex: 9000,
						top: '0px',
						left: '0px',
						width: '100%',
						height: $(document).height(),
						background: '#000',
						display:'none'
					});
					$("#modal_mask").fadeTo("slow", $.simplemodal.maskOpacity);
				break;
				case 'hide':
					$("#modal_mask").remove();
				break;
			}
		},//close _mask
		
		
		
	
		
		_reposition: function() {
			var top = (($(window).height() / 2) - ($("#modal_container").outerHeight() / 2)) + $.simplemodal.verticalOffset;
			var left = (($(window).width() / 2) - ($("#modal_container").outerWidth() / 2)) + $.simplemodal.horizontalOffset;
			if( top < 0 ) top = 0;
			if( left < 0 ) left = 0;
			
			// IE6 fix
			if( $.browser.msie && parseInt($.browser.version) <= 6 ) top = top + $(window).scrollTop();
			
			$("#modal_container, #modal_loader").css({
				top: top + 'px',
				left: left + 'px'
			});
			$("#modal_mask").height( $(document).height() );
		},//close _reposition
		
		_maintainPosition: function(status) {
				switch(status) {
					case true:
						$(window).bind('resize', $.simplemodal._reposition);
					break;
					case false:
						$(window).unbind('resize', $.simplemodal._reposition);
					break;
				}
		}, //close _maintainPosition



		_removemodal: function($close) {
		 //if mask is clicked 
		$("#modal_mask").click(function () {
		$.simplemodal._hide();
	    });
		$($close).click(function (e) {
		 //Cancel the link behavior
		 e.preventDefault();
		 $.simplemodal._hide();
		});
		$($close).live('click', function(e) {
		 //Cancel the link behavior
		 e.preventDefault();
		 $.simplemodal._hide();
		});

		},//close _removemodal
		    
    
		
	}//close simplemodal function

	
	
	// Shortuct function
	
	jsimplemodal = function($url, $close, $loader, callback) {
		$.simplemodal.showmodal($url, $close, $loader, callback);
	};
		

		  
})(jQuery);
