// JavaScript Document
function switchTab(onglet, contenu, id_membre) {
	
    document.getElementById('infosMembres').style.display = 'none';
    document.getElementById('listeArticlesMembre').style.display = 'none';
    document.getElementById('listeDiscussionsMembre').style.display = 'none';
    document.getElementById('formulaireContact').style.display = 'none';
	document.getElementById('infosFavoris').style.display = 'none';
	
    document.getElementById('contenu3').style.display = 'none';
	
    //document.getElementById(contenu).style.display = 'block';    
    $("#"+contenu).fadeIn("slow");
	
	if(contenu=='listeArticlesMembre') {
		pagination('listeArticlesMembre',id_membre,'getPostsMembre',1);
	}
	if(contenu=='listeDiscussionsMembre') {
		pagination('listeDiscussionsMembre',id_membre,'getArticleMembre',1);
	}
	

    document.getElementById('tab_1').className = '';
    document.getElementById('tab_2').className = '';
    document.getElementById('tab_3').className = '';  
    document.getElementById('tab_4').className = '';
	document.getElementById('tab_5').className = '';
    document.getElementById(onglet).className = 'ongletActifMembre';
} 

function pagination(zone, id_membre, action, nbPages) {

    $.post("../actionMembre", {
        page:nbPages,
        action:action,
        id_membre:id_membre
    },
    function(data) {
        $("#"+zone).html(data);
        document.getElementById(zone).style.display = 'block';
    });
    
//$("#"+zone).load("essai.php?nbpage="+nbPages);
}

function testEmail(email) {
    var new_string = new String(email);
    if (!new_string.match('^[-_\.0-9a-zA-Z]{1,}@[-_\.0-9a-zA-Z]{1,}[\.][0-9a-zA-Z]{2,}$')) {
        return false;
    }
    else {
        return true;
    }
}


function envoieMail() {
    email = $("#mailMembre").val();
    message = $("#message").val();
    captcha = $("#captcha").val();
    sujet = $("#sujetMembre").val();
    id_membre = $("#id_membre").val();

    if(captcha!="" && message!="" && email!="") {
        if(testEmail(email)) {

            contenuForm = $("#zoneFormulaireInfosMembre").html();
            $("#zoneFormulaireInfosMembre").html("Veuillez patienter...");
            //alert(sujet+message+captcha);

            $.post("../postMail", {email:email,sujet:sujet,message:message,captcha:captcha,id_membre:id_membre},
            function(resultPost) {
                //alert(resultPost);
                if(resultPost==0) {
                $("#zoneFormulaireInfosMembre").html("Erreur d'envoi, veuillez réessayer plus tard.<br /> \n\
                <a href=\"javascript:afficheFormMail();\">Cliquer ici pour recommencer</a>");

                } else if(resultPost==1) {
                    $("#zoneFormulaireInfosMembre").html("Votre message a bien été envoyé.<br /> \n\
                <a href=\"javascript:afficheFormMailVide();\">Voulez vous en envoyer un autre ?</a>");

                } else if(resultPost==2) {
                    $("#zoneFormulaireInfosMembre").html("Erreur d'envoi, adresse mail invalide.<br /> \n\
                <a href=\"javascript:afficheFormMail();\">Cliquer ici pour recommencer</a>");

                } else if(resultPost==4) {
                    $("#zoneFormulaireInfosMembre").html("Problème de longueur des données envoyées.<br /> \n\
                <a href=\"javascript:afficheFormMail();\">Cliquer ici pour recommencer</a>");

                } else if(resultPost==5) {
                    $("#zoneFormulaireInfosMembre").html("Code de confirmation non valide.<br /> \n\
                <a href=\"javascript:afficheFormMail();\">Cliquer ici pour recommencer</a>");

                }
            });

        } else {
            alert("Adresse mail invalide.");
        }
    } else {
        alert("Veuillez remlir la zone message et la zone du code de validation.");
    }
}

function afficheFormMail() {
    $("#zoneFormulaireInfosMembre").html(contenuForm);
    $("#mailMembre").val(email);
    $("#message").val(message);
    $("#sujetMembre").val(sujet);
    time=new Date();
    $("#imgCaptcha").attr('src',"http://mobcustom:81/captcha?t"+time.getTime());
}

function afficheFormMailVide() {
    $("#zoneFormulaireInfosMembre").html(contenuForm);
    time=new Date();
    $("#imgCaptcha").attr('src',"http://mobcustom:81/captcha?t"+time.getTime());
}



//Redim images
$(document).ready(function() {
    $('.avatarSmall').each(function() {
        var maxWidth = 80; // Max width for the image
        var maxHeight = 200;    // Max height for the image
        var ratio = 0;  // Used for aspect ratio
        var width = $(this).width();    // Current image width
        var height = $(this).height();  // Current image height
 
        // Check if the current width is larger than the max
        if(width > maxWidth){
            ratio = maxWidth / width;   // get ratio for scaling image
            $(this).css("width", maxWidth); // Set new width
            $(this).css("height", height * ratio);  // Scale height based on ratio
            height = height * ratio;    // Reset height to match scaled image
            width = width * ratio;    // Reset width to match scaled image
        }
 
        // Check if current height is larger than max
        if(height > maxHeight){
            ratio = maxHeight / height; // get ratio for scaling image
            $(this).css("height", maxHeight);   // Set new height
            $(this).css("width", width * ratio);    // Scale width based on ratio
            width = width * ratio;    // Reset width to match scaled image
        }
    });
});



function gestionFavoris(onglet, contenu) {
    document.getElementById('marquesFavoris').style.display = 'none';
    document.getElementById('articlesFavoris').style.display = 'none';
    $("#"+contenu).fadeIn("slow");

    document.getElementById('lMarquesFavoris').className = '';
    document.getElementById('lArticlesFavoris').className = '';
    document.getElementById(onglet).className = 'actif';
} 







	
