/* ------------------------ VARIABLES GLOBALES ------------------------ */

var speedShowBoxAlbum = 1500;
var effectShowBoxAlbum = "easeOutExpo";

var speedHideBoxAlbum = 800;
var effectHideBoxAlbum = "easeInExpo";

var speedShowListAlbums = 500;
var effectShowListAlbums = "easeOutExpo";

var speedHideListAlbums = 500;
var effectHideListAlbums = "easeInExpo";

var speedChangePage = 1000;

var currentPage = 1;
var numPagesTotales = 0;

/* ------------------------ INIT ------------------------ */
$(document).ready(function(){
	// On récupère les albums vidéos
	getAlbumsVideos('');
	
	// on initialise la liste déroulante des années
	initListesDeroulantes();
	
	// on initialise le form 
	iniFormSelectAlbum();
});

/* ------------------------ EVENEMENTS ------------------------ */

function initAlbum(){
	
	// OVER/OUT sur l'image de l'album
	$(".imgAlbum").hover(
    	function () {	
			overImgAlbum(this);
      	}, 
      	function () {
	  		outImgAlbum(this);
      	}
    );
}

function iniFormSelectAlbum(){
	// on écoute le changement de la liste déroulante des années
	$('.boxOptionYear .lineOption').click(function(){
		// on remet la page 1
		currentPage = 1;
		// on relance la récupération
		getAlbumsVideos('');
	});
}

/* ------------------------ FONCTIONS ------------------------ */

function overImgAlbum(curDiv){
	$(curDiv).css({"border":"1px solid #0068AB"});
}

function outImgAlbum(curDiv){
	$(curDiv).css({"border":"1px solid #787878"});
}

function getAlbumsVideos(sens){
	aPost = {
		get_albums_videos: true,
		page_to_display: currentPage,
		page: page,
		annee: $('#formSelectAlbum [name=annee]').val()
	};	

	// on soumet le form en ajax
	$.ajax({
		type: "POST",
		url: "ws/medias/get_albums_videos.php",
		data: aPost,
		dataType:"json",
		success:function(returnedJson){			
			if (!returnedJson["b_error"]) {
				// mise à jour de la liste des albums vidéos
				updateContentAlbumsVideos(sens, returnedJson);
			}
		}
	});
}

function updateContentAlbumsVideos(sens, returnedJson){
	// on récupère le html des résultats
	htmlContentAlbumsVideos = returnedJson["html_list_albums"];
	// mise à jour de la pagination
	$('#contentAlbumsVideos .contentPaginationContentAlbumsVideos').html(returnedJson["html_pagination"]);
	// mise à jour du nombre de page
	numPagesTotales = returnedJson["num_pages_totales"];	
	
	// selon le sens de la pagination
	switch(sens)
	{
		case 'next':
			// on ajoute les résultats après ceux affichés
			$('#contentAlbumsVideos .contentDynamic').after(htmlContentAlbumsVideos);
			
			// On cache les résultats affichés (le premier bloc)
			$('#contentAlbumsVideos .contentDynamic:first').slideUp({
				duration:speedChangePage,
				easing:"easeInOutExpo",
				complete:function(){
					// si plus d'un bloc, on supprime le bloc caché
					if($('#contentAlbumsVideos .contentDynamic').length > 1)
					{
						$(this).remove();
					}
				}
			});

			// on affiche les nouveaux résultats (le dernier bloc)
			$('#contentAlbumsVideos .contentDynamic:last').slideDown({
				duration:speedChangePage,
				easing:"easeInOutExpo",
				complete:function(){
					// si plus d'un bloc
					if($('#contentAlbumsVideos .contentDynamic').length > 1)
					{
						// on supprime tous les autres blocs
						$('#contentAlbumsVideos .contentDynamic:not(:last)').remove();
					}
					initContentAlbumsVideosDisplayed();
				}
			});
			break;
		case 'last':
			// on ajoute les résultats avant ceux affichés
			$('#contentAlbumsVideos .contentDynamic').before(htmlContentAlbumsVideos);

			// On cache les résultats affichés (le dernier bloc)
			$('#contentAlbumsVideos .contentDynamic:last').slideUp({
				duration:speedChangePage,
				easing:"easeInOutExpo",
				complete:function(){
					// si plus d'un bloc	
					if($('#contentAlbumsVideos .contentDynamic').length > 1)
					{
						// on supprime le bloc que l'on vient de caché
						$(this).remove();
					}
				}
			});
			
			// on affiche le nouveau contenu (le premier bloc)
			$('#contentAlbumsVideos .contentDynamic:first').slideDown({
				duration:speedChangePage,
				easing:"easeInOutExpo",
				complete:function(){
					// si plus d'un bloc		
					if($('#contentAlbumsVideos .contentDynamic').length > 1)
					{
						// on supprime tous les autres blocs (ceux qui ne sont pas premiers)
						$('#contentAlbumsVideos .contentDynamic:not(:first)').remove();
					}
					initContentAlbumsVideosDisplayed();
				}
			});
			break;
		default:
			// par défaut on affiche directement les résultats
			$('#contentAlbumsVideos #listAlbums').html(htmlContentAlbumsVideos);
			// on montre sans effet le contenu
			$('#contentAlbumsVideos .contentDynamic').show();
			initContentAlbumsVideosDisplayed();
			break;
	}
}

function initContentAlbumsVideosDisplayed(){
	// pngFix
	$('#contentAlbumsVideos #listAlbums').pngFix();
	// On initialise les evenements sur les albums
	initAlbum();
	// initialisation de la pagination
	initPaginationContentAlbumsVideos();
	// mise à jour de la taille du conteneur
	resetHeightSliderContentAlbumsVideos();
}

function resetHeightSliderContentAlbumsVideos()
{
	// initialisation de la hauteur
	var height = 0;
	// pour chaque ligne de résultats
	$('#contentAlbumsVideos .boxAlbum').each(function(){
		// On incrémente la hauteur générale de la hauteur de la ligne
		height += $(this).outerHeight();
	});
	
	// on réinitialise à la fin de l'animation les listes déroulantes pour prendre en compte des changements de position possible
	$('#contentAlbumsVideos #listAlbums').animate({height: height}, 100, initListesDeroulantes);
	$('#contentAlbumsVideos .contentDynamic').animate({height: height}, 100, initListesDeroulantes);
}

function initPaginationContentAlbumsVideos(){
	// click sur le bouton précédent
	$('#contentAlbumsVideos .btnLast').bind('click',handleClickLastPaginationContentAlbumsVideos);
	
	// click sur le bouton suivant
	$('#contentAlbumsVideos .btnNext').bind('click',handleClickNextPaginationContentAlbumsVideos);
	
	// click sur une page
	$('#contentAlbumsVideos .numPage:not(.off)').bind('click',handleClickPagePaginationContentAlbumsVideos);
}

function handleClickLastPaginationContentAlbumsVideos(){
	$(this).unbind('click',handleClickLastPaginationContentAlbumsVideos);
	if(currentPage > 1) currentPage--;
	getAlbumsVideos('last');
}

function handleClickNextPaginationContentAlbumsVideos(){
	$(this).unbind('click',handleClickNextPaginationContentAlbumsVideos);
	if(currentPage < numPagesTotales) currentPage++;
	getAlbumsVideos('next');
}

function handleClickPagePaginationContentAlbumsVideos(){
	$(this).unbind('click',handleClickPagePaginationContentAlbumsVideos);
	curSens = (currentPage > $(this).html()) ? 'last' : 'next';
	currentPage = $(this).html();
	getAlbumsVideos(curSens);
}

