var leBody;

function load(){};

$('document').ready(function() { initialisation(); });


//fonction d'initialisation qui s'exécute apres le chargement du DOM
function initialisation(){

  load();

  //variable
  var url = '';
  var longueur = 0;
  leBody = $('body');
  
  
  //fonction qui met le le href de télécharger dans la phototheque
  $(".ev2_photos").each( function(){
    //je recupere la source de mon image
    url = $(this).find("img").attr("src");
    //si l'url de mon image n'est pas NULL
    if(url!==null){
      //longueur de la chaine
      longueur = url.length;
      //je ne prend la châine qu'à partir du caractere 17 jusqu'à la fin
      url = url.substring("17", longueur);
      //j'attribue l'url à mon lien 
      $(this).parent().find(".ev2_telecharger a").attr("href", "/media" + url);
      $(this).find("a").attr("href", "/media" + url);
    }
  });
  
  
  //pour utiliser ce code il faut mettre #ads_contact_form en display none
	if(leBody.hasClass("offres_emploi")){
	  
	  var contenu = $(".ev2_ads_societe ul li").text();
	  var position2 = contenu.length;
	  var contenu2 = $("#loffre h1").text();
	  contenu = trim(contenu);
	  
	  
    
    $("#socit_concerne option[value='" + contenu + "']").css('color', 'red');
    
	  
    /*tester si l'elment a été trouvé
    if($("#socit_concerne option[value='" + contenu + "']").size()!==1){
      //contenu = "lucas";  
    }*/
    
    //on met selected au champs cachés
    
    
	  $("#socit_concerne option[value='" + contenu + "']").attr('selected','selected');
	  
	  $("#type_doffre_demploiid").css('display', 'none');
	  $("#type_doffre_demploiid").parent().after("<p class='filiale_concerne_p'>" + contenu2 + "</p>");
	  
	  $("#socit_concerne").parent().after("<p class='filiale_concerne_p'>" + contenu + "</p>");
	  $("#socit_concerne + span").css('display', 'none');
	  
	  if($("#formpersonnalise").size()==0){
      $("#loffre").css("display", "none");
      $("#ads_contact_form").css("display", "block");
    }
   
    $(".postuler").click(function(){
      
      $("#loffre").slideUp("slow");
      $("#ads_contact_form").slideDown("slow");
      
    });
     
    
    $(".error_form").each(function(){
      $("#loffre").css("display", "none");
      $("#ads_contact_form").css("display", "block");
    });
   
	  
  }


  //fonction qui active les lightbox
  $('.ev2_lightbox').lightBox();
	
}

//initialisation pour le redimensionnement des images et le centrage
$(window).load(function() { initialisation2(); });

function initialisation2(){
	
}

function trim (myString){
  return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function imgRedimensionner(classe, wMax, hMax){
  
  //alert(classe);
  
  $("." + classe + " img").each(function(){
    
		
		//alert(offsetWidth);
		//alert(offsettHeight);
		
		// vérification de la largeur
  	//if(this.offsetWidth > wMax){
  	if(this.width > wMax){
  		var W = wMax;
  		// Contraint le rééchantillonage à une largeur fixe et maintient le ratio de l'image
  		//var H = Math.round((W / this.offsetWidth) * this.offsetHeight);
  		var H = Math.round((W / this.width) * this.height);
  	}
  	else{
  		//var H = this.offsetHeight;
  		//var W = this.offsetWidth;
  		var H = this.height;
  		var W = this.width;
  	}
  	var int_h= H;
  	var int_w= W;
  	//vérification de la hauteur
  	if(int_h > hMax){
  		H = hMax;
  		// Contraint le rééchantillonage à une hauteur fixe et maintient le ratio de l'image
  		W = Math.round((H / int_h) * int_w);
  	}
  	else{
  		H = int_h;
  		W = int_w;
  	}
  	
  	this.style.width = W + "px";
  	this.style.height = H + "px";
		
	});
  
}




///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function imgCentrer(classe){

	$('.' + classe).each(function () {
	   if($(this).parent().innerHeight() !== 0){
		      
          var nbParent = $(this).innerHeight();
          var Enfant = $(this).find("img");
          var nbEnfant = $(this).find("img").first().innerHeight();
		      if(nbEnfant!==0){
  		      var nbNew = Math.round((nbParent - nbEnfant)/2)-1;
  		      Enfant.css("padding-top", nbNew + "px");
  		    }
		      
      }
  });
  
}
