var bcExp;
var modVP;
var modExp;
var modCon;
var modSocial;
function onTemplateLoaded(experienceID) {
	var expr = el('myExperience');
	var expr2 = el('myExperienceHome');
	
	//if((!expr) && (!expr2)) return;
	
	
    bcExp = brightcove.getExperience(experienceID);
    modVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
    modExp = bcExp.getModule(APIModules.EXPERIENCE);
    modCon = bcExp.getModule(APIModules.CONTENT);
	modMenu = bcExp.getModule(APIModules.MENU);
    modSocial = bcExp.getModule(APIModules.SOCIAL);
    modExp.addEventListener(BCExperienceEvent.TEMPLATE_READY, onTemplateReady);
    modExp.addEventListener(BCExperienceEvent.CONTENT_LOAD, onContentLoad);
    modCon.addEventListener(BCContentEvent.VIDEO_LOAD, onVideoLoad); 
	modVP.addEventListener(BCVideoEvent.VIDEO_CHANGE, onVideoChange);
  	modVP.addEventListener(BCVideoEvent.VIDEO_COMPLETE, onVideoComplete);
}
function advanceToNextVideoInLineup() {
	var IDs = (_isMostViewedVideo) ? _mostViewedVideoIDs : _videoIDs;
	var idx;
	var videoID;
	var url;
	if((IDs == null) || (typeof(IDs) == "undefined") || (IDs.length < 1) ) return;
	idx = getVideoIndex(IDs);
	videoID = IDs[idx];
	if (_selectedVideoIdxInLineup < -999) {
		//The video is being watched from an article page if _selectedVideoIdxInLineup is -1000,
		//It is -2000 for the homepage.
		playArticleVideo(videoID);
	} else {
				
		//The video is being watched from the /sections/video page.  Unload the experience
		//and advance to the next video using a page refresh.
		modExp.unload();
		
		url = _url + "?videoId=" + videoID + "&lineupId=" + _lineupId + "&play=now";
		document.location = url;
	}
}
function getVideoIndex(IDs) {
	var currentIdx = (_isMostViewedVideo) ? _mostViewedIndex : _selectedVideoIdxInLineup;
	var idx = 0;
	var size;
	var upperLimit;
	var IDs;
	if (_selectedVideoIdxInLineup < -999) {
		//The video is being watched from an article page if _selectedVideoIdxInLineup is -1000,
		//It is -2000 for the homepage.
		size = IDs.length;
		upperLimit = size - 1;
		for(i=0; i < size; i++) {
			if(_videoId == IDs[i]) {
				idx = (i == upperLimit) ? 0 : i + 1;
				break;
			}
		}		
	} else {
		//A video from /sections/video is being viewed.
		if( (currentIdx + 1) >= IDs.length) {
			idx = 0;
		} else {
			idx = currentIdx + 1;
		}
	}
	return idx;
}
function displayVideoInfo(videoDTO) {
	var vTitle = el("videoTitle");
	var vFullVersion = el("videoFullVersion");
	
	if( _selectedVideoIdxInLineup == -2000) {
		//The video in the homepage has changed.
		if(vTitle) vTitle.innerHTML = '<a href="/video/?videoId=' + videoDTO.id + '&play=now" target="_top">' + videoDTO.displayName + '</a>' ;
		if(vFullVersion) vFullVersion.innerHTML = '<a  href="/video/?videoId=' + videoDTO.id + '&play=now"  target="_top" title="' + videoDTO.displayName + '">+ Full version</a>';
	}
	//el("videoHeadline").innerHTML = videoDTO.displayName;	
	//el("codetext").value = modSocial.getEmbedCode(videoDTO.id);
	//el("codetext").value = videoDTO.FLVFullLengthURL;
}
function el(name) {
	return document.getElementById(name);	
}
function onTemplateReady(evt) {
	if(_videoId == "")  return;
	if(_play == "now") {
		modVP.loadVideo(_videoId);
	} else {
		modVP.cueVideo(_videoId);
	}
}
function onContentLoad(evt) {
	//var video = modVP.getCurrentVideo();
    //alert("INFO: Currently Loaded videoID: " + video.id);
	if(_videoId != "") {
		if(_play == "now") {
			modVP.play();
		}
	}
}
function onVideoChange(evt) {
	var videoDTO = modVP.getCurrentVideo();
	if(_selectedVideoIdxInLineup == -2000) {
		//A video in the homepage has changed.
		displayVideoInfo(videoDTO);
	}
}
function onVideoComplete(evt) {
	advanceToNextVideoInLineup();
}
function onVideoLoad(evt) {
	var id = evt.video.id;
	if(_play == "now") {
		modVP.play();
	}
}
function playArticleVideo(videoId) {
	var currentVideo = modVP.getCurrentVideo();
	//if(currentVideo.id == videoId) return;   This will not work in Firefox for some reason.
	if((currentVideo.id - videoId) == 0) return;
	_play = "now";
	_videoId = videoId;
	modVP.loadVideo(_videoId);
}
function sendEmail() {
	modMenu.showMenuPage("Email");
}