/**
 * nModal - Minimalist Modaling with jQuery
 */
function		createCss(t) {
	var css = null;
	if (document.createStyleSheet) {
		css = document.createStyleSheet("");
		css.cssText=t;
	}
	else {
		css = document.createElement('style');
		css.type = 'text/css';
		css.appendChild(document.createTextNode(t));
		document.getElementsByTagName('head')[0].appendChild(css);
	}
	return (css);
};
jQuery.each( ("close").split(","), function(i,o){
	// Handle event binding
	jQuery.fn[o] = function(f){
		return f ? this.bind(o, f) : this.trigger(o);
	};
		
});
var offsetTop = 25;
var ieexpr = "(document.documentElement.scrollTop || document.body.scrollTop)" + 
			"+Math.round("+offsetTop+" * (document.documentElement.offsetHeight || document.body.clientHeight) / 100) + 'px'";

createCss(
'#nmodalOverlay					{background-color: white; height:100%; width:100%; position:fixed; left:0; top:0}' +
'.popup							{display:none; position:fixed; top:'+offsetTop+'%; left:50%; margin-left:-300px; width:600px;}' +
//!	Fixed positioning emulation for IE6
(jQuery.browser.msie && jQuery.browser.version <= 6.0?
	'iframe.popup 		{filter:alpha(opacity=0); border:0; }' +
	'.popup				{position:absolute; top:expression('+ieexpr+')}' +
	'.frameOverlay		{filter:alpha(opacity=0); width:100%;height:100%;}': '') +

'.popup							{background-color:#EEE; color:#333; border:1px solid black; padding:12px;}' +
'.popup .item.buttons 			{text-align: right;margin-bottom: 0px;}' +
'.popup .buttons input.button	{height: 22px; font-size: 12px; text-align:center; border:1px solid black; background-color:white;}' +
'.popup .modal.close			{width: 16px;height: 14px;position: absolute;top: 3px;right: 3px;background: url(/js/library/img/close.gif);/*cursor: pointer;*/}' +
'.popup .modal.close.down		{background-image: url(/js/library/img/close.down.gif);}' +
'.popup .modal.close a			{display: none}' +
'.popup .waiter					{display:block;width:16px;height:16px;margin:15px auto;background-image: url(/js/library/img/waiter.gif);}'
);

(function($) {

	var _config = {
		zIndex: 3000,
		stack: [],
		overlay: 50,
		iframe: null
	};
	var overlay = null;
	
	function	displayOverlay() {
		if (overlay)
			return overlay.show();
		overlay = jQuery('<div id="nmodalOverlay"></div>').css({opacity:_config.overlay/100}).prependTo('body');
		if (jQuery.browser.msie && jQuery.browser.version <= 6.0) {
			overlay.css({position: 'absolute'});
			jQuery('<iframe src="javascript:" class="frameOverlay" />').prependTo(overlay);
		}
	}
	function	clean_className(text) {
		var repl = {' ':'-', "'":'-', 'à':'a', 'é':'e', 'è':'e', '(':'', ')':'', ',':'-', '.':''};
		for (var c in repl)
			while ((text.indexOf(c)!=-1))
				text = text.replace(c, repl[c]);
		return (text);
	}
	
	jQuery.extend({
		/** methode statique permettant d'afficher rapidement un texte dans une popup modale */
		alert: function(msg, options){
			options = options || {};
			if (typeof msg == 'string')
				options.text = msg;
			else if (typeof msg == 'object')
				options = msg;
			options.text = options.text || '';
			return (jQuery('<div>'+options.text+'</div>').alert(options));
		}
	});

	jQuery.fn.extend({
		/** methode dynamique permettant de mettre en popup la div selectionnée */
		alert: function(options) {
			this.each(function(i) {
				displayOverlay();
				
				options = options || {};
				var wrap = jQuery(this);
				
				/** detecte si la node est dans le DOM */
				var inDom = true;
				if (wrap.parents('body').length == 0) {
					inDom = false;
					wrap.appendTo('body');
				}
				for (var i=0; i<_config.stack.length; i++)
					_config.stack[i].css({zIndex: _config.zIndex + i});
				overlay.css({zIndex: _config.zIndex + _config.stack.length});
				wrap.addClass('popup')
					.css({zIndex: _config.zIndex + _config.stack.length + 1})
					.show()
					.close(function() {
						wrap[inDom? 'hide': 'remove']();
						_config.stack.splice(array_search(_config.stack, wrap), 1);
						if (_config.stack.length)
							_config.stack[_config.stack.length-1].css({zIndex: _config.zIndex + _config.stack.length + 1})
						overlay.css({zIndex: _config.zIndex + _config.stack.length});
						if (_config.iframe) {
							try {
								_config.iframe.remove();
							}catch(e) {}
						}
						if (!_config.stack.length)
							overlay.hide();
					});
				if (options.className)
					wrap.addClass(options.className);

				/** ajout du bouton de fermeture de la fenetre (si pas desactiv�) */
				if (!options.noclose) {
					wrap.prepend('<div class="modal close"><a class="close">fermer</a></div>');
					var closeButton = jQuery('.modal.close', wrap);
					closeButton.click(function() {
						wrap.close();
						return (false);
					}).mousedown(function() {
						closeButton.addClass('down');
						return (false);
					});
					jQuery(document).mouseup(function() {
						closeButton.removeClass('down');
						return (false);
					});
				}

				/** ajout des boutons en bas de page */
				if (options.buttons) {
					var buttons = jQuery('.item.buttons', wrap);
					if (!buttons.length) {
						buttons = jQuery('<p class="item buttons"></p>');
						wrap.append(buttons);
					}
					for (var text in options.buttons) {
						var className = clean_className(text);
						if (!jQuery('.item.button.'+className, buttons).length) {
							/** ne pas rajouter les boutons s'il existent */
							buttons.append('&nbsp;<input class="item button '+className+'" type="button" value="'+text+'" />');
							var button = jQuery('.item.button.'+className, buttons);
							button.click(options.buttons[text]);
						}
					}
				}

				/** path IE6 pour l'ecrasement des select */
				if (jQuery.browser.msie && jQuery.browser.version <= 6.0) {
					if (!_config.iframe) {
						jQuery('html,body').css({height:'100%'});
						_config.iframe = jQuery('<iframe src="javascript:" class="popup"></iframe>');
					}
					wrap.before(_config.iframe);
					_config.iframe.show();
					_config.iframe[0].style.width = (this.offsetWidth + 2)+'px';
					_config.iframe[0].style.height = this.offsetHeight+'px';
				}
				_config.stack.push(wrap);
			});
			return this;
		}
	});
	function	array_search(arry, value) {
		for (var i=0;i<arry.length; i++)
			if (arry[i]===value)
				return (i);
		return (-1);
	}
	jQuery(document).keydown(function(e){
		if ((e.which || e.keyCode) == 27 && _config.stack.length) {
			_config.stack[_config.stack.length-1].close();
			e.stopPropagation();
		}
	});

})(jQuery);