/*=======================================================================
	-------------------------------------------- LAFORET.JS
=======================================================================*/

$(document).ready(function()
{
	/*-----------------------------------------------------
		Internet Explorer 7
	-----------------------------------------------------*/
	if($.browser.msie && $.browser.version=='7.0')
	{
		$('#rechercheAvancee').css('margin-left', '-355px');
		$('#rechercheAvancee .libelle').css('width', '140px');
		$('#rechercheAvancee .laSurface').css('padding-bottom', '0px');
		$('#rechercheAvancee .lesPieces').css('padding-bottom', '0px');
		
		$('#menu .item').css('line-height', '16px');
		$('#colonne .obligatoire').css('line-height', '16px');
	}
	
	/*-----------------------------------------------------
		Coups de coeurs (P.A.)
	-----------------------------------------------------*/
	$('#coupCoeur .ascenseur').everyTime(5000, function(i) 
	{
		if(parseInt($(this).css('margin-top'))>(4*-150))
		{
			$(this).animate({marginTop:parseInt($(this).css('margin-top'))-150}, 'normal');
		}
		else
		{
			$(this).animate({marginTop:0}, 'normal');
		}
	});
	

	/*-----------------------------------------------------
		Photos/Categories (P.A.)
	-----------------------------------------------------*/
	var nbPhoto=$('#listeCategorie .item').length;
	var j=0;
	var animCat=$(this).oneTime(100,function(){e});
	
	for(j=0; j<nbPhoto; j++)
	{
		$('#listeCategorie .item').eq(j).children('.lien').mouseover(function()
		{
			animCat.stopTime();
			$(this).parent().children('.titre').children('span.or').css('text-decoration', 'underline');
		
			$(this).parent().children('.photo').children('.zoom').stop();
			
			$('#listeCategorie .item').children('.photo').children('.zoom').stop();
			$(this).parent().children('.photo').children('.zoom').animate({width:364, height:240, marginTop:-20, marginLeft:-30}, 'fast');
			$(this).parent().prevAll('.item').children('.photo').children('.zoom').animate({width:304, height:201, marginTop:0, marginLeft:0}, 'fast');
			$(this).parent().nextAll('.item').children('.photo').children('.zoom').animate({width:304, height:201, marginTop:0, marginLeft:0}, 'fast');			
			
			if($.browser.msie!=true)
			{
				$(this).parent().children('.ombre').hide();
				$(this).parent().prevAll('.item').children('.ombre').fadeIn('fast');
				$(this).parent().nextAll('.item').children('.ombre').fadeIn('fast');
			}
		});
		
		
		$('#listeCategorie .item').eq(j).children('.lien').mouseout(function()
		{
			$(this).parent().children('.titre').children('span.or').css('text-decoration', 'none');
			animCat=$(this).oneTime(1000, function()
			{
				$(this).parent().children('.titre').children('span.or').css('text-decoration', 'none');

				$(this).parent().prevAll('.item').children('.ombre').hide();
				$(this).parent().nextAll('.item').children('.ombre').hide();
				
				$(this).parent().children('.photo').children('.zoom').animate({width:304, height:201, marginTop:0, marginLeft:0}, 'fast');
				//$(this).parent().children('.photo').children('.zoom').stop();
			});
		});
		
		$('#listeCategorie .item').eq(j).children('.lien').mousemove(function()
		{
			$(this).parent().children('.ombre').hide();
		});
	}
	
	/*-----------------------------------------------------
		Moteur de recherche (Type de bien)
	-----------------------------------------------------*/
	$('#choixType').click(function()
	{
		hauteurDiv=$('#listeType').height();
		etat=$('#choixTypeCentre').height();		
		if(etat==20)
		{
			$('.flecheGauche img', this).attr('src', '/laforetSkel/images/ico-fleche-bas.png');
			//$('#choixTypeItem').animate({height:200}, 300);
			$('#choixTypeCentre').animate({height:(hauteurDiv+45)}, 300);
		}
		else
		{
			$('.flecheGauche img', this).attr('src', '/laforetSkel/images/ico-fleche-gauche.png');
			//$('#choixTypeItem').animate({height:22}, 300);
			$('#choixTypeCentre').animate({height:20}, 300, function()
			{
				$('#choixTypeCentre').css('overflow', 'hidden');
			});			
			
		}
	});	
	
	$('.listeOptionType').click(function()
	{
		$('#choixType').html('<div class="flecheGauche"><img src="/laforetSkel/images/ico-fleche-gauche.png" alt="" /></div>'+$(this).text());
		$('#choixTypeCentre').animate({height:20}, 300, function()
		{
			$('#choixTypeCentre').css('overflow', 'hidden');
		});
		$('#formRechercheTo').val($(this).text());
	});
	
	$('.listeOptionType').mouseover(function()
	{
		$(this).css('background-image', 'url("/laforetSkel/images/overlay.png")');
	});
	
	$('.listeOptionType').mouseout(function()
	{
		$(this).css('background-image', 'none');
	});
	
	
	/*-----------------------------------------------------
		Moteur de recherche (Budget)
	-----------------------------------------------------*/
	$('#choixBudget').click(function()
	{
		hauteurDiv=$('#listeBudget').height();
		etat=$('#choixBudgetCentre').height();		
		if(etat==20)
		{
			$('.flecheGauche3 img', this).attr('src', '/laforetSkel/images/ico-fleche-bas.png');
			$('#choixBudgetCentre').animate({height:(hauteurDiv+45)}, 300);
		}
		else
		{
			$('.flecheGauche3 img', this).attr('src', '/laforetSkel/images/ico-fleche-gauche.png');
			$('#choixBudgetCentre').animate({height:20}, 300, function()
			{
				$('#choixBudgetCentre').css('overflow', 'hidden');
			});
		}
	});
	
	$('.optionBudget').click(function()
	{
		$('#choixBudget').html('<div class="flecheGauche"><img src="/laforetSkel/images/ico-fleche-gauche.png" alt="" /></div>'+$(this).text());
		$('#choixBudgetCentre').animate({height:20}, 300, function()
		{
			$('#choixBudgetCentre').css('overflow', 'hidden');
		});
				
		if(str_replace(' ', '', $('.min', this).text())=='')
		{
			$('#formRechercheBudgetmin').val('0');
		}
		else
		{
			$('#formRechercheBudgetmin').val(str_replace(' ', '', $('.min', this).text()));
		}
		
		if(str_replace(' ', '', $('.max', this).text())=='')
		{
			$('#formRechercheBudgetmax').val('9999999999999999999999999');
		}
		else
		{
			$('#formRechercheBudgetmax').val(str_replace(' ', '', $('.max', this).text()));
		}		
		
		$('#formRechercheBudgetTexte').val($(this).text());
	});
	
	$('.optionBudget').mouseover(function()
	{
		$(this).css('background-image', 'url("/laforetSkel/images/overlay.png")');
	});
	
	$('.optionBudget').mouseout(function()
	{
		$(this).css('background-image', 'none');
	});
	
	
	/*-----------------------------------------------------
		Moteur de recherche (Ville)
	-----------------------------------------------------*/
	$('#leChoixVille').click(function()
	{
		var etat=$('#choixVilleCentre').height();
		if(etat<=20)
		{
			$('.flecheGauche2 img').attr('src', '/laforetSkel/images/ico-fleche-bas.png');
			valVille=$('#villeValeur').val();
			if(valVille=='choisissez la ville')
			{
				valVille='';
			}
			$.ajax({
				type: "POST",
				url: "spip.php?page=ajax-liste-ville",
				data:"valVille="+valVille,
				success: function(msg){
					$("#listeVille").html(msg);					
					hauteurDiv=($('#listeVille').height());
					if(hauteurDiv==0)
					{
						$("#choixVilleCentre").css('height', 20);
						$("#choixVilleCentre").css('overflow', 'hidden');
					}
					else
					{
						$("#choixVilleCentre").css('height', (hauteurDiv+20));
					}
				}
			});
		}
		else
		{
			$('.flecheGauche2 img').attr('src', '/laforetSkel/images/ico-fleche-gauche.png');
			$("#choixVilleCentre").css('height', 20);
			$("#choixVilleCentre").css('overflow', 'hidden');
		}
	});
	
	
	/*-----------------------------------------------------
		Annonce (Carrousel)
	-----------------------------------------------------*/
	$('.annonce .galerie .item').mouseover(function()
	{
		$('.annonce .photo img').hide();
		$('#photo'+$('span', this).text()).show();
	});
	
	var animationCarrouselAnnonce=0;
	var longueurCarrousel=141*$('.annonce .galerie .ascenseur img').length;
	
	$('.annonce .btnGauche img').click(function()
	{
		espaceGauche=parseInt($('.annonce .galerie .ascenseur').css('margin-left'))+141;
		if(animationCarrouselAnnonce==0 && parseInt($('.annonce .galerie .ascenseur').css('margin-left'))<0)
		{
			animationCarrouselAnnonce=1;
			$('.annonce .galerie .ascenseur').animate({marginLeft:espaceGauche},function()
			{
				animationCarrouselAnnonce=0;
			});
		}
	});
	
	$('.annonce .btnDroite img').click(function()
	{		
		espaceGauche=parseInt($('.annonce .galerie .ascenseur').css('margin-left'))-141;
		val=espaceGauche*(-1);
		val=val+564;
		if(animationCarrouselAnnonce==0 && val<=longueurCarrousel)
		{
			animationCarrouselAnnonce=1;
			$('.annonce .galerie .ascenseur').animate({marginLeft:espaceGauche},function()
			{
				animationCarrouselAnnonce=0;
			});
		}
	});
	
	
	/*-----------------------------------------------------
		Recherche
	-----------------------------------------------------*/
	$('#recherche .typeTransaction .item').click(function()
	{
		$('#recherche .typeTransaction .item').removeClass('on');
		$('#recherche .typeTransaction .item').addClass('off');
		
		$(this).removeClass('off');
		$(this).addClass('on');
		
		valText=$(this).text();
		
		$('#choixBudget').html('<div class="flecheGauche"><img src="/laforetSkel/images/ico-fleche-gauche.png" alt="" /></div>Tous les budgets');
		
		if((valText=='Location' && $('#formRechercheAl').val()=='acheter') || (valText=='Achat' && $('#formRechercheAl').val()=='louer'))
		{
			$('#formBudgetmin').val('');
			$('#formBudgetmax').val('');
			
			if(valText=='Location')
			{	
				$('#formRechercheAl').val('louer');
				$('#pourLocation').show();
				$('#pourAchat').hide();
			}
			else
			{
				$('#formRechercheAl').val('acheter');
				$('#pourAchat').show();
				$('#pourLocation').hide();
			}
		}
	});
	
	$('.optionPiece').click(function()
	{
		var srcVal=$('img', this).attr('src');
		if(srcVal=='/laforetSkel/images/btn-checkbox-on.png')
		{
			$('img', this).attr('src', '/laforetSkel/images/btn-checkbox-off.png');
			$('#formRecherchePiece').val(str_replace($(this).text(), '', $('#formRecherchePiece').val()));
		}
		else
		{
			$('img', this).attr('src', '/laforetSkel/images/btn-checkbox-on.png');
			$('#formRecherchePiece').val($('#formRecherchePiece').val()+$(this).text());
		}
		
	});

	$('.optionChambre').click(function()
	{
		var srcVal=$('img', this).attr('src');
		if(srcVal=='/laforetSkel/images/btn-checkbox-on.png')
		{
			$('img', this).attr('src', '/laforetSkel/images/btn-checkbox-off.png');
			$('#formRechercheChambre').val(str_replace($(this).text(), '', $('#formRechercheChambre').val()));
		}
		else
		{
			$('img', this).attr('src', '/laforetSkel/images/btn-checkbox-on.png');
			$('#formRechercheChambre').val($('#formRechercheChambre').val()+$(this).text());
		}
	});
	
	$('.lienRecherche').click(function()
	{		
		if($('#rechercheAvancee').is(':hidden'))
		{
			$('#rechercheAvancee').fadeIn();
			$('#formRechercheAvancee').val('1');
			
		}
		else
		{
			$('#rechercheAvancee').fadeOut();
			$('#formRechercheAvancee').val('0');
		}		
	});
	
	$('#recherche .submit').click(function()
	{
		$('#formRecherche').submit();
	});	
	
	
	/*-----------------------------------------------------
		Colorbox
	-----------------------------------------------------*/
	$('.colorboxSituer').colorbox(
	{
		close:"Fermer",
		onComplete:function()
		{
			load();
		}
	});
	
	$('.colorboxContact').colorbox(
	{
		close:"Fermer",
		onComplete:function()
		{
			$('#contact .prise .item').click(function()
			{
				$('#contact .prise .item').removeClass('on');
				$('#contact .prise .item').addClass('off');
				
				$(this).removeClass('off');
				$(this).addClass('on');
				
				$('#formulaire_contact').val($('span', this).text());
				
				if($('span', this).text()=='2')
				{
					$('#telObligatoire').show();
				}
				else
				{
					$('#telObligatoire').hide();
				}
			});
			
			
			var optionsForms= 
			{         
				success:afficherReponseFormulaire
			}; 
			
			
			$('#formContact').ajaxForm(optionsForms);
				
				
			function afficherReponseFormulaire(responseText, statusText, xhr, $form)
			{
				$.colorbox({close:"Fermer", html:responseText});
			}
			
			$.colorbox.resize();
		}
	});
	
	$('.colorboxRecherche').colorbox(
	{
		close:"Fermer",
		onComplete:function()
		{
			$.colorbox.resize();
		}
	});
	
	$('#formAlerte').ajaxForm(optionsForms);
	
	$("a[rel='listePhotos']").colorbox({close:"Fermer", current:"{current}/{total}", next:"Suiv.", previous:"Pr&eacute;c.", transition:"elastic"});
	
	/*-----------------------------------------------------
		Selection
	-----------------------------------------------------*/
	$('.ajouterSelection').click(function()
	{
		thisObject=$(this);
		val=$('span', this).text();
		$.ajax({
			type: "POST",
			url: "spip.php?page=ajax-selection-ajouter",
			data:"idAnnonce="+val,
			success: function(msg)
			{				
				thisObject.html('<span class="enAttente">'+val+'</span>Supprimez de votre s&eacute;lection');
				thisObject.attr('title', 'Supprimez de votre s&eacute;lection');
				thisObject.removeClass('ajouterSelection');
				thisObject.addClass('supprimerSelection');
				$.colorbox({close:"Fermer", html:msg});
			}
		});
	});
	
	$('.supprimerSelection').click(function()
	{
		thisObject=$(this);
		val=$('span', this).text();
		$.ajax({
			type: "POST",
			url: "spip.php?page=ajax-selection-supprimer",
			data:"idAnnonce="+val,
			success: function(msg)
			{				
				thisObject.html('<span class="enAttente">'+val+'</span>Ajoutez &agrave; votre s&eacute;lection');
				thisObject.attr('title', 'Ajoutez &agrave; votre s&eacute;lection');
				thisObject.removeClass('supprimerSelection');
				thisObject.addClass('ajouterSelection');
				$.colorbox({close:"Fermer", html:msg});
			}		
		});
	});
	
	/*-----------------------------------------------------
		Selection
	-----------------------------------------------------*/
	$('#annoncesCC .ascenseur').everyTime(4000, function(i) 
	{
		if(parseInt($(this).css('margin-top'))>-910)
		{
			$(this).animate({marginTop:parseInt($(this).css('margin-top'))-130}, 'normal');
		}
		else
		{
			$(this).animate({marginTop:0}, 'normal');
		}
	});
	
});

function ajouterSoulignage(val, valClass)
{
	$('.'+valClass).css('text-decoration', 'none');
	$('#'+val).css('text-decoration', 'underline');
}

function supprimerSoulignage(valClass)
{
	$('.'+valClass).css('text-decoration', 'none');
}

function bookmarksite(title, url)
{
    if (document.all)
		window.external.AddFavorite(url, title);
    else if (window.sidebar)
		window.sidebar.addPanel(title, url, "");
}

function number_format(number,decimals,dec_point,thousands_sep)
{var n=number,prec=decimals;n=!isFinite(+n)?0:+n;prec=!isFinite(+prec)?0:Math.abs(prec);var sep=(typeof thousands_sep=="undefined")?',':thousands_sep;var dec=(typeof dec_point=="undefined")?'.':dec_point;var s=(prec>0)?n.toFixed(prec):Math.round(n).toFixed(prec);var abs=Math.abs(n).toFixed(prec);var _,i;if(abs>=1000){_=abs.split(/\D/);i=_[0].length%3||3;_[0]=s.slice(0,i+(n<0))+
_[0].slice(i).replace(/(\d{3})/g,sep+'$1');s=_.join(dec);}else{s=s.replace('.',dec);}
return s;}

function str_replace(search,replace,subject){var s=subject;var ra=r instanceof Array,sa=s instanceof Array;var f=[].concat(search);var r=[].concat(replace);var i=(s=[].concat(s)).length;var j=0;while(j=0,i--){if(s[i]){while(s[i]=(s[i]+'').split(f[j]).join(ra?r[j]||"":r[0]),++j in f){};}}
return sa?s:s[0];}
