﻿/**** brightcove functions ****/
var player;
var video, content, exp, menu, ads, social;
var tabBar;

function onTemplateLoaded(pPlayer) {
	//trace("templateLoaded");

	player = bcPlayer.getPlayer(pPlayer);

	video 	= player.getModule(APIModules.VIDEO_PLAYER);
	content = player.getModule(APIModules.CONTENT);
	exp 	= player.getModule(APIModules.EXPERIENCE);
	menu 	= player.getModule(APIModules.MENU);
	ads 	= player.getModule(APIModules.ADVERTISING);
	social 	= player.getModule(APIModules.SOCIAL);


	exp.addEventListener(BCExperienceEvent.CONTENT_LOAD, onContentLoad);
	exp.addEventListener(BCExperienceEvent.TEMPLATE_READY, onTemplateReady);
	video.addEventListener(BCVideoEvent.VIDEO_CHANGE, onVideoChange);

}

function onContentLoad(e) {
	//trace(e.type);
}

function onTemplateReady(e) {
	//trace(e.type);

	tabBar = exp.getElementByID("playlistTabs");
	updateLink(video.getCurrentVideo().id, tabBar.getSelectedData().id);

}

function videoPlayerPause() {
    if (video.isPlaying()) {
        video.pause(true);
    }
}
function videoPlayerStart() {
    if (!videoPlayer.isPlaying()) {
        videoPlayer.pause(false);
    }
}

function onVideoChange(e) {
	//trace(e.type);

	if(exp.getReady()) { // If template is Ready

		// Because TemplateReady has already fired we can now access the currentVideo and currentPlaylist from the tabBar module
		updateLink(video.getCurrentVideo().id, tabBar.getSelectedData().id);

	}

}

function updateLink(videoId, playlistId) {

	// 	Brightcove players published using the standard Javascript publishing code automatically
	// listen for bclid and bctid in order to select featured items. If your application is setup
	// in another format, such as Actionsript, you can choose to alter these key names to something
	// compatible with your application. Your application will be responsible for properly setting
	// the featured content

	var playlistKey = "bclid";
	var videoKey = "bctid";
	
	var currentLink = social.getLink();
	//trace("Original Link: " + currentLink);


	// Added for video loacations on amstd. Keys are not likely to intermingle at this point.
	// 
	var addQuery = "";
	var keyValue
	if ((keyValue = getQuerystring("d", "")) != "")
	{// divisionID
		addQuery += '&d=' + keyValue;
	}
	else if ((keyValue = getQuerystring("t", "")) != "")
	{// pressroom categoryID; product category also use this key...
		addQuery += '&t=' + keyValue;
	}
	else if ((keyValue = getQuerystring("id", "")) != "")
	{// pressroom articleID; products also use this key...
		addQuery += '&id=' + keyValue;
	}

	// Get the current URL and remove any existing URL parameter
	if(currentLink.indexOf("?") != -1) {
		currentLink = currentLink.substring(0,currentLink.indexOf("?"));
	}

	var newLink = currentLink + "?" + playlistKey + "=" + playlistId + "&" + videoKey + "=" + videoId + addQuery;
	
	
	//trace("New Link: " + newLink);
	social.setLink(newLink);
}

function getQuerystring(key, default_) {
	if (default_==null) {
		default_="";
	}
	key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
	var qs = regex.exec(window.location.href);
	if(qs == null) {
		return default_;
	} else {
		return qs[1];
	}
}
