/*
Countdown and scripts (c) 2004-2006 James Hoadley, HyneeHoadley AT hotmail DOT com,
Licensed to www.FIHS.net
*/


// Config

// Edit the DebugCountdown variable (true or false). Everything else in countdown-config.js
// Displays various debugging info. Keep as false!
var DebugCountdown = false;

// End config

CountdownActiveIdNum = null;
CountdownConfigs = new Array();

function RegisterCountdown(config) {
	//Records the countdown times, the returns an id number.
	cfg_num = CountdownConfigs.length;
	CountdownConfigs[cfg_num] = config; //This adds the config to the an array of all configs
	return cfg_num;
}

function CountdownInit(cfg_num) {

	if (cfg_num == CountdownActiveIdNum) {
		//If already active, return immediately so we don't have multiple threads
		//doing the update. Set for the info to be updated anyway.
		CountdownConfigs[cfg_num].DoUpdateEventInfo = true;
		return;
	}
	
	//global DebugCountdown
	if (DebugCountdown) {
		for (a in CountdownConfigs[cfg_num].Events) {
			document.write(CountdownConfigs[cfg_num].Events[a].date.toGMTString()+': '+CountdownConfigs[cfg_num].Events[a].name+'<br />');
		}
	}

	CountdownConfigs[cfg_num].DoUpdateEventInfo = true;
	
	// Get the current Event.
	// This assumes events are in time order.
	CurTime = new Date(); // current time
	if (DebugCountdown) {
		alert('# Events\n'+CountdownConfigs[cfg_num].Events.length);
	}
	FoundEvent = false;
	for (var i=0; i < CountdownConfigs[cfg_num].Events.length; i++) {
		cur_event = CountdownConfigs[cfg_num].Events[i];
		if ( (CurTime.getTime() < (cur_event.date.getTime() + CountdownConfigs[cfg_num].EventBuffer)) && !FoundEvent) {
			CountdownConfigs[cfg_num].ActiveEventIndex = i;
			FoundEvent = true;
			if (DebugCountdown) {
				alert(i + '\n' + CountdownConfigs[cfg_num].Events[i].name + '\n' +CountdownConfigs[cfg_num].Events[i].date.toGMTString() + '\n' + (CurTime.getTime() - (CountdownConfigs[cfg_num].Events[i].date.getTime() + CountdownConfigs[cfg_num].EventBuffer)) + '\n' +CountdownConfigs[cfg_num].Events[i].logo_img);
			}
		}
	}
	if (!FoundEvent) {
		CountdownConfigs[cfg_num].ActiveEventIndex = CountdownConfigs[cfg_num].Events.length-1;
	}
	if (DebugCountdown) {
		alert(i + '\n' + CountdownConfigs[cfg_num].Events[CountdownConfigs[cfg_num].ActiveEventIndex].name + '\n' +CountdownConfigs[cfg_num].Events[CountdownConfigs[cfg_num].ActiveEventIndex].date.toGMTString());
	}
	CountdownActiveIdNum = cfg_num;
	CountdownTimeout(cfg_num); //This sets it going. 
}

function toSt(n) {
	s=""
	if(n<10) s+="0"
	return s+n.toString();
}
 
function CountdownTimeout(cfg_num) {

	if (cfg_num == CountdownActiveIdNum) {
		var config = CountdownConfigs[cfg_num];
	
		var CurTime=new Date(); // current time
		var time_diff_ms=(config.Events[config.ActiveEventIndex].date.getTime()-CurTime.getTime());
		
		// Check if event is more than EventBuffer millisecs ago
		if (time_diff_ms < (-config.EventBuffer)) {
			if ((config.ActiveEventIndex+1) < config.Events.length) {
				config.ActiveEventIndex++;
				config.DoUpdateEventInfo = true;
				// Uncomment this line for slower loading images.
				//document.getElementById(COUNTDOWN_PICTURE_ELEM).src = config.PathToImages + 'blank.gif';
			}
		}
	
		var active_event = config.Events[config.ActiveEventIndex];
	
		//Do the timer
		// This assumes there is a timer element, unlike other fields
		
		var time_diff_s = Math.floor((config.Events[config.ActiveEventIndex].date.getTime()-CurTime.getTime())/1000);
	
		var output = '';
		if (!active_event.stall) {
			var count = null;
			if(time_diff_s<=0) { // It is after the event
				output += '<b>+</b>';
				count = -time_diff_s
			}
			else {
				count = time_diff_s;
			}
			var secs=toSt(count%60);
			count=Math.floor(count/60);
			var mins=toSt(count%60);
			count=Math.floor(count/60);
			var hours=toSt(count%24);
			count=Math.floor(count/24);
			var days=count;
			if (days > 0) {
				output += days + ((days <= 1)?" day, ":" days, ");
			}
			output += hours + ":" + mins + ":" + secs;
		} else {
			//Event is defined as a "stall", stay at 0.
			output += '00:00:00';
		}
		document.getElementById(COUNTDOWN_COUNTDOWN_CLOCK_ELEM).innerHTML = output;
		// End timer
		
		// Set everything that doesn't change, if config.DoUpdateEventInfo is true
		if (config.DoUpdateEventInfo) {
			if (COUNTDOWN_EVENT_NAME_ELEM.length > 0)
				document.getElementById(COUNTDOWN_EVENT_NAME_ELEM).innerHTML = active_event.name;
			//if (COUNTDOWN_COUNTDOWN_CLOCK_ELEM.length > 0)
			//	document.getElementById(COUNTDOWN_COUNTDOWN_CLOCK_ELEM).innerHTML = '--'; // Placeholder
			if (COUNTDOWN_DATE_ELEM.length > 0) {
				if (!active_event.stall) {
					document.getElementById(COUNTDOWN_DATE_ELEM).innerHTML = active_event.date.toString();
				} else {
					document.getElementById(COUNTDOWN_DATE_ELEM).innerHTML = '&nbsp;';
				}
			}
			if (COUNTDOWN_PICTURE_ELEM.length > 0)
				document.getElementById(COUNTDOWN_PICTURE_ELEM).src = config.PathToImages + active_event.logo_img;
			config.DoUpdateEventInfo = false;
		}
		
		// Now set the next timeout
		if (document.all)
			setTimeout("CountdownTimeout("+cfg_num+");",config.UpdateTime);
		else if (!document.layers)
			setTimeout("CountdownTimeout("+cfg_num+");",config.UpdateTime);
		else
			setTimeout("CountdownTimeout("+cfg_num+");",config.UpdateTime);
	}
}
