(function ($) {
	$.fn.dialog = function(options) {
		var i;
		var dialog = this;
		
		options = $.extend({
			showTitleBar: true,
			selectorTitleBar: false,
			selectorContent: false,
			markupContainer: 
				'<div class="jquery-dialog">' +
					'<div class="overlay"> </div>' +
				'</div>'
		}, options);
		
		// Initialiser le overlay des dialogues
		
		if (!$.fn.dialog._container_inited) {
			$.fn.dialog._container_inited = true;
			$.fn.dialog._container = $(options.markupContainer);
			$('body').append($.fn.dialog._container);
		}
		
		function Dialog(context) {
			var dialog, layer, overlay, close, self;
			
			dialog = $(context).remove().removeClass('jquery-dialog').addClass('dialog');
			
			//dialog = $(options.markupDialog);
			//dialog.html(context.html());
			
			$.fn.dialog._container.append(dialog);
			
			self = this;
			
			layer = $.fn.dialog._container;
			overlay = $('.overlay', layer);
			this.context = dialog;
			
			overlay.click(function () {
				self.hide();
			});
			
			if (dialog.hasClass('show')) {
				dialog.removeClass('show');
				self.show();
			}
		}
		
		// Cacher une boîte de dialogue
		Dialog.prototype.hide = function() {
			var dialog, layer, overlay, context;
			
			context = this.context;
			
			dialog = $(context);
			layer = $.fn.dialog._container;
			overlay = $('.overlay', layer);
			
			overlay.animate({
				opacity:0
			});
			
			var w = dialog.width();
			var h = dialog.height();
			dialog.addClass('visible');
			dialog.animate({
				width:0,
				height:0
			});
			
			wait = setInterval(function () {
				var group = $().add(dialog).add(overlay);
				if (!group.is(':animated')) {
					clearInterval(wait);
					dialog.css({
						display:'',
						width:'',
						height:''
					});
					layer.removeClass('visible');
					dialog.removeClass('visible');
				}
			}, 11);
		}
		
		// Afficher une boîte de dialogue
		Dialog.prototype.show = function() {
			var dialog, layer, overlay, context;
			
			context = this.context;
			
			dialog = $(context);
			layer = $.fn.dialog._container;
			overlay = $('.overlay', layer);
			
			overlay.css({opacity:0});
			layer.addClass('visible');
			overlay.stop().animate({
				opacity:0.5
			});
			
			var w = dialog.width();
			var h = dialog.height();
			dialog.css({
				top:($(window).height() - dialog.outerHeight()) / 2,
				width:0,
				height:0
			});
			dialog.addClass('visible');
			dialog.stop().animate({
				width:w,
				height:h
			});
		}
		
		return this.map(function (i) {
			return new Dialog(this);
		});
	}
})(jQuery);
