// If helper is already defined, then reuse its definition.
// Otherwise, create a new object for the helper.
var helper = helper || {};

// Add a video package.
helper.video = {};

// Loads a video, using the specified options to control the embedded SWF element.
// The options are passed as an object containing the following things:
//   element_id: (Required) The id for the element that will receive the video object.
//   video_url: (Required) The url for the video to be loaded.
//   background_url: (Optional) A url for an image, to show as the player background. Defaults to undefined.
//   autostart: (Optional) A boolean indicating whether or not this video should preload or not. Defaults to false.
//   analytics_account: (Optional) An account id for Google Analytics, which if given, will be used to track analytics for this video. Defaults to undefined.
//   doubleclick_ad_tag: (Optional) A doubleclick ad tag which will be used as a preload for this video. Defaults to undefined.
helper.video.load = function(options) {
	helper.video.unload(options.element_id);
	
	var swf = new SWFObject('http://cfl.assets.mrx.ca/league/swf/2009/player.swf', 'ply', '100%', '100%', '9', '#FFFFFF');
	swf.addParam('allowfullscreen', 'true');
	swf.addParam('allowscriptacess', 'always');
	swf.addParam('wmode', 'transparent');
	
	swf.addVariable('file', options.video_url);
	if(options.background) {
		swf.addVariable('image', options.background);
	}
	swf.addVariable('autostart', !!options.autostart);
	
	var plugins = [];
	if(options.analytics_account) {
		plugins.push('gapro-1');
		swf.addVariable('gapro.accountid', options.analytics_account);
	}
	if(options.doubleclick_ad_tag) {
		plugins.push('dcinstream');
		swf.addVariable('dcinstream.ad.tag', options.doubleclick_ad_tag);
		swf.addVariable('dcinstream.ad.position', 'pre');
		swf.addVariable('dcinstream.scaled_ads', false);
	}
	
	swf.addVariable('plugins', plugins.join(','))
	
	swf.write(options.element_id);
	$('#' + options.element_id).show();
}

helper.video.unload = function(element_id) {
	$('#' + element_id).html('').hide();
}
