(function($) {
	$(function() {

		/**
		* Delai en millisecondes avant de faire disparaître le message d'information.<br>
		* Fixé à 6 secondes.<br/>
		* Ce délai est utilisé si il n'y a pas de détails
		*/
		var DELAY_WITHOUT_DETAILS = 6000;
		/**
		* Delai en millisecondes avant de faire disparaître le message d'information.<br/>
		* Fixé à 10 secondes.<br/>
		* Ce délai est utilisé si il y a des détails
		*/
		var DELAY_WITH_DETAILS = 10000;

		var $globalInfo = null;
		var globalInfo_timeout = null;
		var dialogContent = null;
		var title = null;
		var details = [];

		/*
		* Initialisation qui va créer le conteneur caché des messages d'information.
		* Cette méthode est appelée automatiquement.
		*/
		function init() {
			$globalInfo = $("#global_info");
			if ($globalInfo.size()==0) {
				$globalInfo = new $("<span></span>");
				$globalInfo.attr('id','global_info');
				$globalInfo.append(new $("<a></a>").attr('href','#').attr('class','delete').html('&nbsp;').click(function() {
					hide();
					return false;
				}));
				$globalInfo.append(new $("<span></span>").attr('name','title'));
				$globalInfo.hide();
				$("body").append( $globalInfo );

				$("#global_info").mouseover(function() {
					if (globalInfo_timeout!=null) {
						window.clearTimeout(globalInfo_timeout);
						globalInfo_timeout=null;
					}
				});
				$("#global_info").mouseout(function() {
					if( details==null || details.length==0 ) globalInfo_timeout = window.setTimeout( hide, DELAY_WITHOUT_DETAILS );
					else globalInfo_timeout = window.setTimeout( hide, DELAY_WITH_DETAILS );
				});
			}
		}

		function clear() {
			init();
			title="";
			details = [];
			$globalInfo.find("li").remove();
		}

		function hide() {
			init();
			$globalInfo.hide();
			clear();
			if (globalInfo_timeout!=null) {
				window.clearTimeout(globalInfo_timeout);
				globalInfo_timeout=null;
			}
		}


		/**
		* Utilitaire pour afficher des messages d'information dans le backOffice.<br/>
		* @class
		* @name boInfo
		* @static
		* @version 1.0
		*/
		"boInfo".namespace(/** @lends boInfo.prototype */ {

			/**
			* Effacer les messages d'information présents.<br/>
			* La fenêtre n'est pas masquée, mais le contenu sera supprimé.
			*/
			clear: function() {
				clear();
				return this;
			},

			/**
			* Fixe le titre de l'information
			*/
			setTitle: function(new_title) {
				title = new_title;
				return this;
			},

			/**
			* Méthode pour ajouter du texte dans les messages d'information.<br/>
			* La fenêtre n'est pas affichée ou masquée, elle reste dans son état courant.
			*/
			addDetail: function(msg) {
				details.push( msg );
				return this;
			},

			/**
			* Méthode pour cacher les messages.<br/>
			* Cacher les messages va aussi supprimer le contenu.
			*/
			hide: function() {
				hide();
				return this;
			},

			/**
			* Méthode pour afficher les messages présents.
			* @param delay force un délai en millisecondes pour maintenir le message visible.<br/>
			* Si cela n'est pas renseigné alors on utilise les valeurs prévues suivant la présence ou non de détail.
			*/
			show: function(delay) {
				init();
				if (globalInfo_timeout!=null) {
					window.clearTimeout(globalInfo_timeout);
					globalInfo_timeout=null;
				}

				$globalInfo.find("> ul").remove();
				var ul = $("<ul></ul>").attr('name','details').hide();
				$globalInfo.append(ul);
				for(var i = 0; i < details.length; i++) {
					ul.append( $("<li></li>").html(details[i].replace(/\n/g, '<br>')) );
				}

				var str_title = ""+title;
				if (details==null || details.length==0) $globalInfo.find("[name='title']").html( str_title );
				else {
					dialogContent = $globalInfo.find("> ul[name='details']");
					dialogContent.dialog({
						autoOpen: false,
						closeOnEscape:true,
						width: $(window).width()-100,
						height: $(window).height()-100,
						maxHeight: $(window).height(),
						maxWidth: $(window).width(),
						modal: true,
						position: 'center',
						resizable: true,
						zIndex: 10000,
						title: str_title,
						open: function(event, ui) {
									$(this).css({'max-height': $(window).height()+'px', 'overflow-y': 'auto', 'max-width': $(window).width()+'px'});
									$(this).focus();
							}

					});

					$globalInfo.find("[name='title']").html( str_title + " ... ");
					$globalInfo.find("[name='title']").append(jQuery("<a></a>").attr('href','#').text("details").click(function() {
						dialogContent.dialog('open');
						return false;
					}) );
				}


				if (typeof(console)!='undefined') {
					// plus facile pour débogguer
					console.log("boInfo.show : title="+title+ " details="+$globalInfo.find("> ul[name='details']").text());
				}
				
				$globalInfo.show();
				var left = $(window).width()-$globalInfo.width();
				left = Math.ceil(left / 2);
				var props = {position:'fixed', left:left+'px', top:'0px'};
				$globalInfo.css( props );

				if (delay!=undefined) globalInfo_timeout = window.setTimeout( hide, delay );
				else if (details==null || details.length==0) globalInfo_timeout = window.setTimeout( hide, DELAY_WITHOUT_DETAILS );
				else globalInfo_timeout = window.setTimeout( hide, DELAY_WITH_DETAILS );
				return this;
			},

			showDialog: function( onClose ) {
				init();
				if( dialogContent == null || dialogContent.size()==0) {
					return;
				}
				dialogContent.bind( "dialogclose", onClose );
				dialogContent.dialog('open');

			}
		});

	});
})(jQuery);



