jq = jQuery.noConflict();

/**********  MOTEUR RECHERCHE **********/
function update_liste_annonce(page, limit_pagination)
{
	var dataMoteur=jq('#form_recherche_encart').serialize();
	dataMoteur = dataMoteur.replace(/\+/g,'%20');
	
	var dataAjax = '';
	var to = jq('#to').val();
	var pub_vendu = jq('#pub_vendu').val();
	if (dataMoteur != '')
		dataAjax = 'page='+page+'&limit_pagination='+limit_pagination+'&to='+to+'&pub_vendu='+pub_vendu+'&'+dataMoteur;
	else
		dataAjax = 'page='+page+'&limit_pagination='+limit_pagination+'&to='+to+'&pub_vendu='+pub_vendu;
	
	if (to!=''){
		jq.ajax({
			type: "POST",
			url: "/ajax/recherche/update_liste_annonce.php",
			async:true,
			beforeSend:function(){
				jq('#LISTE_ANNONCE .div_liste').block({message: "<img src='/images/ajax-loader.gif' alt='Patientez...' />"});
			},
			complete: function() { 
				jq('#LISTE_ANNONCE .div_liste').unblock(); 
			},
			data: dataAjax,
			success: function(data){
				jq('#LISTE_ANNONCE .div_liste').remove();
				jq('<div class="div_liste"></div').insertAfter(jq('#LISTE_ANNONCE .div_entete'));
				cpt = 0;
				lignes = '';
				nb_pages_annonce = 0;
				nb_total_annonce = 0;
				flag_etendu = 0;
				flagpost = 0;
				jq(data).find('annonce').each( function(){
					nb_total_annonce = jq(this).find('nb_total_annonce').text();
					nb_pages_annonce = jq(this).find('nb_pages_annonce').text();
					flag_etendu = jq(this).find('flag_etendu').text();
					flagpost = parseInt(jq(this).find('flagpost').text());
					pub_vendu = parseInt(jq(this).find('pub_vendu').text());
					to = jq(this).find('to').text();
					lignes += '<div class="annonce">';
					if (to == 'emV2dw=='){
						if (pub_vendu == 1)
							lignes += '<img class="type_offre" src="/images/PICTOS_SITE/bien_vente.png" width="60" height="48" alt="Vendu" />';
						else
							lignes += '<img class="type_offre" src="/images/PICTOS_SITE/annonce_vente.png" width="60" height="48" alt="Vente" />';
					}
					else if (to == 'Mg=='){
						if (pub_vendu == 1)
							lignes += '<img class="type_offre" src="/images/PICTOS_SITE/bien_location.png" width="60" height="49" alt="Loué" />';
						else
							lignes += '<img class="type_offre" src="/images/PICTOS_SITE/annonce_location.png" width="60" height="49" alt="Location" />';
					}
					if (parseInt(jq(this).find('contrat_plus').text()) == 1)
						lignes += '<img src="/images/ANNONCE/Pastille_Contrat_Plus.png" class="pastille_cp" alt="Contrat PLUS" width="65" height="65" />';
					lignes += '<div class="detail" onclick="window.location.href=\'/annonce.php?id='+jq(this).find('id_annonce').text()+'\'"><table class="table_titre"><tr><td valign="top" class="td_categorie">';
					if (parseInt(jq(this).find('nb_pieces').text()) > 0){
						lignes += '<h2>'+jq(this).find('categorie').text()+'&nbsp;/&nbsp;'+parseInt(jq(this).find('nb_pieces').text())+'&nbsp;pi&egrave;ce';
						if (parseInt(jq(this).find('nb_pieces').text())>0) 
							lignes += 's';
						lignes += '</h2>';
					}else
						lignes += '<h2>'+jq(this).find('categorie').text()+'</h2>';
					lignes += '</td><td class="td_separateur">&nbsp;</td><td valign="middle"><p class="td_surface">'+jq(this).find('surf_hab').text()+'&nbsp;m<sup>2</sup></p></td>';
					lignes += '<td class="td_separateur10">&nbsp;</td><td valign="middle">';
					if (pub_vendu == 0)
						lignes += '<p class="td_prix">'+jq(this).find('prix').text()+'&nbsp;&euro;</p>';
					lignes += '</td>';
					lignes += '<td class="td_separateur">&nbsp;</td><td class="td_separateur">&nbsp;</td><td valign="middle" class="td_telagence">'+jq(this).find('tel_agence').text()+'</td></tr></table>';
					lignes += '<table class="table_offre"><tr><td valign="top" class="td_thumb">'+jq(this).find('photo_thumb').text()+'</td>';
					lignes += '<td class="td_separateur">&nbsp;</td><td valign="top"><div>';					
					lignes += '<h3>'+jq(this).find('ville_offre').text()+'&nbsp;';
					if (jq(this).find('cp_offre').text() != '')
						lignes += '('+jq(this).find('cp_offre').text()+')&nbsp;';
					lignes += jq(this).find('quartier').text();
					lignes += '</h3>';
					lignes += '<p class="p_ref">Ref&nbsp;:&nbsp;'+jq(this).find('no_asp').text()+'</p>';
					lignes += '<div class="resume_annonce">'+jq(this).find('texte_fr').text()+'</div>';
					lignes += '</div></td></tr></table></div>';
					lignes += '<div class="fonction_annonce">';
					if (pub_vendu == 1){
						lignes += '<table class="table_fonction_annonce" style="width:200px !important;">';
						lignes += '<tr><td valign="middle" class="td_voir_detail" style="width:200px !important;"><a href="/vente.php?id='+jq(this).find('id_annonce').text()+'">&gt;&nbsp;Voir le d&eacute;tail du bien</a></td>';
						lignes += '</tr>';
					}
					else{
						lignes += '<table class="table_fonction_annonce">';
						lignes += '<tr><td valign="middle" class="td_voir_detail"><a href="/annonce.php?id='+jq(this).find('id_annonce').text()+'">&gt;&nbsp;Voir le d&eacute;tail de l\'annonce</a></td>';
						lignes += '<td valign="middle" class="td_select"><img src="/images/PICTOS_SITE/picto_selection_annonce.png" width="18" height="17" alt="Sélection" /><a href="/popup/ajout_fav.php?id='+jq(this).find('id_annonce').text()+'" rel="#overlay_ajout_selection">Ajouter &agrave; mes annonces</a></td>';
						lignes += '<td valign="middle" class="td_mail"><img src="/images/PICTOS_SITE/picto_mail_annonce.png" width="18" height="17" alt="Mail" /><a href="/popup/mail_annonce.php?id='+jq(this).find('id_annonce').text()+'" rel="#overlay_mail_annonce">Envoyer &agrave; un ami</a></td>';
						lignes += '<td valign="middle" class="td_imprimer"><img src="/images/PICTOS_SITE/picto_imprimer_annonce.png" width="18" height="17" alt="Imprimer" /><a href="/export/fiche_visite.php?id='+jq(this).find('id_annonce').text()+'" target="_blank">Imprimer la fiche de visite</a></td>';
						lignes += '</tr>';
					}
					lignes += '</table></div></div>';
				});
				jq(lignes).appendTo(jq('#LISTE_ANNONCE .div_liste'));
				if (parseInt(flag_etendu) == 1){
					jq ('#LISTE_ANNONCE .h3_etendu').remove();
					jq('<h3 class="h3_etendu">Votre recherche a &eacute;t&eacute; &eacute;tendue.</h3>').insertAfter(jq('#LISTE_ANNONCE .div_entete'));
				}
				load_overlay(100);
				jq('.pagination_annonce').empty();
				lignes_pagination = '<table><tbody><tr>';
				if (parseInt(page) > 1)
				{
					lignes_pagination += '<td><span onclick="update_liste_annonce(1, '+limit_pagination+')">&lt;&lt;</span></td>';
					lignes_pagination += '<td><span onclick="update_liste_annonce('+(parseInt(page)-1)+', '+limit_pagination+')">&lt;</span></td>';
					for (var prec=parseInt(page)-3;prec<parseInt(page);prec++)
					{
						if (prec>1)
							lignes_pagination += '<td><span href="#LISTE_ANNONCE" onclick="update_liste_annonce('+prec+', '+limit_pagination+')">'+prec+'</span></td>';
					}
				}
				lignes_pagination += '<td><span class="pagination_en_cours">'+page+'</span></td>';
				if (parseInt(page) < parseInt(nb_pages_annonce))
				{
					for (var suiv=parseInt(page)+1;suiv<=parseInt(page)+3;suiv++)
					{
						if (suiv<parseInt(nb_pages_annonce))
							lignes_pagination += '<td><span onclick="update_liste_annonce('+suiv+', '+limit_pagination+')">'+suiv+'</span></td>';
					}
					lignes_pagination += '<td><span onclick="update_liste_annonce('+(parseInt(page)+1)+', '+limit_pagination+')">&gt;</span></td>';
					lignes_pagination += '<td><span onclick="update_liste_annonce('+nb_pages_annonce+', '+limit_pagination+')">&gt;&gt;</span></td>';
				}
				lignes_pagination += '</tr></tbody></table>';
				jq(lignes_pagination).appendTo(jq('.pagination_annonce'));
				
				jq('#nb_result_recherche').empty();
				if (pub_vendu == 1){
					if (nb_total_annonce>1)
						type_offre = '<br/><span class="txt_result">vendus</span>';
					else
						type_offre = '<br/><span class="txt_result">vendu</span>';
				}
				else{
					if (flagpost > 0){
						if (nb_total_annonce>1)
							type_offre = '<br/><span class="txt_result">correspondent &agrave; votre recherche</span>';
						else
							type_offre = '<br/><span class="txt_result">correspond &agrave; votre recherche</span>';
					}
					else{
						if (to == 'emV2dw==')
							type_offre = '<br/><span class="txt_result">actuellement en vente</span>';
						else if (to == 'Mg==')
							type_offre = '<br/><span class="txt_result">actuellement en location</span>';
					}
				}
				if (nb_total_annonce>1)
					jq('#nb_result_recherche').append(jq('<span class="txt_jaune">'+nb_total_annonce+'&nbsp;annonces</span>'+type_offre));
				else
					jq('#nb_result_recherche').append(jq('<span class="txt_jaune">'+nb_total_annonce+'&nbsp;annonce</span>'+type_offre));
			}
		});
		location.hash = '#' + page;
		jq('body,html').animate({scrollTop:20},100);
	}
	else
		window.location.href='/index.php';		
}

function load_autocomplete_lieu(id_champ)
{
	jq("#"+id_champ).autocomplete({
		source: function( request, response ) {
			jq.ajax({
				url: "/autocomplete/select_lieu.php",
				dataType: "xml",
				type: "POST",
				data: {
					r_filtre: request.term
				},
				success: function( xmlResponse ) {
					response(jq( "db_autocomplete", xmlResponse ).map(function() {
						if (jq( "id_autocomplete", this ).text().substr(0,2) == 'd_'){
							return {
								value: '<strong>'+jq( "lb_autocomplete", this ).text() + '<span class="fin_lieu"></span><span class="ac_nb_bien">&nbsp;('+jq( "nb_annonce", this ).text()+')</span></strong>',
								id: jq( "id_autocomplete", this ).text()
							};
						}
						else{
							return {
								value: jq( "lb_autocomplete", this ).text() + '<span class="fin_lieu"></span><span class="ac_nb_bien">&nbsp;('+jq( "nb_annonce", this ).text()+')</span>',
								id: jq( "id_autocomplete", this ).text()
							};	
						}
					}).get());
					jq('.ui-autocomplete li a').each(function(){
						temp_html = jq(this).html();
						temp_html = temp_html.replace(/&lt;/g, "<");
						temp_html = temp_html.replace(/&gt;/g, ">");
						temp_html = temp_html.replace(/&amp;/g, "&");
						jq(this).html(temp_html);
					});
					jq('.ui-autocomplete').width('auto');
				}
			});
		},
		minLength: 1,
		select: function( event, ui ) {
			ui.item.value = strip_tags((ui.item.value).substr(0, (ui.item.value).indexOf('<span class="fin_lieu"></span>')));
			jq('#hid_lieu').val(ui.item.id);
			jq('#hid_lblieu').val(ui.item.value);
			jq('#hid_lieu_encart').val(ui.item.id);
			jq('#hid_lblieu_encart').val(ui.item.value);
		},
		open: function() {
			jq( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
		},
		close: function() {
			jq( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
		}
	});
	
	jq("#"+id_champ).blur(function() {
  		if (jq.trim(jq(this).val()) == ''){
			jq('#hid_lieu').val('');
			jq('#hid_lblieu').val('');
			jq('#hid_lieu_encart').val('');
			jq('#hid_lblieu_encart').val('');
		}
	});
}

function submit_recherche(isencart, islink, isajax, pagination){
	isajax = parseInt(isajax);
	var idtr = parseInt(jq("input[@name=idtr"+isencart+"]:checked").val());
	if (idtr>0){
		if (idtr == 2){
			if (isajax != 2)
				jq('#form_recherche'+isencart).attr('action', '/Site/louer.php');
			else
				jq('#form_recherche'+isencart).attr('action', 'javascript:update_liste_annonce(1,'+pagination+');');
		}
		else {
			if (isajax != 1)
				jq('#form_recherche'+isencart).attr('action', '/Site/acheter.php');
			else
				jq('#form_recherche'+isencart).attr('action', 'javascript:update_liste_annonce(1,'+pagination+');');
		}
	} else {
		if (isajax == 0)
			jq('#form_recherche'+isencart).attr('action', '/Site/acheter.php');	
		else if (isajax == 1 || isajax == 2)
			jq('#form_recherche'+isencart).attr('action', 'javascript:update_liste_annonce(1,'+pagination+');');
		else 
			jq('#form_recherche'+isencart).attr('action', '/Site/acheter.php');	
	}
	
	var dataMoteur=jq('#form_recherche'+isencart).serialize();
	dataAjax = dataMoteur.replace(/\+/g,'%20');
	
	jq.ajax({
		type: "POST",
		url: "/ajax/recherche/session_search.php",
		async:false,
		data: dataAjax,
		success: function(data){
			if (parseInt(islink) > 0)
				jq('#form_recherche'+isencart).submit();
		}
	});
}

function ajout_recherche(){
	var data_form=jq('#form_critere_recherche').serialize();
	var data_search=jq('#RECHERCHE_ENCART form').serialize();
	var data_ajax = data_form + '&' + data_search;
	jq.ajax({
		type: "POST",
		url: "/ajax/recherche/ajout_recherche.php",
		async:true,
		beforeSend:function(){
			jq('#div_overlay.div_critere_recherche').block({message: "<img src='/images/ajax-loader.gif' alt='Patientez...' />"});
		},
		complete: function() { 
			jq('#div_overlay.div_critere_recherche').unblock(); 
		},
		data: data_ajax,
		success: function(data){
			if (parseInt(data) == 1){
				jq('#p_err').empty();
				close_overlay();
			}
			else if (parseInt(data) == 3){
				jq('#p_err').empty();
				jq('<span>Merci de remplir le formulaire.</span>').appendTo(jq('#p_err'));
			}
		}
	});
}

function load_recherche(id_recherche){
	if (id_recherche != ''){
		jq.ajax({
			type: "POST",
			url: "/ajax/recherche/load_recherche.php",
			async:true,
			beforeSend:function(){
				jq('#CONTENU').block({message: "<img src='/images/ajax-loader.gif' alt='Patientez...' />"});
			},
			complete: function() { 
				jq('#CONTENU').unblock(); 
			},
			data: {id_recherche:id_recherche},
			success: function(data){
				if (parseInt(data) == 2){
					window.location.href = '/Site/louer.php#1';
				} else
					window.location.href = '/Site/acheter.php#1';
			}
		});
	}
}

function supprimer_recherche(id_recherche){
	if (confirm('Confirmez-vous la suppression ?')){
		if (id_recherche != ''){
			jq.ajax({
				type: "POST",
				url: "/ajax/recherche/supprimer_recherche.php",
				async:true,
				data: {id_recherche:id_recherche},
				success: function(data){
					jq('#table_critere_recherche tr[id="tr_' + id_recherche + '"] td').fadeTo("slow", 0, function(){	//animation pour la suppression
						jq(this).hide().empty();
					});
				}
			});
		}
	}
}
/********** FIN  MOTEUR RECHERCHE **********/


/********* ANNONCES ***********/

function mail_annonce(){
	var data_form=jq('#form_mail_annonce').serialize();
	
	jq.ajax({
		type: "POST",
		url: "/ajax/annonce/mail_annonce.php",
		async:true,
		beforeSend:function(){
			jq('#div_overlay.div_mail_annonce').block({message: "<img src='/images/ajax-loader.gif' alt='Patientez...' />"});
		},
		complete: function() { 
			jq('#div_overlay.div_mail_annonce').unblock(); 
		},
		data: data_form,
		success: function(data){
			if (parseInt(data) == 1){
				jq('#p_err').empty();
				close_overlay();
			}
			else if (parseInt(data) == 3){
				jq('#p_err').empty();
				jq('<span>Merci de remplir le formulaire.</span>').appendTo(jq('#p_err'));
			}
		}
	});
}

/*** Agence ***/
function dem_rappel(){
	var data_form=jq('#form_rappel').serialize();
	
	jq.ajax({
		type: "POST",
		url: "/ajax/annonce/dem_rappel.php",
		async:true,
		beforeSend:function(){
			jq('#div_overlay.div_detail_agence').block({message: "<img src='/images/ajax-loader.gif' alt='Patientez...' />"});
		},
		complete: function() { 
			jq('#div_overlay.div_detail_agence').unblock(); 
		},
		data: data_form,
		success: function(data){
			if (parseInt(data) == 1){
				jq('#p_err').empty();
				close_overlay();
			}
			else if (parseInt(data) == 3){
				jq('#p_err').empty();
				jq('<span>Merci de remplir le formulaire.</span>').appendto(jq('#p_err1'));
			}
		}
	});
}

function dem_rdv(){
	var data_form=jq('#form_rdv').serialize();
	
	jq.ajax({
		type: "POST",
		url: "/ajax/annonce/dem_rdv.php",
		async:true,
		beforeSend:function(){
			jq('#div_overlay.div_detail_agence').block({message: "<img src='/images/ajax-loader.gif' alt='Patientez...' />"});
		},
		complete: function() { 
			jq('#div_overlay.div_detail_agence').unblock(); 
		},
		data: data_form,
		success: function(data){
			if (parseInt(data) == 1){
				jq('#p_err').empty();
				close_overlay();
			}
			else if (parseInt(data) == 3){
				jq('#p_err').empty();
				jq('<span>Merci de remplir le formulaire.</span>').appendto(jq('#p_err2'));
			}
		}
	});
}

function contact_agence(){
	var data_form=jq('.r_glob').serialize();
	
	jq.ajax({
		type: "POST",
		url: "/ajax/annonce/contact_agence.php",
		async:true,
		data: data_form,
		success: function(data){
		}
	});
}

/*** Fin Agence ***/

/*** Compte ***/
function creer_compte(page){
	var data_form=jq('#form_creer_compte').serialize();
	
	jq.ajax({
		type: "POST",
		url: "/ajax/annonce/creer_compte.php",
		async:true,
		beforeSend:function(){
			jq('#div_overlay.div_identification_compte').block({message: "<img src='/images/ajax-loader.gif' alt='Patientez...' />"});
		},
		complete: function() { 
			jq('#div_overlay.div_identification_compte').unblock(); 
		},
		data: data_form,
		success: function(data){
			if (parseInt(data) == 1){
				jq('#p_err').empty();
				if (page == 1)
					window.location.href = '/Espace_Client/mon_espace.php';
				else
					close_overlay();
			}
			else if (parseInt(data) == 3){
				jq('#p_err').empty();
				jq('#p_err1').append('<span>Merci de remplir le formulaire.</span>');
			}
			else if (parseInt(data) == 4){
				jq('#p_err').empty();
				jq('#p_err1').append('<span>Cet e-mail est déjà enregistré.</span>');
			}
			else if (parseInt(data) == 22){
				jq('#p_err').empty();
				jq('<br/><span>L\'annonce a bien &eacute;t&eacute; ajout&eacute;e dans vos favoris.<br/><br/>Pour acc&eacute;der &agrave; vos favoris, connectez-vous &agrave; la rubrique <a href="/Espace_Client/espace_client.php">Espace Client</a></span><br/>').appendTo(jq('#p_err'));
			}
			else if (parseInt(data) == 24){
				jq('#p_err').empty();
				jq('<br/><span>Pour des raisons administratives, votre e-mail n\'a pas pu &ecirc;tre enregistr&eacute;. Pour plus d\'information, contactez notre agence.</span><br/>').appendTo(jq('#p_err'));
			}
		}
	});
}

function modifier_compte(){
	var data_form=jq('#form_modifier_compte').serialize();
	
	jq.ajax({
		type: "POST",
		url: "/ajax/annonce/modifier_compte.php",
		async:true,
		beforeSend:function(){
			jq('#div_overlay.div_modification_compte').block({message: "<img src='/images/ajax-loader.gif' alt='Patientez...' />"});
		},
		complete: function() { 
			jq('#div_overlay.div_modification_compte').unblock(); 
		},
		data: data_form,
		success: function(data){
			if (parseInt(data) == 1){
				jq('#p_err').empty();
				close_overlay();
				window.location.href = '/Espace_Client/mon_espace.php';
			}
			else if (parseInt(data) == 3){
				jq('#p_err').empty();
				jq('#p_err').append('<span>Merci de remplir le formulaire.</span><br/>');
			}
			else if (parseInt(data) == 4){
				jq('#p_err').empty();
				jq('#p_err1').append('<span>Cet e-mail est déjà enregistré.</span>');
			}
			else if (parseInt(data) == 24){
				jq('#p_err').empty();
				jq('#p_err').append('<br/><span>Pour des raisons administratives, votre e-mail n\'a pas pu &ecirc;tre enregistr&eacute;. Pour plus d\'information, contactez notre agence.</span><br/>');
			}
			else{
				close_overlay();
				window.location.href = '/Espace_Client/espace_client.php';
			}
		}
	});
}

function mdp_perdu(){
	var data_form=jq('#form_mdp_perdu').serialize();
	
	jq.ajax({
		type: "POST",
		url: "/ajax/annonce/mdp_perdu.php",
		async:true,
		beforeSend:function(){
			jq('#div_overlay.div_identification_compte').block({message: "<img src='/images/ajax-loader.gif' alt='Patientez...' />"});
		},
		complete: function() { 
			jq('#div_overlay.div_identification_compte').unblock(); 
		},
		data: data_form,
		success: function(data){
			if (parseInt(data) == 1){
				jq('#p_err').empty();
				jq('<br/><span>Un email contenant vos identifiant vient de vous être envoyé<br/>.</span>').appendTo(jq('#p_err2'));
			}
			else if (parseInt(data) == 0){
				jq('#p_err').empty();
				jq('<br/><span>Votre compte n\'existe pas.</span><br/>').appendTo(jq('#p_err2'));
			}
			else if (parseInt(data) == 3){
				jq('#p_err').empty();
				jq('<br/><span>Merci de remplir le formulaire.</span><br/>').appendTo(jq('#p_err2'));
			}
		}
	});
}

function identification_compte(page){
	var data_form=jq('#form_identification_compte').serialize();
	
	jq.ajax({
		type: "POST",
		url: "/ajax/annonce/identification_compte.php",
		async:true,
		beforeSend:function(){
			jq('.div_identification_compte').block({message: "<img src='/images/ajax-loader.gif' alt='Patientez...' />"});
		},
		complete: function() { 
			jq('.div_identification_compte').unblock(); 
		},
		data: data_form,
		success: function(data){
			if (parseInt(data) == 1){
				jq('#p_err').empty();
				if (page == 1)
					window.location.href = '/Espace_Client/mon_espace.php';
				else
					close_overlay();
			}
			else if (parseInt(data) == 0){
				jq('#p_err').empty();
				jq('<br/><span>Identifiants invalides.</span><br/>').appendTo(jq('#p_err'));
			}
			else if (parseInt(data) == 3){
				jq('#p_err').empty();
				jq('<br/><span>Merci de remplir le formulaire.</span><br/>').appendTo(jq('#p_err'));
			}
			else if (parseInt(data) == 22){
				jq('#p_err').empty();
				jq('<br/><span>L\'annonce a bien &eacute;t&eacute; ajout&eacute;e dans vos favoris.<br/><br/>Pour acc&eacute;der &agrave; vos favoris, connectez-vous &agrave; la rubrique <a href="/Espace_Client/espace_client.php">Espace Client</a></span><br/>').appendTo(jq('#p_err'));
			}
		}
	});
}
/*** Fin Compte ***/

/********* FIN ANNONCES ***********/

