var EPIC_WEBCAST =
{
	objID : "Epic webcast object",
	_datStartDate : false,
	_objCountdownTimer : false,
	_objVideoTimer : false,
	_intTimerRate : 10,
	_objCountdownLayer : false,
	_objVideoHolder : false,
	_objVideoWrapper : false,
	_objVideoLayer : false,
	_objLateMessageHolder : false,
	_booShowHighQuality : false,

	// START OF EDITBALE VARIABLES
	_strStartDate : "13 July 2010 14:00:00 GMT+0100",	// NOTE the GMT+0100 is british summer time, this will need tweaking if the code is used in winter!
	_strVideoPathPrefix : "http://webcast.epic-staging.co.uk/",
	_strVideoHighQuality : "epic_webcast_640x360.flv",
	_strVideoLowQuality : "epic_webcast_512x288.flv",
	_intHighVideoWidth : 640,
	_intHighVideoHeight : 360,
	_intLowVideoWidth : 512,
	_intLowVideoHeight : 288,
	_intVideoShowDuration : 3600000,	// In milliseconds, currently equates to 1 hours, this is the time window the video is avialable for
	_intVideoDuration : 220000, // The clips duration, currently equates to 3 minutes and 40 seconds.
	// END OF EDITBALE VARIABLES

	_intSecondInMilliSeconds : 1000,
	_intMinuteInMilliSeconds : 60000,
	_intHourInMilliSeconds : 3600000,
	_intDayInMilliSeconds : 86400000,
	_intWeekInMilliSeconds : 604800000,

	init : function ()
	{
		var _datCurrDate = new Date();
		this._datStartDate = new Date(this._strStartDate);
		this._objCountdownLayer = document.getElementById("countdownTimerL");
		this._objTimerLayer = document.getElementById("timeHolderL");
		this._objVideoHolder = document.getElementById("webcastL");
		this._objVideoWrapper = document.getElementById("videoWrapperL");
		this._objVideoLayer = document.getElementById("videoHolderL");
		this._objLateMessageHolder = document.getElementById("toLateL");

		var _intTimeDifference = this._datStartDate.getTime() - _datCurrDate.getTime();
		if (_intTimeDifference > 0)
		{
			this._showTimer();
		}
		else
		{
			if ((_intTimeDifference + this._intVideoShowDuration) > 0)
			{
				this._showVideo();
			}
			else
			{
				this._showToLateMessage();
			}
		}
	},

	cancelTimers : function ()
	{
		if (this._objCountdownTimer)
		{
			window.clearInterval(this._objCountdownTimer);
		}
		if (this._objVideoTimer)
		{
			window.clearInterval(this._objVideoTimer);
		}
	},

	updateCountdown : function ()
	{
		var _strTimeRemaining = this._getCurrentTimeLeft();
		this._objTimerLayer.innerHTML = _strTimeRemaining;
	},

	checkVideoShift : function ()
	{
		var _datCurrDate = new Date();
		var _intRemainingTimeInMilliSeconds = (this._datStartDate.getTime() + this._intVideoShowDuration) - _datCurrDate.getTime();
		if (_intRemainingTimeInMilliSeconds < 0)
		{
			this._showToLateMessage();
		}
	},

	toggleQuality : function (_booShowHighQuality)
	{
		this._booShowHighQuality = _booShowHighQuality;
		if (this._booShowHighQuality)
		{
			document.getElementById("lowQualityLinkL").style.display = "block";
			document.getElementById("highQualityLinkL").style.display = "none";
		}
		else
		{
			document.getElementById("highQualityLinkL").style.display = "block";
			document.getElementById("lowQualityLinkL").style.display = "none";
		}
		this._showVideo();
	},

	_showTimer : function ()
	{
		this._objCountdownTimer = window.setInterval("EPIC_WEBCAST.updateCountdown()", this._intTimerRate);
		this._objCountdownLayer.style.display = "block";
	},

	_showVideo : function ()
	{
		if (this._checkFlashVersion())
		{
			this._startVideoTimer();

			this._objCountdownLayer.style.display = "none";
			this._objVideoLayer.style.display = "block";
			this._objVideoHolder.style.display = "block";

			var objSWF = swfobject;
			if (this._booShowHighQuality)
			{
				var _objFlashvars = {"file":(this._strVideoPathPrefix + this._strVideoHighQuality), "autostart":"true"};
				//alert(_objFlashvars.file);
				var _objParams = {allowfullscreen:"true", allowscriptaccess:"always"};
				var _objAttributes = {"id":"videoHolderL", "name":"videoHolderL", "class":"videoPlayerC"};
				objSWF.embedSWF("webcast_dev/flash/player.swf", this._objVideoLayer.id, this._intHighVideoWidth, this._intHighVideoHeight, "9.0.115", true, _objFlashvars, _objParams, _objAttributes);
			}
			else
			{
				var _objFlashvars = {"file":(this._strVideoPathPrefix + this._strVideoLowQuality), "autostart":"true"};
				//alert(_objFlashvars.file);
				var _objParams = {allowfullscreen:"true", allowscriptaccess:"always"};
				var _objAttributes = {"id":"videoHolderL", "name":"videoHolderL", "class":"videoPlayerC"};
				objSWF.embedSWF("webcast_dev/flash/player.swf", this._objVideoLayer.id, this._intLowVideoWidth, this._intLowVideoHeight, "9.0.115", true, _objFlashvars, _objParams, _objAttributes);
			}
		}
	},

	_showToLateMessage : function ()
	{
		this.cancelTimers();
		this._objVideoWrapper.innerHTML = "";
		this._objCountdownLayer.style.display = "none";
		this._objVideoLayer.style.display = "none";
		this._objVideoHolder.style.display = "none";
		this._objLateMessageHolder.style.display = "block";
	},

	_checkFlashVersion : function ()
	{
		var _strFlashVersion = this._getFlashVersion();
		//alert("_strFlashVersion: " + _strFlashVersion);
		var _intMajorVersion = parseInt(_strFlashVersion.split(",")[0]);
		var _intMinorVersion = parseInt(_strFlashVersion.split(",")[2]);
		//alert("_intMajorVersion: " + _intMajorVersion + "\n_intMinorVersion: " + _intMinorVersion);
		if (_strFlashVersion == "0,0,0")
		{
			//alert(1)
			this._objVideoLayer.style.display = "block";
			this._objVideoHolder.style.display = "block";
			document.getElementById("videoQualityToggleL").style.display = "none";
			document.getElementById("noFlashL").style.display = "block";
			return false;
		}
		if (_intMajorVersion < 9)
		{
			//alert(2)
			this._objVideoLayer.style.display = "block";
			this._objVideoHolder.style.display = "block";
			document.getElementById("videoQualityToggleL").style.display = "none";
			document.getElementById("lowFlashL").style.display = "block";
			return false;
		}
		else if ((_intMajorVersion == 9) && (_intMinorVersion < 115))
		{
			//alert(2)
			this._objVideoLayer.style.display = "block";
			this._objVideoHolder.style.display = "block";
			document.getElementById("videoQualityToggleL").style.display = "none";
			document.getElementById("lowFlashL").style.display = "block";
			return false;
		}
		return true;
	},

	_getCurrentTimeLeft : function ()
	{
		var _datCurrDate = new Date();
		var _strTimeLeft = "";
		var _intRemainingTimeInMilliSeconds = this._datStartDate.getTime() - _datCurrDate.getTime();

		// If we've finished the timer, show the video and start the new duration timer
		if (_intRemainingTimeInMilliSeconds <= 0)
		{
			this.cancelTimers();
			this._showVideo();
			return;
		}

		var _intWeeksLeft = Math.floor(_intRemainingTimeInMilliSeconds / this._intWeekInMilliSeconds);
		_intRemainingTimeInMilliSeconds -= _intWeeksLeft * this._intWeekInMilliSeconds;

		var _intDaysLeft = Math.floor(_intRemainingTimeInMilliSeconds / this._intDayInMilliSeconds);
		_intRemainingTimeInMilliSeconds -= _intDaysLeft * this._intDayInMilliSeconds;

		var _intHoursLeft = Math.floor(_intRemainingTimeInMilliSeconds / this._intHourInMilliSeconds);
		_intRemainingTimeInMilliSeconds -= _intHoursLeft * this._intHourInMilliSeconds;

		var _intMinutesLeft = Math.floor(_intRemainingTimeInMilliSeconds / this._intMinuteInMilliSeconds);
		_intRemainingTimeInMilliSeconds -= _intMinutesLeft * this._intMinuteInMilliSeconds;

		var _intSecondsLeft = Math.floor(_intRemainingTimeInMilliSeconds / this._intSecondInMilliSeconds);
		_intRemainingTimeInMilliSeconds -= _intSecondsLeft * this._intSecondInMilliSeconds;

		var _intMilliSecondsLeft = _intRemainingTimeInMilliSeconds;

		if (_intWeeksLeft > 0)
		{
			if (_intWeeksLeft > 1)
			{
				_strTimeLeft += _intWeeksLeft + " weeks, ";
			}
			else
			{
				_strTimeLeft += _intWeeksLeft + " week, ";
			}
		}
		if (_intDaysLeft > 0)
		{
			if (_intDaysLeft > 1)
			{
				_strTimeLeft += _intDaysLeft + " days, ";
			}
			else
			{
				_strTimeLeft += _intDaysLeft + " day, ";
			}
		}
		if (_intHoursLeft > 0)
		{
			_strTimeLeft += _intHoursLeft + ":";
		}
		_strTimeLeft += this._padDigits(_intMinutesLeft, 2) + ":";
		_strTimeLeft += this._padDigits(_intSecondsLeft, 2) + ":";
		_strTimeLeft += this._padDigits(_intMilliSecondsLeft, 3).slice(0,2) + " ";
		return _strTimeLeft;
	},

	_padDigits : function (_intWhatNumber, _intNumberOfDigits)
	{
		var _strNumber = _intWhatNumber.toString();
		if (_strNumber.length < _intNumberOfDigits)
		{
			while (_strNumber.length < _intNumberOfDigits)
			{
				_strNumber = "0" + _strNumber;
			}
		}
		return _strNumber;
	},

	_getFlashVersion : function()
	{
	  // ie
	  try {
	    try {
	      // avoid fp6 minor version lookup issues
	      // see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
	      var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
	      try { axo.AllowScriptAccess = 'always'; }
	      catch(e) { return '6,0,0'; }
	    } catch(e) {}
	    return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
	  // other browsers
	  } catch(e) {
	    try {
	      if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
	        return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];
	      }
	    } catch(e) {}
	  }
	  return '0,0,0';
	},

	_startVideoTimer : function ()
	{
		var _datCurrDate = new Date();
		var _intVideoFinishTime = _datCurrDate.getTime() + this._intVideoDuration;
		var _intViewTimeFinish = (this._datStartDate.getTime() + this._intVideoShowDuration) - _datCurrDate.getTime();
		if (_intVideoFinishTime > _intViewTimeFinish)
		{
			this._intVideoShowDuration += this._intVideoDuration;
		}
		this._objCountdownTimer = window.setInterval("EPIC_WEBCAST.checkVideoShift()", this._intTimerRate);
	}
}

