/**
 * Dialog simple de messages
 * @author christian@konfiture.com
 * @class
 */
(function($)
	{
		/* Quelques constantes */
		arcadimMsgBox = function(params)
			{
				/**
				 * Construit la boite de message.
				 * Lors de la fermeture, la boite est retirée du DOM
				 */
				this.showBox = function()
					{
						var dlgSelector = '#' + params.div;
						
						if ($(dlgSelector).length > 0)
							$(dlgSelector).remove();
							
						var picto = '';
						
						switch(params.type)
							{
								case 'loading':
									picto = '<span class="side-picto picto-loading"></span>';
									break;
								case 'error':
									picto = '<span class="side-picto picto-error"></span>';
									break;
								case 'yesno':
									picto = '<span class="side-picto picto-yesno"></span>';
									break;
								default:
									picto = '';
							}
							
						var html='<div id="' + params.div + '">' + picto + '<p class="' + params.msgClass + '">' + params.message + '</p></div>';
						
						if ($(dlgSelector).length > 0)
							$(dlgSelector).remove();
						$('body').append(html);
						
						var dlg = $(dlgSelector);
												
						dlg.find('p')
							.css(
								{
									display: 'block',
									padding: '20px',
									marginLeft: 'auto',
									marginRight: 'auto'
								});
						
						var dlgBtns =
							{
								'OK': function()
									{
										$(this).dialog('close');
									}
							};
						
						if (params.type == 'yesno') {
							dlgBtns = {
								'Oui': function()
									{
										$(this).dialog('close');
										if ('function' == typeof params.answer)
											params.answer('yes');
									},
								'Non': function()
									{
										$(this).dialog('close');
										if ('function' == typeof params.answer)
											params.answer('no');
									}
							};
							params.noControls = false;
						}
						
						
						var showEffect = (ie == 7 || ie == 8) ? 'fade' : 'clip';

						dlg.dialog(
								{
									modal: true,
									autoOpen: false,
									resizable: false,
									show: showEffect,
									hide: showEffect,
									title: params.title,
									closeOnEscape: ! params.noControls,
									zIndex: 999999999,
									open: function(e, ui)
										{
											if ( params.noControls )
												$(this).parents('.ui-dialog-titlebar-close').first().hide();
											else
												{
													var pane = $(this).parents('.ui-dialog').first().find('.ui-dialog-buttonpane');
													pane.find('button:contains("OK")').button(
														{
															icons:
																{
																	primary: 'ui-icon-check'
																}
														});
												}
										},
									close: function(e, ui)
										{

											if (params.callback != null && params.callback !== undefined)
												params.callback();
											$('.ui-effects-wrapper').css('zIndex', -1);
										},
									buttons: params.noControls ? {} : dlgBtns
								})
							.dialog('open');
							;
						return dlg;
					};
				
				var defaults =
					{
						div: 'arcadim-message-box-dialog',
						title: 'Message du site Arcadim',
						message: 'Message',
						type: 'standard',
						msgClass: 'message',
						callback: null,
						answer: null,
						noControls: false
					};
				
				params = $.extend( defaults, params );
				
				this.dialog = this.showBox();
			};
		
		/**
		 * Affiche une boite de dialogue standard
		 */
		arcadimMsgBox.show = function(params)
			{
				// var box = new arcadimMsgBox(params || {});
				var box = new arcadimMsgBox(params || {});
				return box.dialog;
				//return box.dialog;
			};
		
		/**
		 * Validation oui/non
		 */
		arcadimMsgBox.ask = function(options)
			{
				var opts = $.extend({
						question: 'Être ou ne pas être ?',
						title: 'Question'
					}, options);
				
				var params = 
					{
						message: opts.question,
						title : opts.title,
						type: 'yesno',
						msgClass: 'yesno-message',
						answer: opts.answer,
						noControls: false
					};
				
				return arcadimMsgBox.show(params);
					
			}
		
		/**
		 * Affiche une boite de dialogue d'erreur
		 */
		arcadimMsgBox.error = function(params)
			{
				var newParams = $.extend(
					{
						type: 'error',
						msgClass: 'error-message',
						title: 'Erreur !'
					}, params);
					
				return arcadimMsgBox.show(newParams);
			};
		
		arcadimMsgBox.loading = function(params)
			{
				var newParams = $.extend(
					{
						type: 'loading',
						msgClass: 'loading-message',
						title: 'Veuillez patienter...',
						noControls: true
					}, params);
				
				return arcadimMsgBox.show(newParams);
			}

	})(jQuery);
