
function liste(id, nb, max, lang){
	
	this.id = id;
	this.liste = new Array();	
	this.maxLigne = max;
	this.page = 1;	
	this.is_loaded = new Array();
	this.langue = lang;
	this.totalRows = nb;
	
	/**
	 * Met à jour la liste des châteaux
	 * @param {int} region
	 * @param {int} appelation
	 * @param {String} order
	 * @param {String} recherche
	 * @param {int} deverouille
	 */
	this.updateListe = function(region, appelation, order, recherche, deverouille){		
		
		var testClass = new TestClass();
		
		this.page = 1;
		
		if(deverouille !== '' && deverouille !== "undefined")
		{
			this.deverouille = deverouille;
		}
		else if(deverouille === 0)
			this.deverouille = 0;
		else
			this.deverouille = '';

		if(region !== null && region != 'undefined' && region != '')
			this.region_ref = region;
		else
			this.region_ref = 0;
		
		if(appelation != 'undefined' && appelation !== '' && appelation !== null)
		{
			this.appelation_ref = appelation;
			document.getElementById('recherche').value = '';
		}
		else
			this.appelation_ref = 0;
		
		//Mise à jour des champs
		document.getElementById('champRegion').value = this.region_ref;
		document.getElementById('champAppelation').value = this.appelation_ref;
		document.getElementById('champOrder').value = order;
		document.getElementById('champDeverouille').value = this.deverouille;
		
		var link =  '?region=' + this.region_ref;
		var link_appelation = '&appelation=' + this.appelation_ref;
		var link_order = (order != null && order != 'undefined') ? '&order=' + order : '';
		
		if(this.deverouille !== 0 && (this.deverouille === '' || this.deverouille == "undefined"))
		{
			var link_dev = '';
		}
		else
			var link_dev = '&deverouille='+deverouille;
			
		var link_recherche = (recherche != null && recherche != 'undefined') ? '&recherche=' + recherche : '';

		//Calcul du nombre de pages
		this.totalRows = this.getNumChateau(this.region_ref, this.appelation_ref, order, recherche, this.deverouille); 
		
		if(this.totalRows > 0)
			this.totalPage = Math.ceil(this.totalRows / this.maxLigne);
		else	
			this.totalPage = 0; 
		
		for(i=1;i<=this.totalPage;i++)
			this.is_loaded[i] = false;
		
		//Liste des châteaux
		var fichier = this.source + link + link_appelation + link_order + link_recherche + link_dev;

		testClass.listeRequest(fichier);
		
		//Mise à jour du nb de chateaux affichés		
		document.getElementById('totalChateau').innerHTML = this.totalRows;
				
		//DEBUG
		//document.getElementById('listeChateaux').innerHTML += this.totalPage;
		//this.enablePngImages('listeChateaux', 1, this.http);	
	}
	
	/**
	 * Page suivante
	 */
	this.suivant = function(){
		if(this.page < this.totalPage)
		{
			this.page = this.page + 1;		
			this.navigation();
		}
	}
	
	/**
	 * Page précédente
	 */
	this.precedent = function(){
		if(this.page > 1)
		{
			this.page = this.page - 1;
			this.navigation();
		}
	}
	
	/**
	 * Aller directement à la page i
	 * @param {Object} i
	 */
	this.changePage = function(i){
		if(i <= this.totalPage && i >= 1)
		{
			this.page = i;
			this.navigation();
		}
	}
	
	this.navigation = function(){
				
		//Calcul de la plage à afficher		
		var debut = (this.page - 1) * this.maxLigne;
		var fin = debut + this.maxLigne;
		
		if(fin > this.totalRows) fin = this.totalRows;
		
		if(this.totalPage == null)
		{
			this.totalPage = Math.ceil(this.totalRows / this.maxLigne); 
		}
		
		//Cache les blocs affichés
		//Avant
		for(i=0;i < debut;i++)
		{
			if(document.getElementById(id + '_' + i +''))
				document.getElementById(id + '_' + i +'').style.display = 'none';
		}
		
		//Après
		for(i=fin;i < this.totalRows;i++)
		{
			if(document.getElementById(id + '_' + i +''))
				document.getElementById(id + '_' + i +'').style.display = 'none';
		}
		
		//Affichage et des blocs		
		for(i=debut; i< fin; i++)
		{
			if(document.getElementById(id + '_' + i))
				document.getElementById(id + '_' + i).style.display = 'block';
		}
		
		//Mise en forme de la pagination
		var html = "";
		
		var start = this.page -(Math.floor(10/2));
		var nbTotPage = 0;
		
		start = start > (this.totalPage) - 10 ? (this.totalPage) - 10 : start;
		start = start < 0 ? 0 : start;
		
		if((this.totalPage + 1 - 10) <= 0)
		{
			nbTotPage = this.totalPage;
		}
		else{
			nbTotPage = start + 10 < this.totalPage ? 10 + start : this.totalPage;
		}
		
		/*
		for(i=start;i< nbTotPage;i++)
		{
			if(i + 1 == this.page)
				html = html + '<b>'+(i + 1)+'</b>';
			else
				html = html + '<a href="javascript:void(0);" onclick="liste.changePage('+(i + 1)+');">'+(i + 1)+'</a>';
		}
		*/
		
		// Pagination		
		if (this.page != 1)
			document.getElementById('info_page_' + this.id).innerHTML = '';			
		
		document.getElementById('info_page_' + this.id).innerHTML = this.page + ' - ' + nbTotPage;
		
		// Liens	
		if(this.totalPage == 1)
		{
			if(document.getElementById('lien_prec' + this.id))
				document.getElementById('lien_prec' + this.id).style.visibility = 'hidden';
			if(document.getElementById('lien_suiv' + this.id))
				document.getElementById('lien_suiv' + this.id).style.visibility = 'hidden';
		}
		else{
			
			/*
			if(this.page == 1)
			{
				if(document.getElementById('lien_prec'))
					document.getElementById('lien_prec').style.visibility = 'hidden';
			}
			else
			{
				if (document.getElementById('lien_prec')) {
					
					document.getElementById('lien_prec').style.visibility = 'visible';
					
				}
			}
			*/	
			if(this.page == this.totalPage)
			{
				if(document.getElementById('lien_suiv' + this.id))
					document.getElementById('lien_suiv' + this.id).style.visibility = 'hidden';
				//window.location.href = '#haut_liste';
			}
			else
			{
				if(document.getElementById('lien_suiv' + this.id))
					document.getElementById('lien_suiv' + this.id).style.visibility = 'visible';
			}			
		}
	}
}