/*
 *
 * This file contains the default JavaScript commands for modal windows.
 *
 */
 
(function($){

	$.fn.modal = function() {
		return this.modalOpen();
	};
	
	$.fn.modalOpen = function() {
		var id = this.attr('id') + 'Modal';
		
		if ($('#' + id).size() === 0) {
			this.wrap('<div id="' + id + '" class="modalWindow"></div>')
				.after('<div class="modalClose" onclick="$(this).modalClose()"></div>');
				
			if (this.hasClass('hidden')) this.removeClass('hidden');
				
			$('#' + id).before('<div id="modalWrapper_' + id + '" class="modalWrapper"></div>');
		}
		
		$('#' + id + ',#modalWrapper_' + id).show();
		return $('#' + id);
	};
	
	$.fn.modalClose = function() {
		var $window = this.parent('.modalWindow');
	
		if ($('.emptyBeforeClose', $window).length) {
			$('.emptyBeforeClose').empty();	
		}
		$window.hide();
		$window.prev().hide();
	};
	
	$.fn.unModal = function() {
		var $window = this.parent('.modalWindow');
		
		$window.prev('.modalWrapper').replaceWith(this);
		$window.remove();
	};
	
	$.fn.modalDestroy = function() {
		var $window = this.parent('.modalWindow');
		
		$window.prev('.modalWrapper').remove();
		$window.remove();
	};
	
})(jQuery);