// EMP config ObjLit
var docURL = location.href;
var docURLTemp = new Array();
docURLTemp = docURL.split('/');
//var site_domain = "http://" + docURLTemp[2] + "/" + docURLTemp[3] + "/";
var site_domain = "http://" + docURLTemp[2] + "/" + docURLTemp[3] + "/";


// set default values (same as site_emp - no need to duplicate?)
var siteEmp = {
	// default values for the site
	width: 512,
	height: 323,
	autoplay: false,
	domId: "empbox",
	vidconfspath: site_domain + "include/video_configs/",
	playlist_url: "example.xml",
	empconfurl: site_domain + "include/video_configs/emp_config.xml",
	// set the window scroll position to make emp viewable? 
	scrollToEmp: false,
	// overridable play function
	play : function(passedArgsObj) {
		// loop through arguments and replace default values with any from passedArgsObj
		for (var n in arguments[0]) { siteEmp[n]=arguments[0][n]; }
		var emp = new bbc.Emp();
		emp.setWidth(siteEmp.width);
		emp.setHeight(siteEmp.height);
		emp.setDomId(siteEmp.domId);
		emp.setPlaylist(siteEmp.vidconfspath + siteEmp.playlist_url);
		emp.setConfig(siteEmp.empconfurl);
		emp.set("config_settings_autoPlay", siteEmp.autoplay);
		emp.write();
		if(siteEmp.scrollToEmp == true) {
			var domIdYPos = glow.dom.get("#" + siteEmp.domId).offset().y;
			domIdYPos -= 10;
			window.scroll(0,domIdYPos);
		};
	}
};

glow.ready(function() {

	// nodeLists for vids
	var vidList = glow.dom.get("a.vl");

	// finds each vl attaches event to switch vid it's description.
	vidList.each(function (i) { 
		var vidXML = vidList.slice(i,i+1).attr("rel");
		var vidDesc = vidList.slice(i,i+1).attr("title");
		glow.events.addListener(vidList.slice(i,i+1),'click', function(e) {
			if (vidXML != null) {
				siteEmp.play({ playlist_url: vidXML, autoplay: true, scrollToEmp: true });
				glow.dom.get('.empdesc').after('<div class="empdesc"><p class="caption"><strong>Now playing:</strong> ' + vidDesc + '</p></div>').remove();
			} else {
				alert("Sorry this video is not available at the moment");
			}
			e.preventDefault(); 
		});
	});
	
}); 


// siteEmp.play({ playlist_url: "whatever.xml" }); - Typical call to add emp to page.
// siteEmp.play({ playlist_url: "whatever.xml", width: 500 }); - This call is overriding the width attribute 