// cbPlayer JavaScript Document

//variable to hold the current time (in milliseconds)
var currentTime;

//variable to tell us the player is loaded
var playerLoaded = false;

// METHOD TO PROPERLY ADDRESS FLASH MOVIE IN FIREFOX AND IE // 
function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName]
	}
	else {
		return document[movieName]
	}
}

function SeekAhead(pos){
    eval('videoSeek(\'' + pos + '\')');
}
//MAKE CALLS TO FLASH PLAYER

// TO PLAY A RELEASE, PASS THE PID, USERNAME, AND PASSWORD //
function videoPlay (releasePID,userName,password,pos,loadAds) {
	//thisMovie('cbPlayer').extPlay(PID,userName,pass);
	if (loadAds == null)
	{
	    loadAds = true;
	}
	
    loadPlayer(loadAds,releasePID,userName,password)
    //tpController.setReleaseURL('http://release.theplatform.com/content.select?pid=' + releasePID + '&amp;UserName=' + userName + '&amp;Password=' + password);
    if (!isNaN(pos))
    {
        if (pos > 0)
        {	
            setTimeout('SeekAhead(' + pos + ')', 3000);	
        }
    }	        
}

// pauseIt IS BOOLEAN - PASS TRUE TO PAUSE, FALSE TO RESUME //
function videoPause (pauseIt) {
	//thisMovie('cbPlayer').extPause(pauseIt);
	if(loadPlayer) {
		tpController.pause(pauseIt);
	}
}

// seekPos IS IN MILLISECONDS (I.E. 1 SECOND = 1000) //
function videoSeek(seekPos) {
	//thisMovie('cbPlayer').extSeek(seekPos);
	if(loadPlayer) {
		tpController.seekToPosition(seekPos);
	}
}

// THE PLAYER WILL CALL onGetVolume AND PASS THE CURRENT VOLUME (0-100) AS A RESULT OF THIS CALL //
// IF THE PLAYER IS MUTED, THE VOLUME RETURNED WILL BE THE RESUME VOLUME (NOT 0) //
function getVolume() {
	//thisMovie('cbPlayer').getVolume();
	//DEPRECATED IN V2
}

// currentTime is updated twice a second //
function getPosition() {
	if(loadPlayer) {
		document.getElementById('infoWindow').value = document.getElementById('infoWindow').value + "current position (in ms) is: " + currentTime + "\n";
		return currentTime;
	}
}


//RECEIVE CALLS FROM FLASH PLAYER

function hello(msg) {
	document.getElementById('infoWindow').value = document.getElementById('infoWindow').value + "message: " + msg + "\n";
}

// CALLED WHEN RELEASE IS STOPPED (PAUSED) //
function onVideoStop(evt) {
	document.getElementById('infoWindow').value = document.getElementById('infoWindow').value + "EVENT: VIDEO STOPPED \n";
}

// CALLED WHEN RELEASE STARTS AND WHEN IT RESUMES FROM PAUSE //
function onVideoStart(evt) {
	document.getElementById('infoWindow').value = document.getElementById('infoWindow').value + "EVENT: VIDEO STARTED\n";
}

// CALLED WHEN RELEASE ENDS //
function onVideoEnd(evt) {
	document.getElementById('infoWindow').value = document.getElementById('infoWindow').value + "EVENT: VIDEO ENDED \n";
}


//OTHER TPCONTROLLER EVENT RESPONDERS
function onSetReleaseUrl(evt) {
	document.getElementById('infoWindow').value = document.getElementById('infoWindow').value + "OnSetReleaseUrl: " + playerLoaded + "\n";
}

function onMediaPlaying(evt) {
//	document.getElementById('infoWindow').value = document.getElementById('infoWindow').value + "currentTimeAggregate: " + evt.data.currentTimeAggregate + "\n";
//	document.getElementById('infoWindow').value = document.getElementById('infoWindow').value + "currentTime: " + evt.data.currentTime + "\n";
	if(evt.data.currentTimeAggregate != undefined) {
		currentTime = Math.round(evt.data.currentTimeAggregate);
	}
	else {
		currentTime = Math.round(evt.data.currentTime);
	}
	//document.getElementById('infoWindow').value = document.getElementById('infoWindow').value + "currentTime: " + currentTime + "\n";
}

function onPlayerLoaded(evt) {
	playerLoaded = true;
	document.getElementById('infoWindow').value = document.getElementById('infoWindow').value + "OnPlayerLoaded: " + playerLoaded + "\n";
}