/*
 * @(#)countdown.js
 *
 *    Version: 0.99.20100301
 * Written by: Yves Kreis <mailto:yves.kreis@education.lu>
 *
 * Copyright (C) 2003-2010 by Yves Kreis
 *
 * This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 Luxembourg License.
 * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/lu/ or send a letter to 
 * Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
 *
 */

/*
 *
 * This JavaScript launches a CountDown and writes the remaining time to the element with id 'countdown'.
 * It uses six variables to specify
 *   - the target day (intDay);
 *   - the target month (intMonth);
 *   - the target year (intYear);
 *   - the target hour (intHour);
 *   - the target minute (intMinute);
 *   - the target second (intSecond);
 *
 * To change its values, use a script environment like in the following example:
 *    <html>
 *    <head>
 *      ...
 *      <script type="text/javascript">
 *      <!--
 *        var intDay    = 5;
 *        var intMonth  = 9;
 *        var intYear   = 2009;
 *        var intHour   = 12;
 *        var intMinute = 0;
 *        var intSecond = 0;
 *      //-->
 *      </script>
 *      <script type="text/javascript" src="countdown.js"></script>
 *      ...
 *    </head>
 *    ...
 *    </html>
 *
 * The text of the element with id 'countdown' can be initialized using a script environment like in the following example:
 *    <html>
 *    <head>
 *      ...
 *      <script type="text/javascript" src="countdown.js"></script>
 *      ...
 *    </head>
 *    <body>
 *      ...
 *      <div id="countdown">
 *        <script type="text/javascript">
 *        <!--
 *          document.writeln(getCountDown());
 *        //-->
 *        </script>
 *      </div>
 *      ...
 *    </body>
 *    </html>
 *
 */

if ("undefined" == typeof(intDay)) {
  var intDay = 1;
}
if ("undefined" == typeof(intMonth)) {
  var intMonth = 1;
}
if ("undefined" == typeof(intYear)) {
  var intYear = 2000;
}
if ("undefined" == typeof(intHour)) {
  var intHour = 0;
}
if ("undefined" == typeof(intMinute)) {
  var intMinute = 0;
}
if ("undefined" == typeof(intSecond)) {
  var intSecond = 0;
}

var dtmTarget = new Date(intYear, intMonth - 1, intDay, intHour, intMinute, intSecond);
var strMonths = new Array('janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'); 

function getCountDown() {
  var intDays    = 0;
  var intMonths  = 0;
  var intYears   = 0;
  var intHours   = 0;
  var intMinutes = 0;
  var intSeconds = 0;
  var dtmStart   = new Date();
  while (dtmStart.setFullYear(dtmStart.getFullYear() + 1) <= dtmTarget) {
    intYears++;
  }
  dtmStart.setFullYear(dtmStart.getFullYear() - 1);
  while (dtmStart.setMonth(dtmStart.getMonth() + 1) <= dtmTarget) {
    intMonths++;
  }
  dtmStart.setMonth(dtmStart.getMonth() - 1);
  while(dtmStart.setDate(dtmStart.getDate() + 1) <= dtmTarget) {
    intDays++;
  }
  dtmStart.setDate(dtmStart.getDate() - 1);
  while(dtmStart.setHours(dtmStart.getHours() + 1) <= dtmTarget) {
    intHours++;
  }
  dtmStart.setHours(dtmStart.getHours() - 1);
  while(dtmStart.setMinutes(dtmStart.getMinutes() + 1) <= dtmTarget) {
    intMinutes++;
  }
  dtmStart.setMinutes(dtmStart.getMinutes() - 1);
  while(dtmStart.setSeconds(dtmStart.getSeconds() + 1) <= dtmTarget) {
    intSeconds++;
  }
  var blnOutput = (0 < intYears);
  var strOutput = '<span class="bold">GLCR-Cup ' + intYear + '</span><br /><br />';
  if (blnOutput) {
    strOutput += intYears + ' année';
    strOutput += (1 != intYears) ? 's' : '';
    strOutput += '<br />';
  }
  blnOutput = blnOutput || (0 < intMonth);
  if (blnOutput) {
    strOutput += (10 > intMonths) ? '<span class="hidden">0</span>' : '';
    strOutput += intMonths + ' mois<br />';
  }
  blnOutput = blnOutput || (0 < intDays);
  if (blnOutput) {
    strOutput += (10 > intDays) ? '<span class="hidden">0</span>' : '';
    strOutput += intDays + ' jour';
    strOutput += (1 != intDays) ? 's' : '<span class="hidden">s</span>';
    strOutput += '<br />';
  }
  blnOutput = blnOutput || (0 < intHours);
  if (blnOutput) {
    strOutput += (10 > intHours) ? '<span class="hidden">0</span>' : '';
    strOutput += intHours + ' heure';
    strOutput += (1 != intHours) ? 's' : '<span class="hidden">s</span>';
    strOutput += '<br />';
  }
  blnOutput = blnOutput || (0 < intMinutes);
  if (blnOutput) {
    strOutput += (10 > intMinutes) ? '<span class="hidden">0</span>' : '';
    strOutput += intMinutes + ' minute';
    strOutput += (1 != intMinutes) ? 's' : '<span class="hidden">s</span>';
    strOutput += '<br />';
  }
  blnOutput = blnOutput || (0 < intSeconds);
  if (blnOutput) {
    strOutput += (10 > intSeconds) ? '<span class="hidden">0</span>' : '';
    strOutput += intSeconds + ' seconde';
    strOutput += (1 != intSeconds) ? 's' : '<span class="hidden">s</span>';
    strOutput += '<br />';
  }
  if ('' != strOutput) {
    strOutput += '<br />';
  }
  if (2000 < intYear) {
    strOutput += '<span class="bold">' + intDay + ' ' + strMonths[intMonth - 1] + ' ' + intYear + '</span>';
  }
  return strOutput;
}

function runCountDown() {
  document.getElementById('countdown').innerHTML = getCountDown();
  window.setTimeout(runCountDown, 1000);
}

if (new Date() < dtmTarget) {
  if (window.addEventListener) {
    window.addEventListener('load', runCountDown, false);
  } else if (window.attachEvent) {
    window.attachEvent('onload', runCountDown);
  }
}