var Player;
var currentVolume = 90;
var fullScreen = false;
var fPlayAR = false;
var start = false;
var currentPosition = 0;
var currentState = null; 
var previousState = null;

var AjaxEnviar = false;
var AjaxEmbed = false;
var AjaxLink = false;

function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
		window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}
}

addLoadEvent('mpl');

function playerReady(thePlayer) {
    Player = document.getElementById(thePlayer.id);
    addListeners();
}

function addListeners()
{
	if (Player)
	{
		Player.addModelListener("STATE", "stateListener");
		Player.addControllerListener("MUTE", " muteListener");
		Player.addControllerListener("VOLUME", "volumeListener");
		Player.addViewListener("FULLSCREEN", "fullScreenListener");
		Player.addViewListener("SEEK", "seekListener");
		Player.addModelListener("TIME", "timeMonitor");
	} 
	else 
	{
		setTimeout("addListeners()",100);
	}
}

function timeMonitor(obj)
{
	currentPosition = obj.position;
}

function seekListener(obj)
{
	if (obj.position > currentPosition)
		_ATM.AR.video.forward(TagATM_Canal, TagATM_Titulo);	
	else
		_ATM.AR.video.rewind(TagATM_Canal, TagATM_Titulo);
	
	currentPosition = obj.position;
	
	if (currentState == 'IDLE' || currentState == 'PAUSED')
		fPlayAR = true;
	else
		fPlayAR = false;	
}

function fullScreenListener(obj)
{
	_ATM.AR.video.fullscreenOn(TagATM_Canal,TagATM_Titulo);
	fullScreen = true;	
}

function muteListener(obj)
{ 
    currentMute = obj.state; 
	if (currentMute) 
		_ATM.AR.video.mute(TagATM_Canal, TagATM_Titulo);
	else
		_ATM.AR.video.unmute(TagATM_Canal, TagATM_Titulo);	

}

function volumeListener(obj)
{ 
	if (obj.percentage > currentVolume)
		_ATM.AR.video.volumeUp(TagATM_Canal,TagATM_Titulo);
	else
		_ATM.AR.video.volumeDown(TagATM_Canal,TagATM_Titulo);
    currentVolume = obj.percentage; 
}

function stateListener(obj)
{
	currentState = obj.newstate; 
	previousState = obj.oldstate;

	//start
	if (!start) {
	    _ATM.AR.video.start(TagATM_Canal, TagATM_Titulo);
	    start = true;
	}
	    
	//play
	if (currentState == 'PLAYING') 
	{
		if (fPlayAR)
			_ATM.AR.video.play(TagATM_Canal, TagATM_Titulo);
		fPlayAR = false;
	}
	//buffering
	if (currentState == 'BUFFERING')
	{
		if (fPlayAR)
			_ATM.AR.video.play(TagATM_Canal, TagATM_Titulo);
		fPlayAR = false;
	}
	//pause
	if (currentState == 'PAUSED')
	{
		_ATM.AR.video.pause(TagATM_Canal, TagATM_Titulo);
		fPlayAR = true;
	}
	//stop
	if (currentState == 'IDLE')
	{
		if (previousState == 'PLAYING' || previousState == 'BUFFERING' || previousState == 'PAUSED')
			_ATM.AR.video.stop(TagATM_Canal, TagATM_Titulo);
		fPlayAR = true;
	}
	//finish
	if (currentState == 'COMPLETED')
	{
		_ATM.AR.video.finish(TagATM_Canal, TagATM_Titulo);
		fPlayAR = true;
	}
}
