$(document).ready(function(){
  var containers = "#recherche_sortie .containers";
  var firstcontainer = "#recherche_sortie .containers:first";
  var activeContainers = '#recherche_sortie .containers.active';
  var links = "#recherche_sortie .rechercheLinks";

  $(containers).hide();
  $(activeContainers).show();
  $(links).toggle(
    function(){$($(this).attr("href")).show();},
    function(){$($(this).attr("href")).hide();}
  );
  
  $("#sortie_lieu").change(function(){
  	$("#sortie_theme").children().each(function(){
  		if ($(this).attr('value') == undefined)
  			$(this).attr('selected', 'selected');
  		else
  			$(this).removeAttr('selected');
  	})
  });
  
  $("#sortie_theme").change(function(){
  	$("#sortie_lieu").children().each(function(){
  		if ($(this).attr('value') == undefined)
  			$(this).attr('selected', 'selected');
  		else
  			$(this).removeAttr('selected');
  	})
  });
  
  $("#strPlagesDates").change(function(){
  	$(this).children().each(function(){
  		if($(this).attr('selected') == true){
  			var begin = new Date();
  			var end = new Date();
  			switch($(this).attr('value'))
  			{
  				case "...":
  					begin = '';
  					end = '';
  					break;
  				case "1":
  					begin = begin.getDate()+"/"+(begin.getMonth()+1)+"/"+begin.getFullYear();
  					end = begin;
  					break;
  				case "2":
  					begin.setDate(begin.getDate() - (begin.getDay() ? begin.getDay() : 7) + 1);
  					begin = begin.getDate() + "/" + (begin.getMonth()+1) + "/" + begin.getFullYear();
  					end.setDate(end.getDate() - (end.getDay() ? end.getDay() : 7) + 7);
  					end = end.getDate() + "/" + (end.getMonth()+1) + "/" + end.getFullYear();
  					break;
  				case "3":
  					begin.setDate(begin.getDate() - (begin.getDay() ? begin.getDay() : 7) + 6);
  					begin = begin.getDate() + "/" + (begin.getMonth()+1) + "/" + begin.getFullYear();
  					end.setDate(end.getDate() - (end.getDay() ? end.getDay() : 7) + 7);
  					end = end.getDate() + "/" + (end.getMonth()+1) + "/" + end.getFullYear();
  					break;
  				case "4":
  					begin.setDate(begin.getDate() - (begin.getDay() ? begin.getDay() : 7) + 8);
  					begin = begin.getDate() + "/" + (begin.getMonth()+1) + "/" + begin.getFullYear();
  					end.setDate(end.getDate() - (end.getDay() ? end.getDay() : 7) + 14);
  					end = end.getDate() + "/" + (end.getMonth()+1) + "/" + end.getFullYear();
  					break;
  				case "5":
  					begin.setDate(begin.getDate() + 1);
  					begin = begin.getDate() + "/" + (begin.getMonth()+1) + "/" + begin.getFullYear();
  					end.setDate(end.getDate() + 30);
  					end = end.getDate() + "/" + (end.getMonth()+1) + "/" + end.getFullYear();
  					break;
  				case "6":
  					begin.setMonth(begin.getMonth() + 1);
  					begin = "1/" + (begin.getMonth()+1) + "/" + begin.getFullYear();
  					end.setMonth(end.getMonth() + 1);
  					end = getLastDay(end.getMonth()) + "/" + (end.getMonth()+1) + "/" + end.getFullYear();
  					break;
  				case "7":
  					begin.setMonth(begin.getMonth() + 1);
  					begin = "1/" + (begin.getMonth()+1) + "/" + begin.getFullYear();
  					end.setMonth(end.getMonth() + 3);
  					end = getLastDay(end.getMonth()) + "/" + (end.getMonth()+1) + "/" + end.getFullYear();
  					break;
  				case "8":
  					begin = begin.getDate() + "/" + (begin.getMonth()+1) + "/" + begin.getFullYear();
  					end.setMonth(11);
  					end = getLastDay(end.getMonth()) + "/" + (end.getMonth()+1) + "/" + end.getFullYear();
  					break;
  			}
  			begin = addZeros(begin);
  			end = addZeros(end);
  			$("#sortie_depart").attr('value', begin);
  			$("#sortie_retour").attr('value', end);
  		}
  	})
  });
});

function getLastDay(month){
	var date = new Date();
	date.setMonth(month+1);
	date.setDate(0);
	return date.getDate();
}

function addZeros(date){
	var tab = new Array();
	$.each(date.split("/"), function(i, n){
		if(n < 10 && n !="")
			n = "0"+n;
		tab[i] = n;
	});
	return tab.join("/");
}
