﻿function obj_nivelator(forClassName){
    var div = document.getElementsByTagName("div");
    var divToNivel = new Array();
    var divToNivelInner = new Array();
    var divToNivelHeight= new Array();
    var maxHeight  = 0;
   
   
   
    // on récupere les div correspondant à la classe désiré
    for(var iDiv = 0; iDiv< div.length; iDiv ++){
        if (div[iDiv].className == forClassName){
            divToNivel.push(div[iDiv]);
        }
    } 
    
    // on recupere tous les éléments contenu dans les divs précédement récupéré
     for(var iDiv = 0; iDiv< divToNivel.length; iDiv ++){
        var currentChild = divToNivel[iDiv].firstChild;
        
        while(currentChild){
            if (currentChild.offsetHeight){
                if (!divToNivelHeight[iDiv]){
                    divToNivelHeight.push(new Array());
                }
                
                if (currentChild.tagName== "UL" || currentChild.tagName== "ul"){
                    var a = currentChild.getElementsByTagName("a");
                    var aCurrentChild = a[0].firstChild;
                 
                    while(aCurrentChild){ 
                        if (aCurrentChild.offsetHeight){ 
                            divToNivelHeight[iDiv].push(aCurrentChild);
                        }
                        aCurrentChild = aCurrentChild.nextSibling;
                    }
                }
                
                
                    divToNivelHeight[iDiv].push(currentChild);
 
            }
            
            currentChild = currentChild.nextSibling;
        }
     }
     
     var nbElmt = 0;
     
    for(var iDiv = 0; iDiv< divToNivelHeight.length; iDiv ++){
        if (divToNivelHeight[iDiv].length > nbElmt){
            nbElmt = divToNivelHeight[iDiv].length
        }
    }
     var marginStock = new Array();
     
     for(var iDiv = 0; iDiv< nbElmt; iDiv ++){
        marginStock[iDiv] = 0;
     }
      
         
     
       
        
     // on parcours chaque éléments compris dans les divs recherché 
     for(var iDiv = 0; iDiv< nbElmt; iDiv ++){
        var maxHeight = 0;
        var paddingTop;
        var paddingBottom;
        // on recupere kla hauteur maximal
        for(var iElmt = 0; iElmt< divToNivelHeight.length; iElmt ++){   
            if (divToNivelHeight[iElmt][iDiv] && divToNivelHeight[iElmt][iDiv].offsetHeight > maxHeight){
                maxHeight = divToNivelHeight[iElmt][iDiv].offsetHeight;
            }
        }
        
        // on mets à jour les champs plus petit
        for(var iElmt = 0; iElmt< divToNivelHeight.length; iElmt ++){   
            if (divToNivelHeight[iElmt][iDiv] && divToNivelHeight[iElmt][iDiv].offsetHeight < maxHeight){
                // on récupere le style CSS du noeud
                var curStyle = recup_style(divToNivelHeight[iElmt][iDiv]);
                
                if ((divToNivelHeight[iElmt][iDiv].tagName == "IMG" || divToNivelHeight[iElmt][iDiv].tagName == "img") && forClassName== 'bloc3col'){
                 
                    // divToNivelHeight[iElmt][iDiv].style.paddingTop =  +  "px";
                    marginStock[iElmt] = parseInt(maxHeight - divToNivelHeight[iElmt][iDiv].offsetHeight + parseInt(curStyle.paddingBottom.replace(/px/, "")) + parseInt(curStyle.paddingTop.replace(/px/, "")));

                    // on récupere le style CSS du noeud parent
                    curStyle = recup_style(divToNivelHeight[iElmt][iDiv].parentNode);
                    
                    // curStyle.backgroundPosition = "-810px -" + maxHeight - divToNivelHeight[iElmt][iDiv].offsetHeight + parseInt(curStyle.paddingBottom.replace(/px/, "")) + parseInt(curStyle.paddingTop.replace(/px/, "")) +  "px";
                    divToNivelHeight[iElmt][iDiv].parentNode.style.backgroundPosition = "-810px -" + marginStock[iElmt] +  "px"
                }
                else {
                    if (marginStock[iElmt] > 0){
                       
                        divToNivelHeight[iElmt][iDiv].style.paddingTop = parseInt(curStyle.paddingTop.replace(/px/, "")) + marginStock[iElmt] + "px"
                        marginStock[iElmt] = 0;
                    }
                    
                    
                    if (divToNivelHeight[iElmt][iDiv].tagName == "H3" || divToNivelHeight[iElmt][iDiv].tagName == "h3"){
                        divToNivelHeight[iElmt][iDiv].style.paddingTop = maxHeight - divToNivelHeight[iElmt][iDiv].offsetHeight + parseInt(curStyle.paddingTop.replace(/px/, "")) +  "px";
                    }
                    else{
                        divToNivelHeight[iElmt][iDiv].style.paddingBottom = maxHeight - divToNivelHeight[iElmt][iDiv].offsetHeight + parseInt(curStyle.paddingBottom.replace(/px/, "")) + parseInt(curStyle.paddingTop.replace(/px/, "")) +  "px";
                    }
                    
                }
                
            }
        }
     }
} 

 
function recup_style(thisElmt){
    if(window.getComputedStyle){
        return window.getComputedStyle(thisElmt,null);
    }
    else{
        return thisElmt.currentStyle;
    }
}

