function ajaxForm(oForm) {
	$('input:submit', oForm).attr('disabled', 'disabled');
	
	$(oForm).append('<img src="/t-annonces/images/loading.gif" alt="" id="loading" />');
	$("#loading").css({ position: 'absolute', top: '1px', right: '1px' });
	
	var sChamps = $(":input", oForm).serialize() + '&ajax=' + $(oForm).attr('id');
	$.post('/p-annonces/formulaires.php', sChamps, function (data, textStatus) {
		data = data.split('|');
		
		if (!data || data.length != 3)
			alert('Err. data corrupted.');
		
		$("#loading").remove();
		
		var context = $("#" + data[0]); /*
		$("span.succes,span.erreur", context).remove();
		$("p", context).prepend('<span class="' + data[1] + '">' + data[2] + '</span>');
		*/
		alert(data[2]);
		if (data[1] == 'succes')
			$(":text,textarea", context).val('');
		
		$('input:submit', oForm).removeAttr('disabled');
	} ) ;
}

$(document).ready( function () {
	  // Les formulaires en ajax
	$("a[rel=ajaxForm]").each( function () {
		var sAncre = this.href.match(/.*(#.+)/) ? this.href.match(/.*(#.+)/)[1] : '';
		$(this).click( function () {
			$(sAncre).popup({ sTitre: $(sAncre).children("h3:first").hide().text(), iMinWidth: 570, iMinWidth: 700 });
			return false;
		} );
	} );
	
	  // Les menu déroulants des catégories
	$("#post_category").change(function () {
		$("#post_child option[value!='']").remove();
		
		var sSelected =  $(this).val();
		if (!sSelected || !aIds[sSelected] || aIds[sSelected].length < 1)
			return;
		
		for (var iI = 0, iChildren = aIds[sSelected].length; iI < iChildren; iI++)
			if (aIds[sSelected][iI])
				$("#post_child").append('<option value="' + aIds[sSelected][iI] + '">' + aNames[aIds[sSelected][iI]] + '</option>');
		
		Fat.fade_element('post_child', 30, 1000);
	} ) ;
	if (!$("#post_child").val()) // En cas de "Page précédente"
		$("#post_category").change();
	
	  // Liens et catégories bicolors
	$("ul").each( function () { $("li:odd", this).addClass("odd"); $("li:first", this).addClass("first"); $("li:last", this).addClass("last"); } ) ;
	
	  // Liens Autres catégories
	$("#autres-categories ul").hide();
	$("#autres-categories").attr('id', 'autres-categories-dynamique').children(".titreEnImageArrondis").addClass('titreAutres')
	.children(".arrondisGauche,.arrondisDroit").remove();
	$("#liste-autres-categories").attr('id', 'liste-autres-categories-dynamique');
	$("#autres-categories-dynamique h2").hover( function () { $(this).addClass('hover'); }, function () { $(this).removeClass('hover'); } )
	.click(function () {
		$("#autres-categories-dynamique ul").popup({ sTitre: $(this).text(), iHeight: 379 });
	} ) ;
	
	  // Lien Déposer
	$("#nav-deposer a[href*='deposer']").toggle(
		function () {
			if ($("#popupDeposer").length)
				$("#popupDeposer").show();
			else {
				$("#nav-deposer").append('<div id="popupDeposer"></div>').css('position', 'relative');
				$("#popupDeposer").load(this.href + " #pageDeposer > div");
			}
			return false;
		},
		function () {
			$("#popupDeposer").hide();
			return false;
		}
	);
	
	  // Lien Vendre
	$("#vendre,#annuler").click( function () { $("#acheterVendre ul").toggleClass('enCours'); return false; } );
	
	  // Lien Assistance / contact
	$("#nav-contact > a").click( function () {
		$('<div id="pageNousContacter"></div>').load(this.href + " #pageNousContacter > form", { }, function () {
			$(this).popup({ sTitre: $("#nav-contact > a").text(), iWidth: 630, iHeight: 350 });
			$(".ajaxForm").submit( function () { ajaxForm(this); return false; } );
		} ) ;
		return false;
	} ) ;
	
	  // Lien Conditions générales
	$("#footer a:eq(1)").click( function () {
		$('<div id="pageConditions"></div>').load(this.href + " #pageConditions > dl", { }, function () {
			$(this).popup({ sTitre: $("#footer a:eq(1)").text(), iWidth: 600, iHeight: 450 });
		} ) ;
		return false;
	} ) ;
	
	  // Forumulaires
	fnPreloadImage('/t-annonces/images/loading.gif');
	$(".ajaxForm").submit( function () { ajaxForm(this); return false; } );
} ) ;