/**************************************************************************

LICENSE
=======

JavaScript Countdown Library, a script that counts down to given events.
Copyright (C) 2006 Kendrick Erickson <jscountdown@wheresmysocks.net>

This library is free software; you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published by 
the Free Software Foundation; either version 2.1 of the License, or (at 
your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to:

  Free Software Foundation, Inc.
  51 Franklin Street
  Fifth Floor
  Boston, MA 02110-1301 USA

The LGPL can be found online at http://www.gnu.org/licenses/lgpl.html.


USING THE COUNTDOWN LIBRARY
===========================

You really only need to do three things.

1) Save this file to a web-accessable directory.

2) Place the following code in the <head> of your HTML document.

      <script>
      
        var countdown_title_node = "countdowntitle";
        var countdown_time_node = "countdown";
        
        var dates = new Array();
        var names = new Array();
        
        dates.push(new Date(Date.parse("October 5, 2006 19:30:00 CDT")));
        names.push("Countdown to Year Six");
        dates.push(new Date(Date.parse("October 6, 2007 19:30:00 CDT")));
        names.push("Countdown to Year Seven");
        
      </script>
      <script type="text/javascript" src="countdown.js">
      </script>

3) Change the code to match your existing HTML document. The 
   countdown_title_node and countdown_time_node should be set to the ID of 
   the elements whose content you want replaced with the countdown 
   information. You may want to place some sort of loading text in the 
   given element so there is something there before the JavaScript and page 
   have time to load.
   
      <h1 id="countdowntitle">Loading</h1>
      <h2 id="countdown">countdown...</h2>

Then, add dates and descriptive names as necessary.

**************************************************************************/

function initialize()
{
  updateCountdown();
}


function updateCountdown()
{
  
  var deltat, days, hours, minutes, seconds;
  
  var now = new Date();
  
  for(var i = 0; i < dates.length; i++) {
  
    // Skip over past dates in the array.
    if(dates[i] <= now)
      continue;
    
    // Save the offset between now and the event...
    deltat = dates[i].getTime() - now.getTime();
    
    // ...and break out of the loop. 
    break;
    
  }
  
  // Do the calculations to convert the offset in milliseconds to a 
  // human-readable date.
  days = Math.floor(deltat / (1000 * 60 * 60 * 24));
  deltat -= days * (1000 * 60 * 60 * 24);
  hours = Math.floor(deltat / (1000 * 60 * 60));
  deltat -= hours * (1000 * 60 * 60);
  minutes = Math.floor(deltat / (1000 * 60));
  deltat -= minutes * (1000 * 60);
  seconds = Math.floor(deltat / 1000);
  
  // Find the countdown title and time nodes.
  var blarg = document.getElementById(countdown_title_node);
  var countdown = document.getElementById(countdown_time_node);
  
  // Check to see that we've found the nodes before attempting to set the
  // information in them.
  if(blarg != null && countdown != null) { 
    
    // If both nodes were found, the page has loaded enough to start updating
    // the countdown.
    
    // Update the title...
    blarg.firstChild.nodeValue = names[i];
    
    // ...and the time.
    countdown.firstChild.nodeValue = days + " days " + sprintf02d(hours) + ":" + sprintf02d(minutes) + ":" + sprintf02d(seconds);
    
  }
  
  // And finally, re-set the timeout so we can update it again relatively soon.
  setTimeout("updateCountdown();", 50);

}


function sprintf02d(x) {

  var number = parseInt(x);

  if(number >= 0) {
    if(number < 10)
      return "0" + number;
    else
      return number;
  }
  
}

window.onLoad = initialize();
