var glow;
gloader.load(["glow", "1", "glow.anim", "glow.data", "glow.dom", "glow.net", "glow.events", "glow.dragdrop", "glow.widgets.InfoPanel"], {
	onLoad: function(glowLocal){
		glowLocal.ready(function(){
			glow = glowLocal;

			initPage();
		});
	}
});

function initPage()
{
	var bodyId = glow.dom.get('body').attr('id');

	switch(bodyId)
	{
		case 'homepage':
			initHomepage();
			break;
		case 'production':
			break;
		case 'video':
			initVideo();
			break;
		case 'snapshots':
			initSnapshots();
			break;
		case 'changing':
			initChanging();
			break;
		case 'mychina':
            initMychina();
			break;
		default:
			break;
	}
}

function initHomepage()
{

	var d = new Date();
	var seconds = d.getSeconds();

	if		(seconds < 12)	{filename = "tian_tian_16x9_512k";}
	else if	(seconds < 24)	{filename = "dr_hao_16x9_512k";}
	else if	(seconds < 36)	{filename = "jiang_feng_16x9_512k";}
	else if	(seconds < 48)	{filename = "cao_fei_16x9_512k";}
	else if	(seconds < 60)	{filename = "liu_xiao_16x9_512k";}

	var emp = new bbc.Emp();
	emp.setWidth("448");
	emp.setHeight("287");
	emp.setDomId("emp-promo");
	emp.setPlaylist("http://"+window.location.hostname+basePath+videoDir+"emp/"+filename+".xml");
	if(lang == 'gd')
	{
		emp.set("config_settings_language","gd");
	}
	emp.write();
	glow.dom.get("#home_main_one a#"+filename).addClass("active");	

	glow.dom.get("#home_main_one ul a").each(function() {
		
		glow.events.addListener(glow.dom.get(this), 'click', function(e){
			e.preventDefault();
			var myId = glow.dom.get(this).attr("id");
			
			resetActive();
			glow.dom.get(this).addClass("active");

			reload("http://"+window.location.hostname+basePath+videoDir+"emp/"+myId+".xml");
		});
	});

	function resetActive()
	{
		// Don't want any thumbnail to look active.
		glow.dom.get("#home_main_one ul a").each(function() {
			glow.dom.get(this).removeClass("active");
		});
	}
}

function initVideo()
{
	var selectVideo = glow.dom.get('#blq-content').attr('class');

	var emp = new bbc.Emp();
	emp.setWidth("512"); 
	emp.setHeight("323");
	emp.setDomId("emp1"); 
	emp.setPlaylist("http://www.bbc.co.uk"+basePath+videoDir+"emp/"+selectVideo+"_16x9_512k.xml");
	if(lang == 'gd')
	{
		emp.set("config_settings_language","gd");
	}
	emp.write();
}

function initSnapshots()
{
	//glow.dom.get('div.egt-caption').append('<div class="egt-back"><p><a href="/scotland/learning/chinastories/snapshots/">Back to the galleries index</a></p></div>');
}

function initChanging()
{
	resizePage();
	makeMap();
	glow.events.addListener('#map_lhn a','click',function(e){
		e.preventDefault();
		glow.dom.get('#overlay').remove();
		var myID = glow.dom.get(this).attr("id");

		EventHandlerOnClick(null, myID);
	});
	loadXML();
	
}

function reload(playlist_url) {
	var emp = new embeddedMedia.Player();
	emp.setWidth("448");
	emp.setHeight("287");
	emp.setDomId("emp-promo");
	emp.setPlaylist(playlist_url);
	emp.write();
}