var timers = new Array();
var loopTimers = 1; // Set to one if the timers are to keep going.
var windowReloading = false;

function initTimers ()
{
    // kill the stack
    timers = timers.slice(0,0);
    
    now = new Date();
    
    var o = 0;
    $('.countdown, .Countdown, #lblCountdown').each(
      function ()
      {
          time = $(this).text().split(':', 3);
          for(i=0;i<3;i++) { time[i] = parseInt(time[i], 10); }
          
          finish = new Date();
          
          finish.setHours(finish.getHours() + time[0]);
          finish.setMinutes(finish.getMinutes() + time[1]);
          finish.setSeconds(finish.getSeconds() + time[2]);
          
          var left = finish - now;
          var totalSecondsTemp;                    
          totalSecondsTemp = left/1000;
          
          
          if ((finish - now) > 0) {
              name = 'timer' + o++;
              
            if ($(this).attr('refresh') == "false")
                timers.push( { 
                    'name' : name, 
                    'finish' : finish, 
                    refresh : false, 
                    newFormating : $(this).attr('format') == 'long' ? true : false,
                    formating_percentage : $(this).attr('format') == 'percent' ? true : false,                    
                    redir :  $(this).attr('redir') ? $(this).attr('redir') : null,
                    redirSilent :  $(this).attr('redirSilent') ? $(this).attr('redirSilent') : null,
                    doneMessage :  $(this).attr('doneMessage') ? $(this).attr('doneMessage') : null,
                    progressBar : $('div.jsCountDownProgressBar'),
                    totalSeconds : totalSecondsTemp
                } );
            else
                timers.push( { 
                    'name' : name, 
                    'finish' : finish, 
                    refresh : true, 
                    newFormating : $(this).attr('id') == 'lblCountdown' ? true : false,
                    redir :  $(this).attr('redir') ? $(this).attr('redir') : null
                } );
            
            $(this).attr('id', name).addClass('countdownReal');
          } else if ((finish - now) > -4000) {
              if ($(this).attr('refresh') == "false") {
                  $(this).text("Finished").addClass('CountdownDone');
                  setTimeout("$('.CountdownDone').text('Refreshing');", 2000);
                  setTimeout("windowReload();", 4000);
              }
          } else {
              if ($(this).attr('refresh') == "false")
                  $(this).text('0:00:00');
              else
                  $(this).empty().append('<a href="/" onclick="windowReload(); return false;">Overdue!</a>').addClass('help').attr('rel', 'jOverdue');
          }
      }
    );
    //DumperAlert(timers);
    
    // arrange the count down
    timerTick();
}
function timerTick ()
{
    // Current time
    now = new Date();

    // Check each timer
    $(timers).each(
        function (i)
        {
            var left = this.finish - now;
            
            if (left < 0) {
                // if the timer is finished, remove the id attr and reload the window
                if ($("#" + this.name).length > 0) {
                    if (this.redir) {
                        $("#" + this.name).text("Redirecting..").attr('id', this.name + "Done").addClass('CountdownDone');
                        window.location = this.redir;
                    } else if (this.redirSilent) {
                        $("#" + this.name).text("finished!").attr('id', this.name + "Done").addClass('CountdownDone');
                        window.location = this.redirSilent;
                    } else if (this.doneMessage) {
                        $("#" + this.name).text(this.doneMessage).attr('id', this.name + "Done").addClass('CountdownDone');
                    } else if (this.refresh) {
                        $("#" + this.name).text("Finished").attr('id', this.name + "Done").addClass('CountdownDone');
                        setTimeout("$('.CountdownDone').text('Refreshing');", 2000);
                        setTimeout("windowReload();", 4000);
                    } else {
                        $("#" + this.name).text("00:00:00").attr('id', this.name + "Done").addClass('CountdownDone');
                    }
                }
            } else {
                var totalSecondsLeft=6000;          
                totalSecondsLeft = left/1000;
                
                hours = Math.floor(left / (1000 * 60 * 60)); 
                left -= hours * (1000 * 60 * 60);

                mins = Math.floor(left / (1000 * 60)); 
                left -= mins * (1000 * 60);

                secs = Math.floor(left / 1000); 
                left -= secs * 1000;
                
                var percentageWidth= Math.ceil(((this.totalSeconds - totalSecondsLeft) / this.totalSeconds) * 100);
                if (percentageWidth > 100) { percentageWidth = 100;}

                if (this.formating_percentage) 
                {
                    $("#" + this.name).text((percentageWidth) + "%");                
                }
                else if (this.newFormating)
                    $("#" + this.name).text(
                        (hours > 0 ? hours + " hours, " : "") +
                        (mins > 0 || hours > 0 ? mins + " minutes, " : "") +
                        secs + " seconds"
                    );
                else 
                    $("#" + this.name).text(hours + ':' + padDigits(mins) + ':' + padDigits(secs));
                    
                if (this.progressBar) 
                {
                    this.progressBar.css( { width : (percentageWidth) + "%" } );
                }

            }
        }
    );
    setTimeout("timerTick();", 100);
}

function windowReload ()
{
    if (windowReloading)
        return;
    else
        windowReloading = true;
        
    url = window.location.toString();
    if (url.match(/\?/)) {
        window.location = window.location.toString() + "&refresh=1";
    } else {
        window.location = window.location.toString() + "?refresh=1";
    }
}

function padDigits (num)
{
    s = num.toString();
    if (s.length == 1) {
        return "0" + s;
    }
    return s;
}

$(function(){
    $('a.delayed-link').each(function () {
        var link = this,
            delay = $(this).attr('rel').match(/wait:(\d+)/)[1],
            text = $(this).text(),
            span = $('<span>' + text + '...' + delay + '</span>');
        
        $(link)
            .hide()
            .after(span);
        
        var timer = setInterval(function () {
            delay--;
            if (delay > 0) {
                $(span).text(text + '...' + delay );
            } else {
                $(span).remove();
                $(link).show();
            }
        }, 1000);
    });
});


$(function(){
    $('a.delayed-link2').each(function () {
        var link = this,
            delay = $(this).attr('rel').match(/wait:(\d+)/)[1],
            text = $(this).text(),
            span = $('<a>' + text + '</a>');
        
        $(link)
            .hide()
            .after(span);
        
        var timer = setInterval(function () {
            delay--;
            if (delay <= 0) {
                $(span).remove();
                $(link).show();
            }
        }, 1000);
    });
});
