/*! **********************************************************************
*  FlowPlayer & Brightcove Videos in Colorbox JQuery Plugin v 1.4.9
*  Copyright (c) 3M 2012. All rights reserved
*  Created: 10/12/2012
*  Last Modified: 12/18/2014
*  Asset ID: 1319239792609
*  Authors: @A1QQ1ZZ
************************************************************************/
var mmmVdCount=0;
var mmmVdSettings;
function MMMVideosWColorbox_complete(){
	// Flowplayer
	if(mmmVdSettings.videoType.indexOf('int-video-fpvideo')>-1){
		
		mmmVdSettings.videoPlayer= $('#cboxLoadedContent .mmmcbfpplayer').flowplayer(MMMPlayer, {
			clip: MMMDefault.clips,
			key: MMMDefault.key,
			plugins: MMMDefault.plugins,
			canvas: MMMDefault.canvas
		});
		
		mmmVdSettings.videoPlayer.load();
		
	}
	// Brightcove
	if(mmmVdSettings.videoType.indexOf('int-video-bcvideo')>-1){
		
		brightcove.createExperiences();
		
	}	
	
}
function MMMVideosWColorbox_closed(){
	if(mmmVdSettings.videoType.indexOf('int-video-fpvideo')>-1){
					
		mmmVdSettings.videoPlayer.unload();
		$('#cboxLoadedContent').html("");
		$('#cboxTitle').html("");
		mmmVdSettings.videoPlayer="";
		mmmVdSettings.videoType="";
		
	}
	
	if(mmmVdSettings.videoType.indexOf('int-video-bcvideo')>-1){
	
		$('#cboxLoadedContent').html("");
		$('#cboxTitle').html("");
		mmmVdSettings.videoType="";
	
	}	
}
(function( $ ) {
	
	//var console=console||{"log":function(){}};
	nVids=$('.int-video').size();
	
	function fnpBCBrowser(){
		return(navigator.appName);
	}
	function fpBCInlineCallBack(){
		
		
			
	}
	
	
	function checkforframeworks(settings){
		
		//console.log(fnpBCBrowser());
			
		jQuery('<div id="myscripts">').insertBefore("#masterwrap");
		
		var apply = document.getElementById("myscripts");
		
		if(!jQuery().colorbox && settings.colorboxOn){
			
			//console.log('colorbox added');
			
			var ltagcb = document.createElement("link");
			ltagcb.type="text/css";
			ltagcb.rel="stylesheet";
			if(window.location.href.indexOf('previewext.')>-1 || window.location.href.indexOf('solutionsstage.')>-1 || window.location.href.indexOf('solutions.')>-1 || window.location.href.indexOf('http://www.')>-1){
				ltagcb.href='/3MContentRetrievalAPI/BlobServlet?assetId=1180619986462&assetType=MMM_Image&blobAttribute=ImageFile';
			}else{
				ltagcb.href='css/colorbox.css';	
			}
			apply.appendChild(ltagcb);
			
			var stagcb = document.createElement("script");
			stagcb.type = "text/javascript";
			if(window.location.href.indexOf('previewext.')>-1 || window.location.href.indexOf('solutionsstage.')>-1 || window.location.href.indexOf('solutions.')>-1 || window.location.href.indexOf('http://www.')>-1){
				stagcb.src='/3MContentRetrievalAPI/BlobServlet?assetId=1180619981559&assetType=MMM_Image&blobAttribute=ImageFile';
			}else{
				stagcb.src='js/colorbox.js';
			}
			apply.appendChild(stagcb);
			
			settings.colorboxFound=false;
			settings.colorboxApplied=true;
			
			$(document).bind('cbox_complete', function(){
				
				MMMVideosWColorbox_complete();
				
			});
		
			$(document).bind('cbox_closed', function(){
				
				MMMVideosWColorbox_closed();
			
			});
			
			
		}else{
			settings.colorboxFound=true;
			settings.colorboxApplied=false;	
		}
		
		if(!jQuery().flowplayer){
			
			//console.log('flowplayer added');
			
			var stagfp = document.createElement("script");
			stagfp.type = "text/javascript";
			stagfp.src='/3MContentRetrievalAPI/BlobServlet?assetId=1258561261415&assetType=MMM_Image&blobAttribute=ImageFile';
			
			apply.appendChild(stagfp);
		
		}
		
		if($('script[src="http://admin.brightcove.com/js/BrightcoveExperiences.js"]').size()==0){
			
			//console.log('brightcove BE added');
			
			var stagbc = document.createElement("script");
			stagbc.type = "text/javascript";
			stagbc.src='http://admin.brightcove.com/js/BrightcoveExperiences.js';
			
			apply.appendChild(stagbc);
			
		}
		/*if($('script[src="http://admin.brightcove.com/js/APIModules_all.js"]').size()==0){
			
			//console.log('brightcove API added');
			
			var stagbcAPI = document.createElement("script");
			stagbcAPI.type = "text/javascript";
			stagbcAPI.src='http://admin.brightcove.com/js/APIModules_all.js';
			
			apply.appendChild(stagbcAPI);
			
		}*/
		
	};
	
	function playVideo(_callobject,_vtypefn, _vurloridfn, _vsizefn, _vtitlefn, settings, objid){
		
		settings.videoType=_vtypefn;
		//console.log(videoType);
		
		settings.videoSize=_vsizefn.split(',');
		//console.log(videoType);
		
		settings.videoPlayer="";
		
		if(settings.videoType.indexOf('int-video-fpvideo')>-1){
			
			//console.log('flowplayer video');
			settings.videoPlayer='<a href="'+_vurloridfn+'" class="mmmcbfpplayer"></a>';
			
		}
		
		if(settings.videoType.indexOf('int-video-bcvideo')>-1){
			
			//console.log('brightcove video');
			
			settings.videoIds=_vurloridfn.split(";");
				
			mmmVdCount++;
			settings.videoPlayer += '<obj'+'ect id="myExperience'+mmmVdCount+'" class="BrightcoveExperience">';
			settings.videoPlayer += '<param name="bgcolor" value="#FFFFFF" />';
			settings.videoPlayer += '<param name="width" value="'+settings.videoSize[0]+'" />';
			settings.videoPlayer += '<param name="height" value="'+settings.videoSize[1]+'" />';
			settings.videoPlayer += '<param name="playerID" value="'+settings.videoIds[0]+'" />';
			settings.videoPlayer += '<param name="playerKey" value="'+settings.videoIds[1]+'" />';
			settings.videoPlayer += '<param name="isVid" value="true" />';
			settings.videoPlayer += '<param name="isUI" value="true" />';
			if(settings.autostart){
				settings.videoPlayer += "<param name='autoStart' value='true'/>";
			}else{
				settings.videoPlayer += "<param name='autoStart' value='false'/>";	
			}
			settings.videoPlayer += '<param name="dynamicStreaming" value="true" />';
			
			if(settings.videoType.indexOf('int-video-playlist')<0){
				
				settings.videoPlayer += '<param name="@videoPlayer"  value="'+settings.videoIds[2]+'" />';
				
			}else{
				
				//console.log('brightcove playlist');	
				
			}

			settings.videoPlayer += '<param name="includeAPI" value="true" />';
			settings.videoPlayer += '<param name="templateLoadHandler" value="'+settings.plyrtempid+'onTemplateLoaded" />';
			settings.videoPlayer += '<param name="templateReadyHandler" value="'+settings.plyrtempid+'myTemplateReady" /> ';

			settings.videoPlayer += '<param name="wmode" value="transparent"/>';
			settings.videoPlayer += '</obj'+'ect>';
				
		}
		
		if(settings.videoType.indexOf('int-video-youtube')>-1){
			
			settings.videoPlayer +='<if'+'rame ';
			settings.videoPlayer +='width="'+settings.videoSize[0]+'" height="'+settings.videoSize[1]+'" src="'+_vurloridfn+'" frameborder="0" allowfullscreen>';
			settings.videoPlayer +='</ifr'+'ame>';
			
		}
		
		if(settings.videoType.indexOf('int-video-inline')>-1){
			//console.log(videoPlayer);
			
			$('#'+_callobject).closest('.videodump').width($('#'+_callobject).find('img').width()).height($('#'+_callobject).find('img').height());
			$('#'+_callobject).closest('.videodump').html(settings.videoPlayer);

			if(settings.videoType.indexOf('int-video-bcvideo')>-1){	
				brightcove.createExperiences();
			}

			
		}else if(settings.colorboxOn && (settings.colorboxFound || settings.colorboxApplied)){
			//console.log(videoPlayer);
			var myTitle="";
			if(_vtitlefn!=''){
				myTitle=_vtitlefn;		
			}
			mmmVdSettings=settings;
			$.colorbox({html:settings.videoPlayer, innerHeight:settings.videoSize[1]+'px', innerWidth: settings.videoSize[0]+'px', title:myTitle, scrolling:false});
			
		}else{
			if(!settings.colorboxFound && !settings.colorboxApplied){
				alert('Brightcove/Flowplayer plugin colorbox option is turned off and no colorbox plugin found on page. Please check page and Brightcove/Flowplayer plugin configuration');
			}
			
		}
		
		var ctid=0;
		if(objid){
			if(objid.indexOf("ctid")>-1){
			ctid=Number(objid.substr(objid.indexOf("ctid")+4,6));
			mmConversionTag(ctid, this, '_self'); return false;
			}
		}
	};
	
	

	$.fn.MMMVideosWColorbox = function(options) {
		
		var _settings = $.extend({
		
			autostart:false,
			includethumbnails:false,
			inlineClickCallback:null,
			plyrtempid:"mmmvd.",
			colorboxOn:true,
			colorboxFound:false,
			colorboxApplied:false,
			videoType:null, 
			videoSize:null, 
			videoPlayer:null, 
			videoIds:null, 
			nVids:0
		
		}, options);
		
		return this.each(function(index, domEle) {
			
			
			if(index==0){
				checkforframeworks(_settings);
			}
			
			var videoInfo="";
				
			if($(this).attr("href")!=''||$(this).attr("href")!='javascript:void(0)'){
				
				$(this).attr('href','javascript:void(0)');	
	
			}
			
			videoInfo=$(this).attr("media");
			//console.log(videoInfo);
			
			/*if(_settings.includethumbnails && $(this).attr('class').indexOf('bcvideo')>-1){
				bcvidinfo=videoInfo.split(';');
				
				var myjsonurl="/apxy/proxy?urlAlias=BC_3M_HC_US&command=find_video_by_id&video_id="+bcvidinfo[2]+"&video_fields=thumbnailURL";
				
				var jqxhr = $.getJSON(myjsonurl, function(data) {
					
				})
				.success(function(){
					jQuery(this).html('<img src="' + data.thumbnailURL + '" />');
				})
				.error(function(jqXHR, textStatus, errorThrown) {
					
				});
				
			}*/
			
			jQuery(this).bind("click",function(){
				//console.log('call click');
				if(_settings.inlineClickCallback!=null){
					//console.log('fircallback');
					$.isFunction(options.inlineClickCallback) && options.inlineClickCallback.call(this);
				}
				playVideo(jQuery(this).find('div').attr('id'),jQuery(this).attr("class"), $(this).attr("media"), jQuery(this).attr("rel"), jQuery(this).attr('title'),_settings, jQuery(this).attr('id'));
				
				
	
				
			});
		
		});
		
		
		
		

	};
	
	

})( jQuery );