﻿﻿/**
 * Gestion de la page liste produit
 */

function Rd(numFound, ttPage){Rayon.display(numFound, ttPage);}


var Rayon = {
	ajaxurl: PATHRACINE + "/AjaxRayon",
	numPage: 1,
	rows: "",
	sort: "",
	facet: "",
	barre: null,	// Element : Barre de pagination
	checkbox: null, // Dernière Checkbox selectionné
	
	init:{
		query: null, 
		ttPage: 1,
		search: "",
		marque: ""
	},
	
	load: function(param){
		$.extend(this, this.init, param);
		this.barre = $(".barre");
		
		$(".btn_g", this.barre).click(function(){
			Rayon.action("page", -1);
			return false;
		});
		
		$(".btn_d", this.barre).click(function(){
			Rayon.action("page", +1);
			return false;
		});
		
		$(".btn_rows", this.barre).click(function(){
			Rayon.action("rows", $(this).attr("req"));
			return false;
		});
		
		$(".btn_sort", this.barre).change(function(){
			$(".btn_sort").val($(this).val());
			Rayon.action("sort", $(this).val());
		});
		
		$(".menu_gauche>.filtres :checkbox").click(function(){
			Rayon.maj(1, this);
		});
	},
	
	// Action sur le rayon
	action: function(param, value){
		if(param == "page"){
			// Pagination : Page Suivante, Precedente
			var newPage = this.numPage + parseInt(value);
			if(newPage > 0 && newPage <= this.ttPage){
				this.maj(newPage);
			}
			
		}else if(param == "rows"){
			// Nombre de produit par page
			if(parseInt(value)){
				this.rows = value;
				this.maj(1);
			}
			
		}else if(param == "sort"){
			// Definit l'ordre d'affichage (prix croissant ou pas) 
			this.sort = value;
			this.maj(1);
		}
	},
	
	// MAJ de la page
	maj: function(newPage, check){
		if(newPage != null){
			this.numPage = newPage;
		}
		if(check != null){
			this.checkbox = check.getAttribute("name");
			this.facet = this.getFacet();
		}

		var params = {'q': this.query, 'facet': this.facet, 'sort': this.sort, 'rows': this.rows, 'numPage': this.numPage, 'search': this.search, 'marque': this.marque};
		$.getScript(this.ajaxurl +"?"+ PARAMGLOBAL +"&"+ jQuery.param(params));
	},
	
	// Affiche le résultat
	display: function(numFound, ttPage){
		this.ttPage = ttPage;
		Produit.display();
		Facette.display();
		$(".numPage", this.barre).text((this.numPage < 10) ? "0" + this.numPage : this.numPage);
		$(".numFound", this.barre).text(numFound);
		$(".ttPage", this.barre).text(ttPage);
	},
	
	// Genere la requete pour les facettes
	getFacet: function(){
		var result = "";
		var input = new Array();
		
		$(".menu_gauche>.filtres :checked").each(function(){
			var name = $(this).attr("name");
			var value = $(this).val();
			
			if(name != "facet_queries"){
				value = name +':"'+  value +'"';
			}
			
			input[name] = (input[name] != null) ? input[name] +" OR "+ value : value;
		});
		
		for(field in input){
			if(result != ""){result += " AND ";}
			result += "(" + input[field] + ")";
		}
		return result;
	}
}



// Raccourci de fonctions
function P(id, lib, prix, prixcents, prixbarre, img, tabStick, urlProd){Produit.add(id, lib, prix, prixcents, prixbarre, img, tabStick, urlProd);}
function Pe(img){Produit.ems(img);} 	// 	Incrustation EMS
function B(param){Produit.sep(param);}	//	Placement des separateur

// Classe de MAJ des produits (description, prix, img)
var Produit = {
	body: "",
	barre: [
		'<div class="bg_pcarre_v floatl"><img height="244" border="0" width="1" alt="" src="'+ PATHRES +'/img/00-commun/1ptrans.gif"/></div>', 
		'<div class="bg_pcarre_h clear"><img height="1" border="0" width="769" alt="" src="'+ PATHRES +'/img/00-commun/1ptrans.gif"/></div>'
	],

	ems: function(html){
		this.body += '<div class="zone"><div class="expert">'+ html +'</div></div>';	
	},
	
	sep: function(param){
		this.body += this.barre[param];
	},
	
	add: function (id, lib, prix, prixcents, prixbarre, img, tabStick, urlProd){
		var stickhaut =	(tabStick[0] != "") ? '<img height="20" border="0" width="100" alt="" src="'+tabStick[0]+'"/>': '';
		var stickbas = 	(tabStick[1] != "") ? '<img height="30" border="0" width="100" alt="" src="'+tabStick[1]+'"/>': ''; 
		var stickprix = (tabStick[2] != "") ? '<img height="20" border="0" width="100" alt="" src="'+tabStick[2]+'"/>': '';
		
		var classprix = (prixbarre != "") ? 'chiffre_1 prix_rouge': 'chiffre prix_bleu';
		var prix 	  = '<div class="'+ classprix +'">'+ prix +',<span>'+ prixcents +'</span> &#128;</div>';
		var prixbarre = (prixbarre != "") ? '<div class="chiffre_2">'+ prixbarre +' &#128;</div>': '';
		
		var img_prod = '<a href="'+ urlProd +'"><img height="92" border="0" width="92" alt="'+ lib +'" src="'+ PATHIMAGES +'/'+ img +'"/></a>';
		
		this.body += '<div class="produit_generique">'
		 + '<div class="article_generique">'
		 + '<div class="haut">' + stickhaut + '</div>'
		 + '<div class="achatexpress bgpng" rel="'+ id +'"/>'
		 + '<div class="centre bgpng">'+ img_prod +'</div>'
		 + '<div class="bas">' + stickbas + '</div></div>'
		 + '<div class="designation"><a href="'+ urlProd +'">'+ lib +'</a></div>'
		 + '<div class="prix prix_2">'
		 + '<div class="txt">A partir de</div>'+ prix + prixbarre +'</div>'
		 + '<div class="flag">'+ stickprix +'</div>'
		 + '</div>';
	},
	
	display: function(){
		$(".page_liste>#liste").html(this.body + '<div class="clear"></div>');
		QL.load();
		this.body = "";
	}
}


function F(name, value, Count){Facette.add(name, value, Count)}

// Classe de MAJ des facettes (disabled ou pas)
var Facette = {
	fields: new Array(),
	
	add: function(name, value, Count){
		if(this.fields[name] == null)
			this.fields[name] = new Array();
		this.fields[name][value] = Count;
	},
	
	display: function(){
		$(".menu_gauche>.filtres :checkbox").each(function() {
			var name = $(this).attr("name");
			var value = $(this).val();
			
			var count = (Facette.fields[name] != null ) ? Facette.fields[name][value] : 0;
			
			if(parseInt(count) > 0) {
				$(this).removeAttr('disabled');
			}else if(Rayon.checkbox != name && $(this).attr('checked') != true) {
				$(this).attr("disabled", "disabled");
			}
		});
		
		this.fields = new Array();
	}
}