/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/* Affiche un élement */

/* Fonction d'affichage d'un logo. */
function printLogo(elmt){
    return '<a href="'+elmt.url+'" target="_self"><img src="/edito/'+elmt.file_path+elmt.file_name+
                '" alt="'+elmt.title+'" /></a>';
}

function showLogo(nLogo,url,idname){
    /* SANS DOJO : */ 
    var req = new XMLHttpRequest();
    req.open("GET", "/edito"+url, true);
    req.onreadystatechange = function() { 
        if (req.readyState==4)
            if (req.status==200){
                monCode(nLogo,idname,req.responseText);
            }
    }
    req.send(null); 
    
}

function monCode(nbLogo,idname,response) {
    var obj = JSON.parse(response); 
   if(typeof(obj) == 'undefined'){
       return;
   }
    var pubDrive = document.getElementById(idname);   // trouver un champ
    var result = "";
    /* On les affiches tous ? */
    if (nbLogo == 0){
     nbLogo = obj.bloque.length+obj.nonbloque.length;   
    }
    
    
   /* Si on a plus de nbLogo bloqués : */
   if(obj.bloque.length >= nbLogo ){
     /* On en prend nbLogo au hasard (mais différent) */
     for(i=0;  i<nbLogo; i++){
        var a = Math.floor(Math.random()*obj.bloque.length);
        /* Fonction d'affichage d'un elément */
        result += printLogo(obj.bloque[a]);
        obj.bloque.splice(a,1);
     }
     
   }else{
   /* On affiche tout les bloqués : */
   for(var i in obj.bloque){
       result += printLogo(obj.bloque[i]);
        nbLogo--;
   }
      if(nbLogo > 0){
        /* Il faut encore en afficher... */
        for(i=0;  i<nbLogo; i++){
          var a = Math.floor(Math.random()*obj.nonbloque.length);
          /* Fonction d'affichage d'un elément */
          result += printLogo(obj.nonbloque[a]);
          obj.nonbloque.splice(a,1);
       }
      }
   }
   pubDrive.innerHTML = result;
   return;
}
