/* For version and legal information, see info.html file in the top directory of the AdotubeRuntime distribution.
 * 
 * Param names explicitly referenced in Sorge:
 * runtime_location, player_width, player_height, player_div_id, player_video_stream_url, player_video_thumbnail_url,
 * player_video_preroll_stream_url, player_video_playlist_url, player_video_postroll_stream_url 
 */

(function() {
	////////////// Global Utility Functions ////////////////////////
	var w = window, a = w.adotube_global = {},  evars=w.adotube_embed;
	
	// Add params from source to sink
	a.addParamsTo = function(sink, source, override) {
		for (var i in source) {
			if (override || (typeof sink[i] == "undefined")) {
				sink[i] = source[i];
			}
		}
	}
	a.array2assoc = function(ar) {
		for (var i in ar) {
			ar[ar[i]] = true;
		}
		return ar;
	}
	/////////////////////////////////////////////////////////////////
	
	// Sorge defines window.adotube_embed_player_callback by itself.
	// Sorge3p will expect the 3rd Party to define this window.adotube_embed_player_callback.
	window.adotube_embed_player_callback = function(avparams) {
		// Note that adotube_embed_player_callback() refers to the Player Embed namespace params,
		// while createFallbackOML() refers to the Embed namespace params.
		function createFallbackOML() {
			var w=window, evars=w.adotube_embed;
			
			var thumbnail = (evars.player_video_thumbnail_url?(' thumbnailURL="' + evars.player_video_thumbnail_url + '"'):'');
			var oml = '<oml oml_version="2.0"><multistreamList>';
			// preroll
			if (evars.player_video_preroll_stream_url) {
				oml += '<multistream><video'+thumbnail+'>'+evars.player_video_preroll_stream_url+'</video></multistream>';
				thumbnail = ''; 
			}
			// main video 
			var isPlaylist = Boolean(evars.player_video_playlist_url);
			oml += '<multistream><video'+(isPlaylist?' playlist="true"':'')+thumbnail+'>'+(isPlaylist?evars.player_video_playlist_url:evars.player_video_stream_url)+'</video></multistream>';
			thumbnail = '';
			// postroll
			if (evars.player_video_postroll_stream_url) {
				oml += '<multistream><video'+thumbnail+'>'+evars.player_video_postroll_stream_url+'</video></multistream>'; 
			}
			oml += '</multistreamList></oml>'; 
			
			return oml;			 
			// return '<oml oml_version="1.3.4"><video'+thumbnail+'>' + evars.player_video_stream_url + '</video></oml>';
		}
		
		var w=window, a=w.adotube_global, evars=w.adotube_embed;

		// Adotube Video Default Values (to be used if not overridden) 
		var av_defaults = {
			player_width: "430",
			player_height: "370"
		};
		
	  	// Create AdotubeVideo param set by merging the params from avp and evars
		var av = {};
		
		a.addParamsTo(av, av_defaults);

		if (typeof avparams != "undefined") {
			// If avparams are available (from the server!) then use them for the embed
			a.addParamsTo(av, avparams, true);
		} else {
			// otherwise, use the evars from the embed - this will happen in the fallback scenario
			// when e.g., courier fails to load
			a.addParamsTo(av, evars, true);
		}
		
		// if OML is not defined, create the fallback (minimal) OML that will allow just the video to play
		if (!av.oml) {
			av.oml = createFallbackOML();
		} 		
		
		// Translate the parameter names from the Embed namespace to the PlayerEmbed namespace
		var embed2PlayerEmbedNS = {
			player_auto_start: 'autoStart',
			player_bg_logo_url: 'cbglogo',
			player_bg_color: 'cbgcol',
			player_fg_color: 'cfgcol',
			player_custom_site_name: 'csname',
			player_video_page_url: 'vpgurl',
			player_replay_button: 'rbtn',
			player_link_button: 'lbtn',
			player_email_button: 'mbtn',
			player_embed_button: 'ebtn',
			player_custom_overlay_logo_url: 'cologourl',
			player_custom_overlay_logo_position: 'cologopos',
			player_custom_overlay_logo_site_url: 'colsurl',
			player_custom_context_menu_message: 'ccmmess',
			player_custom_context_menu_message_url: 'ccmurl',
			player_rv_playlist_url: 'rvSource',
			player_skin_menu_button: 'skmbtn',
			player_skin_format: 'skin'
		}
		for (var i in embed2PlayerEmbedNS) {
			if (typeof av[i] != "undefined") {
				av[embed2PlayerEmbedNS[i]] = av[i];
				delete av[i];
			}
		}
		
		var so = new SWFObject(av.runtime_location + "/player/APL.swf", "AdotubePlayer_" + Math.floor(Math.random()*10000), av.player_width, av.player_height, "8", "#FFFFFF");
		
		av['skin'] = ((!av['skin']) || (av['skin']=='Default'))?'':av['skin'];
		for (var i in av) {
			// Exclude the variables which don't need to be passed to the player just to minimize pollution 
			if (!(i in a.array2assoc(["runtime_location", "player_width", "player_height", "player_div_id"]))) {
				so.addVariable(i, escape(av[i]));
			}
		}
		so.addParam("allowFullScreen", "true");
		so.addParam("allowNetworking", "all");
		so.addParam("allowScriptAccess", "always");
		
		so.useExpressInstall(av.runtime_location + "/swfobject/expressinstall.swf");
		// so.setAttribute('redirectUrl', 'http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash');
		// so.addParam("allowScriptAccess", "always");
		so.write(av.player_div_id); 
	}

	// If the player div can not be found, create a new one.
	// Techinically, this should be done in adotube_embed_player_callback() (for the
	// case when the player_div_id is not specified in the embed but is specified
	// in the placement; however, we would like to generate the dynamic div
	// as close as possible to the location of the adotube embed code block,
	// so we do it here (overriding the placement value with the value of dynamic div id). 
	if (!(evars.player_div_id && document.getElementById(evars.player_div_id))) {
		evars.player_div_id = 'AdotubePlayerDiv'+Math.floor(Math.random()*10000);
		document.write('\n<div id="'+evars.player_div_id+'"></div>\n');
	}
	

	// Courier defines the function window.adotube_courier, if it's not there it means courier has failed to load.
	var courier = window.adotube_courier;
	if (typeof courier == 'function') {
		courier();
	} else {
		// embed the video without parameters
		window.adotube_embed_player_callback();
	}
})()