/*
 * Title:	counter.js
 * Author:	Peter Sterling
 * URI:		http://www.sterling-adventures.co.uk/
 * Date:	26 October 2007
 * Version:	1.0 Initial version.
 * Purpose:	Javascript to count down Months, Days, Hours, Minutes and Seconds to a target date and time.
 */


// Global to hold the target date (& time) for the countdown.
var target;


function set_digits(el_name, value, pad)
{
	// Optional padding parameter.
	if(pad === null) {
		pad = false;
	}

	// Convert value to a string and pad if required.
	var html = "";
	var val = value.toString();
	if(val.length == 1) {
		if(pad) {
			val = "0" + val;
		}
		else {
			// Spacer.
			html += '<div style="position: relative; z-index: 1001; width: 10px; height: 57px;"> </div>';
		}
	}

	// Loop the digits of value string to build HTML.
	for(var i = 0; i < val.length; i++) {
		html += '<div style="position: relative; z-index: 1001; width: 25px; height: 57px; background: transparent url(../counter/digits.png) -' + (val.charAt(i) * 25) + 'px; 0"> </div>';
	}

	// Set the innerHTML of the named element...
	var el = document.getElementById(el_name);
	if(el) {
		if(el.innerHTML != html) {
			el.innerHTML = html;
		}
	}
}


function update_count_down()
{
	var now = new Date(), months, days, hours, mins, secs;

	if(target > now) {
		// Figure out how many months to go...
		months = (target.getFullYear() - now.getFullYear()) * 12;
		months += target.getMonth() - now.getMonth();
		if(target.getDate() < now.getDate()) {
			months--;
		}
	
		// Days left in the next month...
		if(target.getDate() < now.getDate()) {
			days = 32 - now.getDate() + target.getDate() - new Date(now.getFullYear(), now.getMonth(), 32).getDate();
		}
		else {
			days = target.getDate() - now.getDate();
		}
	
		// Hours, minutes and seconds left till midnight...
		secs = Math.floor((new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1) - now) / 1000);
	
		// Add the number of hours and minutes from the target...
		secs += target.getHours() * 60 * 60;
		secs += target.getMinutes() * 60;
	
		// Hours, minutes and seconds.
		hours = Math.floor(secs / (60 * 60));
		secs -= hours * 60 * 60;
		mins = Math.floor(secs / 60);
		secs -= mins * 60;
	
		// Adjust the hours and days if more than a day of hours...
		if(hours >= 24) {
			days++;
			hours -= 24;
		}
	
		// Set the digits.
		if(days - 1 < 0) {
			set_digits('count-down-months', months - 1);
			set_digits('count-down-days', days - 1 + (32 - new Date(target.getFullYear(), target.getMonth(), 32).getDate()));

		}
		else {
			set_digits('count-down-months', months);
			set_digits('count-down-days', days - 1);
		}
		set_digits('count-down-hours', hours);
		set_digits('count-down-minutes', mins, true);
		set_digits('count-down-seconds', secs, true);
	
		// Go again in a second.
		setTimeout(update_count_down, 1000);
	}
}


function set_count_down_target(target_day, target_month, target_year, target_hour, target_minute)
{
	target = new Date(target_year, target_month - 1, target_day, target_hour, target_minute, 0);
	if(target > new Date()) {
		update_count_down();
	}
}
