if (typeof CB === 'undefined' || !CB) {
    var CB = {};
}

CB.videoPlayer = (function(CB){
	var  _embedObj,	
	init = function (paramsObj){
		createEmbedObj(
			jQuery.extend({
				idToSwapWithVideo: null,
				accessibleVideoId: null,
				idToAccessSwf: null,
				methodToStopAndResetVideo: null,
				methodToPlayVideo: null,
				videoStartFlashCall: null,
				videoStopFlashCall: null,
				showBigPlayBtn: null,				
				tabIndex: null,
				callback: null,
				nodeId: null
			}, paramsObj)
		);
		
		jQuery('#' + _embedObj.flashvars.accessibleVideoId).addClass('onlyScreenReaderVisible');
		
		CB.utils.swfObject(_embedObj);
		//outputHtml5Video();
	},

	createEmbedObj = function (o){
		_embedObj = {
				swfUrl :  '/video/homePlayer.swf',
				width : jQuery('#' + o.idToSwapWithVideo).parent().width(), 
				height : jQuery('#' + o.idToSwapWithVideo).parent().height(),
				id: o.idToSwapWithVideo,
				version: '10.0.0',

				flashvars: {
					videoSrc: jQuery('#' + o.accessibleVideoId + ' a').attr('href'), 
					posterSrc: jQuery('#' + o.idToSwapWithVideo + ' img').attr('src') || "",
					stopAndResetCallBack: o.methodToStopAndResetVideo, 
					accessibleVideoId: o.accessibleVideoId,
					playVideoCallBack: o.methodToPlayVideo,
					videoStartCall: o.videoStartFlashCall,
					videoStopCall: o.videoStopFlashCall,
					showBigPlayBtn: o.showBigPlayBtn,					
					aSrcAccount: s_account					
				},

				attributes: {
					id: o.idToAccessSwf, // init param
					tabindex: o.tabIndex
				},

				params: {
					menu: 'false',
					scale: 'noScale',
					allowFullscreen: 'true',
					allowScriptAccess: 'always',
					bgcolor: '#FFFFFF',
					quality: 'best',
					wmode: 'transparent'
				},
				
				callbackFn: function(e){ 
					onEmbedAttempt(e);
					if(typeof o.callback == 'function')
						o.callback();
				}
			};	
	},
	
	onEmbedAttempt = function(e) {
		if(!e.success) outputHtml5Video();
		_embedObj = null;
	},

	outputHtml5Video = function(){
			jQuery('#' + _embedObj.id).html(
				$('<video id="' + _embedObj.attributes.id + '"/>')
					.attr('controls', 'controls')
					.attr('width', _embedObj.width)
					.attr('height', _embedObj.height)
					.attr('preload', 'none')
					.attr('poster', _embedObj.flashvars.posterSrc)
					.append(
						$('<source />')
							.attr('src',  _embedObj.flashvars.videoSrc)
							.attr('type', 'video/mp4')
					) 
					.append(
						$('<source />')
							.attr('src', _embedObj.flashvars.videoSrc.replace('.mp4', '.ogv'))
							.attr('type', 'video/ogg')
					)
					.append(
						jQuery('#' + _embedObj.id).html()
					).append(
						jQuery('#' + _embedObj.flashvars.accessibleVideoId).html()
					)
			);
			jQuery('#' + _embedObj.flashvars.accessibleVideoId).css('display', 'none');
	};//end Var statement
	
	return {
		'init': init
	};
})(CB);
