	$(function() {
		
		var name = $("#notify-problem-form-name"),
			email = $("#notify-problem-form-email"),
			message = $("#notify-problem-form-message"),
			allFields = $([]).add(name).add(email).add(message),
			tips = $("#notify-problem-window-validateTips");

		function updateTips(t) {
			tips.text(t).effect("highlight",{},1500);
		}

		function checkLength(o,n,min,max) {

			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("La longueur " + n + " doit etre comprise entre "+min+" et "+max+".");
				return false;
			} else {
				return true;
			}

		}

		function checkRegexp(o,regexp,n) {

			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}

		}
		
		$("#notify-problem-window").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 400,
			width: 500,
			modal: true,
			buttons: {
				'Signaler un probleme': function() {
					var bValid = true;
					var thisWindow = $(this);
					allFields.removeClass('ui-state-error');

					bValid = bValid && checkLength(name,"du nom",3,25);
					bValid = bValid && checkLength(email,"de l'email",6,80);
					bValid = bValid && checkLength(message,"du message",10,500);

					bValid = bValid && checkRegexp(name,/^[a-z]([0-9a-z_ '])+$/i,"Le nom doit comporter que des lettres et des espaces");
					bValid = bValid && checkRegexp(email,/^((([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,"Merci d'indiquer une adresse d'email valide.");
					
					if (bValid) {
						$.post(
							$("#notify-problem-form").attr('action'),
							{name: name.val(), email: email.val(), message: message.val()},
							function(data) {
								//thisWindow.html("<div style='margin-top:100px; text-align:center;'><b>Votre message a bien été transmis à notre équipe de modération et il va être traité dans les meilleurs délais</b></div>");
								allFields.val('');
								updateTips("Votre message a bien été transmis à notre équipe de modération et il va être traité dans les meilleurs délais");
								setTimeout(function(){ thisWindow.dialog("close"); }, 3000);  
								//thisWindow.dialog('close');
							},
							'json'
						);
						
					}
				},
				Cancel: function() {
					updateTips("Mauvaise catégorie, annonce expirée, illégale, arnaque, etc.");
					$(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		
		
		
		$('#notify-problem').click(function() {
			$('#notify-problem-window').dialog('open');
		})
		.hover(
			function(){ 
				//$(this).addClass("ui-state-hover"); 
			},
			function(){
				//$(this).removeClass("ui-state-hover"); 
			}
		).mousedown(function(){
			$(this).addClass("ui-state-active"); 
		})
		.mouseup(function(){
				$(this).removeClass("ui-state-active");
		});

	});
