/*  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 link to the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Copyright © 2009 Nigel Jones, Mistweb Ltd.
    Version 1.1 2009-09-08

    Typical usage in a web page (see http://j-can.org.je/wiki/doku.php?id=show_the_counter for more details):

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
      var mistweb_countdown_headerStyle = 'color: red;';
      var mistweb_countdown_figureStyle = 'font-size: x-large; font-weight: bold; color: red;';
    </script>
    <script type="text/javascript" src="http://j-can.org.je/script/countdown.js"></script>
    <div id="counter"></div>
*/

// jQuery
$(document).ready(function() {
  $('div#counter').html(mistweb_countdown.buildUI());
  mistweb_countdown.updateCount();
  setInterval('mistweb_countdown.updateCount()', 1000);
});

// global variables passed from HTML page
var mistweb_countdown_headerStyle, mistweb_countdown_figureStyle;

// mistweb_countdown module, as a closure
var mistweb_countdown = function() {

  var target = new Date(2009, 11, 7, 12, 00);
  var linkUrl = "http://j-can.org.je";
  var linkTitle = "J-CAN Countdown to Copenhagen";

  var secsInMin = minsInHr = 60;
  var hrsInDay = 24;
  var oneSec = 1000;
  var oneMin = oneSec * secsInMin;
  var oneHr  = oneMin * minsInHr;
  var oneDay = oneHr  * hrsInDay;

  function buildUI() {
    $('head').append(buildStylesheet());
    var table = '<table id="countdown" border="0"><tr><th>Days</th><th></th><th>Hrs</th><th></th><th>Mins</th><th></th><th>Secs</th></tr></table>';

    return table
  }

  function buildStylesheet() {
    stylesheet = '<style type="text/css"> #countdown { border-collapse: collapse; } #countdown th, #countdown td   { padding: 0; text-align: center; } #countdown a:link, #countdown a:visited { color:inherit; text-decoration: none; } ';
    if (mistweb_countdown_headerStyle) 
        stylesheet += ' #countdown th {' + mistweb_countdown_headerStyle + '}';
    if (mistweb_countdown_figureStyle)
        stylesheet += ' #countdown td {' + mistweb_countdown_figureStyle + '}';
    stylesheet += '</style>';
    return stylesheet;
  }

  function updateCount() {
    $('tr.data').remove();
    $(getCount()).prependTo('table#countdown');
    $('table#countdown').find('th, td').each(function() {
      $(this).html('<a href="' + linkUrl + '" title="' + linkTitle + '">' + $(this).text() + '</a>')
    });
  }

  function getCount() {
    diff = target.getTime() - new Date().getTime();
    return '<tr class="data"><td>' + getDays(diff) + 
        '</td><td>,</td><td>' + getHrs(diff) + 
        '</td><td>:</td><td>' + getMins(diff) + 
        '</td><td>:</td><td>' + getSecs(diff) + 
        '</td></tr>';
  }

  function getDays(diff) {
    return Math.floor(diff/oneDay);
  }

  function getHrs(diff) {
    return doCalc(diff, oneHr, hrsInDay);
  }

  function getMins(diff) {
    return doCalc(diff, oneMin, minsInHr);
  }

  function getSecs(diff) {
    return doCalc(diff, oneSec, secsInMin);
  }

  function doCalc(diff, unit, noOfUnitsInNextLargerUnit) {
    result = Math.floor((diff/unit) % noOfUnitsInNextLargerUnit).toString();
    if (result.length < 2) result = '0' + result;
    return result;
  }

  // reveal public pointers
  return {
    buildUI: buildUI,
    updateCount: updateCount
  }

}();


