var blocage_inscrip = false ;
$(document).ready(function(){

	$( "#menu_destinations .nb-person-link" ).tooltip({
		track: true,
		tooltipClass: "custom-tooltip-calendar",
		show: null,
		hide: null
	});

	$('span.descriptiontri').click(function(){
		$(this).parents(".numeropage").find(".listingtri").show() ;
    });
	
	$('div.listingtri_close').click(function(){
		$(this).parents(".listingtri").hide() ;
    });
	
	$('a#inscrirenewsletter_bouton').click(function(){
	
		if (!blocage_inscrip)
		{
			blocage_inscrip = true ;
			$.ajax({
				type: "POST",
				url: ajax_inscrippromo_listing,
				data: {
					mail : $('input#inscrirenewsletter_mail').val()
				}
			}).done(function(msg){
				
				var oj = typeof JSON != 'undefined' ? JSON.parse(msg) : eval('('+msg+')');
				
				if (oj.ok)
					$("#inscrirenewsletter_into").html(oj.txt) ;
				else
					alert(oj.txt) ;
					
			}).fail(function(jqXHR, textStatus){
				console.log("Erreur avec le chargement des infos newsletter.") ;
			});
		}
		
	});
	
	charge_infos_listing()  ;


	// Gestion des block promo
	$('.bloc_promo_intro').each(function(){
		$(this).on('click',function(){
			var bloc = $(this).parent().find('.bloc_promo_liste');
			var arrow = $(this).find('.bloc_promo_intro_fleche');
			if (bloc.is(':visible'))
			{
				arrow.html('&#9660;');
				bloc.slideUp();
			}
			else
			{
				arrow.html('&#9650;');
				bloc.slideDown();
			}
		});
	});


	// Gestion des calendriers dans le listing
	function show_calendar_group()
	{

		// 
		var aid = 0;
		var i = 0;
		var item = $(this);
		while (!item.attr('idproperty') && i++<20)
		{
			item = item.parent();
		}
		if (!item.attr('idproperty')) return false;
		aid = item.attr('idproperty');

		// Position courante
		var pos = parseInt($('#property-'+aid+' .listing_quick_calendar_slider').position().left,10);
		var bWidth = parseInt($('#property-'+aid+' .listing_quick_container #p0').innerWidth(), 10);
		bWidth = 200;

		// 
		var cpt = $('#property-'+aid+' .listing_quick_calendar_group').length;
		var mWidth = -(cpt-4) * bWidth;

		// Détection du sens
		if ($(this).hasClass('right')) {
			pos-= bWidth;
		}
		else {
			pos+= bWidth;
		}
		if (pos>0) pos = 0;
		if (pos<mWidth) pos = mWidth;
		$('#property-'+aid+' .listing_quick_calendar_slider').animate({ left: pos+'px' });

	}


	// Gestion "quick details"
	var last_opt = new Array();
	var maps_list = new Array();
	$('.listing_quick_details').on('click',function(){
		var annonce_id = $(this).attr('data-annonce');
		var option = $(this).attr('data-option');
		if (annonce_id && option)
		{
			var tmp = $(this).parent().parent().find('.listing_quick_container');
			if ($(this).hasClass('active'))
			{
				$(this).removeClass('active');
				tmp.parent().find('.listing_quick_bloc').removeClass('opened-tabs');
				tmp.slideUp();
				delete(last_opt[annonce_id]);
			}
			else
			{
				$(this).addClass('active');
				if (typeof(last_opt[annonce_id])!='undefined') last_opt[annonce_id].removeClass('active');
				last_opt[annonce_id] = $(this);
				tmp.parent().find('.listing_quick_bloc').addClass('opened-tabs');
				$.ajax({
					url: '/ajax_listing-quickdetails.php',
					data: {
						'option': option,
						'annonce': annonce_id
					}
				}).done(function(data){
					switch (option)
					{
						case 'maps':
							data = JSON.parse(data);
							tmp.html('<div class="listing_quick_map_banner"><a href="'+data.url+'">'+quicklisting_maps_link+'</a></div><div class="listing_quick_map"></div>');
							var nmap = tmp.find('.listing_quick_map').get(0);
							var position = new google.maps.LatLng(data.annonce_lat, data.annonce_lng);
							window.setTimeout(function(){
								maps_list[annonce_id] = new google.maps.Map(nmap,{
									center: position,
									scrollwheel: false,
									name: 'ann'+annonce_id,
									zoom: 14
								});
								new google.maps.Marker({
									position: position,
									icon: '/img/site/gmap/picto_location_32.png',
									map: maps_list[annonce_id]
								});
							},100);
							break;
						case 'photos':
							data = JSON.parse(data);
							var btn = '<div class="listing_quick_photos_buttons">';
							btn+= '<div class="left"></div>';
							btn+= '<div class="right"></div>';
							btn+= '</div>';
							var list = '<div class="jcarousel-wrapper listing_quick_photos" id="jc'+annonce_id+'">';
							list+= '<div class="jcarousel listing_quick_photos_list">';
							list+= '<ul class="jcarousel-list">';
							var ctrl = '<div class="listing_quick_photos_controls jcarousel-pagination">';
							for (var i=0; i<data.photos.length; i++)
							{
								list+= '<li><div class="photo" style="background-image:url(\''+data.photos[i]+'\');" onclick="javascript:document.location.href=\''+data.url+'#photos/'+(i+1)+'\';"></div></li>';
								ctrl+= '<span class="switch'+(i==0 ? ' active' : '')+'" onclick="javascript:listing_show_photo('+annonce_id+','+i+');"></span>';
							}
							ctrl+= '</div>';
							list+= '</ul>';
							list+= '</div>';
							list+= ctrl;
							list+= btn;
							list+= '</div>';
							tmp.html(list);
							$('#jc'+annonce_id+' .jcarousel').jcarousel({ animation: 'slow' });
							$('#property-'+annonce_id+' .listing_quick_photos_buttons div').attr('data-annonceID',annonce_id);
							$('#property-'+annonce_id+' .listing_quick_photos_buttons div').each(function(){ $(this).on('click',listing_show_photo_event); });
							window.setTimeout('listing_show_photo('+annonce_id+','+data.startImage+');',100);
							break;
						default:
							tmp.html(data);
							if (option=='calendar')
							{
								$('#property-'+annonce_id+' .listing_quick_calendar_control div').each(function(){ $(this).on('click',show_calendar_group); });
							}
							break;
					}
					if (!tmp.is(':visible'))
					{
						tmp.slideDown();
					}
				}).fail(function(){
					tmp.slideUp();
				});
			}
		}
	});

	// Détail photo
	var hashtag = document.location.hash;
	if (hashtag.substr(1,6)=='photos')
	{
		window.setTimeout(function(){
			openvignettefullscreen(hashtag.substr(8));
		},500);
	}
	
});

function charge_infos_listing() 
{
	ids = new Array() ;
	
	$.each( $(".blocproperty"), function( i, val ) {
		ids.push($(".blocproperty").eq(i).attr("idproperty")) ;
	}) ;
	
	var texte_deja = '<ul class="alertes_annonce"><li class="alerte_verte">'+zl_envoimailannonce+'</li></ul>';

	// Si on n'a pas d'ids, on ne fait pas d'appel
	if (ids.length!=0)
	{
		$.ajax({
			type: "POST",
			url: ajax_infos_listing,
			data: {
				annonces : ids.join(",")
			}
		}).done(function(msg){
			
			// Lecture de la réponse AJAX (c'est du JSON)
			var oj = typeof JSON != 'undefined' ? JSON.parse(msg) : eval('('+msg+')');

			for(i=0;i<oj.length;i++)
			{
				if (oj[i].cart)
					$("#remove-"+oj[i].idannonce).show() ;
				else
					$("#add-"+oj[i].idannonce).show() ;
				
				if (oj[i].deja)
					$("#bloc_alerte-"+oj[i].idannonce).html(texte_deja) ;
			}
			
			
			
		}).fail(function(jqXHR, textStatus){
			console.log("Erreur avec le chargement des infos.") ;
		});
	}
}


function listing_show_photo_event(e)
{
	var a = $(this).attr('data-annonceID');
	$('#jc'+a+' .jcarousel').jcarousel('scroll',$(this).hasClass('left')?'-=1':'+=1');
	var b = $('#jc'+a+' .jcarousel').jcarousel('target').index();
	$('#jc'+a).find('span.active').removeClass('active');
	$('#jc'+a).find('span.switch').eq(b).addClass('active');
}


function listing_show_photo(a,b)
{
	$('#jc'+a).find('span.active').removeClass('active');
	$('#jc'+a).find('span.switch').eq(b).addClass('active');
	$('#jc'+a+' .jcarousel').jcarousel('scroll',$('#jc'+a+' ul li').eq(b));
}

