//JavaScrip Document
$(document).ready(function(){
	$(window).load(chargement);
	$("div[class=zone]").click(anim);
	$("input").click(paint);
	$("div.edt li").click(edt_clic);
	$("div.edt ul").mousedown(protec);
	
	function paint()
	{
		var paint = $(this);
		if (paint[0].value != 0)
		{
			$("label[class="+paint[0].name+"]").css('color','#801C55');
			$("label[class="+paint[0].name+"]").css('font-weight','bold');
		}
		else
		{
			$("label[class="+paint[0].name+"]").css('color','gray');
			$("label[class="+paint[0].name+"]").css('font-weight','400');
		}
		var inputs = $("input");
		var max = inputs.length;
		for (var i = 0;i<max;i++)
		{
			if (inputs[i].value == 1)
			{
				var jour = inputs[i].name.substr(0,inputs[i].name.lastIndexOf('_'));
				var heure = inputs[i].name.substr(inputs[i].name.lastIndexOf('_') + 1);
				$("ul[id="+jour+"] li[rel="+heure+"]").addClass("colored");
			}
		}
	}
	function chargement()
	{
		$("div[class=line]").hide();
		$("div[class=first]").hide();
		$("div[class=elem]").hide();
		var nom_0 = $("div.competence input[value=0][checked=true]");
		var nom_1 = $("div.competence input[value=1][checked=true]");
		var nom_2 = $("div.competence input[value=2][checked=true]");
		var nom_3 = $("div.competence input[value=3][checked=true]");
		for (var i = 0;i<nom_0.length;i++)
		{
			$("div.competence label[class="+nom_0[i].name+"]").css('color','gray');
			$("div.competence label[class="+nom_0[i].name+"]").css('font-weight','400');
		}
		for (var i = 0;i<nom_1.length;i++)
		{
			$("div.competence label[class="+nom_1[i].name+"]").css('color','#801C55');
			$("div.competence label[class="+nom_1[i].name+"]").css('font-weight','bold');
		}
		for (var i = 0;i<nom_2.length;i++)
		{
			$("div.competence label[class="+nom_2[i].name+"]").css('color','#801C55');
			$("div.competence label[class="+nom_2[i].name+"]").css('font-weight','bold');
		}
		for (var i = 0;i<nom_3.length;i++)
		{
			$("div.competence label[class="+nom_3[i].name+"]").css('color','#801C55');
			$("div.competence label[class="+nom_3[i].name+"]").css('font-weight','bold');
		}
		var inputs = $("#inputs input");
		var max = inputs.length;
		for (var i = 0;i<max;i++)
		{
			if (inputs[i].value == 1)
			{
				var jour = inputs[i].name.substr(0,inputs[i].name.lastIndexOf('_'));
				var heure = inputs[i].name.substr(inputs[i].name.lastIndexOf('_') + 1);
				$("ul[id="+jour+"] li[rel="+heure+"]").addClass("colored");
			}
		}
	}
	function anim()
	{
		var zone = $(this).parent().attr('id');
		if ($("div[id="+zone+"] div[class=line]").css('display') == "none")
		{
		$("div[id="+zone+"] div[class=line]").fadeIn(1000);
		$("div[id="+zone+"] div[class=first]").fadeIn(1000);
		$("div[id="+zone+"] div[class=elem]").show(1000);
		}
		else 
		{
		$("div[id="+zone+"] div[class=line]").fadeOut(1000);
		$("div[id="+zone+"] div[class=first]").fadeOut(1000);
		$("div[id="+zone+"] div[class=elem]").hide(1000);
		}
	}
	/* Fonction protection clic */
	function protec() {return false;}
	/* Fonction event clic */ 
	function edt_clic()
	{
		var li = $(this);
		var heure = li.attr("rel");
		var jour = li.parent().attr("id");
		var temp = 0;
		var cumul = 0;
		/* Si clic sur case */
		if (heure < 24)
		{
			var inputs = $("input[name="+jour+"_"+heure+"]");
			if (inputs[0].value == 0){inputs[0].value = 1;li.addClass("colored");}
			else {inputs[0].value = 0;li.removeClass("colored");}
		}
		/*   Si clic sur le matin  */
		if (heure == 30)
		{
			for (var j = 8;j<12;j++)
			{
				var inputs = $("input[name="+jour+"_"+j+"]");
				if (inputs[0].value == 0)break;
				else cumul++;
			}
			if (cumul == 4)
			{
				for (var j = 8;j<12;j++)
				{
					var inputs = $("input[name="+jour+"_"+j+"]");
					inputs[0].value = 0;
					$("ul[id="+jour+"] li[rel="+j+"]").removeClass("colored");
				}
			}
			else 
			{
				for (var j = 8;j<12;j++)
				{
					var inputs = $("input[name="+jour+"_"+j+"]");
					inputs[0].value = 1;
					$("ul[id="+jour+"] li[rel="+j+"]").addClass("colored");
				}
			}
		}
		/* Si clic sur l'après midi */
		if (heure == 31)
		{
			for (var j = 12;j<20;j++)
			{
				var inputs = $("input[name="+jour+"_"+j+"]");
				if (inputs[0].value == 0)break;
				else cumul++;
			}
			if (cumul == 8)
			{
				for (var j = 12;j<20;j++)
				{
					var inputs = $("input[name="+jour+"_"+j+"]");
					inputs[0].value = 0;
					$("ul[id="+jour+"] li[rel="+j+"]").removeClass("colored");
				}
			}
			else 
			{
				for (var j = 12;j<20;j++)
				{
					var inputs = $("input[name="+jour+"_"+j+"]");
					inputs[0].value = 1;
					$("ul[id="+jour+"] li[rel="+j+"]").addClass("colored");
				}
			}
		}
		/* Si clic sur la nuit  */		
		if (heure == 32)
		{
			for (var j = 20;j<24;j++)
			{
				var inputs = $("input[name="+jour+"_"+j+"]");
				if (inputs[0].value == 0)break;
				else cumul++;
			}
			for (var j = 0;j<8;j++)
			{
				var inputs = $("input[name="+jour+"_"+j+"]");
				if (inputs[0].value == 0)break;
				else cumul++;
			}
			if (cumul == 12)
			{
				for (var j = 0;j<8;j++)
				{
					var inputs = $("input[name="+jour+"_"+j+"]");
					inputs[0].value = 0;
					$("ul[id="+jour+"] li[rel="+j+"]").removeClass("colored");
				}
				for (var j = 20;j<24;j++)
				{
					var inputs = $("input[name="+jour+"_"+j+"]");
					inputs[0].value = 0;
					$("ul[id="+jour+"] li[rel="+j+"]").removeClass("colored");
				}
			}
			else 
			{
				for (var j = 0;j<8;j++)
				{
					var inputs = $("input[name="+jour+"_"+j+"]");
					inputs[0].value = 1;
					$("ul[id="+jour+"] li[rel="+j+"]").addClass("colored");
				}
				for (var j = 20;j<24;j++)
				{
					var inputs = $("input[name="+jour+"_"+j+"]");
					inputs[0].value = 1;
					$("ul[id="+jour+"] li[rel="+j+"]").addClass("colored");
				}
			}
		}
		/* Si clic sur un jour */
		if (heure == 33)
		{
			for (var i = 0;i<24;i++)
			{
				var inputs = $("input[name="+jour+"_"+i+"]");
				if (inputs[0].value == 0)break;
				else cumul++;
			}
			if (cumul == 24)
			{
				for (var i = 0;i<24;i++)
				{
					var inputs = $("input[name="+jour+"_"+i+"]");
					inputs[0].value = 0;
					$("ul[id="+jour+"] li[rel="+i+"]").removeClass("colored");
				}
			}
			else
			{
				for (var i = 0;i<24;i++)
				{
					var inputs = $("input[name="+jour+"_"+i+"]");
					inputs[0].value = 1;
					$("ul[id="+jour+"] li[rel="+i+"]").addClass("colored");
				}
			}
		}
	}
})