
///------------/ BRIGHTCOVE API /----------///

var bcExp;
var bc_videoPlayer;
var bc_experience;
var bc_content;
var bc_menu;
var bc_social;
var bc_cue;
var bc_currentVideo;
var bc_currentID;
var bc_isReady = false;
var bc_playCount = 0;
var vDuration;


function onTemplateLoaded(experienceID) {
	bcExp = brightcove.getExperience(experienceID);
	
	// EXPERIENCE MODULE API //
	bc_experience = bcExp.getModule(APIModules.EXPERIENCE);
	bc_experience.addEventListener(BCExperienceEvent.CONTENT_LOAD, onContentLoad);
	bc_experience.addEventListener(BCExperienceEvent.TEMPLATE_READY, onTemplateReady);

	
	// CONTENT MODULE API //
	bc_content = bcExp.getModule(APIModules.CONTENT);
	bc_content.addEventListener(BCContentEvent.VIDEO_LOAD, onVideoLoad);
	
	// PLAYER MODULE API //		
	bc_videoPlayer = bcExp.getModule(APIModules.VIDEO_PLAYER);
	bc_videoPlayer.addEventListener(BCVideoEvent.VIDEO_CONNECT, onConnect);
	bc_videoPlayer.addEventListener(BCVideoEvent.VIDEO_LOAD, onVideoPlayerLoad);
	bc_videoPlayer.addEventListener(BCVideoEvent.STREAM_START, onStreamStart);
	bc_videoPlayer.addEventListener(BCVideoEvent.VIDEO_START, onVideoStart);
	bc_videoPlayer.addEventListener(BCVideoEvent.VIDEO_STOP, onVideoStop);
	bc_videoPlayer.addEventListener(BCVideoEvent.VIDEO_COMPLETE, onVideoComplete);
	
	// MENU MODULE //
	bc_menu = bcExp.getModule(APIModules.MENU);
	bc_menu.addEventListener(BCMenuEvent.VIDEO_REQUEST, onRequest);
	
	// CUE POINTS MODULE //
	bc_cue = bcExp.getModule(APIModules.CUE_POINTS);
	bc_cue.addEventListener(BCCuePointEvent.CUE, onCuePoint);
		
}

function onTemplateReady(evt){
}

function onConnect(evt){
	bc_menu.closeMenuPage();
}

function onContentLoad(evt) {
	
	callCounter();
	bc_currentVideo = bc_videoPlayer.getCurrentVideo();
	bc_social = bcExp.getModule(APIModules.SOCIAL);
	bc_social.setLink('http://www.'+bc_config['site']+'.com/common/media/video/player.php?videoRef='+bc_config["videoRef"]);
}

function onVideoPlayerLoad(evt){
}

function onVideoLoad(evt) {
	bc_currentID = evt.video.id;
	bc_videoPlayer.loadVideo(evt.video.id);
}

function onStreamStart(evt) {
	vDuration = Math.round(bc_videoPlayer.getVideoDuration());
	if(vDuration > 0){
		bc_cue.clearCodeCuePoints(bc_currentVideo.id);
		var vCue = new Array(Math.round(vDuration * .25), Math.round(vDuration * .50),Math.round(vDuration * .75), vDuration);
		var pass = bc_cue.addCuePoints(bc_currentVideo.id, vCue);
		hbxMedia(bc_config["videoRef"], 0, vDuration, 'play');
	}
	
}

function onVideoStart(evt){
	var pos = Math.round(evt.position);
	if(pos > 0 && vDuration > 0) hbxMedia(bc_config["videoRef"], pos, vDuration, 'playp');
}

function onVideoStop(evt){
	var pos = Math.round(evt.position);
	if(pos > 0 && pos != vDuration) hbxMedia(bc_config["videoRef"], pos, vDuration, 'pause');
}

function onVideoComplete(evt){
	hbxMedia(bc_config["videoRef"], vDuration, vDuration, 'stop');
}

function onCuePoint(evt){
	if(evt.cuePoint.time > 0 && vDuration > 0){
		hbxMedia(bc_config["videoRef"], evt.cuePoint.time, vDuration, 'playp');	
	}
}



function setVideo(vID, guid){
	bc_content.getVideoAsynch(vID,"referenceId");
	bc_config["videoRef"] = vID;
	bc_config["videoGuid"] = guid;
	bc_social.setLink('http://www.'+bc_config['site']+'.com/common/media/video/player.php?videoRef='+vID);
	callCounter();
}
		
function onRequest(evt){
	config['videoRef'] = evt.args.referenceId;
	bc_social.setLink('http://www.'+bc_config['site']+'.com/common/media/video/player.php?videoRef='+bc_config["videoRef"]);
	replaceTitle(evt.args.referenceId, evt.args.displayName, evt.args.longDescription);
}


function callCounter(){
	var url = 'http://www.'+bc_config['site']+'.com/common/brightcove/bc_flash_functions.php';
	var re = new Ajax.Request(url, { parameters:{mode:"incrementCounter", guid:bc_config['videoGuid']}});
}

function hbxMedia(vFileName,vCurrentPos,vEndPos,vAction){
	_hbSet('m.s',vAction);
	_hbSet('m.f',vFileName);
	_hbSet('m.cp',vCurrentPos*1000);
	_hbSet('m.ep',vEndPos*1000);
	_hbSet('m.cl','Flash Player');
	_hbSet('m.cv','9.0');
	_hbSet('m.tt','e');
	_hbSend();
}
///--------/ END BRIGHTCOVE API /----------///



function replaceTitle(ref,title,desc){
	setShareLink(ref);
	$('titleText').innerHTML = title;
	$('descText').innerHTML = '<p>'+desc+'</p><div id="pk_com_rec" class="right"></div>';
	setTitle(title);
	ArticleID = ref;
	novaapi.GetComments(1);
	_hbPageView(title,document.URL);
}

function setPlayerVideo(arg){

	ArticleID = replaceData(arg[0]); 
	setShareLink(ArticleID);
	var pUrl = replaceData(arg[4]);
	var pLogo = replaceData(arg[5]);
	var title = replaceData(arg[2]);
	var desc ='<p>'+replaceData(arg[3])+'</p>';
	if(pUrl != 'undefined' && pLogo != 'undefined'){
		var imgDomain = (document.domain=='space.com') ? 'a52.g.akamaitech.net/f/52/827/1d/www.livescience.com' : 'i'+document.domain;
		desc = '<div class="cobrand"><a href="'+pUrl+'"><img src="http://'+imgDomain+'/template_images/partners/'+pLogo+'" width="138" height="80" /></a></div>'+desc;
	}
	$('titleText').innerHTML = title;
	$('descText').innerHTML = desc+'<div id="pk_com_rec" class="right"></div>';
	
	setTitle(title);
	setVideo(ArticleID,replaceData(arg[1]));
	window.location.hash = 'playerTop';
	
	_hbPageView(title,document.URL);
	community.LoadComments();
	community.LoadComRec();
}

function setTitle(str){
	var sn;
	switch (document.domain){	
		case 'livescience.com': sn = "Livescience"; break;
		case 'newsarama.com': sn = "Newsarama"; break;
		case 'space.com': sn = "Space.com"; break;
	}
	document.getElementsByTagName('title')[0].text = sn+" | "+str;
}

function setShareLink(ref){
	var shareLink;
	switch (document.domain){
		case 'livescience.com':
			shareLink = 'http%253A%252F%252Fwww.livescience.com%252Fcommon%252Fmedia%252Fvideo%252Fplayer.php%253FvideoRef%253D'+ref; 
			$('yahooBuzzBadge-form').getElementsByTagName('a')[0].href = 'http://buzz.yahoo.com/article/livescience/'+shareLink;
		break;
		
		case 'newsarama.com':
			shareLink = 'http%253A%252F%252Fwww.newsarama.com%252Fcommon%252Fmedia%252Fvideo%252Fplayer.php%253FvideoRef%253D'+ref; 
			$('yahooBuzzBadge-form').getElementsByTagName('a')[0].href = 'http://buzz.yahoo.com/article/newsarama550/'+shareLink;
		break; 
			
	}
	
	getShare(shareLink);
}



function replaceData(arg){
	arg = arg.replace(/%22/g, "\"").replace(/%5c/g, "\\").replace(/%26/g, "&").replace(/%25/g, "%");
	return arg
}

function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}


function displayCompanionAds(banners) {
	var defaultSize = "300x250";
	var size = defaultSize;
	var width = 300;
	var height = 250;
	var adDiv = $('expandedBanner');
		adDiv.innerHTML = '';
	for (var i in banners) {
		var banner = banners[i];
		var bannerSize = banner.size ? banner.size : banner._size;
		if ((bannerSize == null) || (bannerSize == "undefinedxundefined") || (bannerSize == "NaNxNaN"))
			bannerSize = defaultSize;
		if (bannerSize == size) {
			var htmlUrl = banner.htmlUrl ? banner.htmlUrl : banner._htmlUrl;
			var swfUrl = banner.swfUrl ? banner.swfUrl : banner._swfUrl;
			var imageUrl = banner.imageUrl ? banner.imageUrl : banner._imageUrl;
			var clickUrl = banner.clickUrl ? banner.clickUrl : banner._clickUrl;
			if (htmlUrl) {
				adDiv.innerHTML = '<iframe src="'+ htmlUrl +'" width="300" height="250" scrolling="no" frameborder="0" marginheight="0" marginwidth="0"></iframe>';
			}else if (swfUrl) {				
				var fplayer = new FlashTag(swfUrl, 300, 250);
					adDiv.innerHTML = fplayer.toString();
			}else if (imageUrl) {
				var img = '<a href="'+clickUrl+'" target="_blank"><img src="'+imageUrl+'" width="'+width+'" height="'+height+'" /></a>';
				adDiv.innerHTML= img;
      		}
		}
	}
}


