var maxCredits	= 5;
var nbCredits	= 0;
var fxCredits	= [];

function setCreditStatus(cr, disabled) {
	num = cr.id.slice(-1).toInt();
	fxCredits[num].start(disabled ? 0 : cr.scrollHeight);
	// Enable/Disable all inputs in a "credit" div so they won't be reached with "<tab>" when hidden
	cr.getChildren().each(function (p) {
							var tags = p.getChildren("input");
							if (tags != "")
								tags[0].disabled = disabled;
							tags = p.getChildren("select");
							if (tags != "")
								tags[0].disabled = disabled;
						});
}

window.addEvent('domready', function() {
	var cr = $("nbcredits");
	if (cr === null)
		return;
	nbCredits = cr.value; // so we get the right value when the form has been POSTed with errors and more than one credit
	showCreditsS();
	if (nbCredits > 1) {
		$("delcredit").setStyle("visibility", "visible");
		if (nbCredits == maxCredits)
			$("addcredit").setStyle("visibility", "hidden");
	}

	$$("div.dynamic").each(function(el) {
						num = el.id.slice(-1).toInt();
						fxCredits[num]  = new Fx.Style(el, "height");
						setCreditStatus(el, (num > nbCredits));
					});
});

function showCreditsS() {
	// show/hide the plural "s" of the fieldset "Credit(s)" title :
	if (nbCredits > 1)
		$("creditss").setStyle("visibility", "visible");
	else
		$("creditss").setStyle("visibility", "hidden");
}

function addCredit() {
	if (nbCredits < maxCredits) {
		nbCredits++;
		$("nbcredits").value = nbCredits;

		var cr = $("credit_" + nbCredits);
		setCreditStatus(cr, false);
		showCreditsS();

		if (nbCredits == maxCredits)
			$("addcredit").setStyle("visibility", "hidden");
		$("delcredit").setStyle("visibility", "visible");
	}
}

function delCredit() {
	if (nbCredits > 1) {
		var cr = $("credit_" + nbCredits);
		setCreditStatus(cr, true);

		nbCredits--;
		$("nbcredits").value = nbCredits;
		showCreditsS();

		if (nbCredits == 1)
			$("delcredit").setStyle("visibility", "hidden");
		$("addcredit").setStyle("visibility", "visible");
	}
}


