/*--------------------------------------------------------------------------*/
/*	Mbox
*	This is a script for creating modal dialog windows (like the ones your operating
*	system uses)
*
*/

var Mbox = {
	/* hideAll - closes all open lightbox windows */
	hideAll: function(){
		lboxes = document.getElementsByClassName('lbox')
		/*lboxes.each(function(box){
				Element.hide(box)
			}
		)*/
		if ($('mboxOverlay')){
			Element.remove('mboxOverlay');
			}
	}
}
Mbox.base = Class.create();
Mbox.base.prototype = {

	initialize: function(element, options){
		//start by hiding all lightboxes
		Mbox.hideAll();
	  var externalControlEl = null;

	  var mbUrl;
	  var allScripts = document.getElementsByTagName("script"); // Récupère tous les noeuds <script> du document courant
	  for (var i = 0 ; i < allScripts.length ; i++) { // Pour chacun des noeuds...
	  	var currentScript = allScripts.item(i);
	  	if (currentScript.src	&& /mbox.js\?mbUrl=(.*)/.test(currentScript.src)) {
	  		var param = currentScript.src.match(/mbox.js\?mbUrl=(.*)/); // ...on capture de tout ce qu'il y a après 'mbUrl='
	  		// param[1] contient tout ce qu'il y a après 'mbUrl='
	  		mbUrl = param[1];
	  	}
	  }

		this.element = $(element);
		this.options = Object.extend({
			lightboxClassName : 'mbox',
			closeOnOverlayClick : false,
			externalControl : false,
			title: 'Message Box',
			message: 'Ceci est une message box',
			icon: 'warning',
			width: 300,
			effectDuration: 0.1
		}, options || {} )

		//create the overlay
		new Insertion.Before(this.element, "<div id='mboxOverlay' style='display:none;'></div>");

		Element.addClassName(this.element, this.options.lightboxClassName)

		//also add a default lbox class to the lightbox div so we can find and close all lightboxes if we need to
		Element.addClassName(this.element, 'lbox')

		//Tip: make sure the path to the close.gif image below is correct for your setup
		closer = '<img id="mboxCloseX" src="'+mbUrl+'close_x.jpg" alt="Fermer" title="Fermer" />';

		//insert the closer image into the div
		new Insertion.Top(this.element, closer);

		Event.observe($('mboxCloseX'), 'click', this.hideBox.bindAsEventListener(this) );

		if (this.options.closeOnOverlayClick){
			Event.observe($('mboxOverlay'), 'click', this.hideBox.bindAsEventListener(this) );
		}
		if (this.options.externalControl){
			externalControlEl = $(this.options.externalControl);
			Event.observe(externalControlEl, 'click', this.hideBox.bindAsEventListener(this));
			if (Element.visible(externalControlEl)) {
				Event.observe(externalControlEl, 'mousedown', function() {
					$(externalControlEl).className = 'mboxFooterButtonClick';
				});
				Event.observe(externalControlEl, 'mouseup', function() {
					$(externalControlEl).className = 'mboxFooterButtonOver';
				});
				Event.observe(externalControlEl, 'mouseover', function() {
					$(externalControlEl).className = 'mboxFooterButtonOver';
				});
				Event.observe(externalControlEl, 'mouseout', function() {
					$(externalControlEl).className = 'mboxFooterButton';
				});
			}
		}

		Event.observe(window,'resize', this.center.bindAsEventListener(this));

		Element.update($('mboxHeaderContent'), '<font class="mboxHeaderTxt">'+this.options.title+'</font>');
		Element.update($('mboxMessageContent'), '<font class="mboxMessageTxt">'+this.options.message+'</font>');

		var iconBackground = '';
		var iconPosition = '';
		switch(this.options.icon) {
			case 'warning':
				iconBackground = 'background:url(\''+mbUrl+'icon_warning.jpg\') no-repeat;';
	  		iconPosition = 'background-position:50% 50%;';
			break;

			case 'confirm':
				iconBackground = 'background:url(\''+mbUrl+'icon_confirm.jpg\') no-repeat;';
	  		iconPosition = 'background-position:50% 50%;';
			break;

			case 'information':
				iconBackground = 'background:url(\''+mbUrl+'icon_information.jpg\') no-repeat;';
	  		iconPosition = 'background-position:50% 50%;';
			break;

			case 'error':
				iconBackground = 'background:url(\''+mbUrl+'icon_error.jpg\') no-repeat;';
	  		iconPosition = 'background-position:50% 50%;';
			break;

			case 'ask':
				iconBackground = 'background:url(\''+mbUrl+'icon_ask.jpg\') no-repeat;';
	  		iconPosition = 'background-position:50% 50%;';
			break;

			default:
				iconBackground = 'background:url(\''+mbUrl+'icon_warning.jpg\') no-repeat;';
	  		iconPosition = 'background-position:50% 50%;';
		}

		Element.setStyle($('mboxMessageIcon'),iconBackground);
		Element.setStyle($('mboxMessageIcon'),iconPosition);

		Element.setStyle(this.element, 'width:'+this.options.width+'px;');

		// L'objet devient déplacable
		new Draggable(this.element,{
			// Après un déplacement, l'objet se replace
			revert:true
		});

		this.showBox();
	},

	showBox : function(){
		//show the overlay
    //Element.show('overlay');
	  Effect.Appear($('mboxOverlay'), {
	   	duration:this.options.effectDuration,
	   	from:0.0,
	   	to:0.7,
	   	queue: {position:'end', scope: 'mboxscope'}
	   });

		 //center the lightbox
	   this.center();

	   //show the lightbox
	   //Element.show(this.element);
	   Effect.Appear(this.element, {
	   	duration:0.1,
	   	from:0.0,
	   	to:1.0,
	   	beforeStart: function() {

	   	},
	   	queue: {position:'end', scope: 'mboxscope'}
	   });

	   return false;
	},

	hideBox : function(evt){
		Element.removeClassName(this.element, this.options.lightboxClassName)
		Element.hide(this.element);
		//remove the overlay element from the DOM completely
		Element.remove('mboxOverlay');
		return false;
	},

	center : function(){
		var my_width  = 0;
		var my_height = 0;
		if(Prototype.Browser.IE) {
			my_width  = document.documentElement.clientWidth;
			my_height = document.documentElement.clientHeight;
		}
		else {
			if ( typeof( window.innerWidth ) == 'number' ){
			my_width  = window.innerWidth;
			my_height = window.innerHeight;
			}else if ( document.documentElement &&
					 ( document.documentElement.clientWidth ||
					   document.documentElement.clientHeight ) ){
				my_width  = document.documentElement.clientWidth;
				my_height = document.documentElement.clientHeight;
			}
			else if ( document.body &&
					( document.body.clientWidth || document.body.clientHeight ) ){
				my_width  = document.body.clientWidth;
				my_height = document.body.clientHeight;
			}
		}

		/*if ( typeof( window.innerWidth ) == 'number' ){
			my_width  = window.innerWidth;
			my_height = window.innerHeight;
		}else if ( document.documentElement &&
				 ( document.documentElement.clientWidth ||
				   document.documentElement.clientHeight ) ){
			my_width  = document.documentElement.clientWidth;
			my_height = document.documentElement.clientHeight;
		}
		else if ( document.body &&
				( document.body.clientWidth || document.body.clientHeight ) ){
			my_width  = document.body.clientWidth;
			my_height = document.body.clientHeight;
		}*/

		this.element.style.position = 'absolute';
		this.element.style.zIndex   = 999999;

		var scrollY = 0;

		if ( document.documentElement && document.documentElement.scrollTop ){
			scrollY = document.documentElement.scrollTop;
		}else if ( document.body && document.body.scrollTop ){
			scrollY = document.body.scrollTop;
		}else if ( window.pageYOffset ){
			scrollY = window.pageYOffset;
		}else if ( window.scrollY ){
			scrollY = window.scrollY;
		}

		var elementDimensions = Element.getDimensions(this.element);


		var setX = ( my_width  - elementDimensions.width  ) / 2;
		var setY = ( my_height - elementDimensions.height ) / 2 + scrollY;

		setX = ( setX < 0 ) ? 0 : setX;
		setY = ( setY < 0 ) ? 0 : setY;

		this.element.style.left = setX + "px";
		this.element.style.top  = setY + "px";

	}

}
