$(document).ready(function(){

	// POUR LE RECHERCHE
	$(document).ready(function(){
	  /////////////////////////////////////////////////////////// IE bug //////////////////////////////////////////////////////////////////////////////////////
		if ($.browser.msie) {
			$(".mini_classement_fiche").css("marginLeft", "-15px");
			$("#conteneur_meme_lettre").css("marginTop", "-5px");
			$("#table_abcedaire_index").css("marginLeft", "5px");
			var version = $.browser.version.substring(0, 1);
		  $("#resCherche").css("margin-left", "-300px");
		  $("#resCherche").css("margin-top", "30px");
		}

	  cacheChamps();
	  
	  $("#rechercheChamp").keyup(function(event){
	    var r = $("#rechercheChamp").val();
	    if(r != "") {
	      affChamps();
	      $.post("rechercheFicheSerie.php", { name : r}, function(data){
	        $("#resCherche").empty();
	        $("#resCherche").append(data);
	      });
	    }else {
	      cacheChamps();
	    }
	  });
	  
	  $(document).click(function () {
	    cacheChamps();
	  });
	  
	  function cacheChamps() {
	    $("#resCherche").hide();
	  }
	  
	  function affChamps() {
	    $("#resCherche").show();
	  }
	});
	
	// POUR LE SYSTEME DE VOTE
	var bool_vote = false;
	$(".col_vote_series:first").css("background-color", "#e6c8e0");
	$(".col_vote_series:first").children(".cont_vote_series").css("visibility", "visible");
	$(".col_vote_series").mouseenter(function() {
		show_vote($(this));
		$(this).css("background-color", "#e6c8e0");
		$(this).children(".cont_vote_series").css("visibility", "visible");
	});
	
	$(".col_vote_series").mouseleave(function() {
		$(this).css("background-color", "#ffffff");
		$(this).children(".cont_vote_series").css("visibility", "hidden");
	});

	$(".vote_series").fadeTo(200, 0.4);
	$(".vote_series").mouseenter(function() {
		$(this).fadeTo(200, 1);
	});
	
	$(".vote_series").mouseleave(function() {
		$(this).fadeTo(200, 0.4);
	});
	$(".vote_series_chiffre").click(function() {
		var chiffre = $(this).html();
		$(this).parent(".vote_series").parent(".cont_vote_series").prev(".res_vote_series").html(chiffre + "/5");
		updateTotal();
	});
	
	function show_vote(obj) {
		$(".col_vote_series").css("background-color", "#ffffff");
		$(".col_vote_series").children(".cont_vote_series").css("visibility", "hidden");
		$(this).css("background-color", "#e6c8e0");
		$(this).children(".cont_vote_series").css("visibility", "visible");
	}
	
	function updateTotal() {
		var newTt = 0;
		$(".res_vote_series").each(function(i){
			if($(this).html().replace("/5", "") != "") {
				newTt += parseInt($(this).html().replace("/5", ""));
			}
		});
		$("#total_vote_series").html(newTt + "/20");
		check_vote();
	}
	
	$("#btn_vote_series").fadeTo(0, 0.4);
	$("#btn_vote_series").click(function() {
		if(bool_vote) {
			$('#vote_realisation').val($('.res_vote_series:eq(0)').html().replace("/5", ""));
			$('#vote_ecriture').val($('.res_vote_series:eq(1)').html().replace("/5", ""));
			$('#vote_casting').val($('.res_vote_series:eq(2)').html().replace("/5", ""));
			$('#vote_ensemble').val($('.res_vote_series:eq(3)').html().replace("/5", ""));
			$('#form_vote_series').submit();
		}
	});
	
	function check_vote() {
		if(($('.res_vote_series:eq(0)').html().replace("/5", "") != "")
			&& ($('.res_vote_series:eq(1)').html().replace("/5", "") != "")
				&& ($('.res_vote_series:eq(2)').html().replace("/5", "") != "")
					&& ($('.res_vote_series:eq(3)').html().replace("/5", "") != "")) {	
			bool_vote = true;
			$("#btn_vote_series").fadeTo(300, 1);
		}
	}
	
	$("#table_vote_series").mouseleave(function() {
		if($('.res_vote_series:eq(0)').html().replace("/5", "") == "") {
			$(".col_vote_series:eq(0)").css("background-color", "#e6c8e0");
			$(".col_vote_series:eq(0)").children(".cont_vote_series").css("visibility", "visible");
		}else if($('.res_vote_series:eq(1)').html().replace("/5", "") == "") {
			$(".col_vote_series:eq(1)").css("background-color", "#e6c8e0");
			$(".col_vote_series:eq(1)").children(".cont_vote_series").css("visibility", "visible");
		}else if($('.res_vote_series:eq(2)').html().replace("/5", "") == "") {
			$(".col_vote_series:eq(2)").css("background-color", "#e6c8e0");
			$(".col_vote_series:eq(2)").children(".cont_vote_series").css("visibility", "visible");
		}else if($('.res_vote_series:eq(3)').html().replace("/5", "") == "") {
			$(".col_vote_series:eq(3)").css("background-color", "#e6c8e0");
			$(".col_vote_series:eq(3)").children(".cont_vote_series").css("visibility", "visible");
		}
		if(($('.res_vote_series:eq(0)').html().replace("/5", "") != "")
				&& ($('.res_vote_series:eq(1)').html().replace("/5", "") != "")
					&& ($('.res_vote_series:eq(2)').html().replace("/5", "") != "")
						&& ($('.res_vote_series:eq(3)').html().replace("/5", "") != "")) {
			
			$(".vote_series").fadeTo(0, 0.2, function() {
			
				var c1 = 5 - $('.res_vote_series:eq(0)').html().replace("/5", "");
				$(".col_vote_series:eq(0)").children(".cont_vote_series").children(".vote_series:eq(" + c1 + ")").fadeTo(0, 1);
				$(".col_vote_series:eq(0)").children(".cont_vote_series").css("visibility", "visible");
	
				var c2 = 5 -$('.res_vote_series:eq(1)').html().replace("/5", "");
				$(".col_vote_series:eq(1)").children(".cont_vote_series").children(".vote_series:eq(" + c2 + ")").fadeTo(0, 1);
				$(".col_vote_series:eq(1)").children(".cont_vote_series").css("visibility", "visible");
	
				var c3 = 5 -$('.res_vote_series:eq(2)').html().replace("/5", "");
				$(".col_vote_series:eq(2)").children(".cont_vote_series").children(".vote_series:eq(" + c3 + ")").fadeTo(0, 1);
				$(".col_vote_series:eq(2)").children(".cont_vote_series").css("visibility", "visible");
	
				var c4 = 5 -$('.res_vote_series:eq(3)').html().replace("/5", "");
				$(".col_vote_series:eq(3)").children(".cont_vote_series").children(".vote_series:eq(" + c4 + ")").fadeTo(0, 1);
				$(".col_vote_series:eq(3)").children(".cont_vote_series").css("visibility", "visible");
			});
		}
	});
});
