function centrerPopin(){
	// var top = ($(document).height()-$('.popin').height())/2
	//$(".popin").css({paddingTop: top + "px"});		
	$('#display-popin .popin').center(); 
}

$(function(){
	if($('.fancyZoom,.fancyZoom1').length){
		
		$('#carousel ul').clone(true).appendTo('#display-Img');
		$('#display-Img a').each(function(i) {		
			$(this).attr('id',$(this).attr('id')+'C');
		});	
				
		showPopin(false , '', '');
		
		
		$('.styles').click(function(){
			filtrerRecherche();	
		});
		$('.architecture').click(function(){
			filtrerRecherche();	
		});
	}
	
	if($('.modeleZoom').length){
		
		$('.modeleZoom').fancybox();
		
	}
})

var current = 0;
function filtrerRecherche()
{	
	
	valStyle = $(".styles:checked").val();
	valArchitecture = $(".architecture:checked").val();
	
	$('#plain2,#combles2,#etages2').attr('checked',false);
	
	if(valArchitecture == "a_p")
		$('#plain2').attr('checked',true);
	else if(valArchitecture == "a_c")
		$('#combles2').attr('checked',true);
	else if(valArchitecture == "a_e")
		$('#etages2').attr('checked',true);

	$('#authentique2,#contemporain2').attr('checked',false);
	
	if(valStyle == "s_a")
		$('#authentique2').attr('checked',true);
	else if(valStyle == "s_c")
		$('#contemporain2').attr('checked',true);
	
	showPopin(true, valStyle, valArchitecture);
	
}

function showPopin(filtre, valS, valA)
{	
	if(filtre)
	{		
		$('#display-Img').empty();
		$('.currentList').clone(true).appendTo('#display-Img');
		$('#display-Img a').each(function(i) {		
			$(this).attr('id',$(this).attr('id')+'C');
		});		
		
		totalImgF = 0;
		lesImages = new Array();			
		$(".currentList").find('img').each(function(i) {			
			valRel = $(this).parent().attr('rel');
			valTabRel = valRel.split('|');
			var useMode=-1;
			if(valS=="s_all" && valA=="a_all")
				useMode=3;
			else if(valS=="s_all" && valA!="a_all")
				useMode=1;
			else if(valS!="s_all" && valA=="a_all")
				useMode=2;
			else
				useMode=0;
				
			switch(useMode)
			{
				case 3:
					lesImages[totalImgF] = $(this).parent().attr('id');
					totalImgF = totalImgF+1;	
				break;
				case  1:
					if(valTabRel[1]==valA)
					{
						lesImages[totalImgF] = $(this).parent().attr('id');
						totalImgF = totalImgF+1;
					}
				break;
				case  2:
					if(valTabRel[0]==valS)
					{
						lesImages[totalImgF] = $(this).parent().attr('id');
						totalImgF = totalImgF+1;
					}
				break;
				
				case 0:
					if(valTabRel[0]==valS && valTabRel[1]==valA)
					{
						lesImages[totalImgF] = $(this).parent().attr('id');
						totalImgF = totalImgF+1;
					}
				break;
				
			
			}

		
		});
		
		maj_realisations();
		
		if(totalImgF >0)
		{
			$(".currentList").find('img').each(function(i) {
				if(jQuery.inArray($(this).parent().attr('id'), lesImages) == -1)
				{
					$('#'+$(this).parent().attr('id')+'C').remove();			
				}	
			});
		}
		else
		{
			$('.popin .visuel img').attr('src','/maisonfamiliale/media/img/no-result.gif');
			$('.paging').html('');
			$('.slider .next').hide();
			$('.slider .prev').hide();
			return false;
		}
		
		var totalimg = $("#display-Img").find("img").length;
		$('.paging').html('1'+'/'+totalimg);
		if(!$('#display-popin').is(':visible')){
			$('#display-popin').show();	
		}
				
		var img = $("#display-Img li img").eq(0).parent().attr('href');
		var indexElem = 0;		
		current = indexElem;
		$('.popin .visuel img').attr('src',img);
		resizeOverlay();
		centrerPopin();
		addSlider();
		filtre = false;			
		return false;
	}	
		
	$('.fancyZoom').click(function(){
	    $('#filtre').hide();
	    $('.link-offer').show();
		
		$('#display-Img').empty();
		$('#carousel ul').clone(true).appendTo('#display-Img');
		$('#display-Img a').each(function(i) {		
			$(this).attr('id',$(this).attr('id')+'C');
		});

		/****modification de la région****/
		/*if($('li.active a:first-child').html())
			$('.link-offer').html('Découvrez notre offre dans la région '+$('li.active a:first-child').html());
		else
			$('.link-offer').html('Découvrez notre offre dans la région ');*/
		
		if($('li.active a:first-child').html()){
			if( $('li.active a:first-child').attr('href') == '20137' ){
				//$('.link-offer').html('Découvrez notre offre ');
				$('.link-offer').hide();
			}else{
				$('.link-offer').show();
				$('.link-offer').html('Découvrez notre offre dans la région '+$('li.active a:first-child').html());
			}
		}else{
			$('.link-offer').hide();
			//$('.link-offer').html('Découvrez notre offre dans la région ');
		}

		if( $('li.active a:first-child').attr('href') != '20137' )
			$('.link-offer').attr('href', '/id/'+$('li.active a:first-child').attr('href'));
 	
		/**/

		var totalimg = $("#display-Img").find("img").length;

		var $idimglink=$(this).attr('id').split("img");
			
		var	$nbimg=$idimglink[1];
		 $('.paging').html($nbimg+'/'+totalimg);
		/**********/
		if(!$('#display-popin').is(':visible')){
			$('#display-popin').show();		
		}
		var img = $(this).attr('href');	

		var indexElem = $('#display-Img li a').index($('#'+$(this).attr('id')+'C'));		
		current = indexElem;//index de l'image selectionner

		$('.popin .visuel img').attr('src',img);
		resizeOverlay();
		centrerPopin();
		addSlider();
		
		return false;
	});	
	
    $('.fancyZoom1').click(function(){
		$('#filtre').show();
	    $('.link-offer').hide();


		$('#display-Img').empty();

		var ListVisu = $('.visu_list');
		
		ListVisu.clone(true).appendTo('#display-Img');

		/**** modification de la région ****/

		var totalimg = $("#display-Img").find("img").length;

		var $idimglink=$(this).attr('id').split("img");
			
		var	$nbimg=$idimglink[1];
		
		$('.paging').html('1/'+totalimg);
		/* ******** */
		if(!$('#display-popin').is(':visible')){
			$('#display-popin').show();		
		}

		var img = $($("#display-Img li img")[0]).parent().attr('href');
		console.info(img);
		
		$('.popin .visuel img').attr('src',img);
		resizeOverlay();
		centrerPopin();
		addSlider();
		
		return false;
	});  
	
	$('.close,#generate-popin').bind('click',closePopin);	
	
}

function resizeOverlay(){
	if($('#display-popin').is(':visible')){
		var bodyHeight = $(window).height()+$(window).scrollTop();	
		$("#generate-popin").height(bodyHeight);	
		
		$(window).unbind('scroll');
		$(window).bind('scroll',function(){
			centrerPopin();	
			resizeOverlay();
		});	
		
		$(window).resize(function() {
			centrerPopin();	
		});	
	}	
}

// close popin 
function closePopin() {
	$('#display-popin').hide();
	current=0;
}

function displayNavSlider(){
	var len = $('#display-Img li a').length-1;
	
	$('.slider .prev').show();
	$('.slider .next').show();

	if(current==len){
		$('.slider .next').hide();
	}
	
	if(current==0){
		$('.slider .prev').hide();
	}
}

// défilement image popin
function addSlider(){
	displayNavSlider();
	
	var len = $('#display-Img li a').length;
	
	$('.slider .next').unbind('click');
	$('.slider .next').bind('click',function(){	
		if(current<len){
			current++;
			var img = $($('#display-Img li a')[current]).attr('href');
			var totalimg=$("#display-Img").find("img").length;		
			$('.paging').html((current+1)+'/'+totalimg);
			
			$('.popin .visuel img').attr('src',img);
		}
		displayNavSlider();
		return false;
	})
	
	$('.slider .prev').unbind('click');
	$('.slider .prev').bind('click',function(){	
		if(current>0){
			current--;
			var totalimg=$("#display-Img").find("img").length;			
			$('.paging').html((current+1)+'/'+totalimg);						
			var img = $($('#display-Img li a')[current]).attr('href');						
			$('.popin .visuel img').attr('src',img);
		}
		displayNavSlider();
		return false;
	})
	
	
}

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

