/**
 * <p>Title: countdownTimer.js</p>
 * <p>Description: Javascript to display a countdown timer.</p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: Kreber Graphics, Inc.</p>
 * @author Dave Schultz / Charlie Reading
 */

	// Includes
	var countdownTimer_js = 1;
	var includes = ((typeof browser_js == 'undefined') ? " browser.js\n" : "");
	if (includes != "")
	{
		alert("countdownTimer.js: you must first include:\n" + includes);
	}
	
	// Defaults
	kDefault_Interval = 1000; // default to 1 second interval
	kDefault_Time = 1; // default time in minutes
	kDefault_Text = "Timer ";
	kDefault_Message = "";
	kDefault_Style = "color:red; font-family:Arial,Helvetica,sans-serif; font-size:10pt;";
	
	// Globals
	var gHasCountdownTimerDiv = false; // has <div> tag to display timer?
	var gStartTime; // Time timer started
	var gTime = kDefault_Time;
	var gText = "";
	var gCallback = null;
	var gMessage = "";
	var gTimer = null;
	
	/**
	 * Create a div-tag timer.
	 *
	 * @param time - time in seconds
	 * @param interval - interval to update timer in seconds
	 * @param text - text to display to left of timer
	 * @param callback - function to call when timer is updated
	 * @param message - message to display on 2nd line
	 * @param style - style to use when displaying text in tag
	 */
	function createTimer(time, interval, text, callback, message, style)
	{
		if ((typeof style == "undefined")
				|| (style == null))
		{
			style = kDefault_Style;
		}
		initTimer(time, interval, text, callback, message);
		document.write('<div id="countdownTimer" style="' + style + '"></div>');
		gHasCountdownTimerDiv = true;
		updateTimer();
	}
	
	/**
	 * Create a raw (non-visual) timer.
	 *
	 * @param time - time in seconds
	 * @param interval - interval to update timer in seconds
	 * @param text - text to display to left of timer
	 * @param callback - function to call when timer is updated
	 * @param message - message to display on 2nd line
	 * @return
	 */
	function createRawTimer(time, interval, text, callback, message)
	{
		initTimer(time, interval, text, callback, message);
		updateTimer();
	}
	
	/**
	 * @param time - time in seconds
	 * @param interval - interval to update timer in seconds
	 * @param text - text to display to left of timer
	 * @param style - stylesheet to use when displaying
	 * @return
	 */
	function disposeTimer()
	{
		if (gTimer != null)
		{
			clearInterval(gTimer);
			gTimer = null;
		}
	}
	
	/**
	 * @param time - time in seconds
	 * @param interval - interval to update timer in seconds
	 * @param text - text to display to left of timer
	 * @param style - stylesheet to use when displaying
	 * @return
	 */
	function initTimer(time, interval, text, callback, message)
	{
		switch (initTimer.arguments.length)
		{
			case 0:
				time = kDefault_Time;
			case 1:
				interval = kDefault_Interval;
			case 2:
				text = kDefault_Text;
			case 3:
				callback = null;
			case 4:
				message = kDefault_Message;
		}
	
		gDate = new Date();
		gStartTime = gDate.getTime();
		gHourOffset = (new Date(0)).getHours();
		gTimer = setInterval("updateTimer()", interval*1000);
		gTime = time * 1000; // Convert to ms;
		gText = text;
		gCallback = callback;
		gMessage = message;
		currentDate = new Date();
	}
	
	function updateTimer()
	{
		var currentDate = new Date();
		var timeLeft = new Date(gTime - (currentDate.getTime() - gStartTime)); // Create new date object so we can get hours/min/sec
		var sTimeLeft;
		var sHTML;
		// if timer is <= 0 then we hit the end so call callback if it exists
		if ((gTime - (currentDate.getTime() - gStartTime)) <= 0)
		{
			disposeTimer();
			if (gCallback != null)
			{
				gCallback();
			}
		}
		else if (gHasCountdownTimerDiv)
		{
			sTimeLeft = (timeLeft.getMinutes()<10?"0":"") + timeLeft.getMinutes() + ":" +
					(timeLeft.getSeconds()<10?"0":"") + timeLeft.getSeconds();
			sHTML = gText + sTimeLeft + "<br>" + gMessage;
			idToTag("countdownTimer").innerHTML = sHTML;
		}
	}
	
	function resetTimer()
	{
		gDate = new Date();
		gStartTime = gDate.getTime();
	}
