// -- iChat & Skype dynamic update --
// id des images => "ichatstatus" et "skypestatus"
// an idea from http://www.petervannes.nl/files/76481f5674c2b8f555b770c8eeec84f2-21.php
// and modified by Joël Guillod
var onlineStatus = (function(){
    // set your interval in milliseconds 
    var reloadInterval = 60000, intervalID,
        ichats, // { id:[domEl, 'src original value'], id2:[…] }
        skypes, me;
        
        return {
            /*
            trace: function(){
                console.log('ichats',ichats);
                console.log('skypes',skypes);
                console.log('intervalID =',intervalID, 'reloadInterval =',reloadInterval);
            }, */
            // this will run when the document is fully loaded 
            init: function(ics,sks,reloadInt) {
                if (!me) me = this;
                if (reloadInt && reloadInt > 2999) reloadInterval = reloadInt;
                window.setTimeout(me.load,reloadInterval,ics,sks); 
            },
            clear: function(id2Clear){ // id2Clear : "|"-separated liste des id à ne plus rafraîchir,
                id2Clear.split('|').forEach(function(id){
                    if (ichats[id]) delete ichats[id];
                    if (skypes[id]) delete skypes[id];
                });
                if (Object.keys(ichats) == 0 && Object.keys(skypes) == 0){
                    window.clearInterval(intervalID);
                }
            },
            load: function(ics,sks){
                if (ics) {
                    ichats = {};
                    ics.split('|').forEach(function(e){
                        var d = document.getElementById(e);
                        ichats[e] = [d, d.src]
                    });
                }
                if (sks){
                    skypes = {};
                    sks.split('|').forEach(function(e){
                        var d = document.getElementById(e);
                        skypes[e] = [d, d.src]
                    });
                }
                intervalID = window.setInterval(me.reload,reloadInterval); 
            },
            // this reloads the src, and triggers the next reload interval 
            reload: function() {
              // get the object once
              // if the object exists
              if (ichats) {
                  for (var i in ichats){
                      e = ichats[i];
                      // Add a new uident variable to the url and change it in
                      // the object. This forces the img to reload.
                      e[0].src = e[1] + "&uident=" + Date.now(); //now() nécessaire pour éviter les effets du cache
                  }
              };
              // Now for SKYPE
              if (skypes) {
                  for (i in skypes){
                      e = skypes[i];
                      // Add a new uident variable to the url and change it in
                      // the object. This forces the img to reload.
                      e[0].src = e[1] + "?uident=" + Date.now(); //now() nécessaire pour éviter les effets du cache
                  }
              }
            } 
            
        }
})();
