var _bcPlayer;
var _bcExperience;
var _bcVideo;
var _bcExperienceId;
var _bcVideoId = "";
var _bcVideoTitle = "";
var _bcVideoDuration;
var _bcStartPosition;
var _bcCurrentPosition;
var _bcNowPlaying = false;
var _bcNowTracking = false;

function onTemplateLoaded(pEvent) {
	s.account = "jstreamnejp";
	s.visitorNamespace = "jstream";
	s.charSet = "UTF-8";
	s.currencyCode = "JPY";
	s.trackClickMap = true;
	s.debugTracking = true;
	s.trackLocal = true;
	_bcPlayer = bcPlayer.getExperience(pEvent);
	_bcVideo = _bcPlayer.getModule(APIModules.VIDEO_PLAYER);
	_bcExperience = _bcPlayer.getModule(APIModules.EXPERIENCE);
	if(_bcVideo) {
		_bcVideo.addEventListener("streamStart", onVideoEvent);
		_bcVideo.addEventListener("videoComplete", onVideoEvent);
		_bcVideo.addEventListener("videoStart", onVideoEvent);
		_bcVideo.addEventListener("videoStop", onVideoEvent);
		_bcVideo.addEventListener("videoProgress", onVideoEvent);
	}
}

function onVideoEvent(event) {
	if (event.type == "videoProgress") {
		_bcCurrentPosition = Math.round(event.position);
	} else if (event.type == "videoStart" ) {
		var video = _bcVideo.getCurrentVideo();
		if (_bcNowTracking && _bcVideoId != video.id) {
			s.Media.close(_bcVideoTitle);
			_bcNowTracking = false;
		}
		_bcExperienceId  = _bcExperience.getExperienceID();
		_bcVideoId       = video.id;
		_bcVideoTitle    = video.displayName;
		_bcVideoDuration = Math.round(video.length/1000);
		_bcCurrentPosition  = Math.round(event.position);
		_bcStartPosition    = _bcCurrentPosition;
		if (!_bcNowTracking) {
			_bcNowTracking = true;
			s.Media.open(_bcVideoTitle,_bcVideoDuration,_bcExperienceId);
			s.Media.trackVars="prop3";
			s.prop3=_bcVideoTitle + " start";
		}
		_bcNowPlaying = true;
		s.Media.play(_bcVideoTitle,_bcCurrentPosition);
		trace("s.Media.play " + _bcVideoTitle + " " + _bcCurrentPosition);
	} else if (event.type == "videoStop" ) {
		if (_bcNowPlaying) {
			s.Media.stop(_bcVideoTitle,_bcCurrentPosition);
			_bcNowPlaying = false;
		}
	} else if (event.type == "videoComplete" ) {
		if (_bcNowTracking) {
			s.Media.close(_bcVideoTitle);
			_bcNowTracking = false;
		}
	}
}

