var MCP = {
	init: function(options){
		this.options = $extend({
			removeForm: true,
			nameError : '<p>Vous avez oublié d\'indiquer votre pseudo ;)</p>',
			emailError : '<p>Vous avez oublié d\'indiquer votre adresse email ou elle n\'est pas valide.</p>',
      questionError: '<p>Vous n\'avez pas répondu à la question, si vous n\'êtes pas un robot, elle ne devrait pas être trop dure... ;)</p>',
			msgError : '<p>Où est votre commentaire ?</p>',
			inmoderation : '<p>Merci de votre contribution ! Votre commentaire est en cours de modération : il s\'affichera dès que je l\'aurai lu et vérifié que ce n\'est pas du spam.</p>'
		}, options || {});
		$('commentform').addEvent('submit', function(ent) {
			this.sendForm(ent);
			return false;
		}.bind(this));
	},
	sendForm : function(ent) {
		try {
			var error = '';
			if($('commentform').author && $('commentform').author.value == '') error = ''+MCP.options.nameError;
			else if($('commentform').email && !(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test($('commentform').email.value))) error = ''+MCP.options.emailError;
      else if($('commentform').response && $('commentform').response.value == '') error = ''+MCP.options.questionError;
			else if($('commentform').comment.value == '' || $('commentform').comment.value == $('commentform').comment.title) error = ''+MCP.options.msgError;
			if(error !='') 
				MCP.errorMsg(error);
			else {
				$('commentform').addClass('chargement');
				$('commentform').send({
					evalScripts : false,
					onFailure : function(transport) {
						try {
							er = transport.responseText.match(/<p>(.*)<\/p>/);
							if(er.length) er = er[1];
							MCP.errorMsg(er);
							MCP.mcpDone();
						} catch(e) {
							MCP.errorMsg("Erreur AJAX - Cela peut-être dû à votre navigateur ou bien au serveur. Veuillez réessayer.");
							$('commentform').removeEvents('submit');
							$('commentform').fireEvent('submit');
						}
					},
					onSuccess : function() {
            MCP.errorMsg("Votre message a bien été envoyé, merci !");
            MCP.mcpDone();
						var tr = this.response.text.split(/<body[^>]*?>/);
							tr = tr[1].split(/<\/body>/);
						tempDump = new Element('div').setHTML(tr[0]);
						if($E('.commentlist')){
							if($E('.commentlist').getChildren().length == $E('.commentlist', tempDump).getChildren().length)
								new Element('li').setHTML(MCP.options.inmoderation).addClass('in-moderation').injectInside($E('.commentlist'));
							else $E('.commentlist', tempDump).getLast().injectInside($E('.commentlist'));
						}
						else {
							if($E('ol',tempDump)) el = $E('ol',tempDump);
							else el = $E('ul',tempDump);
							if(!$E("comments",el) && $E('#comments',tempDump)) $E('#comments',tempDump).injectBefore($('commentform'))
							el.injectBefore($('commentform'));
						}
						tempDump.remove();
					}
				});
			}
			new Event(ent).stop();
			return false;
		} catch(e) {
			return true;
		}
	},
	removeForm : function() {
		if(MCP.options.removeForm) {
			new Fx.Slide('commentform',{duration:1800,onComplete:function() {$('commentform').remove(); if($('respond')) $('respond').remove()}}).slideOut();
		} else
			$('comment').value = '';
	},
	errorMsg : function(errorMessage) {
		MCP.mcpDone();
		var err = new Element('div', {'class': 'error' }).setHTML(errorMessage).injectBefore($('comment'));
		var errSlide = new Fx.Slide(err);
		var errFx = new Fx.Style(err, 'opacity', {duration:10000});
		errFx.start(1,1).chain(function(){
			errFx.start(1,1);
		}).chain(function(){
			errFx.start(1,1);
		}).chain(function() {
			errSlide.slideOut();
		});
	},
	mcpDone: function () {
		$('commentform').removeClass('chargement');
	}
};
window.addEvent('domready', function() {	if($('commentform')) MCP.init((window.mcpOptions) ? mcpOptions :  {});	});