var photosEquipe = new Array();
var activeMembreIndex = 0;
$(document).ready(function(){
    //AFFICHE LE BON MEMBRE DE L'ÉQUIPE (/EQUIPE.PHP)
    
    // CACHE TOUS LES MEMBRES, AFFICHE LE PREMIER
    $(".membre").hide();
    
    $("#menuEquipe li a").click(function(){

        // RETIRE LA CLASSE "CURRENT" À TOUS
        $("#menuEquipe li a").removeClass("current");
		
        activeMembreIndex = $("#menuEquipe li a").index(this);
        $(".membre").hide();
		$(".membre").eq(activeMembreIndex).show();
		
        // AJOUTE LA CLASS "CURRENT"
        $(this).addClass("current");
		
		// Construit les 3 thumbnails
		buildThumbnails();
		
		// Affiche la première photo
		showBigPhoto(0);
		
		// Stop toutes les videos
		stopAllVideos();
		
        return false;
    });
    //AFFICHE LA BONNE PHOTO DU MEMBRE DE L'ÉQUIPE (/EQUIPE.PHP)
    /*$(".photos a").click(function(){
        
        var photoCode = $(this).attr('href').substr(1); // photo-georgesBardagi1
		
        // GROUPE
        var mainDivCode = photoCode.substr(6);
        mainDivCode = mainDivCode.substr(0, mainDivCode.length-1); // georgesBardagi
		
        // CACHE LES PHOTOS DU GROUPE
        $("div#"+mainDivCode+" .photo").hide();
        $("img#"+photoCode).fadeIn();
		
        return false;
    });*/
    
    // OUVRE LE PREMIER
    $("#menuEquipe li a:first").trigger("click");
    
    //AFFICHE LE BON MEMBRE DE L'ÉQUIPE SI LA REQUÊTE VIENT D'UNE AUTRE PAGE
    var txtURI = document.location.href ;
    ancorID = txtURI.substring(txtURI.lastIndexOf("#") +1);
    $("#menuEquipe li a[href='#" + ancorID + "']").trigger("click");
	
    // Videos dans le corps
	buildVideos();
	
	// Preload images
	for (var key in photosEquipe) {
		for (var i=0; i < photosEquipe[key].length; i++) {
			jQuery.preLoadImages( photosEquipe[key][i] );	
		}
	}
});

function buildThumbnails() {
	var photosArr = photosEquipe[activeMembreIndex];
	var thumbnailsCtnr = $("div.membre:eq("+activeMembreIndex+") .photos");
	var anchor = $("div.membre:eq("+activeMembreIndex+")").attr("id");
	thumbnailsCtnr.empty();
	for (var i = 0; i < photosArr.length; i++) {
		var tmpName = anchor + (i+1).toString();
		thumbnailsCtnr.append('<a href="#photo-'+tmpName+'"><img id="thumb-'+tmpName+'" src="lib/createThumbnail.php?src='+photosArr[i]+'&type=jpg&width=110&height=110" width="110" height="110" /></a>');
	}
	$("a", thumbnailsCtnr).click(function(){
									clickThumbnail($(this));
								});
}

function clickThumbnail(sel) {
	var href = sel.attr("href");
	var photoIndex = Number(href.substr( href.length-1 )) - 1;
	showBigPhoto(photoIndex);
}

function showBigPhoto(photoIndex) {
	//alert("Show photo ("+activeMembreIndex+") : "+photoIndex);
	var photoCtnr = $("div.membre .col-01").eq(activeMembreIndex);
	var photoId = "photo"+activeMembreIndex+"-"+photoIndex;
	var photo = $('<img id="'+photoId+'" class="mainPhoto photo" src="'+photosEquipe[activeMembreIndex][photoIndex]+'" />');
	photoCtnr.find("img").remove();
	photoCtnr.append(photo);
	$("img#"+photoId).hide().fadeIn();
}

function equipe_addPhoto(key, path) {
	photosEquipe[key].push(path);
}