var m_filmEnCours = 1;
var m_photoCharge = 0;


function afficherDescription(numero) {
	var description = document.getElementById("Film-Description");
	var titre = document.getElementById("Film-Titre");
	var lien = document.getElementById("Film-Lien");
	description.innerHTML = tableauFilm[numero - 1].description;
	titre.innerHTML = tableauFilm[numero - 1].titre;
	lien.href = "/films/details.php?id=" + tableauFilm[numero - 1].numero;
	lien.innerHTML = "SUITE &gt;";

	/*if(numero == 1)
		$("#Bouton-Precedent").css('display' , 'none');
	else
		$("#Bouton-Precedent").css('display' , 'block');

	if(numero == tableauFilm.length)
		$("#Bouton-Suivant").css('display' , 'none');
	else
		$("#Bouton-Suivant").css('display' , 'block');*/

}

function initFilms() {
	var conteneur = document.getElementById("CarousselFilm-Cadre");
	for(var iFilm = 0; iFilm < tableauFilm.length; iFilm++) {
		conteneur.innerHTML += '<img rel="' + (iFilm + 1 ) +'" id="Photo-Film-' + (iFilm + 1 ) +'" class="photo-film"  src="'+ tableauFilm[iFilm].src +'" alt="" />';
	}
	
	
	
	$('#CarousselFilm-Cadre img').load(function(){
		m_photoCharge++;
		var description = document.getElementById("Film-Description");
		if(m_photoCharge == tableauFilm.length) {
			$("#Bouton-Suivant").css('display' , 'block');
			$("#Bouton-Precedent").css('display' , 'block');

			$(".photo-film").css("display", "block");

			for(var iPhoto = 2; iPhoto <= tableauFilm.length; iPhoto++) {
				var left= 40 + (90 * iPhoto );
				$("#Photo-Film-" + iPhoto).animate({"left": left+"px","height":"109px","width":"76px"}, 700);
			}
			
			$(".photo-film").click(function(){
				if(this.getAttribute("rel") == m_filmEnCours)
					location.href = "/films/details.php?id=" + tableauFilm[m_filmEnCours - 1].numero;
				else
					allerA(this.getAttribute("rel"));
			});
			
			afficherDescription(1);
		}
	});
}

function allerA(numero) {
	if(numero > m_filmEnCours) {
		while(m_filmEnCours != numero) {
			$("#Bouton-Suivant").click();
		}
	} else {
		while(m_filmEnCours != numero) {
			if(m_filmEnCours > tableauFilm.length)
				m_filmEnCours = 1;
			$("#Bouton-Suivant").click();
		}
	}
}

$(document).ready(function () {
	
	$("#Bouton-Precedent").click(function(){
	
		if(!(m_filmEnCours - 1 < 1)) {
			m_filmEnCours = m_filmEnCours - 1;
			$("#Photo-Film-" + (m_filmEnCours + 1)).animate({"left":"130px","height":"109px","width":"76px"}, 400);
			$(".photo-film").animate({"left": "+=90px"}, 400);
			$("#Photo-Film-" + m_filmEnCours).animate({"bottom":"-109px"}, 400).animate({"left":"-90px","bottom":"0px"}, 1).animate({"left":"0px","width":"200px","height":"301px"}, 400);
		 } else {
			m_filmEnCours = tableauFilm.length;
			$("#Photo-Film-" + (1)).animate({"left":"130px","height":"109px","width":"76px"}, 400);
			$(".photo-film").animate({"left": "+=90px"}, 400);
			$("#Photo-Film-" + m_filmEnCours).animate({"bottom":"-109px"}, 400).animate({"left":"-90px","bottom":"0px"}, 1).animate({"left":"0px","width":"200px","height":"301px"}, 400);
		 }
	  
		afficherDescription(m_filmEnCours);
	});
	
	$("#Bouton-Precedent").mouseenter(function() {
		$("#Bouton-Precedent").css('background-position' , '-38px');
		$("#Bouton-Precedent").css('cursor' , 'pointer');
	}).mouseleave(function() {
		$("#Bouton-Precedent").css('background-position' , '0px');
		$("#Bouton-Precedent").css('cursor' , 'auto');
	});
	
	$("#Bouton-Suivant").mouseenter(function() {
		$("#Bouton-Suivant").css('background-position' , '-58px');
		$("#Bouton-Suivant").css('cursor' , 'pointer');
	}).mouseleave(function() {
		$("#Bouton-Suivant").css('background-position' , '0px');
		$("#Bouton-Suivant").css('cursor' , 'auto');
	});

	$("#Bouton-Suivant").click(function(){
	  
		if(!(m_filmEnCours + 1 > tableauFilm.length)) {
			m_filmEnCours = m_filmEnCours + 1;
			$(".photo-film").animate({"left": "-=90px"}, 400);
			$("#Photo-Film-" + m_filmEnCours).animate({"left":"0px","width":"200px","height":"301px"}, 400);
			var decalage = 40 + (90 * (tableauFilm.length) );
			$("#Photo-Film-" + (m_filmEnCours - 1)).animate({"left":"-=120px"}, 200).animate({"height":"109px","width":"76px","bottom":"-109px"}, 0).animate({"left":decalage+"px"}, 0).animate({"bottom":"0px"},400);

		} else {
			m_filmEnCours = 1;
			$(".photo-film").animate({"left": "-=90px"}, 400);	
			$("#Photo-Film-" + m_filmEnCours).animate({"left":"0px","width":"200px","height":"301px"}, 400);
			var decalage = 40 + (90 * (tableauFilm.length) );
			$("#Photo-Film-" + (tableauFilm.length)).animate({"left":"-=120px"}, 200).animate({"height":"109px","width":"76px","bottom":"-109px"}, 0).animate({"left":decalage+"px"}, 0).animate({"bottom":"0px"},400);

		}
		

		afficherDescription(m_filmEnCours);
	});
	
	initFilms();

});


