var DialogBox = new Class({
	
	Implements: Options,
	
	dialogBox : null,
	type: 'alert',
	
	options : {
		adminPart: false,
		image: 'erreur.png',

		alertBoxOptions : {
			type: 'form',		//'form' pour les erreurs de saisies dans les formulaires et 'page' pour les erreurs au chargement de la page.
			locationName: 'formulaire',
			location: null,
			displayType: 'replace',
			button: false,
			redirection: '/',
			onClickOkFunction: false,
			message: '',
			buttonLabel: 'Ok',
			alertType: 'error',
			formRedirection: false,
			mooeditable: false
		},

		promptBoxOptions: {
			type: 'text',
			input: null,
			locationName: 'formulaire',
			location: null,
			button: false,
			variable: 'var',
			message: 'Veuillez pr&eacute;ciser l\'information requise :',
			inputContent: '',
			inputLabel: false,
			buttonLabel: 'Valider',
			validate: false,
			onSubmit: {
				url: false,
				method: 'post',
				formData: true,
				otherData: false,
				reponseOk: $empty,
				responseNull: false,
				responseError: false
			},
			onSubmitFunction: false
		},
		
		confirmBoxOptions: {
			locationName: 'formulaire',
			location: null,
			button: false,
			message: '&Ecirc;tes-vous s&ucirc;r(e) de votre choix ?',
			clickYes: false,
			onClickYes: {
				url: '',
				method: 'post',
				data: '',
				reponseOk: $empty,
				responseNull: false,
				responseError: false
			},
			onClickYesFunction: false,
			onClickNoFunction: false
		}
	},
		
	initialize : function(type, options)
	{
		this.dialogBox = null;
		this.type = ( (type == undefined) ? 'alert' : type );
		
		var reg = new RegExp("/admin/");
		this.options.adminPart = reg.test(document.URL);
		
		if(this.options.adminPart)
		{
			this.options.alertBoxOptions.redirection = '/admin'
		}
		
		this.setOptions(options);
		
		this.options.image = this.getBoxImage();
		
		$(document.body).getElements('.fc-tbx').setStyle('display', 'none');
		
		if(this.type == 'alert')
		{
			this.options.alertBoxOptions.message = ( (this.options.alertBoxOptions.alertType == 'error') ? ( (this.options.alertBoxOptions.message == '') ? 'Une erreur inconnue s\'est produite' : 'Erreur : ' + this.options.alertBoxOptions.message) : this.options.alertBoxOptions.message );
			
			if(this.options.alertBoxOptions.type != 'page')
			{
				if(this.options.alertBoxOptions.location == null)
				{
					this.options.alertBoxOptions.location = $(this.options.alertBoxOptions.locationName);
					if(this.options.alertBoxOptions.locationName == 'affichage')
					{
						this.options.alertBoxOptions.displayType = 'changeDisplayStyle';
					}
				}
				else if(this.options.alertBoxOptions.location.dialogBox != null)
				{
					this.options.alertBoxOptions.location = this.options.alertBoxOptions.location.dialogBox;
				}
				
				var location = this.options.alertBoxOptions.location;

				if(this.options.alertBoxOptions.formRedirection)
				{
					this.options.alertBoxOptions.redirection = this.options.alertBoxOptions.formRedirection;
					this.options.alertBoxOptions.formRedirection = true;
				}
				
				this.options.alertBoxOptions.mooeditable = ( (location.getElement('.mooeditable') == null) ? false : true);
			}
		}
		else if(this.type == 'prompt')
		{
			if(this.options.promptBoxOptions.location == null)
			{
				this.options.promptBoxOptions.location = $(this.options.promptBoxOptions.locationName);
			}
			else if(this.options.promptBoxOptions.location.dialogBox != null)
			{
				this.options.promptBoxOptions.location = this.options.promptBoxOptions.location.dialogBox;
			}
			
			var input = null;
			
			switch(this.options.promptBoxOptions.type)
			{
				case 'text':
					input = new Element('input', {'type': 'text', 'value': this.options.promptBoxOptions.inputContent });
					break;
			}
			
			input.setProperties({name: this.options.promptBoxOptions.variable, id: this.options.promptBoxOptions.variable});
			
			if(this.options.promptBoxOptions.validate)
			{
				input.addClass("validate["+this.options.promptBoxOptions.validate+"]");
			}
			
			this.options.promptBoxOptions.input = input;
		}
		else if(this.type == 'confirm')
		{
			if(this.options.confirmBoxOptions.location == null)
			{
				this.options.confirmBoxOptions.location = $(this.options.confirmBoxOptions.locationName);
			}
			else if(this.options.confirmBoxOptions.location.dialogBox != null)
			{
				this.options.confirmBoxOptions.location = this.options.confirmBoxOptions.location.dialogBox;
			}
		}
		
		this.displayBox();
	},
	
	getBoxImage : function()
	{
		if(this.type == 'alert')
		{
			switch(this.options.alertBoxOptions.alertType)
			{
				case 'error': 			return "erreur.png";
				case 'info':			return "alertInfo.png";
				case 'accessDenied':	return "accessDenied.png";
				case 'done':			return "valid.png";
				default:				return "erreur.png";
			}
		}
		else if(this.type == 'prompt')
		{
			return "prompt.png";
		}
		else if(this.type == 'confirm')
		{
			return "confirm.png";
		}
		
		//Par défaut :
		return "erreur.png";
	},
	
	displayBox : function()
	{
		if(this.type == 'alert')
		{
			this.buildAlertBox();
		}
		else if(this.type == 'prompt')
		{
			this.buildPromptBox();
		}
		else if(this.type == 'confirm')
		{
			this.buildConfirmBox();
		}
		
		this.injectBox();
	},
	             
	buildAlertBox : function()
	{	
		var image = new Element('img', {
			'src': '/images/icons/'+this.options.image,
			'alt': 'Alerte',
			'title': 'Alerte'
		});
		
		var bouton = new Element('input', {
			'id': 'dialogBoxButton',
			'type': 'button',
			'value': this.options.alertBoxOptions.buttonLabel
		}).addEvent('click', function(e){ this.onFinish(); }.bind(this));
		
		var tableau = new Element('table', {'id': 'dialogBox'});
		
		tableau.adopt(
						new Element('tr', {'id': 'tr1'}).adopt(
								new Element('td', { 'id': 'tdImage' }).adopt(image), 
								new Element('td', { 'id': 'tdMessage', 'colspan': '2', 'html': this.options.alertBoxOptions.message })
						),
						new Element('tr', {'id': 'tr2'}).adopt(
								new Element('td', { 'id': 'tdEmptyAlert', 'colspan': '2' }),
								new Element('td', { 'id': 'tdButton' } ).adopt(bouton)
						)
					 );
		
		this.dialogBox = tableau;
	},
	
	buildPromptBox : function()
	{
		var image = new Element('img', {
			'src': '/images/icons/'+this.options.image,
			'alt': 'Attention',
			'title': 'Attention'
		});
		
		var bouton = new Element('input', {
			'id': 'dialogBoxButton',
			'type': 'submit',
			'value': this.options.promptBoxOptions.buttonLabel
		});
	
		var form = new Element('form', {'id': 'form_' + this.options.promptBoxOptions.variable});
		var input = this.options.promptBoxOptions.input;
		
		var tableau = new Element('table', {'id': 'dialogBox'});
		
		var tdMessage = new Element('td', { 'id': 'tdMessage', 'colspan': '3', 'html': this.options.promptBoxOptions.message });
		var tdLabel   = new Element('td', { 'id': 'tdLabel', 'colspan': '2' } );
		var tdInput	  = new Element('td', { 'id': 'tdInput'} );
		
		if(!this.options.promptBoxOptions.inputLabel)
		{
			tdMessage.adopt(new Element('br'), input);
		}
		else
		{
			tdLabel.adopt(new Element('label', {'html': this.options.promptBoxOptions.inputLabel}));
			tdInput.adopt(input);
		}
		
		tableau.adopt(
						new Element('tr', {'id': 'tr1'}).adopt(
								new Element('td', { 'id': 'tdImage' }).adopt(image), 
								tdMessage
						),
						new Element('tr', {'id': 'tr2'}).adopt(
								tdLabel,
								tdInput,
								new Element('td', { 'id': 'tdButton' } ).adopt(bouton)
						)
					 );
		
		this.dialogBox = form.adopt(tableau);
		
		if(!this.options.promptBoxOptions.validate)
		{
			form.addEvent('submit', function(e){ 
				e.stop();
				this.onFinish(); 
			}.bind(this));
		}
	},
	
	buildConfirmBox : function()
	{
		var image = new Element('img', {
			'src': '/images/icons/'+this.options.image,
			'alt': 'Confirmation',
			'title': 'Confirmation'
		});
		
		var boutonYes = new Element('input', {
			'id': 'dialogBoxButtonYes',
			'type': 'button',
			'value': 'Oui'
		}).addEvent('click', function(e){ this.options.confirmBoxOptions.clickYes = true; this.onFinish(); }.bind(this));
		
		var boutonNo = new Element('input', {
			'id': 'dialogBoxButtonNo',
			'type': 'button',
			'value': 'Non'
		}).addEvent('click', function(e){ this.options.confirmBoxOptions.clickYes = false; this.onFinish(); }.bind(this));
		
		var tableau = new Element('table', {'id': 'dialogBox'});
		
		tableau.adopt(
						new Element('tr', {'id': 'tr1'}).adopt(
								new Element('td', { 'id': 'tdImage' }).adopt(image), 
								new Element('td', { 'id': 'tdMessage', 'colspan': '3', 'html': this.options.confirmBoxOptions.message })
						),
						new Element('tr', {'id': 'tr2'}).adopt(
								new Element('td', { 'id': 'tdEmptyConfirm', 'colspan': '2' }),
								new Element('td', { 'id': 'tdButtonYes' } ).adopt(boutonYes),
								new Element('td', { 'id': 'tdButtonNo' } ).adopt(boutonNo)
						)
					 );
		
		this.dialogBox = tableau;		
	},
	
	injectBox : function()
	{
		var dialogBox = this.dialogBox;

		if(this.type == 'alert')
		{
			if(this.options.alertBoxOptions.type == 'page')
			{
				dialogBox.inject('content');
			}
			else
			{
				var location = this.options.alertBoxOptions.location;
				this.destroyBoutonChargement(location);	
				
				if(!this.options.alertBoxOptions.mooeditable && this.options.alertBoxOptions.displayType == 'replace')
				{
					dialogBox.replaces(location);
				}
				else
				{
					location.setStyle('display', 'none');
					dialogBox.inject(location,'after');
				}
			}
		}
		else if(this.type == 'prompt')
		{
			var location = this.options.promptBoxOptions.location;
			this.destroyBoutonChargement(location);
			dialogBox.replaces(location);
			
			if(this.options.promptBoxOptions.validate)
			{
				var formName = 'form_' + this.options.promptBoxOptions.variable;
				var promptBox = this;
				
				if(!this.options.promptBoxOptions.onSubmitFunction)
				{
					var o = this.options.promptBoxOptions.onSubmit;
					
					window.addEvent('domready', function(e) {
						new FormCheck(formName, { 
							ajaxOptions: 
							{
								url: o.url,
								method: o.method,
								formData: o.formData,
								otherData: o.otherData,
								responseNull: (!o.responseNull ? function() { new DialogBox('alert', { alertBoxOptions: { location: promptBox } }); } : o.responseNull),
								responseOk: o.responseOk,
								responseError: (!o.responseError ? function() { new DialogBox('alert', {alertBoxOptions: { message: response.errorText, location: promptBox } }); } : o.responseError )
							},
							promptBox: promptBox
						});
					}.bind(this));
				}
				else
				{
					window.addEvent('domready', function(e) {
						new FormCheck(formName, { useOtherAjaxFunction: true, onAjaxRequest: this.options.promptBoxOptions.onSubmitFunction, promptBox: promptBox });
					}.bind(this));
				}
			}
		}
		else if(this.type == 'confirm')
		{
			var location = this.options.confirmBoxOptions.location;
			this.destroyBoutonChargement(location);
			dialogBox.replaces(location);
		}
		
		new Fx.Scroll(window).toElement(dialogBox, -30);
	},
	
	destroyBoutonChargement : function(location)
	{			
		if($('boutonChargement') != null)
		{
			$('boutonChargement').destroy();
			
			if(location.getElement('#dialogBoxButtonYes') != null)
			{
				$('dialogBoxButtonYes').setStyle('display', 'block');
				$('dialogBoxButtonNo').setStyle('display', 'block');
			}
			else
			{
				var buttonLocation;
				
				switch(this.type)
				{
					case 'alert':
						buttonLocation = this.options.alertBoxOptions.button;
						break;
					case 'prompt':
						buttonLocation = this.options.promptBoxOptions.button;
						break;
					case 'confirm':
						buttonLocation = this.options.confirmBoxOptions.button;
						break;
				}
				
				if(!buttonLocation)
				{
					buttonLocation = ( location.getElement('#dialogBoxButton') ? $('dialogBoxButton') : (location.getElement('#controles') ? $('controles') : (location.getElement('#bouton') ? $('bouton') : (($('affichage') != null) ? $('affichage') : false) ) ) );
				}
				else
				{
					buttonLocation = $(buttonLocation);
				}
				
				if(buttonLocation)
				{
					buttonLocation.setStyle('display', 'block');
				}
			}
		}
	},
	
	onFinish : function()
	{
		var dialogBox = this.dialogBox;
		var boutonChargement = new Element('div', {'id': 'boutonChargement', 'class': 'bouton_chargement'});
		
		if(this.type == 'alert')
		{
			if(this.options.alertBoxOptions.type == 'page')
			{
				window.location.replace(this.options.alertBoxOptions.redirection);
			}
			else
			{
				var location = this.options.alertBoxOptions.location;
	
				if(!this.options.alertBoxOptions.onClickOkFunction)
				{
					if(!this.options.alertBoxOptions.formRedirection)
					{
						if(this.options.alertBoxOptions.mooeditable || this.options.alertBoxOptions.displayType != 'replace')
						{
							dialogBox.destroy(); 
							location.setStyle('display', 'block');
						}
						else
						{
							location.replaces(dialogBox);
						}
						$(document.body).getElements('.fc-tbx').setStyle('display', 'block');
					}
					else
					{
						window.location.replace(this.options.alertBoxOptions.redirection);
					}
				}
				else
				{
					var dialogBoxButton = $('dialogBoxButton');
					
					dialogBoxButton.setStyle('display', 'none');
					boutonChargement.inject(dialogBoxButton,'after');
					
					var closeDialogBox = this.options.alertBoxOptions.onClickOkFunction(this);
					
					if(closeDialogBox)
					{
						boutonChargement.destroy();
						dialogBoxButton.setStyle('display', 'block');
						$(document.body).getElements('.fc-tbx').setStyle('display', 'block');
					}
				}
			}
		}
		else if(this.type == 'prompt')
		{
			var dialogBoxButton = $('dialogBoxButton');
			
			dialogBoxButton.setStyle('display', 'none');
			boutonChargement.inject(dialogBoxButton,'after');
				
			var closeDialogBox = this.options.promptBoxOptions.onSubmit(this);
			
			if(closeDialogBox)
			{
				var location = this.options.promptBoxOptions.location;
				location.replaces(dialogBox);
				$(document.body).getElements('.fc-tbx').setStyle('display', 'block');
			}
		}
		else if(this.type == 'confirm')
		{
			var closeDialogBox = false;
			
			if(this.options.confirmBoxOptions.clickYes)
			{
				var dialogBoxButtonYes = $('dialogBoxButtonYes');
				var dialogBoxButtonNo = $('dialogBoxButtonNo');
					
				dialogBoxButtonYes.setStyle('display', 'none');
				dialogBoxButtonNo.setStyle('display', 'none');
				boutonChargement.inject(dialogBoxButtonNo,'after');
				
				if(!this.options.confirmBoxOptions.onClickYesFunction)
				{
					var o = this.options.confirmBoxOptions.onClickYes;
					var confirmBox = this;
					
					new Request.JSON({
						url: o.url,
						method: o.method,
						data : o.data,
						onComplete: function(response)
						{
							if(response == null)			
							{
								(!o.responseNull ? new DialogBox('alert', { alertBoxOptions: { location: confirmBox } }) : o.responseNull(confirmBox) );
							}
							else if(response.error == 0)
							{
								o.responseOk(confirmBox);
							}
							else
							{
								(!o.responseError ? new DialogBox('alert', {alertBoxOptions: { message: response.errorText, location: confirmBox } }) : o.responseError(response.error, response.errorText, confirmBox) );
							}
						}
					}).send();
				}
				else
				{
					closeDialogBox = this.options.confirmBoxOptions.onClickYesFunction(this);
				}
			}
			else
			{
				if(!this.options.confirmBoxOptions.onClickNoFunction)
				{
					closeDialogBox = true;
				}
				else
				{
					closeDialogBox = this.options.confirmBoxOptions.onClickNoFunction(this);
				}
			}
			
			if(closeDialogBox)
			{
				var location = this.options.confirmBoxOptions.location;
				location.replaces(dialogBox);
				$(document.body).getElements('.fc-tbx').setStyle('display', 'block');
			}
		}
	},
	
	restore : function()
	{
		var dialogBox = this.dialogBox;
		var location;
		
		switch(this.type)
		{
			case 'alert':
				location  = this.options.alertBoxOptions.location;
				break;
			case 'prompt':
				location  = this.options.promptBoxOptions.location;
				break;
			case 'confirm':
				location  = this.options.confirmBoxOptions.location;
				break;
		}
		
		location.replaces(dialogBox);
		$(document.body).getElements('.fc-tbx').setStyle('display', 'block');
	}
});