/* ----------------------------------------------------------------------------------------------------
 * Javascript specific to the market education video page
 ------------------------------------------------------------------------------------------------------ */


function playVideo(newVideoId){ 



/*	
	  switch(newVideoId){
			case 1: 
			alert('made it 1')
			$j('#videoPlayer' + newVideoId).toggleClass('videoPlayerOn');
			break;
			case 2: 
			$j('#videoPlayer' + newVideoId).toggleClass('videoPlayerOn');
			break;
			case 3: 
			$j('#videoPlayer' + newVideoId).toggleClass('videoPlayerOn');
			break;
			}
*/	

}




// Tab switcher for video categories
function switchCategory(newCategory){
	for (i=1;i<=5;i++){
		document.getElementById("videoSection" + i).style.display="none";
		$j('#videoCategory' + i).removeClass('on');
	}
	document.getElementById("videoSection" + newCategory).style.display="block";
	$j('#videoCategory' + newCategory).toggleClass('on');
}

// Bug fix for IE (can be removed)
function videoTopicChanger()
{
	changeToThis = document.videoTopicForm.videoTopicSelector.options.selectedIndex;
	changeToThis++;
	switchTopic(changeToThis);
}

// Tab switcher for video topic categories
function switchTopic(newTopic){
	for (i=1;i<=8;i++){
		document.getElementById("topicSection" + i).style.display="none";
		$j('#video_topic' + i).removeClass('video_topic_on');
	}
	document.getElementById("topicSection" + newTopic).style.display="block";
	$j('#video_topic' + newTopic).addClass('video_topic_on');
}

// Tab switcher for asset class topic categories
function switchTopicA(newTopic){
	for (i=1;i<=6;i++){
		document.getElementById("topicSectionA" + i).style.display="none";
		$j('#video_topicA' + i).removeClass('video_topic_on');
	}
	document.getElementById("topicSectionA" + newTopic).style.display="block";
	$j('#video_topicA' + newTopic).addClass('video_topic_on');
}



var oldVideoId = null;

function getRowId(vidId) {
	var z = $j('#videoDescription'+vidId).attr('onclick');
	return z.toString().match(/[\d]+/)[0];

}

playerName = "player1";
isPlayerAdded = false;

function addPlayer(vidId) {

	if(isPlayerAdded == false) {
		isPlayerAdded = true;

		var params = {};
		params.playerID = '84581823001';
		params.bgcolor = '#993300';
		if (videoEmbeds[vidId].chapterPlayer) {//Chapter player embed
			params.isUI = true;
			params.playerID = videoEmbeds[vidId].number; 		
			params.height = "336";
			params.width = "720";
			$j('#playerText').width('220px').height('315px')

		} else {
			params.videoId = videoEmbeds[vidId].number;
			//params.autoStart = "true";
			params.height = "315";
			params.width = "374";
		}
		params.isVid = "true";

		var player = brightcove.createElement("object");
		player.id = playerName;
		var parameter;
		for (var i in params) {
			parameter = brightcove.createElement("param");
			parameter.name = i;
			parameter.value = params[i];
			player.appendChild(parameter);
		}

		var playerContainer = document.getElementById('playerContainer');

		brightcove.createExperience(player, playerContainer, true);
	}
}

function removePlayer() {

	if(isPlayerAdded == true) {
		isPlayerAdded = false;

		brightcove.removeExperience(playerName);
	}

}


function switchVideo(row, newVideoId){ 
	var rowId = 0;
	if (row !== null && typeof row !== 'undefined' && !isNaN(row) ) {
		rowId = row;
	} else {
		rowId = getRowId(newVideoId);
	}
	location.hash = newVideoId; 

	if (oldVideoId !== null)
	{
		$j('#videoDescription' + oldVideoId).toggleClass('video_highlight');
		var oldRowId = getRowId(oldVideoId);
		//Add BC remove code
		removePlayer();
		$j("#videoPlayer" + oldRowId).html('').hide();
	}
		var newHtml = 	'<div class="video_iframe_container">';
		newHtml +=	'		<div id="playerContainer" class="video_iframe_container"></div>';
		newHtml +=	'	</div>';
		newHtml +=	'<div id="playerText" class="video_content_frame" style="width: 553px; height: 292px;">';
		newHtml +=	'	<div class="articletitle">'+ videoEmbeds[newVideoId].title +'</div>';
		newHtml +=	'	<div class="video_article_description">'+ videoEmbeds[newVideoId].article +'</div>';
		newHtml +=	'</div>';
	$j('#videoDescription' + newVideoId).toggleClass('video_highlight');
	$j('#videoPlayer' + rowId).html(newHtml).slideDown('slow');

	//embedCode = escapedEmbed1.replace(targetIdRegEx , videoEmbeds[newVideoId].number);		
	addPlayer(newVideoId);


	oldVideoId = newVideoId;

	/****** Sets the proper height and width of each content frame
	iframewidth = $j('#videoPlayer' + newVideoId + ' .video_iframe_container embed').width();
	contentframewidth = 940 - iframewidth;
	$j('#videoPlayer' + newVideoId + ' .video_content_frame').css("width",contentframewidth);

	iframeheight = $j('#videoPlayer' + newVideoId + ' .video_iframe_container').height();
	contentframeheight = iframeheight - 30;
	$j('#videoPlayer' + newVideoId + ' .video_content_frame').css("height",contentframeheight);
	*******/
}

 
/*--------------------------------------------------------
 *  Run this code after the document has loaded
 --------------------------------------------------------*/

$j(document).ready(function(){ 
//	$j('.videoPlayerSkinOn').removeClass('videoPlayerSkinOn').hide();//This is temporary and should be removed after JSP updated to no longer add videoPlayerSkin to the videoPlayers
	
// if there is a hash string present - play that video on loadup
	var playVideo = location.hash.match(/[\d]+/g);
	if (playVideo !== null)
	{ 
		
		m = $j("#videoDescription" + playVideo).parent().attr("class");
		if (m == "videoSection")
		{
			videoCategory = $j("#videoDescription" + playVideo).parent().attr("id");
			videoCategory = videoCategory.substr(12);
			switchCategory(videoCategory);
			switchVideo(getRowId(playVideo[0]), playVideo[0] );
		}
		else if (m == "topicSection")
		{ 
			switchCategory("2");
			videoTopic = $j("#videoDescription" + playVideo).parent().attr("id");
			videoTopic = videoTopic.substr(12);
			switchTopic(videoTopic);
			switchVideo(getRowId(playVideo[0]), playVideo[0] );
			videoTopic--;
		//	document.getElementById('videoTopicSelector').selectedIndex = videoTopic;
		}
		else if (m == "topicSectionA")
		{
			switchCategory("3");
			videoTopic = $j("#videoDescription" + playVideo).parent().attr("id");
			videoTopic = videoTopic.substr(13);
			switchTopicA(videoTopic);
			switchVideo(getRowId(playVideo[0]), playVideo[0] );
			videoTopic--;
		//	document.getElementById('videoTopicSelector').selectedIndex = videoTopic;
		}
		
	} 
})   	
