/**
 * @author Martyn
 */
var SHOW;
if (!SHOW) SHOW = {};

/**
* @classDescription Emits a countdown timer to a webpage.
* @author Robert Crowley, www.crowcoder.com 2008
* @version 1.0
* @constructor
* @param {integer} weekDay		The day of the week the show is on (0 = sunday, 1 = monday, ...)
* @param {integer} showHour	The start hour of the show in UTC 
* @param {integer}  showMin		The start nminute of the show in UTC
* @param {integer}  duration		The duration of the show in minutes
* @param {string}  element		The name of the element which contains the countdown HTML
* @param {string}  nextShow		The prefix for the next show (Next show in ...)
* @param {string}  onAir			The string to display when on air
*/

SHOW.Countdown = function(weekDay, showHour, showMin, duration, element, nextShow, onAir)
{
	var SECS_IN_MIN = 60;
	var SECS_IN_HOUR = 60 * SECS_IN_MIN;
	var SECS_IN_DAY = 24 * SECS_IN_HOUR; 

	//Set the target datetime
	var m_displayElem = document.getElementById(element);
	var m_weekDay = weekDay;
	var m_showHour = showHour;
	var m_showMin = showMin;
	var m_duration = duration;
	var m_nextShow = nextShow;
	var m_onAir = onAir;
	var getCountdown;
	var showCountdown;
	var _interval;
    
	showCountdown = function()
	{ 
		m_displayElem.innerHTML = getCountdown();
	}
    
	this.start = function ()
	{
		_interval = setInterval(showCountdown, 1000);
	} 
    
	function getDHMS(secs)
	{
		var num_days = Math.floor(secs / SECS_IN_DAY);
		var days_secs = num_days * SECS_IN_DAY;
		var diff_hrs = Math.abs(days_secs - secs);
		var num_hrs = Math.floor(diff_hrs / SECS_IN_HOUR);
		var hrs_secs = num_hrs * SECS_IN_HOUR;
		var diff_mins = Math.abs(((hrs_secs + days_secs)) - secs);
		var num_mins = Math.floor(diff_mins / SECS_IN_MIN);
		var mins_secs = num_mins * SECS_IN_MIN;
		var diff_secs = Math.abs((days_secs + hrs_secs + mins_secs) - secs);
		var num_secs = Math.floor(diff_secs);
		
		var sDHMS = '';
		
		if ((num_days > 0) || (sDHMS.length > 0))
		{
			sDHMS = sDHMS + num_days.toString() + ' days ';
		}
		if ((num_hrs > 0) || (sDHMS.length > 0))
		{
			sDHMS = sDHMS + num_hrs.toString() + ' hours ';
		}
		if ((num_mins > 0) || (sDHMS.length > 0))
		{
			sDHMS = sDHMS + num_mins.toString() + ' mins ';
		}
		if ((num_secs > 0) || (sDHMS.length > 0))
		{
			sDHMS = sDHMS + num_secs.toString() + ' secs ';
		}
		
		return '<span class="nextshow">' + m_nextShow + ' ' + sDHMS + '</span>';
	}

	getCountdown = function()
	{
		var today = new Date();
		today.setMinutes(today.getMinutes() + today.getTimezoneOffset());
		var thisDay = today.getDay();
		var todaySecs = today.getSeconds() + (today.getMinutes() * SECS_IN_MIN) + (today.getHours() * SECS_IN_HOUR);
	  var showStart = (m_showMin * SECS_IN_MIN) + (m_showHour * SECS_IN_HOUR);
		var showEnd = showStart + (m_duration * SECS_IN_MIN);
		
		if (thisDay < m_weekDay)
		{
			var weekSecs = thisDay * SECS_IN_DAY + todaySecs;
			var showSecs = m_weekDay * SECS_IN_DAY + showStart;
			return getDHMS(showSecs - weekSecs);
		}
		
		if (thisDay == m_weekDay)
		{
			if (todaySecs < showStart)
			{
				return getDHMS(showStart - todaySecs);
			}
			
			if (todaySecs <= showEnd)
			{
				return '<span class="onair">' + m_onAir + '</span>';
			}
		}
		
		weekSecs = thisDay * SECS_IN_DAY + todaySecs;
		showSecs = (m_weekDay + 7) * SECS_IN_DAY + showStart;
		return getDHMS(showSecs - weekSecs);
	}
}
