jq = jQuery.noConflict();
jq = jQuery.noConflict();

/**********  MOTEUR RECHERCHE **********/
function update_liste_annonce(page, limit_pagination)
{
	var dataMoteur=jq('#form_recherche').serialize();
	dataMoteur = dataMoteur.replace(/\+/g,'%20');
	
	var dataAjax = '';
	if (dataMoteur != '')
		dataAjax = 'page='+page+'&limit_pagination='+limit_pagination+'&'+dataMoteur;
	else
		dataAjax = 'page='+page+'&limit_pagination='+limit_pagination;
	
	jq.ajax({
		type: "POST",
		url: "/ajax/recherche/update_liste_annonce.php",
		async:true,
		beforeSend:function(){
			jq('#RES_RECHERCHE').block({message: "<img src='/images/ajax-loader.gif' alt='Patientez...' />"});
		},
		complete: function() { 
			jq('#RES_RECHERCHE').unblock(); 
		},
		data: dataAjax,
		success: function(data){
			jq('#RES_RECHERCHE .div_liste').remove();
			jq('<div class="div_liste"></div').insertAfter(jq('#RES_RECHERCHE .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();
				txt_etendu = jq(this).find('txt_etendu').text();
				flagpost = parseInt(jq(this).find('flagpost').text());
				numero_fiche = jq(this).find('numero_fiche').text();
				annonce_titre = jq(this).find('annonce_titre').text();
				annonce_surface_reelle = jq(this).find('annonce_surface_reelle').text();
				annonce_prix = jq(this).find('annonce_prix').text();
				annonce_date = jq(this).find('annonce_date').text();
				annonce_intitule = jq(this).find('annonce_intitule').text();
				annonce_thumb = jq(this).find('annonce_thumb').text();
				annonce_transport = jq(this).find('annonce_transport').text();
				annonce_commerciale = jq(this).find('annonce_commerciale').text();
				flag_selection = parseInt(jq(this).find('flag_selection').text());
				lignes += '<div class="objet_recherche">';
				lignes += '<h3>'+annonce_titre+'</h3>';
				lignes += '<div class="contenu">';
				lignes += '<div class="div_surface_prix">';
				lignes += '<table class="table_collapse"><tr>';
				if (flag_selection==1){
					lignes += '<td valign="top">';
					lignes += '<img height="60" class="pastille_cc_result" alt="Coup de Coeur" src="/images/ANNONCE/picto_coup_coeur.png" />';
					lignes += '</td>';
				}
				if (parseInt(annonce_surface_reelle)>0){
					lignes += '<td valign="top">';
					lignes += '<table class="table_collapse"><tr>';
					lignes += '<td><p class="btn_noir_left"></p></td>';
					lignes += '<td><p class="btn_noir_fond">'+annonce_surface_reelle+' m<sup>2</sup></p></td>';
					lignes += '<td><p class="btn_noir_right"></p></td>';
					lignes += '</tr></table>';
					lignes += '</td>';
					if (parseInt(annonce_prix)>0)
						lignes += '<td class="td_separateur10">&nbsp;</td>';
				}
				if (parseInt(annonce_prix)>0){
					lignes += '<td valign="top">';
					lignes += '<table class="table_collapse"><tr>';
					lignes += '<td><p class="btn_rouge_left"></p></td>';
					lignes += '<td><p class="btn_rouge_fond">'+annonce_prix+' &euro; CC</p></td>';
					lignes += '<td><p class="btn_rouge_right"></p></td>';
					lignes += '</tr></table>';
					lignes += '</td>';
				}
				lignes += '</tr></table>';
				lignes += '</div>';
				lignes += '<p class="date">'+annonce_date+'</p>';
				lignes += '<h2 class="intitule">'+annonce_intitule+'</h2>';
				lignes += '<div class="texte">';
				lignes += '<table style="width: 100%"><tbody><tr>';
				lignes += '<td valign="top" style="width:100px" align="center">'+annonce_thumb+'</td>';
				lignes += '<td style="padding-left: 15px;" valign="top">';
				if (strlen(annonce_transport)>0){
					lignes += '<div class="transport"><table class="table_collapse"><tr>';
					lignes += '<td><img src="/images/ANNONCE/transport.png" alt="Transports" width="61" height="20" /></td><td class="td_separateur10">&nbsp;</td><td>'+annonce_transport+'</td>';
					lignes += '</tr></table></div>';
				}
				lignes += annonce_commerciale;
				lignes += '</td>';
				lignes += '</tr></tbody></table>';
				lignes += '</div>';
				lignes += '<div class="fonction_annonce">';
				lignes += '<table class="table_fonction_annonce">';
				lignes += '<tr>';
				lignes += '<td valign="middle" class="td_voir_detail"><a href="/annonce.php?id='+numero_fiche+'">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="15" height="15" alt="Sélection" /><a href="#">S&eacute;lectionner cette annonce</a></td>';
				lignes += '<td valign="middle" class="td_mail"><img src="/images/PICTOS_SITE/picto_mail_annonce.png" width="15" height="15" alt="Mail" /><a href="/popup/mail_annonce.php?id='+numero_fiche+'" 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="15" height="15" alt="Imprimer" /><a href="/export/fiche_visite.php?id='+numero_fiche+'" target="_blank">Imprimer la fiche de visite</a></td>';
				lignes += '</tr>';
				lignes += '</table>';
				lignes += '</div>';
				lignes += '</div>';
				lignes += '</div>';
			});
			jq(lignes).appendTo(jq('#RES_RECHERCHE .div_liste'));
			mainForPageLoad();
			jq('#RES_RECHERCHE .h3_etendu').remove();
			jq('#txt_etendu').remove();
			if (parseInt(flag_etendu) == 1){
				jq('<h3 class="h3_etendu">Votre recherche a &eacute;t&eacute; &eacute;tendue.</h3>').insertAfter(jq('#RES_RECHERCHE .div_entete'));
				if (txt_etendu != ''){
					txt_etendu = '<ul id="txt_etendu">'+txt_etendu+'</ul>';
					jq(txt_etendu).insertAfter(jq('#RES_RECHERCHE .h3_etendu'));
				}
			}
			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>0)
						lignes_pagination += '<td><span href="#RES_RECHERCHE" 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 (flagpost > 0){
				if (nb_total_annonce>1)
					type_offre = '&nbsp;<span class="txt_result">correspondent &agrave; votre recherche</span>';
				else
					type_offre = '&nbsp;<span class="txt_result">correspond &agrave; votre recherche</span>';
			}
			else{
				type_offre = '&nbsp;<span class="txt_result">actuellement en location</span>';
			}
			if (nb_total_annonce>1)
				jq('#nb_result_recherche').append(jq('<span class="txt_noir">'+nb_total_annonce+'&nbsp;annonces</span>'+type_offre));
			else
				jq('#nb_result_recherche').append(jq('<span class="txt_noir">'+nb_total_annonce+'&nbsp;annonce</span>'+type_offre));
			jq('#h1_sep').remove();
			jq('<p class="hr_rouge_ombre" id="h1_sep" style="margin:0 !important;"></p>').insertAfter(jq('#nb_result_recherche'));
			
			jq.ajax({
				type: "POST",
				url: "/ajax/recherche/session_search.php",
				async:false,
				data: dataAjax,
				success: function(data){
					
				}
			});
		}
	});
	mainForPageLoad();
	location.hash = '#' + page;
	top_scroll = jq('#RES_RECHERCHE').offset().top;
	jq('body,html').animate({scrollTop:top_scroll},100);	
}

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 ) {
			var nb_lieu;
			nb_lieu = parseInt(jq('#nb_lieu').val());
			nb_lieu = nb_lieu + 1;
			jq('#nb_lieu').val(nb_lieu);
			ui.item.value = '<li class="li_item" id="li_item'+nb_lieu+'"><p>'+strip_tags((ui.item.value).substr(0, (ui.item.value).indexOf('<span class="fin_lieu"></span>')))+'</p><span class="close">x</span></li>';
			jq(ui.item.value).insertBefore(jq('ul.ul_lieu li.li_input'));
			jq('ul.ul_lieu li.li_input input').val('');
			remove_lieu();
			ui.item.value  = strip_tags((ui.item.value));
			ui.item.value  = (ui.item.value).substr(0, ((ui.item.value).length)-1)
			jq('<input type="hidden" name="hid_lieu'+nb_lieu+'" id="hid_lieu'+nb_lieu+'" value="'+(ui.item.id)+'" />').insertAfter(jq('#nb_lieu'));
			jq('<input type="hidden" name="hid_lblieu'+nb_lieu+'" id="hid_lblieu'+nb_lieu+'" value="'+(ui.item.value)+'" />').insertAfter(jq('#nb_lieu'));
		},
		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('ul.ul_lieu li.li_input input').val('');
			remove_lieu();
			set_height_lieu();
		}
	});
	
	jq("#"+id_champ).blur(function() {
  		if (jq.trim(jq(this).val()) == ''){
			jq('#hid_lieu').val('');
			jq('#hid_lblieu').val('');
		}
	});
}

function submit_recherche(islink, isajax, pagination){
	isajax = parseInt(isajax);
	if (isajax == 0)
		jq('#form_recherche').attr('action', '/Site/annonces_locations.php');	
	else if (isajax == 1 || isajax == 2)
		jq('#form_recherche').attr('action', 'javascript:update_liste_annonce(1,'+pagination+');');
	else 
		jq('#form_recherche').attr('action', '/Site/annonces_locations.php');	
}

/********** 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'));
			}
		}
	});
}

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

/********** RESULTATS MOTEUR RECHERCHE **********/
function resultat_recherches_home()
{
	var dataMoteur=jq('#form_loc .r_moteur').serialize();
	dataMoteur = dataMoteur.replace(/\+/g,'%20');
	
	jq.ajax({
		type: "POST",
		url: "ajax/recherche/resultat_recherches_home.php",
		async:true,
		beforeSend:function(){
			jq.blockUI({message: "<img src='/images/ajax-loader.gif' alt='Patientez...' />"})
		},
		complete: function() { 
			jq.unblockUI(); 
		},
		data: dataMoteur,
		success: function(data){
			//jq('#OFFRES').remove();
			//jq('#ACTUS_HOME').remove();
			jq('#RECHERCHE_LOC').remove();
			jq('<div id="RES_RECHERCHE"></div>').insertAfter(jq('#p_recherche'));
			cpt = 0;
			lignes = '';
			
			var offre_date;

			/*jq(data).find('recherche').each( function(){
				cpt++;
				id_recherche = jq(this).find('id_recherche').text();
				offre_titre = jq(this).find('offre_titre').text();
				offre_intitule = jq(this).find('offre_intitule').text();
				offre_texte = jq(this).find('offre_texte').text();
				offre_img = jq(this).find('offre_img').text();
				offre_date = jq(this).find('offre_date').text();
				/*offre_date = new Date();
				offre_date.locale = "fr";
				offre_date = offre_date.strftime("%A %d %B %Y");
				
				lignes += '<div class="objet_recherche">';
				lignes += '<h3>'+offre_titre+'</h3>';
				lignes += '<div class="contenu">';
				lignes += '<p class="date">'+offre_date+'</p>';
				lignes += '<h2 class="intitule">'+offre_intitule+'</h2>';
				lignes += '<div class="texte">';
				lignes += '<table style="width: 100%"><tbody><tr>';
				lignes += '<td><img src="/userfiles/images/OFFRES/'+offre_img+'"/></td>';
				lignes += '<td style="padding-left: 15px;">'+offre_texte+'</td>';
				lignes += '</tr></tbody></table>';
				lignes += '</div>';
				lignes += '</div>';
				lignes += '</div>';
			});*/
			lignes += '<p><span style="font-size: 22px">Afin de mieux r&eacute;pondre &agrave; vos attentes, la recherche de biens en location est en cours d&rsquo;&eacute;volution.<br/>Pour toutes questions ou demandes d&rsquo;informations veuillez &eacute;crire&nbsp;&agrave; <a href="mailto:location@gerer.fr">location@gerer.fr</a>&nbsp;ou&nbsp;appeler notre service client au&nbsp;<span style="color: #c02127">01 53 98 58 58</span>.</span></p><br/>';
			jq(lignes).appendTo(jq('#RES_RECHERCHE'));
			mainForPageLoad();
		}
	});
}
	
/********** FIN RESULTATS MOTEUR RECHERCHE **********/
