/**
 * Mets en place les actions javascripts du menu.
 */
$(document).ready(
		function() {
			initAjax("rebate-center");
			// menu
			$("#specifications label").click(
					function() {
						var id = $(this).attr("for");
						$("#" + id).attr("checked", !$("#" + id).attr("checked"));
						if($("#" + id).attr("checked"))
							$("#showhide_" + id).hide();
						else 
							$("#showhide_" + id).show();
						updateContent(1);
						return false;
					}
			);
			$("#specifications input").click(
					function() {
						var id = $(this).attr("id");
						if($("#" + id).attr("checked"))
							$("#showhide_" + id).hide();
						else 
							$("#showhide_" + id).show();
						updateContent(1);
					}
			);
			// navigation par page
			if($("ul.navigation").length)
				$("ul.navigation").navigationBar(updateContent);
			// Accordeon des menus
			initAccordion();
			// Comparaison
			initAjaxCompare();
		});

function updateMenu(jsonObj) {
	if(jsonObj) {
		for(var s in jsonObj) {
			var taille = jsonObj[s].length;
			for (var i = 0; i < taille; i++) {
//				if(jsonObj[s][i].quantity == 0) {
//					$("#li_" + s + "_" + jsonObj[s][i].id).hide();
//					$("#" + s + "_" + jsonObj[s][i].id).attr("disabled", "disabled");
//				}
//				else {
//					$("#li_" + s + "_" + jsonObj[s][i].id).show();
//					$("#" + s + "_" + jsonObj[s][i].id).removeAttr("disabled");
//					$("#qty_" + s + "_" + jsonObj[s][i].id).text(jsonObj[s][i].quantity);
//				}
					$("#qty_" + s + "_" + jsonObj[s][i].id).text(jsonObj[s][i].quantity);
			}
		}
	}
}

function updateContent(num) {
	var sections = $("#sections").val();
	var data = {
			action: "ajaxUpdateContent",
			sections: sections,
			page: num
	};
	var arrSections = sections.split(",");
	// ajoute les attributs à l'objet data
	var taille = arrSections.length;
	for (var i = 0; i < taille; i++)
		data[arrSections[i]+"[]"] = [];
	$("#specifications input:checked").each(
			function() {
				data[$(this).attr("name")].push($(this).val());
			});
	$.ajax({
		data: data,
		success: function(s) {
			s = $.trim(s);
			if(s.length > 0)
				$("#results").html(s);
		},
		complete: function(xhr) {
			updateMenu($.parseJSON(xhr.getResponseHeader("JSON-MENU"), true));
			viewAjaxInfos($.parseJSON(xhr.getResponseHeader("JSON-MSG"), true));
			$.navigationBar.ajaxInit($.parseJSON(xhr.getResponseHeader("JSON-NAVIGATION-BAR"), true));
		}
	});
}

