﻿/* Initializations */

var timerID;
var timerRunning = false;
var today = new Date();
var enday = new Date();
var secPerDay = 0;
var minPerDay = 0;
var hourPerDay = 0;
var secsLeft = 0;
var secsRound = 0;
var secsRemain = 0;
var minLeft = 0;
var minRound = 0;
var minRemain = 0;
var timeRemain = 0;

/* This function will stop the clock */
function stopclock (){
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;
}

/* This function will start the clock */
function startclock () {
stopclock();
showtime();
}

/* This is the heart of the script */
function showtime () {
today = new Date();
enday = new Date("May, 13 2012 00:00");
enday.setYear("2012");
secsPerDay = 1000 ;
minPerDay = 60 * 1000 ;
hoursPerDay = 60 * 60 * 1000;
PerDay = 24 * 60 * 60 * 1000;

/*Seconds*/
/*
secsLeft is the raw seconds remaining until the date.
secsRound is the rounded seconds remaining until the date.
secsRemain is the re-configured seconds. The seconds need to be
limited from 59 to 0 much like a real clock counts.
*/
secsLeft = (enday.getTime() - today.getTime()) / minPerDay;
secsRound = Math.round(secsLeft);
secsRemain = secsLeft - secsRound;
secsRemain = (secsRemain < 0) ? secsRemain = 60 - ((secsRound - secsLeft) * 60) : secsRemain = (secsLeft - secsRound) * 60;
secsRemain = Math.round(secsRemain);

/*Minutes*/
/*
minLeft is the raw minutes remaining until the date.
minRound is the rounded seconds remaining until the date.
minRemain is the re-configured minutes. The minutes need to be
limited from 59 to 0 much like a real clock.
*/
minLeft = ((enday.getTime() - today.getTime()) / hoursPerDay);
minRound = Math.round(minLeft);
minRemain = minLeft - minRound;
minRemain = (minRemain < 0) ? minRemain = 60 - ((minRound - minLeft)  * 60) : minRemain = ((minLeft - minRound) * 60);
minRemain = Math.round(minRemain - 0.495);

/*Hours*/
/*
hoursLeft is the raw hours remaining until the date.
hoursRound is the rounded hours remaining until the date.
hoursRemain is the re-configured hours. The hours need to be
limited from 23 to 0 much like a real clock.
*/
hoursLeft = ((enday.getTime() - today.getTime()) / PerDay);
hoursRound = Math.round(hoursLeft);
hoursRemain = hoursLeft - hoursRound;
hoursRemain = (hoursRemain < 0) ? hoursRemain = 24 - ((hoursRound - hoursLeft)  * 24) : hoursRemain = ((hoursLeft - hoursRound) * 24);
hoursRemain = Math.round(hoursRemain - 0.5);

/*Days*/
/*
daysLeft is the raw days remaining until the date.
daysRound is the rounded days remaining until the date.
daysRemain is the re-configured days.
*/
daysLeft = ((enday.getTime() - today.getTime()) / PerDay);
/*daysLeft = (daysLeft - 0.5);
daysRound = Math.round(daysLeft)*/;
daysRound = Math.ceil(daysLeft);
daysRemain = daysRound;

/*Time*/
/*
timeRemain makes the clock more elegant.
document.clock.face.value is used to show the time remaining.
timeID controls the refresh rate of the clock. The number is the rate in 
milliseconds. The if statement will stop the clock when the time has expired.
*/
/*timeRemain = "" + daysRemain + " dagen, " + hoursRemain + " uren, " + minRemain + " minuten en " + secsRemain + " seconden"*/;
timeRemain = "Nog " + daysRemain + " dagen tot de 38e Terwinseler Straatmarkt! ";

document.clock.face.value = timeRemain;
timerID = setTimeout("showtime()",1000);
/*timerRunning = true*/;
timerRunning = false;

if (daysRemain < 2) {
clearTimeout(timerID);
timerRunning = false;
document.clock.face.value = "Morgen wordt de 38e Terwinseler Straatmarkt gehouden! " + daysLeft + "; " + daysRound + "; " + daysRemain ;
}

if (daysRemain == 0) {
clearTimeout(timerID);
timerRunning = false;
document.clock.face.value = "Vandaag wordt de 38e Terwinseler Straatmarkt gehouden! ";
}

if (daysRemain < 0) {
clearTimeout(timerID);
timerRunning = false;
document.clock.face.value = "De Terwinseler Straatmarkt is voorbij, tot ziens op 13 mei 2012! ";
}

}

