
/* 
BBC Scotland Beag air Bheag Site 2008
Glow/JS Code By Cris Walsh
Further Info: http://www.bbc.co.uk/glow/
*/
// console.log('console ready');
var DownloadsServer = 'http://downloads.bbc.co.uk';
var PlaylistServer = 'http://www.bbc.co.uk';

// create glow object
var glow;
// load an instance of glow
gloader.load(["glow", "1", "glow.dom","glow.events","glow.net","glow.widgets.Panel","glow.anim","glow.tweens"], {
	// if load successful, set var glowLocal to the current loaded instance
	onLoad: function(glowLocal) {
		glowLocal.ready(function() {
			// now set the current loaded instance to the var "glow"
			glow = glowLocal;
			// glow is now ready to use
			// // console.log('glow ready');
			 Site.EnhanceSite();
			 Site.pageOptions();
		});
	}
});



/* ------------------------------------------------------------------------ */
/* -------------------->>>   G L O B A L  O B J E C T   <<<---------------- */
/* ------------------------------------------------------------------------ */
var Site = {
/* ------------------------------------------------------------------------ */
/* --------------------->>>   E N H A N C E  S I T E   <<<----------------- */
/* ------------------------------------------------------------------------ */
	EnhanceSite: function(){
		// console.log("EnhanceSite");
		Site.clearObject();
	},

/* ------------------------------------------------------------------------ */
/* ---------------------->>> P A G E    O P T I O N S  <<<----------------- */
/* ------------------------------------------------------------------------ */
	pageOptions: function(){
		// set the var "classname" to the pages body class
		var pageID = glow.dom.get('body').attr('id');
		// run a conditional statement on the var "classname" 
		switch(pageID)
		{
		
		/* 
			if the page <body> element has an id of home 
		*/
			case 'home':
				// // console.log("body id=\"home\"");
				// load any homepage functions required
				break;
		/* 
			if the page <body> element has an id of language-unit 
		*/
			case 'language-unit':
				// // console.log("body id=\"language-unit\"");
				// create audio nodelist
				var audioNodeList = glow.dom.get("a.audio");
					Site.findFirstAudioPlaylist(audioNodeList);
					Site.createPlayListLinks(audioNodeList);

				glow.events.addListener(glow.dom.get('.inlinetranslation'), 'mouseover', function(e){
					// // console.log(this);
					glow.dom.get(this).get('.popup').css('display', 'block');
				});
				glow.events.addListener(glow.dom.get('.inlinetranslation'), 'mouseout', function(e){
					// // console.log(this);
					glow.dom.get(this).get('.popup').css('display', 'none');
				});

				var transcriptNodeList = glow.dom.get("a.transcript");
				transcriptNodeList.css('display', 'none');
				break;
			/* 
			if the page <body> element has an id of test-yourself 
			*/	
			case 'test-yourself':
				 // // console.log("body id=\"test-yourself\"");
				break;
			/* 
			if the page <body> element has an id of gallery 
			*/
			case 'gallery':
				 // // console.log("body id=\"gallery\"");
				break;
			/* 
			if the page <body> element has an id of soap 
			*/	
			case 'soap':
				 // console.log("body id=\"soap\"");
				// create audio nodelist
				var audioNodeList = glow.dom.get("a.audio");
				Site.findFirstAudioPlaylist(audioNodeList);
				break;
			/* 
			if the page <body> element has an id of gramar 
			*/
			case 'grammar':
				 // // console.log("body id=\"grammar\"");
				break;
			/* 
			if the page <body> element has an id of sounds 
			*/
			case 'sounds':
				  // console.log("body id=\"sounds\"");
				// create audio nodelist
				 var audioNodeList = glow.dom.get("a.audio");
					Site.findFirstAudioPlaylist(audioNodeList);
					Site.createPlayListLinks(audioNodeList);
				break;
			
			/* 
			if the page <body> element has an id of quickfixes 
			*/
			case 'quickfixes':
			//	 console.log("body id=\"quickfixes\"");
				// create nodelist 
				var nodeList = glow.dom.get("a.audio");	
					Site.findFirstAudioPlaylist(nodeList);
					Site.createPlayListLinks(nodeList);
				break;
			/* 
			if the page <body> element has an id of phrasebook 
			*/
			case 'phrasebook':
				 // // console.log("body id=\"phrasebook\"");
				// create nodelist 
				var nodeList = glow.dom.get("a.audio");
					Site.findFirstAudioPlaylist(nodeList);
					Site.createPlayListLinks(nodeList);
				break;
			/* 
			if the page <body> element has an id of resources 
			*/
			case 'resources':
				 // // console.log("body id=\"resources\"");
				break;
			/* 
			if the page <body> element has an id of song 
			*/
			case 'songs':
				 //console.log("body id=\"songs\"");
				// create audio nodelist
				 var audioNodeList = glow.dom.get("a.audio");
					 Site.findFirstAudioPlaylist(audioNodeList);
					 Site.createPlayListLinks(audioNodeList);
				  // create video nodelist
					 var videoNode = glow.dom.get("a.video");
					 Site.findFirstVideoPlaylist(videoNode);
				break;
			default:
				break;
		}
	},

/* ------------------------------------------------------------------------ */
/* Function:Site.findFirstAudioPlaylist() */
/* ------------------------------------------------------------------------ */
		findFirstAudioPlaylist: function(myNodeList){
			 // console.log('myNodeList = ' + glow.dom.get(myNodeList[0]).attr("href"));
			firstPlaylist = Site.setAudioPlaylist(glow.dom.get(myNodeList[0]).attr("href"));
			//  console.log('firstPlaylist = ' + firstPlaylist);
			Site.loadAudioEMP(firstPlaylist);
		},

/* ------------------------------------------------------------------------ */
/* Function:Site.setAudioPlaylist() */
/* ------------------------------------------------------------------------ */
	 setAudioPlaylist: function(myURL)
	{
	//	console.log("setAudioPlaylist called");
	//	console.log("myURL " + myURL);
		var tempURL = myURL.replace(DownloadsServer,PlaylistServer);
		var tempURL = tempURL.replace("/alba/foghlam/beag_air_bheag/","/alba/foghlam/beag_air_bheag/media/");
		var tempURL = tempURL.replace("mp3","xml");
		return tempURL;
	},


/* ------------------------------------------------------------------------ */
/* Function:Site.createPlayListLinks() */
/* ------------------------------------------------------------------------ */
	createPlayListLinks: function(myNodeList)
	{
		// console.log("createPlayListLinks called");
		myNodeList.each(function(i){
			this == myNodeList[i];
			glow.events.addListener(glow.dom.get(this), 'click', function(e){
				e.preventDefault();
				// console.log(this);
				thePlaylist = Site.setAudioPlaylist(glow.dom.get(this).attr("href"));
				Site.loadAudioEMP(thePlaylist);
			});
		});
	},

/* ------------------------------------------------------------------------ */
/* Function:Site.loadAudioEMP() */
/* ------------------------------------------------------------------------ */
	loadAudioEMP:function (myPlaylist)
	{
	//	 console.log("xml =  " + myPlaylist);
		var audioemp = new embeddedMedia.Player();
		audioemp.setDomId("audioemp1"); 
		audioemp.setWidth("446");
		audioemp.setHeight("106");
		audioemp.setPlaylist(myPlaylist);
		audioemp.set("config_settings_displayMode","audio");
		audioemp.set("config_settings_skin", "silver");  
		audioemp.set("config_settings_autoPlay", "true"); 
		audioemp.write();
	},

/* ------------------------------------------------------------------------ */
/* Function:Site.findFirstVideoPlaylist() */
/* ------------------------------------------------------------------------ */
		findFirstVideoPlaylist: function(myNodeList){
			firstPlaylist = Site.setVideoPlaylist(glow.dom.get(myNodeList[0]).attr("href"));
		//	 // console.log('first video playlist = ' + firstPlaylist);
			 Site.loadVideoEMP(firstPlaylist);
		},

/* ------------------------------------------------------------------------ */
/* Function:Site.setVideoPlaylist() */
/* ------------------------------------------------------------------------ */
	 setVideoPlaylist: function(myURL)
	{
	//	// console.log("setVideoPlaylist called")
	//	// console.log("myURL = "+ myURL)
		var tempURL = myURL.replace(DownloadsServer,PlaylistServer);
		var tempURL = tempURL.replace("/alba/foghlam/beag_air_bheag/","/alba/foghlam/beag_air_bheag/media/");
		var tempURL = tempURL.replace("flv","xml");
		return tempURL;
	},

/* ------------------------------------------------------------------------ */
/* Function:Site.loadVideoEMP() */
/* ------------------------------------------------------------------------ */
	loadVideoEMP:function (myPlaylist)
	{
		// // console.log(myPlaylist)
		//// console.log("load video EMP with " + myPlaylist)
		var videoemp = new embeddedMedia.Player();
		videoemp.setDomId("emp1"); 
		videoemp.setWidth("448");
		videoemp.setHeight("287");
		videoemp.setPlaylist(myPlaylist);
		videoemp.set("config_settings_skin", "silver"); 
		videoemp.write();
	},

/* ------------------------------------------------------------------------ */
/* Function:Site.clearObject(); */
/* ------------------------------------------------------------------------ */	
	clearObject: function(currentObject){
		// var $ = glow.dom.get, object = $(currentObject).children().remove();
		// // // console.log("clearObject");
	}
}
