/*
 * Copyright (c) 2009 Patrick Lai <patrick.lai@stanford.edu>
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

(function($) {
	function setupOverlay(object, options) {
		object
			.attr('id', options.overlayIdentifier)
			.css({
				'position': 'fixed',
				'top': 0,
				'left': 0,
				'z-index': options.zIndex,
				'background-color': options.overlayColor,
				'opacity': options.overlayOpacity,
				'width': $(window).width(),
				'height': $(document).height()
			})
			.hide()
			.appendTo(options.target);
			
    	if (options.overlayCloseOnClick) {
        	object.click(function() {
				$.modalDestroy();
				return false;
        	});
    	}
			
		return object;
	};
	
	function setupContent(object, options) {
		object
	    	.hide()
			.appendTo(options.target)
			.css({
				'position': 'absolute',
				'top': Math.floor($(window).height() / 2) - (object.outerHeight() / 2),
	    		'left': Math.floor($(window).width() / 2) - (object.outerWidth() / 2),
	    		'z-index': options.zIndex + 2000
	    	});
			
    	if (options.closeTrigger) {
    		object.find(options.closeTrigger).click(function() {
				$.modalDestroy();
        		return false;
    		});
    	}
			
		return object;
	};
	
	$.extend({
		modalOverlay: null,
		modalContent: null,
		modalCreate: function(object, options) {
			var options = $.extend({
				zIndex: 8000,
				target: 'body',
				timeout: 0,
				overlay: true,
				overlayCloseOnClick: true,
				overlayIdentifier: 'overlay',
				overlayColor: '#000000',
				overlayOpacity: 0.8
			}, options);
			
			if (this.modalOverlay == null && options.overlay) {
				this.modalOverlay = setupOverlay($('<div></div>'), options);
				this.modalOverlay.fadeIn('400');
			}
			
			if (this.modalContent != null)
				this.modalContent.fadeOut('400', function() {
					$(this).remove();
				});
				
			this.modalContent = setupContent(object, options);
			this.modalContent.fadeIn('400');
			
			if (options.timeout > 0) {
				setTimeout(function() {
					$.modalDestroy();
				},options.timeout);
			}
		},
		modalDestroy: function() {
			if (this.modalOverlay != null) {
				this.modalOverlay.fadeOut('400', function() {
					$(this).remove();
				});
				this.modalOverlay = null;
			}
			
			if (this.modalContent != null) {
				this.modalContent.fadeOut('400', function() {
					$(this).remove();
				});
				this.modalContent = null;
			}
		}
	});
})(jQuery);