/*
* Based on the javascript timer by Oliver James Gosling
* (http://www.goslingo.com/)  -extedned to include days.
******************************************************************************/

var ndays, nhours,  nmins,  nsec,  today;

function initialiseClock() {
	//Set the two dates
	var startdate =new Date(2005, 8, 15); //Month is 0-11 in JavaScript
	today=new Date();
	
	// time in seconds bewteen today and start
	var secondsToDate = (Math.ceil((today.getTime()/1000)-(startdate.getTime()/1000)));
	
	// assume that 220 days per year spent working with 7 hours per day
	var secondsPerYear = 220*7*3600;
	var secondsInYear = 365*24*3600;
	var workPercentage = Math.ceil((secondsPerYear/secondsInYear)*100);
	var secondsSpentWorking = secondsToDate * (workPercentage/100);
	
	ndays = Math.floor(secondsSpentWorking/86400);
	nhours=Math.floor(secondsSpentWorking/3600)  - (ndays * 24); 
	nmins=Math.floor(secondsSpentWorking/60)- (ndays * 1440) - (nhours*60); 
	nsec=Math.ceil(secondsSpentWorking%60);
	
	nmins = nmins + "";
	nsec = nsec + "";
	ndays = ndays + "";
	
	nhours = phours = nhours + "";
	nmins = pmins = nmins + "";
	nsec = psec = nsec + "";
	
	if(nhours.length == 1) { phours = "0"+nhours; }
	if(nmins.length == 1) { pmins = "0"+nmins; }
	if(nsec.length == 1) { psec = "0"+nsec; }
	
	$("#date_stats span").html('<span class="days">' + ndays+ '</span><span class="hours">'+phours+ '</span><span class="mins">'+pmins+'</span><span class="secs">'+psec+'</span>');
	clockTick();
}


function clockTick() {

	// Don't tick when I'm not working!
	currentDay = today.getDay();
	currentHour = today.getHours();
	if(((currentDay == 0) || (currentDay == 6)) || ((currentHour < 9) || (currentHour > 17))) {
		return 0;
	}
	
	nsec = parseInt(nsec);
	nmins = parseInt(nmins);
	nhours = parseInt(nhours);
	ndays = parseInt(ndays);
	
	nsec = nsec + 1;
	
	if(nsec == 60) {
		//console.log('reset sec');
		nsec = 0; 
		nmins = nmins + 1;
		if(nmins == 60) {
			//console.log('reset min');
			nmins = 0;
			nhours = nhours + 1;
			if(nhours == 24) {
				//console.log('reset hours');
				nhours = 0;
				ndays = ndays + 1;
			}
		}
	}
	
	
	nhours = phours = nhours + "";
	nmins = pmins = nmins + "";
	nsec = psec = nsec + "";
	
	if(nhours.length == 1) { phours = "0"+nhours; }
	if(nmins.length == 1) { pmins = "0"+nmins; }
	if(nsec.length == 1) { psec = "0"+nsec; }
	
	$("#date_stats span").html('<span class="days">' + ndays+ '</span><span class="hours">'+phours+ '</span><span class="mins">'+pmins+'</span><span class="secs">'+psec+'</span>');
	setTimeout("clockTick()", 1000);
}