
/* - general.js - */
var lst;
var index = -1;

function testmoo() {
  if (!document.getElementById("wrapper-content")) {
    return;
  }
  jq("#type1").click(function() {jq("#field-statut").slideUp("slow");jq("#field-organisme").slideUp("slow");});
  jq("#type2").click(function() {jq("#field-statut").slideDown("slow");jq("#field-organisme").slideDown("slow");});
  jq("#type3").click(function() {jq("#field-statut").slideUp("slow");jq("#field-organisme").slideDown("slow");});
  jq("#type4").click(function() {jq("#field-statut").slideUp("slow");jq("#field-organisme").slideDown("slow");});
  if (jq("#type2").is(':checked') || jq("#type3").is(':checked') || jq("#type4").is(':checked')) {
    jq("#field-statut").slideDown("slow");
  }
  if (jq("#type2").is(':checked')) {
    jq("#field-organisme").slideDown("slow");
  }
  jq("#bord-alpha").animate({"height":"+=" + (document.getElementById("wrapper-content").offsetHeight - 378) + "px"},200,'linear');
  montrePhoto();
}

function montrePhoto() {
  index++;
  lst = jq("#photo img");
  if (index < lst.length) {
    jq(lst[index]).animate({"left":"+=165px"},400,'linear',function () {jq(this).effect("bounce", {times:4,direction:"left",distance:10}, 150, montrePhoto);});
  }
}

registerPloneFunction(testmoo);

var indexGP = 0;

function gpaccueil() {
  if (!document.getElementById("gp-accueil")) {
    return;
  }
  
  jq("#gp-accueil").css({"backgroundImage":"url('" + gpa[indexGP] + "')"});
  nextGP();
  setTimeout("changegpaccueil()",4000);  
}

function nextGP() {
  indexGP += 1;
  if (indexGP == gpa.length) {
    indexGP = 0;
  }
}

function changegpaccueil() {
  jq("#gp-accueil").fadeTo(600,0.1,function() {jq("#gp-accueil").css({"backgroundImage":"url('" + gpa[indexGP] + "')"}).fadeTo(900,1,nextGP)});
  setTimeout("changegpaccueil()",4000);
}
  
registerPloneFunction(gpaccueil);

function openSimu(url) {
  window.open(url,'Simulateur','width=700,height=500');
  return false;
}


/*** Gestion formulaire ***/

function initFormulaire() {
  var val = jq("input:checked[name=sitFam]").val();
  if (!val || val == "seul") {
    jq("#field-conjointTravail").css("display","none");
    jq("#field-socConjoint").css("display","none");
  } else if (jq("input:checked[name=conjointTravail]").val() != "o") {
    jq("#field-socConjoint").css("display","none");    
  }
  //changeSitFam();
  //changeConjointTravail();
  val = jq("select[name=nbenfant] option:selected").val();
  if (val == "0") {
    jq("#detail-enfant").css("display","none");
  } else {
    jq("#detail-enfant").css("display","block");
    changeNbEnfant();
  }
  changeIntCreche();
  jq(".satis-mg").each(function (i,elt) {
      if (jq(elt).find("input:checked").val() == 'n') {
	jq(elt).parent().find(".detail-satis-mg textarea").css("display","block");
      } else {
	jq(elt).parent().find(".detail-satis-mg textarea").css("display","none");
      }
    });
  var nb = parseInt(jq("#nbenfant").val());
  var enfant_creche = 1;
  jq(".mg-enfant select option:selected").each(function (i,elt) {
      if (i < nb) {
	if (elt.value > 1) {
	  jq("#field-choixpc").css("display","block");
	  enfant_creche = 0;
	  return false;
	}
      } else {
	return false;
      }
    });
  if (enfant_creche == 1) {
    jq("#field-choixpc").css("display","none");
  }
  //changeMG();
  if (! (jq("#naissance-prevue input:checked") && jq("#naissance-prevue input:checked").val() == 'o')) {
    jq("#field-date-naissance").css("display","none");
  }
  jq(".jour input").each(function(i,elt) {
      if (jq(elt).is(":checked")) {
	jq(elt).parent().parent().find(".detail-journee div.content-detail-journee").css("display","block");
	jq(elt).parent().parent().find('.detail-journee input[type=checkbox]').each(function(i,elt) {
	    if (jq(elt).is(":checked")) {
	      jq(elt).parent().parent().find("div[class!=journee]").css("display","none");
	    }
	  });
      } else {
	jq(elt).parent().parent().find(".detail-journee div.content-detail-journee").css("display","none");
      }
    });

  jq.datepicker.regional['fr'];
  jq("input[name=sitFam]").change(changeSitFam);
  jq("input[name=conjointTravail]").change(changeConjointTravail);
  jq("select[name=nbenfant]").change(changeNbEnfant);
  jq(".satis-mg input").change(changeSatisMG);
  jq("input[name=intcreche]").change(changeIntCreche);
  jq(".mg-enfant select").change(changeMG);
  jq(".jour input").change(changeJour);
  jq(".detail-journee input[type=checkbox]").change(changeJourComplet);
  jq(".sem-complete input").click(clickSemaineComplete);
  jq("#pas_creche input").click(compteChoixPC);
  jq("#bloc-pas-int-creche input").click(compteChoixPC2);
  jq(".dn-enfant input").datepicker({changeMonth: true,changeYear: true, maxDate: '0d',  minDate: '-4y'});
  jq("input#dateaccueil").datepicker({maxDate: '+1y',  minDate: '0d'});
  jq("#field-date-naissance input").datepicker({maxDate: '+1y',  minDate: '0d'});
  jq("#naissance-prevue input").change(changeNaissancePrevu);
  jq(".detail-satis-mg textarea").focus(function () {
      var parentthis = this;
      jq.fancybox({content:"<div class='popup-dsmg' style='width:400px;height:300px'><div style='font-weight:bold;margin-bottom:10px'>Pourquoi n'êtes vous pas satisfait de ce mode de garde ?</div><div><textarea style='display:block;width:350px;height:200px' name='dummy' id='dummy'>"+this.value+"</textarea></div><div style='margin:10px 50px 0 0;float:right'><a onclick='jq.fancybox.close()'> </a></div></div>",
	    onCleanup:function() {parentthis.value = jq("#dummy").val();},
	    autoDimensions:false,
	    width:'400px',
	    height:'300px'
	});
    });
}

function changeNaissancePrevu() {
  if (jq(this).val() == 'o') {
    jq("#field-date-naissance").slideDown();
  } else {
    jq("#field-date-naissance").slideUp();
  }
}

function compteChoixPC() {
  if (jq("#field-choixpc input:checked").size() > 3) {
    alert("Vous ne pouvez selectionner que 3 choix");
    jq(this).attr("checked","");
  }

}

function compteChoixPC2() {
  if (jq("#bloc-pas-int-creche input:checked").size() > 3) {
    alert("Vous ne pouvez selectionner que 3 choix");
    jq(this).attr("checked","");
  }
}

function changeNbEnfant() {
  var val = jq("select[name=nbenfant] option:selected").val();
  if (val == "0") {
    jq("#detail-enfant").slideUp();
  } else {
    val = parseInt(val);
    jq("#detail-enfant tbody tr").each(function (i,elt) {
	if (i <= val) {jq(elt).css("display","table-row");}
	else {jq(elt).css("display","none");}
      });
    changeMG();
    jq("#detail-enfant").slideDown();
  }
}

function changeSitFam() {
  var val = jq("input:checked[name=sitFam]").val();
  if (!val || val == "seul") {
    //jq("#field-conjointTravail").css("display","none");
    //jq("#field-socConjoint").css("display","none");
    jq("#field-conjointTravail").slideUp();
    jq("#field-socConjoint").slideUp();
  } else {
    //jq("#field-conjointTravail").css("display","block");
    jq("#field-conjointTravail").slideDown();
  }
}

function changeConjointTravail() {
  var val = jq("input:checked[name=conjointTravail]").val();
  if (val == "o") {
    //jq("#field-socConjoint").css("display","block");
    jq("#field-socConjoint").slideDown();
  } else {
    //jq("#field-socConjoint").css("display","none");
    jq("#field-socConjoint").slideUp();
  }
}

function changeMG() {
  var nb = parseInt(jq("#nbenfant").val());
  var enfant_creche = 1;
  jq(".mg-enfant select option:selected").each(function (i,elt) {
      if (i < nb) {
	if (elt.value > 1) {
	  jq("#field-choixpc").slideDown();
	  enfant_creche = 0;
	  return false;
	}
      } else {
	return false;
      }
    });
  if (enfant_creche == 1) {
    jq("#field-choixpc").slideUp();
  }
}

function changeSatisMG() {
  var parent = jq(this).parent().parent().parent();
  if (parent.find("input:checked").val() == 'n') {
    parent.find(".detail-satis-mg textarea").css("display","block");
  } else {
    parent.find(".detail-satis-mg textarea").css("display","none");
  }
}

function changeIntCreche() {
  if (jq("input:checked[name=intcreche]").val() == 'o') {
    //jq("#bloc-int-creche").css("display","block");
    //jq("#bloc-pas-int-creche").css("display","none");
    jq("#bloc-int-creche").slideDown();
    jq("#bloc-pas-int-creche").slideUp();
  } else if (jq("input:checked[name=intcreche]").val() == 'n') {
    //jq("#bloc-int-creche").css("display","none");
    //jq("#bloc-pas-int-creche").css("display","block");
    jq("#bloc-int-creche").slideUp();
    jq("#bloc-pas-int-creche").slideDown();
  } else {
    //jq("#bloc-int-creche").css("display","none");
    //jq("#bloc-pas-int-creche").css("display","none");
    jq("#bloc-int-creche").slideUp();
    jq("#bloc-pas-int-creche").slideUp();
  }
}

function clickSemaineComplete() {
  var nb = "1";
  if (jq(this).attr("name")=="semainecomplete2") {
    nb = "2";
  }
  
  if (jq(this).is(":checked")) {
    jq("#demande-horaire-creche" + nb + " .detail-journee input[type=checkbox]").each(function (i,elt) {
	if (!jq(elt).is(":checked")) {
	  jq(elt).attr("checked","checked");
	  jq(elt).change();
	}
      });
    jq("#demande-horaire-creche" + nb + " .jour input[type=checkbox]").each(function (i,elt) {
	if (!jq(elt).is(":checked")) {
	  jq(elt).attr("checked","checked");
	  jq(elt).change();
	}
      });
  } else {
    jq("#demande-horaire-creche" + nb + " input[type=checkbox]").each(function (i,elt) {
	if (jq(elt).is(":checked")) {
	  jq(elt).attr("checked","");
	  jq(elt).change();
	}
      });
  }
}

function changeJour() {
  if (jq(this).is(":checked")) {
    //jq(this).parent().parent().find(".detail-journee div").css("display","block");
    jq(this).parent().parent().find(".detail-journee div.content-detail-journee").slideDown();
  } else {
    //jq(this).parent().parent().find(".detail-journee div").css("display","none");
    jq(this).parent().parent().find(".detail-journee div.content-detail-journee").slideUp();
  }
}

function changeJourComplet() {
  if (jq(this).is(":checked")) {
    jq(this).parent().parent().find("div[class!=journee]").slideUp();
    jq(this).parent().parent().find("div[class!=journee]").css("display","none");
  } else {
    jq(this).parent().parent().find("div[class!=journee]").slideDown();
    jq(this).parent().parent().find("div[class!=journee]").css("display","block");
  }
}

registerPloneFunction(initFormulaire);


