// Rend la pagination et les entête de liste ajaxienne
function ajaxLinksPages() {
	$('.pagination a,table.products thead th a').each(function(){
		$(this).click(function(){
			$.ajax({
				type:"GET",
				url:window.location.pathname+unescape(encodeURIComponent(unescape($(this).attr('href').substring($(this).attr('href').indexOf('?'))))),

				dataType:"text",
				success: function(html) {
					$('.pagination,.products').remove();
					$('#main-content').append(html);
					ajaxLinksPages();
				}

			});
			return false;
		});
	});
}

// Pour fermer la popup d'erreur ajax
function closeErrorAjax()
{
	$('#errorAjax').fadeOut();
}

$(document).ready(function() {
	
	var form = $('.prd-list-tools form');
	
	// Activation des filtres en ajax
	$('.prd-list-tools form select').each(function(){
		$(this).change(function(){
			$.ajax({
				type:form.attr('method'),
				url:window.location.pathname,
				data:form.serialize()+"&cat="+id_category,
				dataType:"text",
				cache:false,
				success: function(html) {
					$('.pagination,.products').remove();
					$('#main-content').append(html);
					ajaxLinksPages();
				}
			});		
		});
	});
	
	// Activation de la pagination en ajax
	ajaxLinksPages();
	
	//Suppression du bouton pour filtrer
	$('.prd-list-tools form input[type="submit"]').remove();
	$('.prd-list-tools ').css('padding','5px 4px');
	
	// Ajout de la popup d'erreur
	$('#content').prepend('<div style="position:absolute"><div id="errorAjax" style="position:absolute;display:none;left:257px;border:solid 5px #B32F34;background-color:white;padding:4px;width:500px;top:150px;text-align:center;">Erreur dans le traitement de la requête.<br/>Veuillez vérifier votre connexion à Internet ou réessayez ultérieurement.<br/><input type="submit" onclick="closeErrorAjax();" value="Fermer" /></div></div>');
	  
	// Trigger exécutés 
	// lors de l'envoi de la requete ajax
	$('#ajaxLoading').ajaxStart( function(){$(this).fadeIn();} );
	// Erreur de la réponse
	$('#ajaxLoading').ajaxError( function(){$(this).fadeOut();
											$('#errorAjax').fadeIn();
									} );
	// Succès de la réponse
	$('#ajaxLoading').ajaxSuccess( function(){$(this).fadeOut();} );
});