$(document).ready(function(){contForm._Init();});

var contForm={
	//attributes
	repObject:{}
	,uri:window.location.pathname
	,type:false
	,currentClick:false
	,isSubmited:false
	//methods
	,_Init:function(){
		//récupération de la liste des éléments
		$("[class*=popinForm_]").click(function(event){
			event.preventDefault();
			contForm.currentClick=$(this);
			contForm._GetForm(contForm._GetContext($(this)));
			event.stopPropagation();
		});
	}
	,_GetContext:function(targE){
		var tabClass=$(targE).get(0).className.split(" ");
		var idForm=false;
		var i=0;
		for(i; i<tabClass.length; i++)
			if(tabClass[i].indexOf("popinForm_")!=-1) idForm = tabClass[i].replace("popinForm_","");
		return idForm;
	}
	,_GetForm:function(idForm){
		this.type=idForm;
		// si le form est de type "postulez"
		var datas={"type":idForm,"uri":contForm.uri};
		if(contForm.type=="postulez"||contForm.type=="cooptez"||contForm.type=="recrutez"){
			contForm.date = $(contForm.currentClick).prev().children("strong").text();
			contForm.entreprise = $(contForm.currentClick).parent().prev().children("span.entreprise").children("span.text").text();
			contForm.annonce = $(contForm.currentClick).parent().prev().children("strong").text();
			datas.postulDate=contForm.date;
			datas.postulBoite=contForm.entreprise;
			datas.postulAnnonce=contForm.annonce;
		}
		this.request=$.ajax({
			type: "POST"
			,url: "/actions/getForm.php"
			,data: datas
			,success:contForm._SetForm
			,beforeSend:contForm._SetLoading
		});
	}
	,_SetForm:function(response){
		$("#contentPopin").html(response);
		$("#popin").removeClass("hide");
		// si le form est de type "postulez"
		if((contForm.type=="postulez" && !contForm.isSubmited)||(contForm.type=="cooptez" && !contForm.isSubmited)||(contForm.type=="recrutez" && !contForm.isSubmited)){
			$("#contentPopin #postulDate").get(0).value=contForm.date;
			$("#contentPopin #postulBoite").get(0).value=contForm.entreprise;
			$("#contentPopin #postulAnnonce").get(0).value=contForm.annonce;
		}
		
		//fermeture de la popin
		$("#headerPopin").click(function(event){
			event.preventDefault();
			contForm._Close(contForm._GetContext($(this)));
			event.stopPropagation();
		});
		//soumission du formulaire
		$("#contentPopin form").eq(0).submit(function(event){
			event.preventDefault();
			contForm.isSubmited=true;
			var datas=$(this).serialize();
			datas.uri=contForm.uri;
			$.post("/actions/getForm.php", datas, contForm._SetForm);
			window.location.hash="#bgPopin";
			event.stopPropagation();
		});
		if(contForm.isSubmited) contForm.isSubmited=false;
	}
	,_SetLoading:function(){
		var bodyHeight=$(document).height();
		$("#bgPopin").removeClass("hide").height(bodyHeight);
		$(document).scrollTop(0);
	}
	,_Close:function(){
		$("#popin").addClass("hide");
		$("#bgPopin").addClass("hide");
		/*
		$("body div").eq(0).css({
			"overflow" : "hidden"
			,"height": "auto"
		});
		*/
	}
}
