/**
 * Mets en place les actions javascripts du menu.
 */
$(document).ready(
		function() {
			initAjax("liquidation-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);
//			// Ajout au panier
//			$("div.panier a.addToCart").live("click", function() {
//				var href = $(this).attr('href');
//				var reg = new RegExp("productId=([0-9]+)", "");
//				var result = reg.exec(href);
//				var id = '';
//				if(result)
//					id = result[1];
//				initAjaxAddToCart({productId: id});
//				return false;
//			});
			// Restaurer les filtres
			restoreFilters();
			// Zoom sur image
			initZoom();
			// Comparaison
			initAjaxCompare("liquidation-center");
			// Accordeon des menus
			initAccordion();
			// Accordéon du tableau de spécifications
			initSpecifAccordion();
		});

function updateMenu(jsonObj) {
	if(jsonObj) {
		for(var s in jsonObj.sections) {
			var taille = jsonObj.sections[s].length;
			for (var i = 0; i < taille; i++) {
				$("#qty_" + s + "_" + jsonObj.sections[s][i].id).text(jsonObj.sections[s][i].quantity);
				if(jsonObj.sections[s][i].quantity == 0) {
					$("#li_"+s+"_"+jsonObj.sections[s][i].id).fadeOut("slow");
				} else {
					$("#li_"+s+"_"+jsonObj.sections[s][i].id).fadeIn("slow");				
				}
			}
		}
		if($('#anyPrice').attr('checked')) {
			$('#price-slider').slider('values', 0, jsonObj.prices.min);
			$('#price-slider').slider('values', 1, jsonObj.prices.max);
		}
	}
}

function updateContent(num) {
	var sections = $("#sections").val();
	var data = {
			action: "ajaxUpdateContent",
			sections: sections,
			page: num,
			min: ($('#anyPrice').attr('checked')) ? 0 : $("#price-slider").slider("values", 0),
			max: ($('#anyPrice').attr('checked')) ? 0 : $("#price-slider").slider("values", 1)
	};
	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 .groupe input:checked").each(
			function() {
				data[$(this).attr("name")].push($(this).val());
			});
	$.ajax({
		data: data,
		success: function(s) {
			s = $.trim(s);
			if(s.length > 0)
				$("#liste").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));
		}
	});
}

