
/// Fonction pour ouvrir un lien dans un nouvel onglet / page

$(document).ready(function(){
	$(".external-link").click(function(event){
	  event.preventDefault();
	  window.open($(this).attr('href'));
	});

	$("a.scrollTo").click(function(event){
		event.preventDefault();
		$.scrollTo($("div#"+$(this).attr('rel')+""),500);
	});

	$("a.scrollTop").click(function(event){
		event.preventDefault();
		$.scrollTo("body",500);
	});
	
	$("#news-archives").click(function(event){
		event.preventDefault();
		$(".news_bloc_archive:first").slideDown(function(){
			$(this).removeClass("news_bloc_archive");
			if($(".news_bloc_archive").length == 0){
				$("#news-archives").fadeOut();
			}
		});
	});
	
	$("select[name='date_debut']").change(function()
	{
		if($(this).val() != "")
		{
			filterDate();
		}
	});
	
	$("select[name='date_debut'], select[name='date_fin']").change(function()
	{
		$("select[name='tlot']").empty().append("<option value=''>"+$("input[name='waiting_for_data']").val()+"</option>");
		$("select[name='imme']").empty().append("<option value=''>"+$("input[name='waiting_for_data']").val()+"</option>");
		$("input[name='lib_imme']").val("");
		if($("select[name='date_debut']").val() != "" && $("select[name='date_fin']").val() != "")
		{
			$("select[name='tlot']").attr("disabled","disabled");
			$.post("scripts/xhrdata/getTypeLogement.php",
			{
				dateDebut: $("select[name='date_debut']").val(),
				dateFin: $("select[name='date_fin']").val()
			},function(data)
			{
				$("select[name='tlot']").empty();
				if(!data.error)
				{
					if(data.response.length > 0)
					{
						for(var i=0;i<data.response.length;i++)
						{
							$("select[name='tlot']").append("<option value='"+data.response[i].crit_val+"'>"+data.response[i].crit_lib+" <span>("+data.response[i].crit_nb+")</span></option>");
						}
						getResidences();
					}
					else
					{
						$("select[name='tlot']").append("<option value=''>"+$("input[name='nothing_to_show']").val()+"</option>");
					}
					
					$("select[name='tlot']").removeAttr("disabled");
				}
			},"json");
		}
	});
	
	$("select[name='tlot']").change(function()
	{
		getResidences();
	});

	$("select[name='imme']").change(function()
	{
		libImme();
	});
	
});

function libImme()
{
	if($("select[name='imme'] option:selected").text() != "")
		$("input[name='lib_imme']").val($("select[name='imme'] option:selected").text());
}

function getResidences()
{
	if($("select[name='date_debut']").val() != "" && $("select[name='date_fin']").val() != "" && $("select[name='tlot']").val() != "")
	{
		$("select[name='imme']").attr("disabled","disabled");
		$.post("scripts/xhrdata/getResidence.php",
		{
			dateDebut: $("select[name='date_debut']").val(),
			dateFin: $("select[name='date_fin']").val(),
			typeLocation: $("select[name='tlot']").val()
		},function(data)
		{
			$("select[name='imme']").empty();
			if(!data.error)
			{
				if(data.response.length > 0)
				{
					for(var i=0;i<data.response.length;i++)
					{
						$("select[name='imme']").append("<option value='"+data.response[i].crit_val+"'>"+data.response[i].crit_lib+" <span>("+data.response[i].crit_nb+")</span></option>");
					}
					libImme();
				}
				else
				{
					$("select[name='imme']").append("<option value=''>"+$("input[name='nothing_to_show']").val()+"</option>");
				}
				
				$("select[name='imme']").removeAttr("disabled");
			}
		},"json");
	}
}

function filterDate()
{
	var $this = $("select[name='date_debut']");
	var sDate = $this.val().split("/");
	var oDate = new Date();
	/* The order must be Year Month Day, when we set date, 
	because the date is check before doing the change 
	and it use the actual data if there not already set.
	*/
	oDate.setFullYear(sDate[2]);
	oDate.setMonth(parseInt(sDate[1])-1);
	oDate.setDate(sDate[0]);
	
	var sRange = $this.find("option[value='"+$this.val()+"']").attr("rel").split("-");

	$("select[name='date_fin'] option:not(:first)").attr("disabled", "disabled").hide();
	$("select[name='date_fin'] option:first").attr("selected","selected");
	
	for(var i=sRange[0];i<=sRange[1];i++)
	{
		for(var j=-1;j<2;j++)
		{
			var oNewDate = new Date;
			oNewDate.setTime(oDate.getTime()+((24*60*60*1000*7)*i) + ((24*60*60*1000)*j));
			var sDay = oNewDate.getDate();
			var sMonth = oNewDate.getMonth()+1;
			
			if(sDay < 10)
				sDay = "0"+sDay;
			
			if(sMonth < 10)
				sMonth = "0"+sMonth;
			
			$("select[name='date_fin'] option[value='"+sDay+"/"+sMonth+"/"+oNewDate.getFullYear()+"']").removeAttr("disabled").show();
		}
	}
}
