
// Set the length of the timer, in seconds
var secs_to_delay = 7; // seconds between each pledge count refresh
var maxReloads = 2000; // max number of times the pledge count can refresh


// ============================------------------------
// Internal vars
var reloadCt = 0;
var timerRunning = false;
var timerID = null;

function InitializeTimer()
{
   secs_delay = secs_to_delay;
   StopTheClock(); // first stop any previous running clocks.
   StartTheTimer();
}

function StopTheClock()
{
    if(timerRunning)
        clearTimeout(timerID);
    timerRunning = false;
}

function StartTheTimer()
{
    if (secs_delay==0)
    {
        insertCount(reloadCt); // ajax call function
        reloadCt = reloadCt + 1;
        if(maxReloads > reloadCt)
           InitializeTimer();
        else
           StopTheClock();
    }
    else
    {
        self.status = secs_delay;
        secs_delay = secs_delay - 1;
        timerRunning = true;
        timerID = self.setTimeout("StartTheTimer()", 1000);
    }
}

InitializeTimer();



//----------------- AJAX CRAP ----------------------------

function insertCount(reloadCt) {
   var succID = 'signUps';
   var url    = 'ajax/insertCount.php';
   var params = 'reloadCt='+reloadCt;
   var ajax   = new Ajax.Updater({success: succID},  url, {method:'get', parameters:params, onFailure:reportError});
}
function reportError(request) {
   // $F('signUps') = "Error";
}
