/**
 * Mets en place les actions javascripts du menu.
 */
$(document).ready(
		function() {
			initAjax("product");
			// 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, a.add-to-cart").live("click", function() {
				var href = $(this).attr('href');
				var inCollection = false;
				var reg = new RegExp('(buy|achat)-p([0-9]+)', '');
				if(href.indexOf('/co') > 0)
					inCollection = true;
				var result = reg.exec(href);
				var id = '';
				if(result) {
					id = result[2];
					if(inCollection)
						initAjaxAddToCart({collectionId: id});
					else
						initAjaxAddToCart({productId: id});
				}
				return false;
			});
			// Restaurer les filtres
			restoreFilters();
			// Zoom sur image
			initZoom();
			// Comparaison
			initAjaxCompare("product");
			// Accordéon des menus
			initAccordion();
			// Accordéon du tableau de spécifications
			initSpecifAccordion();
		});

function updateMenu(jsonObj) {
	if(jsonObj) {
		var taille = jsonObj.specifications.length;
		for (var i = 0; i < taille; i++) {
			$("#qty_" + jsonObj.specifications[i].groupId + "_" + jsonObj.specifications[i].id).text(jsonObj.specifications[i].quantity);
			if(jsonObj.specifications[i].quantity == 0) {
				$("#li_"+jsonObj.specifications[i].groupId+"_"+jsonObj.specifications[i].id).fadeOut("slow");
			} else {
				$("#li_"+jsonObj.specifications[i].groupId+"_"+jsonObj.specifications[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 data = {
			action: ($('#categoryId').val()) ? 'ajaxUpdateCategory' : 'ajaxUpdateCollectionList',
			categoryId: $('#categoryId').val(),
			departmentId: $('#departmentId').val(),
			page: num,
			min: ($('#anyPrice').length == 0 || $('#anyPrice').attr('checked')) ? 0 : $('#price-slider').slider('values', 0),
			max: ($('#anyPrice').length == 0 || $('#anyPrice').attr('checked')) ? 0 : $('#price-slider').slider('values', 1)
	};
	// Pour les listes de produits
	if($('#sections').length) {
		var sections = $('#sections').val();
		data['sections'] = sections;
		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() {
				if ($(this).attr("name"))
					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));
		}
	});
}

