if (!embeddedMedia) var embeddedMedia = {};

embeddedMedia.Player = function(version)
{

	this.so = new SWFObject("http://www.bbc.co.uk/emp/player.swf","bbc_emp_embed",this.height,this.width,version || "8","#000000");
	this.revision= "10344_10753";
	this.so.addParam("wmode","default");
	this.so.addParam("quality","high");
	this.so.addParam("allowFullScreen","true");
	this.so.addParam("allowScriptAccess","always");
	this.so.addVariable("embedReferer",document.referrer);
	this.so.addVariable("embedPageUrl",location.href);

	// this.so.useExpressInstall('http://www.bbc.co.uk/emp/expressinstall.swf');
};

embeddedMedia.playerInstances = {};

embeddedMedia.Player.prototype = {
	
	call: function (functionName, params, callbackFunction)
	{
		this.swf.call(functionName, params, callbackFunction);
	},
	
	handleEvent: function ( event )
	{
		if (!this[event.type]) return;
		this[event.type](event);
	},
	
	register: function (eventType)
	{
		this.swf.register(eventType);
	},
	
	unregister: function (eventType)
	{
		this.swf.unregister(eventType);
	},

	write: function(guidanceFlag) {
		
		embeddedMedia.playerInstances[this.domId] = this;

		// if bbc guidance system installed, attach required hooks
		if (bbc.guidance) {
			var instance = embeddedMedia.playerInstances,
				guidance = bbc.guidance;

			if (!embeddedMedia.guidanceInstalled) {
				embeddedMedia.guidanceInstalled = true;
				guidance.callbacks.toggle = guidance.callbacks.pass = function(status,callbackId) { if (instance[callbackId]) instance[callbackId].write(status); };
			}
			
			this.set("guidance",guidanceFlag || guidance.status());
		}

		var flashVersion = deconcept.SWFObjectUtil.getPlayerVersion(),
			swf = this.so.getAttribute("swf");

		if (!this.swf)
		{
			this.so.setAttribute("id",this.so.getAttribute("id") + "_" + this.domId);
		}

		if (flashVersion.major < 8) {
			swf = "7player.swf" ;
		} else if ((flashVersion.major == 9 && (flashVersion.minor > 0 || flashVersion.rev >= 115)) || (flashVersion.major > 9)) {
			swf = "9player.swf" ;
		} else {
			swf = "player.swf" ;
		}

		this.so.setAttribute("swf","http://www.bbc.co.uk/emp/" + swf + "?revision=" + this.revision);
		
		this.so.write(this.domId);
		
		this.swf = document.getElementById(this.so.getAttribute("id"));
	},

	setWidth: function(value) { this.so.setAttribute("width",value); },

	setHeight: function(value) { this.so.setAttribute("height",value); },

	setPlaylist: function(value) { this.so.addVariable("playlist",value); },

	setConfig: function(value) { this.so.addVariable("config",value); },

	setDomId: function(value) { this.domId = value; this.so.addVariable("domId",value); },

	setRevision: function(value) { this.revision = value; },

	set: function(key,value) {
		
		this.so.addVariable(key,value);
	}

};


embeddedMedia.console = function() {

	var popDetail = {};

	function popoutSimulcast(pid,locale,colour) {
		var root = (locale && locale!="en") ? "/iplayer/" + locale : "/iplayer"; 
		
		createPopup(
			root + "/console/" + pid + (colour ? ("/colour/" + colour) : "/colour/silver"),
			"simulcastPop",429,512);
	}

	function popoutRadioInvoke(pid, locale) { createPopup(getRadioConsoleUrl(pid, locale),"radioPop",270,512); }

	function getRadioConsoleUrl(pid, locale) {
		var lookup = {
			"bbc_radio_one":						"radio1",
			"bbc_radio_two":						"radio2",
			"bbc_radio_three":						"radio3",
			"bbc_radio_fourfm":						"radio4fm",
			"bbc_radio_five_live":					"fivelive",
			"bbc_radio_five_live_sports_extra":		"5livesportsextra",
			"bbc_1xtra":							"1xtra",
			"bbc_6music":							"6music",
			"bbc_7":								"bbc7",
			"bbc_asian_network":					"asiannetwork",
			"bbc_radio_ulster":						"radioulster",
			"bbc_radio_foyle":						"radiofoyle",
			"bbc_radio_wales_fm":					"radiowales",
			"bbc_radio_scotland_fm":				"radioscotland",
			"bbc_world_service":					"worldservice"
		};
		var root = (locale && locale!="en") ? "/iplayer/" + locale : "/iplayer"; 
		return root + "/console/" + (pid);
	}

	function popout(params,height,width,mode) {
		height = height * 1;
		width = width * 1;
		popDetail = {
			params: params,
			mode: mode,
			height: height,
			width: width
		};
		createPopup("/emp/pop.html","videoPop" + mode,height + ((mode == "standard") ? 77 : 32),width);
	}

	function createPopup(url,name,height,width) {
		var win = window.open(
			url,name,
			"width=" + width + ",height=" + height + ",toolbar=no,resizable=no,scrollbars=no"
		);
		if (!win.opener) win.opener = window.self;
		if (win.focus) win.focus();
		return win;
	}

	return {
		popoutRadio: function(pid) { popoutRadioInvoke(pid); },
		popoutRadioInvoke: popoutRadioInvoke,
		popoutAudio: function(params,height,width) { popout(params,height,width,"audio"); },
		popoutVideo: function(params,height,width) { popout(params,height,width,"standard"); },
		popoutiPlayerVideo: function(params,height,width) { popout(params,"323","512","standard"); },
		popoutSimulcast: function(pid,locale,colour) { popoutSimulcast(pid,locale,colour); },
		popoutBlackSimulcast: function(pid) { popoutSimulcast(pid,"black"); },
		notifyParent: function(childWin) { if (childWin && childWin.updatePlayer) childWin.updatePlayer(popDetail); }
	};
}();

embeddedMedia.diagnostics = function() {
	return {
		openDiagnostics: function() {
			var href = ((/bbc.co.uk(:\d{2,5})?\/iplayer/).test(location.href) ) ? "/iplayer/diagnostics" : "http://www.bbc.co.uk/iplayer/diagnostics";
			var isPopup = (/emp\/pop/).test(location) || (/iplayer\/console/).test(location.href);
			if (isPopup) {
				window.open(href,"_blank");
			} else {
				location.href = href;
			}
		}
	};
}();

embeddedMedia.api = function() {
	return {
		handleEvent: function ( id, event )
		{
			if (!embeddedMedia.playerInstances[id]) return;
			embeddedMedia.playerInstances[id].handleEvent (event);
		}
	};
}();

// alias for existing bbc.Emp() namespace
if (!bbc) var bbc = {};
bbc.Emp = embeddedMedia.Player;

