$(document).ready(function(){
	/***********************************************************************/
    // Plugin form traitement des envoi de formulaires
    var options = { 
        //target:        '#output1',   	// target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  	// pre-submit callback 
        success:       showResponse,  	// post-submit callback 
 
        // other available options: 
        url:       	'/moteur/actions_post.php',    // override for form's 'action' attribute 
        type:      	'post'	       	// 'get' or 'post', override for form's 'method' attribute 
        //dataType:   'json'        	// 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        		// clear all form fields after successful submit 
        //resetForm: true        		// reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
	// pre-submit callback 
	function showRequest(formData, jqForm, options) { 
		$('.LogoAttente').show();
	}
	// post-submit callback 
	function showResponse(responseText, statusText)  { 
		jAlert(responseText, 'Information', function(r){
			$('.LogoAttente').hide();
		});
	}
	// Plugin Form
	// Envoi tout les formulaire donc l'id commence par formAjax...
	$('[id^=formAjax]').submit(function() {	   
		$(this).ajaxSubmit(options);
		return false;
     });    
	/* Tabs de base */	
	$(function() {
		$("#tabs").tabs({ cookie: { expires: 30 } });
	});	
	/* Tableau avec 1 ligne sur 2 via tbody */
	$('.uiTable tbody tr:nth-child(even)').addClass('LigneSelected');
	
	/***********************************************************************/
	
	/*
	Page accueil
	*/
    // Tabs accueil
    	$(function() {
		$("#tabsAccueil").tabs({ cookie: { expires: 30 } });
	});
    	$(function() {
		$("#tabsAlbum").tabs({ cookie: { expires: 30 } });
	});
	
	// Google
	$('#cse-search-box').submit(function(){
		if($('#q').val()==''){
			jAlert('Vous devez indiquer une expression de recherche.', 'Attention', function(r){
				$('#q').focus();
			});
			return false;
		}
	});
	
	/********************************************************************************************** 
	Page contact 
	*/
	$('#formContact').submit(function(){
		if($.trim($('#EMail').val())==''){
			jAlert('Vous devez renseigner votre adresse email.', 'Attention',function(r){
				$('#EMail').focus();
			});
			return false;
		}else if($.trim($('#Telephone').val())==''){
			jAlert('Vous devez renseigner votre téléphone.', 'Attention',function(r){
				$('#Telephone').focus();
			});
			return false;
		}else if($.trim($('#Sujet').val())==''){
			jAlert('Vous devez renseigner un sujet.', 'Attention',function(r){
				$('#Sujet').focus();
			});
			return false;
		}else if($.trim($('#Message').val())==''){
			jAlert('Vous devez renseigner un message.', 'Attention',function(r){
				$('#Message').focus();
			});
			return false;
		}else{
			$('.ImgAttente').show();
			$(this).ajaxSubmit({ 
				url			: '/moteur/actions_post.php',    	// override for form's 'action' attribute 
				type			: 'post',	       				// 'get' or 'post', override for form's 'method' attribute 
				success		: function(msg) {
					$('.ImgAttente').hide();
					if(msg==1){
						jAlert('Votre message nous est bien parvenu', 'Information');						
					}else{
						jAlert(msg, 'Information');
					}
				} 
			});
			return false;
		}
	});
	/********************************************************************************************** 
	Page connexion 
	*/
	// Connexion
	$('#formConnexion').submit(function(){
		var Login = $('#Login').val();
		var Passe = $('#Passe').val();
		if(Login==''){
			jAlert('Vous devez indiquer votre <b>nom d\'utilisateur</b>.', 'Attention', function(r){
				$('.ImgAttente').hide();
				$('#Login').focus();
			});
			return false;
		}else if(Passe ==''){
			$('.ImgAttente').show();
			jAlert('Vous devez indiquer votre <b>mot de passe</b>.', 'Attention', function(r){
				$('.ImgAttente').hide();
				$('#Passe').focus();
			});
			return false;
		}else{
			$('.ImgAttente').show();
			$.ajax({
				type: "POST",
				url: "/moteur/actions_post.php",
				data: "Login="+Login+"&Passe="+Passe+"&TFA=Login",
				success: function(msg){
					if(msg==1){
						jAlert('Erreur, <b>nom d\'utilisateur</b> ou <b>mot de passe</b> invalide', 'Information',function(r){
								$('.ImgAttente').hide();
								$('#Passe').val('');
								$('#Passe').focus();
						});
					}else{
						window.location.replace(msg); 
					}
				}
			});
			return false;
		}
	});
	
	// Inscription
	$('#formNewUser').submit(function(){
		var Prenom = $('#Prenom').val();
		var Email = $('#Email').val();
		if($('#News').is(':checked')==true){
			var News = 1;
		}else{
			var News = 0;
		}
		
		if(Prenom==''){
			jAlert('Vous devez indiquer votre <b>Prénom</b>.', 'Attention', function(r){
				$('.AttenteNewCpte').hide();
				$('#Prenom').focus();
			});
			return false;
		}else if(Email ==''){
			jAlert('Vous devez indiquer votre <b>E-Mail</b>.', 'Attention', function(r){
				$('.ImgAttente').hide();
				$('#Email').focus();
			});
			return false;
		}else{
			$('.ImgAttente').show();
			$.ajax({
				type: "POST",
				url: "/moteur/actions_post.php",
				data: "Prenom="+Prenom+"&Email="+Email+"&News="+News+"&TFA=NewUser",
				success: function(msg){
					$('.ImgAttente').hide();
					jAlert(msg, 'Attention');
				}
			});
			return false;
		}
	});
	// Envoi mot de passe
	$('#formDemandePasse').submit(function(){
		var Email = $('#EMailPassePerdu').val();
		if(Email==''){
			jAlert('Vous devez indiquer votre <b>Email</b>.', 'Attention', function(r){
				$('.ImgAttente').hide();
				$('#EMailPassePerdu').focus();
			});
			return false;
		}else{
			$('.ImgAttente').show();
			$.ajax({
				type: "POST",
				url: "/moteur/actions_post.php",
				data: "EMailPassePerdu="+Email+"&TFA=PassePerdu",
				success: function(msg){
					$('.ImgAttente').hide();
					jAlert(msg, 'Information');
				}
			});
			return false;
		}
	});
	// Abonnement/desabonnent
	$('#buttonDesabonnement').click(function(){
		$('#DivButtonDesabonnement').hide();
		$('#DivButtonAbonnement').show();
	});
	// Abonnement/desabonnent
	$('#buttonAbonnement').click(function(){
		$('#DivButtonAbonnement').hide();
		$('#DivButtonDesabonnement').show();
	});
	/**********************************************************************************************
	Page Annuaire
	*/
	$('[id^=ItemLien]').click(function(){
		var id=($(this).attr("id"));
		var tab=id.split('_');
		//alert(tab);
		$.get("/moteur/annuaire_categories.php",
			{ id:tab[1],mc:tab[2] },
			function(data){
				$('#Annuaire').html(data);
		});	
		$.get("/moteur/annuaire_liste.php",
			{ id:tab[1],mc:tab[2] },
			function(data){
				$('#AnnuaireListe').html(data);
		});	
		return false;
	});
	/**********************************************************************************************
	Page Album Photo
	*/
	$('[id^=ItemLienAlbum]').click(function(){
		var id=($(this).attr("id"));
		var tab=id.split('_');
		//alert(tab);
		$.get("/moteur/albumphoto_categories.php",
			{ id:tab[1],mc:tab[2] },
			function(data){
				$('#Album').html(data);
		});	
		$.get("/moteur/albumphoto_liste.php",
			{ id:tab[1],mc:tab[2] },
			function(data){
				$('#AlbumListe').html(data);
		});	
		return false;
	});
	/**********************************************************************************************
	Page Annonce
	*/
	$('#AnnoncesCategories').change(function(){
		$('#formAnnonces').submit();
	});
	//
	$('#formAnnonces').submit(function(){
		$('.ImgAttente').show();
		/*var mc = $('#AnnoncesCategories').val();
		var rc = $('#AnnoncesRecherche').val();
		$.get("/moteur/annonces_liste.php",
			{ mc:mc,rc:rc },
			function(data){
				$('#AnnoncesListe').html(data);
				$('.ImgAttente').hide();
		});	
		return false;*/
	});

	/**********************************************************************************************
	Page Admin annonce
	*/ 
	// Sauvegarde une annonce
	$('#formAnnonce').submit(function(){
		if($('#AdminAnnoncesCategories').val()==0){
			jAlert('Vous devez choisir une catégorie.', 'Attention');
			$('#AdminAnnoncesCategories').focus();
			return false;
		}else if($.trim($('#AnnonceTitre').val())==''){
			jAlert('Vous devez indiquer le titre de votre annonce.', 'Attention');
			$('#AnnonceTitre').focus();
			return false;
		}else if($('#DateDebut').val()=='' || $('#DateDebut').val()=='00/00/0000' || $('#DateFin').val()=='' || $('#DateFin').val()=='00/00/0000'){
			jAlert('Vous devez indiquer une date de début et de fin pour votre annonce.', 'Attention');
			return false;
		}else{
			// Récupere le contenu de fckeditor et le copie dans un champs caché
			 var   content = FCKeditorAPI. GetInstance( 'AnnonceContenu' ).GetXHTML();
			 $('#fckContenu').val(content);
			$(this).ajaxSubmit({ 
				url			: '/moteur/actions_post.php',    	// override for form's 'action' attribute 
				type			: 'post',	       				// 'get' or 'post', override for form's 'method' attribute 
				dataType		: 'json', 
				success		: function(data) {
					//alert(data.NewIDAnnonce);
					// Remplace l'id dans le champs caché si création de l'enregistrement
					$('#tabsAnnonces').data('disabled.tabs', []);
					jAlert('Vos informations sont enregistrées. ', 'Information');
					$('.IDAnnonce').val(data.NewIDAnnonce);
				} 
			});
			return false;
		}
	});
	// Active une annonce
	$('[id^=btnActive]').click(function(){
		var id=($(this).attr("id"));
		var tab=id.split('-');
		$.ajax({
			type: "POST",
			url: '/moteur/actions_post.php',
			data: "ida="+tab[1]+"&TFA=DeactiveAnnonce",
			success: function(msg){
				$('#btnActive-'+tab[1]).hide();
				$('#btnDeActive-'+tab[1]).show();
				jAlert(msg, 'Information');
			}
		});
		return false;
	});
	// Desactive une annonce
	$('[id^=btnDeActive]').click(function(){
		var id=($(this).attr("id"));
		var tab=id.split('-');
		$.ajax({
			type: "POST",
			url: '/moteur/actions_post.php',
			data: "ida="+tab[1]+"&TFA=ActiveAnnonce",
			success: function(msg){
				$('#btnActive-'+tab[1]).show();
				$('#btnDeActive-'+tab[1]).hide();
				jAlert(msg, 'Information');
			}
		});
		return false;
	});
	// Demande de suppresion d'une annonce
	$('[id^=AnnonceSup]').click(function(){
		var id=($(this).attr("id"));
		var tab=id.split('-');
		jConfirm('Souhaitez-vous supprimer cette annonce ?', 'Information', function(r){
			if(r==true){
				$.ajax({
					type: "POST",
					url: '/moteur/actions_post.php',
					data: "ida="+tab[1]+"&TFA=SupAnnonce",
					success: function(msg){
						$('#trAnnonce-'+tab[1]).hide();
						jAlert(msg, 'Information');
					}
				});
			}else{
				return false;
			}
		});
 	});
	// envoi photo annonce
	$('#formPhoto').submit(function(){
		if($('#photos').val()==''){
			jAlert('Vous devez choisir une image.', 'Attention');
			$('#photos').focus();
			return false;
		}else{
			$(this).ajaxSubmit({ 
				url			: '/moteur/actions_post.php',    	// override for form's 'action' attribute 
				type			: 'post',	       				// 'get' or 'post', override for form's 'method' attribute 
				success		: function(msg) {
					//alert(data.NewIDAnnonce);
					jAlert(msg, 'Information');
					$('#photos').val('');
				} 
			});
			return false;
		}
	});
	// Détail annonce
	$('#Message').autogrow();
	
	/**********************************************************************************************
	Page Admin horaires
	*/ 
	$('#formHoraires').submit(function() {	   
		$('#formHoraires').ajaxSubmit(options);
		return false;
	});
	/**********************************************************************************************
	Page Admin Présentation
	*/ 
	$('#formAdminPresentation').submit(function(){
		// Récupere le contenu de fckeditor et le copie dans un champs caché
		 var content = FCKeditorAPI. GetInstance( 'MaPresentation' ).GetXHTML();
		 var content = escape(content);
		 var Titre = $('#Titre').val();
		 $('.ImgAttente').show();
		$.ajax({
			type: "POST",
			url: '/moteur/actions_post.php',
			data: "Titre="+Titre+"&Presentation="+content+"&TFA=SavMaPresentation",
			success: function(msg){
				$('.ImgAttente').hide();
				jAlert('Présentation enregistrée.', 'Attention');
			}
		});
		return false;
	});

	// envoi Logo
	$('#fromAdminLogo').submit(function(){
		if($('#photos').val()==''){
			jAlert('Vous devez choisir une image.', 'Attention');
			$('#photos').focus();
			return false;
		}else{
			$('.ImgAttente').show();
		}
	});

	// envoi photo Annuaire
	$('#formPhotoAnnuaire').submit(function(){
		if($('#photosAnnuaire').val()==''){
			jAlert('Vous devez choisir une image.', 'Attention');
			$('#photosAnnuaire').focus();
			return false;
		}else{
			$('.LogoAttente').show();
			$(this).ajaxSubmit({ 
				url			: '/moteur/actions_post.php',    	// override for form's 'action' attribute 
				type			: 'post',	       				// 'get' or 'post', override for form's 'method' attribute 
				success		: function() {
					$('.LogoAttente').hide();
					jAlert('Photo enregistrée. ', 'Information');
					$('#PhotoAnnauaire').load("/profils/Includes/Service_Annuaire_Photos.php");
				} 
			});
			return false;
		}
	});


}); // END $(document).ready(function(){



