
// BEGIN EMBED Player
self.EDITION = parent.pageEnv['edition_code'];

self.getCurrentPlayerIdentId = function(embedElement) {
	var elId = embedElement.getAttribute('id');
	if (!elId) return false; 
	var videoclipId = elId.split(/_/)[2];
	var playerId = elId.split(/_/)[3];
	return videoclipId+'_'+ playerId;
};
self.currentPlayerIdentId = null;
self.currentPlayerObject = null;
Event.observe(window, 'load', function() {
	self.gAPIclient = new parent.gAPIclientClass({
		debug:false, 
		base_url: 'http://gapi.euro.real.com/', 
		edition_code: self.EDITION 
	});
	var embedPlayersElements = document.getElementsByClassName("embed_player");
	embedPlayersElements.each(function(embedElement) {
		var identId = self.getCurrentPlayerIdentId(embedElement);
		var currentVideoclipId = identId.split(/_/)[0];
		var methodCall = {
		"function":"freedom::getMetadataFromVideoclip",
			"parameters":{
				"videoclip_id":	currentVideoclipId,
				"edition_code": self.EDITION
			}		
		};
		self.gAPIclient.runMethodCall(methodCall,{
			onResponse: (function (r) {
				var m = r.response.metadata[0];
				var videoclipId = m.id;
				var streamUrl = null;
				$H(m.medias).each(function(media) {	
					if (media.value.format == 'rm') {
						streamUrl = media.value.stream_url;
					}
				});
				var identId = self.getCurrentPlayerIdentId(this);				
				var streamUrl = self.urlhelper.replaceGetParamInUrl(streamUrl, 'ident',identId );
				var imageUrl = (m.image_url_425x318) ? m.image_url_425x318 : false;
				var paneEl = $("infopane_"+identId);
				if(!videoclipId) {
					if(self.pageEnv['previewmode'] == true) {
						paneEl.style.background = "#e9e9e9";
						paneEl.innerHTML = '<p style="font-size: 14px; color: #ff7171;">Sorry!<br/><br/>There is no Videoclip defined:<br/><br/>Videoclip Id: <b>'+currentVideoclipId+'</b><br/>Edition: <b>'+self.EDITION+'</b></p>';
					}
				return;
				}
				paneEl._playVideoClip = function() {
					if (self.currentPlayerObject!=null) {
						var lastIdentId = self.currentPlayerIdentId;
						self.currentPlayerObject.DoStop();
						self.currentPlayerObject = null;	
						$("video_"+lastIdentId).innerHTML = '';
						Element.setStyle($("embed_player_"+lastIdentId), {'height': '318px'});
						Element.show($("infopane_"+lastIdentId));
					}
					// netscape plugin architecture
					var attrs = $H({
							src: streamUrl,
							width: 425,
							height: 368,
							controls: 'ImageWindow,ControlPanel',
							console: 'video'+identId,
							autostart: false,
							autogotourl: false,
							center: true,
							maintainaspect: true,
							pluginspage: false,
							loop: false,
							nojava: true,
							nologo: false,
							nolabels: true,
							numloop: 0,
							shuffle: false
						});
						var NSPlugin = (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length);
						if (NSPlugin) {
							output = '<embed SCRIPTCALLBACKS="None"';
							output += ' id="RMPlayer_'+identId+'_EMBED" name="RMPlayer_'+identId+'_EMBED" type="audio/x-pn-realaudio-plugin" ';
							attrs.each(function(attr){
								output += attr.key +'="'+ attr.value +'" \n'; 
							});
							output += '/>';
						}
						
						// PC IE ActiveX
						else {
							output = '<object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" id="RMPlayer_'+identId+'_ACVTX" width="'+attrs.width+'" height="'+attrs.height+'">';
							attrs.each(function(attr){
								output += '<param name="'+ attr.key +'" value="'+ attr.value +'" />';
							})
							output += "</object>";
						}
											
						$("video_"+identId).innerHTML = output;
						self.setTimeout(function() {
							var RVOCX_version = null;
							var isEMBED = false;
							var isACVTX = false;
							var RVOCX = null;
							try {
								if (NSPlugin) {
										var ELEMENT_EMBED = document.getElementsByName('RMPlayer_'+identId+'_EMBED')[0];
										RVOCX = ELEMENT_EMBED;
										RVOCX_version = RVOCX.GetVersionInfo();
										isEMBED = true;
								} 
								else {
										var ELEMENT_ACVTX = $('RMPlayer_'+identId+'_ACVTX');
										RVOCX = ELEMENT_ACVTX;
										RVOCX.GetVolume();
										RVOCX_version = RVOCX.GetVersionInfo();
										isACVTX = true;
								}
							} 
							catch (e) {
								self.console.log('playerobj: <b style="color:red">Catched expected EXCEPTION</b>: %o', e);
							}
							if (RVOCX_version == null) {
								self.console.log('playerobj: <b style="color:red">NO SCRIPTING!!!!</b>');
									var ec = self.EDITION;
								var cc = (ec == 'uk')? 'en_uk' : ((ec=='eu')? 'en_eu' : ec);
								var os = (BrowserDetect.OS=='Mac')  ? 'mac' : ((BrowserDetect.OS=="Windows") ? 'win' : ((BrowserDetect.OS=="Linux") ? 'linux' : ''));
								paneEl.innerHTML = '\
								<br/><p style="color:#f00; font-weight:bold;font-size:12px; text-align: center;">To view or listen to this content please download the free<br/>RealPlayer by starting the download below.<br/></p>\
								<div class="buttonarea">\
								<div id="btn_plus" class="btn" style="width: 220px;">\
										<div class="btn_tl"></div><div class="btn_tr"></div><br class="btn_clear"><div class="btn_mid"><div class="btn_left"><div class="btn_right">\
											<a href="javascript:void(window.open(\'/download/player/'+os+'/?src=fd_'+ec+'_blog&rsrc=fd_'+ec+'_blog\',\'freeplayer\',\'width=800,height=800,scrollbars=yes,resizable=yes\'));"">Download RealPlayer</a>\
										</div></div></div><div class="btn_bl"></div><div class="btn_br"></div><br class="btn_clear">\
									</div>\
								</div>';
							}
							else {
								self.currentPlayerObject = RVOCX;
								self.currentPlayerIdentId = identId;
								Element.setStyle($("video_"+identId), {'top': '0px'});
								Element.setStyle($("embed_player_"+identId), {'height': '368px'});
								Element.hide(paneEl);
								self.setTimeout(function() {
								   RVOCX.DoPlay();
								},500);
							}
						},1000);
				}; // end play click handler 
				if(imageUrl) paneEl.style.background = "url("+imageUrl+") no-repeat";
				paneEl.innerHTML = '\
				<br/>\
				<a class="infoplay" href="#" onclick="$(\'infopane_'+identId+'\')._playVideoClip();return false"></a>\
				';
				Element.show(paneEl);
			}).bind(embedElement),
			onFailed: function(e) {
				self.console.log("Failed");
			}
		});			
	}); // foreach
}); // onload

