<!--

//
//    countdown.js
//     
//    Copyright 2010 David Spångberg <david30002@hotmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program 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 General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

/*  
Example usage:

<div id="countdown">
  <script type="text/javascript">
    <!-- 
    c = new countdown('countdown', 2010, 2, 29, 10, 0, 0);
    c.showCountdown();
    -->
  </script>
</div>
*/

// elementId = html-element to use
// all other parameters are self-explanatory
function countdown(elementId, year, month, date, hour, minute, second) {
  var countdownElement = document.getElementById(elementId);
  var validDate = false;
  var cdDate;
  var now;
  this.dateIsInThePast = false;
  // This is needed to access the parent object within class functions.
  // There may be some other way to do this.
  var self = this;
  
  if(checkValidDate(year, month, date, hour, minute, second)) {
    cdDate = new Date(year, month-1, date, hour, minute, second);
    now = new Date();
    if(cdDate.getTime() - now.getTime() < 0)
      this.dateIsInThePast = true;
    validDate = true;
  }
  
  this.showCountdown = function() {
    if(!validDate)
      countdownElement.innerHTML = "Not a valid date!";
    else
      updateCountdown();
  }
  
  function updateCountdown() {
    now.setTime(now.getTime() + 1000);
    
    if(cdDate.getTime() - now.getTime() < 0)
      self.dateIsInThePast = true;
    
    if(!self.dateIsInThePast) {
      var mSeconds = cdDate.getTime() - now.getTime();
      var seconds = ( mSeconds - mSeconds % 1000 ) / 1000;
      var days = ( seconds - seconds % (60*60*24) ) / (60*60*24);
      seconds = seconds - days * 60 * 60 * 24;
      var hours = ( seconds - seconds % (60*60) ) / (60*60);
      seconds = seconds - hours * 60 * 60;
      var minutes = ( seconds - seconds % 60 ) / 60;
      seconds = seconds - minutes * 60;
    } else {
      days    = 0;
      hours   = 0;
      minutes = 0;
      seconds = 0;
    }
    
    var br = "<br />";
    countdownElement.innerHTML = 
      days    + " dag"    + (( days != 1) ? "ar" : "") + br +
      hours   + " timm"   + (( hours != 1) ? "ar" : "e") + br +
      minutes + " minut"  + (( minutes != 1) ? "er" : "") + " och" + br +
      seconds + " sekund" + (( seconds != 1) ? "er" : "") + br + 
      "kvar till nästa BSLx2";
    
    setTimeout(function() { updateCountdown(); },1000);
  }
}

function checkValidDate(year, month, date, hour, minute, second) {
  if(month > 12)
    return false;
  if(month == 2 && (date > 28 && !(date == 29 && isLeapYear(year))))
    return false;
  if(date > 31)
    return false;
  if(date > 30 && (month == 4 || month == 6 || month == 9 || month == 11))
    return false;
    
  if(hour > 23 || hour < 0)
    return false;
  if(minute > 59 || minute < 0)
    return false;
  if(second > 59 || second < 0)
    return false;
  
  return true;
}

function isLeapYear(year) {
  if (year % 400 == 0)
    return true;
  if (year % 100 == 0)
    return false;
  if (year % 4 == 0)
    return true;
  return false;
}

-->

