var Rayon = {
	ajaxurl: PATHRACINE + "/AjaxListProduct",	// Servlet executée ajax
	redirectUrl : PATHRACINE + "/ListProductCategorie",
	numPage: 1,							// Numero de la page
	rows: "",							// Nombre de réponse par page
	sort: "",							// Ordre de trie des produit
	facet: "",							// Requete Temporaire pour facette
	from : "",  						// Différencie le clic catégorie du click facette
	search: "", 						// mot moteur de recherche
	nivCategorie:1, 					// niveau de catégorie pour les requêtes solr
	
	// Valeur par défaut
	init:{
		idCategorie: null,	// Id de catégorie
		nivCategorie: null, // Niveau de la catégorie
		ttPage: 1			// Nombre de page totale
	},
	
	load: function(param){
		$.extend(this, this.init, param);
		
		// Gestionnaire d'evenement
		// Initialise l'history

//		var firstCheckedFacet=$(".facette :checked[name != '']");
//		if(firstCheckedFacet.length >= 1 ){
//			Rayon.queryFacet(firstCheckedFacet,$(firstCheckedFacet[0]).attr("name"));
//		}
		
//        $.historyInit(this.pageload, location.pathname);
//
//        $("a[rel='history']", Rayon.divPagination).live("click", function () {
//            var hash = this.href.replace(/^.*#/, '');
//            $.historyLoad(hash);
//            return false;
//      });


		
		$(".btnRows").click(function(){
			if(Rayon.nivCategorie>1)
				Rayon.nivCategorie = Rayon.nivCategorie-1;
			
			Rayon.action("rows", $(this).attr("req"));
			return false;
		});
		
		$(".btnSort").change(function(){
			if(Rayon.nivCategorie>1)
				Rayon.nivCategorie = Rayon.nivCategorie-1;
			
			$(".btnSort").val($(this).val());
			Rayon.action("sort", $(this).val());
		});
		
		$(".facette :checkbox[name]").click(function(){
			//if(Rayon.nivCategorie>2){
			    $(".facette :checkbox[name]").unbind("click");
			    $(".facette :checkbox[name]").attr('disabled', 'disabled');
			    if(Rayon.nivCategorie>1)
			    	Rayon.nivCategorie = Rayon.nivCategorie-1;
			//}
			Rayon.queryFacet(this, $(this).attr("name"));
		});
		
		// Gestion de la redirection dans la navigation par facettes (gammes) pour les niveaux 4 & 5 (Descente par marques et par univers)
		$(".liste_1 > li > a[req]").click(function(){
			//$(".liste_1 > li > a[req]").removeClass("bold");
			//$(this).addClass("bold");
			if( $(".liste_1 > li > a[req]").hasClass("bold") ) {		// Si le bold est présent, on est sur la derniere catégorie
				Rayon.nivCategorie = Rayon.nivCategorie - 1;			// Donc, on ne change pas de niveau dans la nav par facettes gammes
			}
			Rayon.action("categorieFromCategorie", $(this).attr("req"));
			return false;
		});
		
		$("#titre_gamme").click(function(){
			$("#gamme").slideDown("slow");
			$("#ligne_produit").slideUp("slow");
		});
		
		$("#titre_ligne_produit").click(function(){
			$("#gamme").slideUp("slow");
			$("#Facette").slideUp("slow");
			$("#ligne_produit").slideDown("slow");
		});
		$(".btnNext").click(function(){
			if(Rayon.nivCategorie>1)
				Rayon.nivCategorie = Rayon.nivCategorie-1;
			
			Rayon.action("page",Rayon.numPage+1);
		});
		$(".btnBack").click(function(){
			if(Rayon.nivCategorie>1)
				Rayon.nivCategorie = Rayon.nivCategorie-1;
			
			Rayon.action("page",Rayon.numPage-1);
		});
		
		
	},
	
		
	// Action sur la page
	action: function(param, value){
		switch (param) {
		case "page":
			// Pagination : Page Suivante, Precedente
			//var newPage = this.numPage + parseInt(value);
			//if(newPage < 1 || newPage > this.ttPage){
			//	return false;
			//}
			//this.numPage = newPage;
			var newPage=parseInt(value);
			if(newPage < 1 || newPage > this.ttPage){
				return false;
			}
			this.numPage = newPage;
			
		break;
		case "sort":
			var precBtn=$(".btnBack");
			this.numPage = 1;
			this.sort = value;
		break;
		case "rows":
			this.numPage = 1;
			this.rows = value;
		break;
		case "categorie":
			this.idCategorie = value;
		break;
		case "categorieFromCategorie":
			this.numPage = 1;
			this.from = "categorie";
			this.idCategorie = value;
		break;
		case "liste_ligne_produit":
			this.nivCategorie = 3;
			this.idCategorie = value;
		break;
		
		default: 
			// Si action inconnu alors rien
			return false;
		break;
		}
				
		// Mise a jour de la liste
		this.maj();
	},
	
	// Mise a jour de la liste
	maj: function(){
	
		var search = $("#Rechercher").val();
		if (search != "Rechercher un produit" && $.trim(search) != ''){
			this.search = $.trim(search);
		}	

		if (this.page == "homeSearch" && this.nivCategorie == 2){
			this.nivCategorie -= 1;
		}	
		
		
		var params = {
			'CategorieID': this.idCategorie, 
			'NivCategorie': this.nivCategorie,
			'sort': this.sort, 
			'rows': this.rows,
			'numPage': this.numPage, 
			'facet': this.facet,
			'from': this.from,
			'CategorieMarqueID':this.categorieMarqueID, // utilisée pour la maj de la liste produit Marque
			'search': this.search, // utilisé pour le moteur de recherche
			'TypeID': this.nivCategorie,
			'CategorieParentID':this.idCategorieParent

		};
		 
		if (this.from == "categorie"){
			params.facet = '';
		}
		//if (this.nivCategorie == 1 && this.page != "homeSearch" && this.page != "listBellesAffaires"  && catalogueID == "1"){	
			// on est dans la servlet listProductUnivers
			var paramFacet = {
				'facet': this.facet,
				'CategorieID': this.idCategorie,
				'TypeID': this.nivCategorie,
				'search': this.search
			};
			var url = this.redirectUrl +"?"+ paramsGlobal+"&"+jQuery.param(params);
			window.location = url;
			return false;
		//}

		//$.getScript(this.ajaxurl +"?"+ paramsGlobal +"&"+ jQuery.param(params));
		//$.ajax({ type: "GET", url: this.ajaxurl +"?"+ paramsGlobal +"&"+ jQuery.param(params), success: function(){} , dataType: "script", cache: true });
	},
	
	// Filtre par facettes
	queryFacet: function(checked,from){
		var result = "";
		var input = new Array();
		
		$(".facette :checked[name != '']").each(function(){
			var name = $(this).attr("name");
			var value = $(this).val();
			// Utilisé pour facette tranche prix.
			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] + ")";
		}
		
		// Sauvegarde la requete
		this.facet = result;
		this.numPage = 1;
		this.from=from;
		this.maj();
	}
}

