function goLive(video) {
	var emp = new embeddedMedia.Player();
	emp.setWidth("640");
	emp.setHeight("395");
	emp.setDomId("emp1"); 
	emp.setPlaylist(video);
	emp.write();
}

var countdownvid = false;
var almost = false;
var lastep = false;

/*
Author: Robert Hashemian
http://www.hashemian.com/

You can use this code in any manner so long as the author's
name, Web address and this disclaimer is kept intact.
********************************************************
*/

function calcage(secs, num1, num2) {
  	if (num1 == 3600) {
		var s = (Math.floor(secs/num1)).toString();
  	} else {
  		var s = ((Math.floor(secs/num1))%num2).toString();
	}
  	if (LeadingZero && s.length < 2){
		s = "0" + s;
  	}
  return s;
}

function CountBack(secs,SetTimeOutPeriod, cntdwnMin, almostMin) {
	var almostSec = almostMin * 60;
	var cntdwnSec = cntdwnMin * 60;
	var DisplayStr = DisplayFormat.replace(/%%H%%/g, calcage(secs,3600,24));
	DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
	DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));
	var divFound = glow.dom.get('div#emp1').length;
	
	// COUNTDOWN
	if (secs < cntdwnSec && countdownvid == false) {
		if (divFound > 0) {
			if (LiveOrPrerecord == "prerecord") {
				goLive("http://www.bbc.co.uk/switch/the519show/xml/countdown.xml");
			} else {
				goLive("http://www.bbc.co.uk/switch/the519show/xml/live.xml");
			}
		}
		countdownvid = true;
	  }

	// LIVE
	if (secs < 0) {
		if (LiveOrPrerecord == "prerecord") {
			if (divFound > 0) {
				goLive("http://www.bbc.co.uk/switch/the519show/xml/prerecord.xml");
			}
		}
		document.getElementById("cntdwn").innerHTML = LiveMessage;
		return;
	}

	// NEARLY LIVE
	if (secs < almostSec && almost == false) {
		document.getElementById("cntdwn").innerHTML = AlmostMessage;
		almost = true;	

	// ANY OTHER TIME
	} else {
		if (almost == false) {
			if (lastep == false && countdownvid == false) {
				if (divFound > 0) {
					goLive("http://www.bbc.co.uk/switch/videos/xml/" + LastEpisode + ".xml");
				}
				lastep = true;	
			}
			document.getElementById("cntdwn").innerHTML = DisplayStr;
		}
	}

	// call this function again when live - without reloading page
	if (CountActive) {
		secs = secs+CountStepper;
		setTimeout("CountBack(" + secs + "," + SetTimeOutPeriod + "," + cntdwnMin + "," + almostMin + ")", SetTimeOutPeriod);
	}
}

function putspan() {
	glow.dom.get('div#cntdwn-con').append('<span id="cntdwn"></span>');
}

function countDown(TargetDate,LiveOrPrerecord,LastEpisode,CountActive,CountStepper,LeadingZero,DisplayFormat,LiveMessage,AlmostMessage,dnow,cntdwnMin,almostMin){	
	CountStepper = Math.ceil(CountStepper);
	if (CountStepper == 0)
		CountActive = false;
	var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
	putspan();
	var dthen = new Date(TargetDate);
	if(CountStepper>0)
		var ddiff = new Date(dnow-dthen);
	else
		var ddiff = new Date(dthen-dnow);
	var gsecs = Math.floor(ddiff.valueOf()/1000);
	CountBack(gsecs,SetTimeOutPeriod, cntdwnMin, almostMin, LiveOrPrerecord);
}