/**
 * Mets en place les actions javascripts du menu.
 */
$(document).ready(
		function() {
			initAjax('product');
			// menu
			$('#submenu .specification label').click(
					function() {
						var id = $(this).attr('for');
						$('#' + id).attr('checked', !$('#' + id).attr('checked'));
						if($('#' + id).attr('checked'))
							$('#showhide-qty-' + id).hide();
						else
							$('#showhide-qty-' + id).show();
						updateContent(1);
						return false;
					}
			);
			$('#submenu .specification input').click(
					function() {
						var id = $(this).attr('id');
						if($('#' + id).attr('checked'))
							$('#showhide-qty-' + id).hide();
						else
							$('#showhide-qty-' + id).show();
						updateContent(1);
					}
			);
			// navigation par page
			if($('ul.navigation').length)
				$('ul.navigation').navigationBar(updateContent);
			// Ajout au panier
			$('a.add-to-cart').live('click', function() {
				var href = $(this).attr('href');
				var reg = new RegExp('(buy|achat)-p([0-9]+)', '');
				var result = reg.exec(href);
				var id = '';
				if(result)
					id = result[2];
				initAjaxAddToCart({productId: id});
				return false;
			});
			// Zoom sur image
			initZoom();
			// Comparaison
			initAjaxCompare('product');
			// Accordeon des menus
			initAccordion();
			// 
			initAjaxCheckProductAvailability();
		});

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($('#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: 'ajaxUpdateCategory',
			categoryId: $('#categoryId').val(),
			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]+'[]'] = [];
	$('#submenu .section input:checked').each(
			function() {
				data[$(this).attr('name')].push($(this).val());
			});
	$.ajax({
		data: data,
		success: function(s) {
			s = $.trim(s);
			if(s.length > 0)
				$('#products-list').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));
		}
	});
}

