// JSTimers v0.2 - http://dev.psix.org/jstimers/
function timers_start(){
  if(timers){
    setInterval("timers_tick();",1000);
  }
}

function timers_tick(){
  for(var name in timers){   
    var trigger = false;

    if(timers[name][1] & 1){
      timers[name][0]--;
    }else{
      timers[name][0]++;
    }
    if(timers[name][2] != undefined){                                           // does this item have a limit?
      if(timers[name][1] & 1){                                                  // this is an decrementing counter
        if(timers[name][0] <= timers[name][2]){
          trigger = true;
          if(timers[name][1] & 2 && timers[name][0] < timers[name][2]){
            timers[name][0] = timers[name][2];                                  // lower limit
            trigger = false;
          }

        }
      }else{
        if(timers[name][0] >= timers[name][2]){
          trigger = true;
          if(timers[name][1] & 2 && timers[name][0] > timers[name][2]){
            timers[name][0] = timers[name][2];                                  // upper limit
            trigger = false;
          }
        }
      }
    }
    
    var doc = document.getElementById(name);
    if(doc){
      var seconds = 0 + timers[name][0];

      if(trigger){
        if(timers[name][1] & 8){
          doc.innerHTML = format_timediff(seconds);
          eval(timers[name][3]);
        }else{
          doc.innerHTML = format_timediff(seconds) + timers[name][3];
        }
      }else{
        doc.innerHTML = format_timediff(seconds);
      }
    }
  }
}

function format_timediff(seconds,max,accuracy){
    if(accuracy == undefined) accuracy = 2;
    
    var string = "";
    if(seconds < 0){
        string += "-";
        seconds = -seconds;
    }
    if(max == undefined){
        if(seconds < 60){
            max = 0;
        }else if(seconds < 3600){
            max = 1;
        }else if(seconds < 86400){
            max = 2;
        }else{
            max = 3;
        }
    }
    if(max == 3){
        var days = Math.floor(seconds / 86400);
        seconds  = seconds % 86400;
        if(string.length > 1) string += ":";
        if(days < 10) string += "0";
        string += days + "d";
    }
    if(max >= 2 && max <= (2 + accuracy)){
        var hours = Math.floor(seconds / 3600);
        seconds   = seconds % 3600;
        if(string.length > 1) string += ":";
        if(hours < 10) string += "0";
        string += hours + "h";
    }
    if(max >= 1 && max <= (1 + accuracy)){
        var minutes = Math.floor(seconds / 60);
        seconds  = seconds % 60;
        if(string.length > 1) string += ":";
        if(minutes < 10) string += "0";
        string += minutes + "m";
    }
    if(max >= 0 && max <= (accuracy)){
        if(string.length > 1) string += ":";
        if(seconds < 10) string += "0";
        string += seconds + "s";
    }
    
    return string;
}
