/**
 * 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();
	// Zoom sur image
	initInquiry();
	// Comparaison
	initAjaxCompare("product");
	// Accordeon des menus
	initAccordion();
	// Accord�on du tableau de sp�cifications
	initSpecifAccordion();
	// Restaurer la page en AJAX
	if(document.location.hash.length > 0) {
		restoreAnchor();
	}
});

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 restoreAnchor() {
	var regGeneral = new RegExp('^#([a-z]+)-([0-9]+)(.*)-p([0-9]+)$', 'i');
	var resultGeneral = regGeneral.exec(document.location.hash);
	if(resultGeneral) {
		var action = resultGeneral[1];
		var p = resultGeneral[4];
		if('ajaxUpdateCategory' == action) {
			var regCategory = new RegExp('^-(.*)-min([0-9]+)-max([0-9]+)$', 'i');
			var resultCategory = regCategory.exec(resultGeneral[3]);
			if (resultCategory) {
				var min = resultCategory[2];
				var max = resultCategory[3];
				var arrSections = resultCategory[1].split('|');
				var sections = {sections: ''};
				for(var i in arrSections) {
					var regSection = new RegExp('^([a-z_0-9]+)(\[[0-9,]*\])$', 'i');
					resultSection = regSection.exec(arrSections[i]);
					if(resultSection) {
						sections['sections'] += resultSection[1] + ',';
						sections[resultSection[1] + '[]'] = resultSection[2].substring(1, resultSection[2].length - 1).split(',');
					}
				}
				sections['sections'] = sections['sections'].substring(0, sections['sections'].length - 1);
				updateContent(action, resultGeneral[2], sections, min, max, p);
			}
		}
		else {
			updateContent(action, resultGeneral[2], p);
		}
	}
}

function updateContent() {
	var data = {};
	if(arguments.length > 1) {
		data['action'] = arguments[0];
		if('ajaxUpdateCategory' == arguments[0]) {
			data['categoryId'] = arguments[1];
			data = $.extend({}, data, arguments[2]);
			data['min'] = parseFloat(arguments[3]);
			data['max'] = parseFloat(arguments[4]);
			data['page'] = arguments[5];
			if(data['max'] != 0) {
				setTimeout(function() {
					$('#price-slider').slider('values', 0, data['min']);
					$('#price-slider').slider('values', 1, data['max']);
					$('#anyPrice').attr('checked', null);
				}, 200);
			}
		}
		else if('ajaxUpdateCollectionList' == arguments[0]) {
			data['departmentId'] = arguments[1];
			data['page'] = arguments[2];
		}
	}
	else if(arguments.length == 1) {
		data = {
				action: ($('#categoryId').val()) ? 'ajaxUpdateCategory' : 'ajaxUpdateCollectionList',
						categoryId: $('#categoryId').val(),
						departmentId: $('#departmentId').val(),
						page: arguments[0],
						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 input:checked').each(
					function() {
						if ($(this).attr("name"))
							data[$(this).attr('name')].push($(this).val());
					});
		}
	}
	else {
		return;
	}

	$.ajax({
		data: data,
		success: function(s) {
			s = $.trim(s);
			if(s.length > 0) {
				$("#liste").html(s);
				$("html, body").animate({scrollTop: '0px'}, 300);
			}
		},
		complete: function(xhr) {
			updateAnchor(data);
			updateMenu($.parseJSON(xhr.getResponseHeader("JSON-MENU"), true));
			viewAjaxInfos($.parseJSON(xhr.getResponseHeader("JSON-MSG"), true));
			$.navigationBar.ajaxInit($.parseJSON(xhr.getResponseHeader("JSON-NAVIGATION-BAR"), true));
		}
	});
}

