$(function() {

	var debut = $('#debut');
	var motLiaison = $('input[name=motLiaison]');
	var fin = $('#fin');
	var pseudo = $('#pseudo');
	var email = $('#email');
	var lien_debut = $('#lien_debut');
	var lien_fin = $('#lien_fin');
	

	function isEmail (s) {
	
		// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation
		
		var regex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;
		return regex.test (s);
	
	}
	
	function sendPost (addPseudo) {
	
		$('#poilalaLoading').css('display', 'block');
		
		var datas = {
			'debut' : debut.val(),
			'motLiaison' : motLiaison.serializeArray()[0].value,
			'fin' : fin.val(),
			'lien_debut' : lien_debut.val(),
			'lien_fin' : lien_fin.val()
		};
		
		if (addPseudo) {
			datas.pseudo = pseudo.val();
			datas.email = email.val();
		}
		
		$.post('/post/', datas, function (data) {
			if (data == 'ok') {
				$('#poilalaOK').css ('display', 'block');
				$('#poilalaLoading').css ('display', 'none');
				$('#poilalaNOK').css ('display', 'none');
				$('#poilalaOK').effect("highlight",{},1500);
			} else {
				$('#poilalaOK').css ('display', 'none');
				$('#poilalaNOK').css ('display', 'block');
				$('#poilalaLoading').css ('display', 'none');
				$('#poilalaNOK').effect("highlight",{},1500);
			}
		});
		
		$("#addPseudo").dialog('close');
	}
	
	function resetPoilala () {
		debut.val('');
		motLiaison[0].checked = 'checked';
		fin.val ('');
		
		$([$('#poilalaOK'), $('#poilalaNOK'), $('#poilalaLoading')]).each (function () {
			this.css ('display', 'none');
		});
	}
	
	$("#addPseudo").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 210,
		modal: true,
		buttons: {
			'Non merci': function() {
				sendPost(false);
				$(this).dialog('close');
			},
			'Ajouter': function() {
				var error = false;
				
				if (!(pseudo.val().length > 2 && pseudo.val().length <= 50)) {
					$('#tipsPseudo_pseudo').text ('Votre pseudo doit contenir entre 3 et 50 caractères.').effect("highlight",{},1500);
					error = true;
				}
				
				if (!isEmail (email.val())) {
					$('#tipsPseudo_email').text ('Veuillez vérifier votre adresse email.').effect("highlight",{},1500);
					error = true;
				}
				
				if (!error) {
					sendPost(true);
				}
			}
		},
		close: function() {
			
		}
	});

	
	$('#btn_addPoilala').click(function () {
		
		var error = false;
		
		$([debut, fin]).each (function () {
			if (this.val().length == 0 || this.val() == this.title) {
				this.effect("highlight",{
					color : 'red'
				},1500);
				error = true;
			} else {
				this.removeClass ('error');
			}
		});
		
		if (!error) {
			$('#addPseudo').dialog('open');
		}
	});
	
	$('#btn_resetPoilala').click(function () {
		resetPoilala();
	});
	
	$('textarea.empty').focus (function () {
		if ($(this).val() == 'Ajoutez votre texte ici') {
			$(this).val('').removeClass ('empty');
		}
	});
	
	$('#addPoilala textarea').blur (function () {
		if ($(this).val() == '') {
			$(this).val('Ajoutez votre texte ici').addClass ('empty');
		}
	});
	
	$('.help').each (function () {
		$(this).val(this.title);
		$(this).addClass('empty');
	});
	
	$('.help').focus (function () {
		if ($(this).val() == this.title) {
			$(this).val('').removeClass ('empty');
		}
	});
	$('.help').blur (function () {
		if ($(this).val() == '') {
			$(this).val(this.title).addClass ('empty');
		}
	});
	
	/*
	$('input[name=motLiaison]:selected').each (function () {
		alert ($('input[name=motLiaison]:selected').size());
	});*/
});

function statut (id, statut) {
	
	$.post('/post/?setStatut&id_post='+id+'&statut='+statut, {}, function (t) {
		$($('#post_'+id+' .nbVotes .nb')[0]).html (t).fadeIn();
	});
	
	$($('#post_'+id+' .actions')[0]).hide();
	
	var m = (statut == 'pasContent' ? 'Merci :(' : 'Merci :)');
	
	$($('#post_'+id+' .nbVotes .nb')[0]).fadeOut();
	$($('#post_'+id+' .actions')[0]).html (m).fadeIn();

}
