
$(document).ready(function() {
	jQuery("#choix_saveurs  input[type=checkbox]").removeAttr("checked");
});


function ecouteur(){
	jQuery("#choix_saveurs .checkbox").click(function(){
		var input = jQuery(this).next("input");
		var nbChecked = jQuery("#choix_saveurs input[type=checkbox]:checked").length;

		if(nbChecked<5){
			var tab = jQuery(this).next("input").attr("id").split(/_/, 2);
			if(input.attr("checked")){
				var color = jQuery(this).parent().parent().parent().parent().attr("class").split(/_/, 2);
				var text = jQuery(this).parent("td").next("td").children("label").html();
				addToCast(tab[1], color[1], text);
			} else {
				removeToCast(tab[1]);
			}

			if(nbChecked==4){
				jQuery("#btn_valide").show();
			} else {
				jQuery("#btn_valide").hide();
			}
		} else {
			jQuery(this).css("background-position", "0pt 0px");
			input.removeAttr("checked");
		}
	});
}

var casting = new Array(false, false, false, false);
var textBaseCasting = new Array(
	jQuery("#text_casting_1").html(),
	jQuery("#text_casting_2").html(),
	jQuery("#text_casting_3").html(),
	jQuery("#text_casting_4").html()
);

function addToCast(id, color, text) {
	for(var i in casting){
		if(!casting[i]){
			var index = ++i;
			--i;
			var cc = "#chiffre_casting_"+index;
			jQuery(cc).addClass("selected");
			var lc = "#lots_casting_"+index;
			jQuery(lc).html(jQuery("<img>", {src:baseUrlImageSaveur.replace(/%s/, color), alt:""}));
			var colorCast = "#color_casting_"+index;
			jQuery(colorCast).removeAttr("class").addClass("color_"+color);
			var tc = "#text_casting_"+index;
			jQuery(tc).html(text);
			casting[i] = id;
			break;
		}
	}
}

function removeToCast(id){
	var index = -1;
	if(!Array.indexOf){
		for(var i=0; i<casting.length; i++){
			if(casting[i]==id){
				index = i;
				break;
			}
		}
	} else {
		index = casting.indexOf(id);
	}

	if(index>=0){
		index++;
		var cc = "#chiffre_casting_"+index;
		jQuery(cc).removeClass("selected");
		var lc = "#lots_casting_"+index;
		jQuery(lc).html("");
		var colorCast = "#color_casting_"+index;
		jQuery(colorCast).removeAttr("class");
		var tc = "#text_casting_"+index;
		jQuery(tc).html(textBaseCasting[--index]);
		casting[index] = false;
	}
}
