// Place une image (appendChild) dans cont
function placerImg(source, alt, cont){
	// Création de l'image
	var img = document.createElement("img");
	// Affectation des attributs
	img.setAttribute('src', source);
	img.setAttribute('alt', alt);
	
	var divLoading = document.createElement("div");
	divLoading.appendChild(img);
	cont.appendChild(divLoading);
	
	var JQDivLoading = $(cont).find('div');
	JQDivLoading.css('margin', '20px');
	JQDivLoading.css('width', '650px');
	JQDivLoading.css('text-align', 'center');
	
	var JQImgLoading = JQDivLoading.find('img');
	JQImgLoading.css('height', '30px');
	JQImgLoading.css('width', '30px');
} // function placerImg

// Ajoute une annonce (appendChild) dans cont
function ajouterAnnonce(annonce, cont){
	// Création du modèle location
	var divLocation = creerDivLocation(annonce);
	
	// Création de hr.sep_ann clearer
	var hrSepAnnClearer = document.createElement("hr");
	hrSepAnnClearer.className = 'sep_ann clearer';
	
	// Ajout du modèle à la page
	cont.appendChild(divLocation);
	cont.appendChild(hrSepAnnClearer);
	
	// Remplissage
	divLocation = $(cont).find('.location:last');
	
	// Photo
	if(1==annonce.photo1)
		divLocation.find('.location_img').html('<img src="http://www.vendee-immobilier.com/photo/'+annonce.identifiant+'/'+annonce.codedescrip+'a.jpg" alt="">');
	// Desc
	divLocation.find('.location_desc').append('<span class="titre_location bold">'+annonce.titre+'</span>');
	divLocation.find('.location_desc').append('<br><br>');
	// Surface
	if(0!=annonce.surface){
		divLocation.find('.location_desc').append('<span class="surface_location bold">'+annonce.surface+' m²</span>');
		divLocation.find('.location_desc').append('<br><br>');
	}
	// NbPers
	if(0!=annonce.nb_pers && 'saison'==annonce.type){
		divLocation.find('.location_desc').append('<span class="nbpers_location bold">Pour '+annonce.nb_pers+' Personnes</span>');
		divLocation.find('.location_desc').append('<br><br>');
	}
	// Prix
	if('saison'==annonce.type){
		if(''!=annonce.prix_jour)
			divLocation.find('.location_prix').append('À partir de <span class="bold">'+annonce.prix_jour+'</span>&euro; la journée<br>');
		if(''!=annonce.prix_we)
			divLocation.find('.location_prix').append('À partir de <span class="bold">'+annonce.prix_we+'</span>&euro; le week-end<br>');
		if(''!=annonce.prix_sem)
			divLocation.find('.location_prix').append('À partir de <span class="bold">'+annonce.prix_sem+'</span>&euro; la semaine<br>');
		if(''!=annonce.prix_quinz)
			divLocation.find('.location_prix').append('À partir de <span class="bold">'+annonce.prix_quinz+'</span>&euro; la quinzaine<br>');
		if(''!=annonce.prix_mois)
			divLocation.find('.location_prix').append('À partir de <span class="bold">'+annonce.prix_mois+'</span>&euro; le mois<br>');
		if( (''==annonce.prix_jour) && (''==annonce.prix_we) && (''==annonce.prix_sem) && (''==annonce.prix_quinz) && (''==annonce.prix_mois) )
			divLocation.find('.location_prix').append('<span class="bold">Nous consulter</span>');
	}else if('annee'==annonce.type){
		divLocation.find('.location_prix').append('Loyer : <span class="bold">'+annonce.prix+'</span>&euro;');
	}
	// Description
	divLocation.find('.ext_desc').find('.description_location').append(annonce.desc);
	// Annonceur
	divLocation.find('.ext_desc').find('.annonceur_location').append('<strong>'+annonce.qualite+' '+annonce.nom+'</strong><br>');
	divLocation.find('.ext_desc').find('.annonceur_location').append(annonce.adresse+'<br>');
	divLocation.find('.ext_desc').find('.annonceur_location').append(annonce.cp+' '+annonce.ville+'<br>');
	divLocation.find('.ext_desc').find('.annonceur_location').append('Tel. : '+annonce.tel+'<br>');
	divLocation.find('.ext_desc').find('.annonceur_location').append('Fax. : '+annonce.fax+'<br>');
	divLocation.find('.ext_desc').find('.annonceur_location').append('Email : <strong><a class="souligne" href="mailto:'+annonce.email+'">'+annonce.email+'</a></strong><br>');
	divLocation.find('.ext_desc').find('.annonceur_location').append('Site Web : <strong><a class="souligne newWindow" href="'+annonce.siteweb+'">'+annonce.siteweb+'</a></strong>');
	
	initDetailOnClick(divLocation.find('.ext, .titre_location')); //  .location_img,
	divLocation.find('.location_img img').fancyzoom({Speed:200});
	initNewWindowOnClick(divLocation.find('a.newWindow'));
	
} // function ajouterAnnonce

// Renvoi une div vide de la formation location
function creerDivLocation(annonce){
	// Création de div.location
	var divLocation = document.createElement("div");
	// divLocation.setAttribute('class', 'location');
	divLocation.className = 'location';
	
	// Création de div.location_img
	var divLocationImg = document.createElement("div");
	divLocationImg.className = 'location_img';
	// Création de div.location_desc
	var divLocationDesc = document.createElement("div");
	divLocationDesc.className = 'location_desc';
	// Création de div.location_prix
	var divLocationPrix = document.createElement("div");
	divLocationPrix.className = 'location_prix';
	// Création de div.ext_desc
	var divExtDesc = document.createElement("div");
	divExtDesc.className = 'ext_desc clearer';
	// Création de div.ext
	var divExt = document.createElement("div");
	divExt.className = 'ext';
	
	// Création de div.description_location
	var divDescriptionLocation = document.createElement("div");
	divDescriptionLocation.className = 'description_location';
	// Création de div.annonceur_location
	var divAnnonceurLocation = document.createElement("div");
	divAnnonceurLocation.className = 'annonceur_location';
	// Création de div.clearer
	var divClearer = document.createElement("div");
	divClearer.className = 'clearer';
	
	divExtDesc.appendChild(divDescriptionLocation);
	divExtDesc.appendChild(divAnnonceurLocation);
	divExtDesc.appendChild(divClearer);
	
	divLocation.appendChild(divLocationImg);
	divLocation.appendChild(divLocationDesc);
	divLocation.appendChild(divLocationPrix);
	divLocation.appendChild(divExt);
	divLocation.appendChild(divExtDesc);
	
	$(divLocation).css('display', 'none');
	
	return divLocation;
} // function creerDivLocation

// Charge un objet annonce à partir de son équivalent xml
function chargerAnnonce(annonceXML, type){
	var annonce = new Object();
	
	annonce.codedescrip = 	annonceXML.attr('id');
	annonce.identifiant = 	annonceXML.find('identifiant').text();
	annonce.titre = 		annonceXML.find('titre').text();
	annonce.desc = 			annonceXML.find('desc').text();
	annonce.surface = 		annonceXML.find('surface').text();
	annonce.photo1 =		annonceXML.find('photo1').text();
	annonce.qualite =		annonceXML.find('qualite').text();
	annonce.nom =			annonceXML.find('nom').text();
	annonce.adresse =		annonceXML.find('adresse').text();
	annonce.cp =			annonceXML.find('cp').text();
	annonce.ville =			annonceXML.find('ville').text();
	annonce.tel =			annonceXML.find('tel').text();
	annonce.fax =			annonceXML.find('fax').text();
	annonce.email =			annonceXML.find('email').text();
	annonce.siteweb =		annonceXML.find('siteweb').text();
	annonce.type = 			type;
	
	if('saison'==type){
		annonce.nb_pers = 		annonceXML.find('nb_pers').text();
		annonce.prix_jour =		annonceXML.find('prix_jour').text();
		annonce.prix_we =		annonceXML.find('prix_we').text();
		annonce.prix_sem =		annonceXML.find('prix_sem').text();
		annonce.prix_quinz =	annonceXML.find('prix_quinz').text();
		annonce.prix_mois =		annonceXML.find('prix_mois').text();
	}else if('annee'==type){
		annonce.prix = 	annonceXML.find('prix').text();
	}
	
	return annonce;
} // function chargerAnnonceSaison

// Incrémente le n° de page
function incrementerPage(type, numPage, nbAnn){
	numPage.html(parseInt(numPage.html())+1);
	if('annee'==type){
		return debutLocAnnee = eval(parseInt(debutLocAnnee)+parseInt(nbAnn));
	}else if('saison'==type){
		return debutLocSaison = eval(parseInt(debutLocSaison)+parseInt(nbAnn));
	}
} // function incrementerPage

// Décrémente le n° de page
function decrementerPage(type, numPage, nbAnn){
	numPage.html(parseInt(numPage.html())-1);
	if('annee'==type){
		return debutLocAnnee = eval(parseInt(debutLocAnnee)-parseInt(nbAnn));
	}else if('saison'==type){
		return debutLocSaison = eval(parseInt(debutLocSaison)-parseInt(nbAnn));
	}
} // function decrementerPage

// Affiche une nouvelle page
function tournerPage(action, type, ville, localisation, nbAnn){
	// var btnPrecedent = $('#precedent_'+type+'_'+ville+'_'+localisation+'_'+nbAnn);
	// var btnSuivant = $('#suivant_'+type+'_'+ville+'_'+localisation+'_'+nbAnn);
	var btnPrecedent = $('.btn_ajax.precedent.'+type);
	var btnSuivant = $('.btn_ajax.suivant.'+type);
	
	// L'element qui contient le n° et le nombre de pages
	// Nom du XML et conteneur des annonces
	if('annee'==type){
		var numPage=$('#numPageLocAnnee');
		var nbPage=$('#nbPageLocAnnee');
		var nomXML='annoncesAnnee';
		var loc=document.getElementById('loc_annee');
		var nbAnnTotal=$('#nbLocAnnee');
	}else if('saison'==type){
		var numPage=$('#numPageLocSaison');
		var nbPage=$('#nbPageLocSaison');
		var nomXML='annoncesSaison';
		var loc=document.getElementById('loc_saison');
		var nbAnnTotal=$('#nbLocSaison');
	}
	
	var heightLoc = $(loc).height();
		
	// On mets à jour le num de début (sauf en cas de changement du nombre d'annonce par page)
	if('suivant'==action)
		debut = incrementerPage(type, numPage, nbAnn);
	else if('precedent'==action)
		debut = decrementerPage(type, numPage, nbAnn);
	else{
		if('annee'==type){
			debut = debutLocAnnee;
		}else if('saison'==type){
			debut = debutLocSaison;
		}
	}
	
	// Mise à jour des num de page, nb de page et boutons (en cas d'actualisation seulement)
	if('actu'==action){
		nbPage.html(Math.ceil(nbAnnTotal.html()/nbAnn));
		numPage.html(Math.ceil(debut/nbPage.html()));
	}
	
	// On affiche/cache les flèches selon le n° de page
	if(1==numPage.html()){
		btnPrecedent.addClass('dNone');
		if(btnSuivant.hasClass('dNone'))
			btnSuivant.removeClass('dNone');
	}else if(nbPage.html()==numPage.html()){
		btnSuivant.addClass('dNone');
		if(btnPrecedent.hasClass('dNone'))
			btnPrecedent.removeClass('dNone');
	}else{
		if(btnPrecedent.hasClass('dNone'))
			btnPrecedent.removeClass('dNone');
		if(btnSuivant.hasClass('dNone'))
			btnSuivant.removeClass('dNone');
	}
	
	var data = "ville="+ville+"&debut="+debut+'&localisation='+localisation+'&nb_ann='+nbAnn;
	
	// Fade Out des location;
	$(loc).find('.location').fadeOut('slow');
	
	// On vide les annonces
	$(loc).empty();
	
	// On conserver la taille
	$(loc).css('height', heightLoc);
	
	// On place une image d'attente
	placerImg('images/loading.gif', 'Chargement...', loc);
	
	$.ajax({
		type: "GET",
		url: "../xml/"+nomXML+".php?"+data,
		dataType: "xml",
		
		// Au renvoi de l'object xml
		success: function(xml){
		
			// On vide les annonces
			$(loc).empty();
				
			// Ajout du br
			var br = document.createElement("br");
			loc.appendChild(br);
			
			// Pour chaque annonce
			$(xml).find('annonce').each(function(){
				// Récupération des champs
				var annonce = chargerAnnonce($(this), type);
				
				// On ajoute l'annonce
				ajouterAnnonce(annonce, loc);
			});
				
			// Fade In des location;
			$(loc).find('.location').fadeIn('slow');
			
			// On me une taille adpatée
			$(loc).css('height', 'auto');
		}
	});
} // function tournerPage
