
var tpTimer;
var tpDuration = 0;
var tpPosition = 0;
var tpBackupUrl = "";
var tpPlayTO;
var tpStarting = true;
var tpIsHidden = false;
var tpClosed = true;
var tpMP;

window.onbeforeunload = tpCheckRefresh;

// checks for refresh and clears tpTimer if fired

function tpCheckRefresh()
{
	clearInterval(tpTimer);
}

// helper function for getting the "top" coordinate of an object
function tpGetTop(obj)
{
	result = 0;
	while (obj)
	{
		result += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return result;
}

// helper function for getting the "left" coordinate of an object
function tpGetLeft(obj)
{
	result = 0;
	while(obj)
	{
		result += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return result;
}

// dynamically resize an external media element... the top and left
// deltas are adjustments on the current position
function tpResizeMP(divID, height, width, topDelta, leftDelta)
{
	var element = document.getElementById(divID);
	element.style.height = height + "px";
	element.style.width = width + "px";
	element.style.top = topDelta + "px";
	element.style.left = leftDelta + "px";
	element.style.position = "absolute";
	
	tpMP = element;
}

function tpHideDIV() 
{
	tpIsHidden = true;
	document.getElementById("externalDiv").style.visibility = "hidden";
}

// dynamically show a div
function tpShowDIV()
{
	tpIsHidden = false;
	document.getElementById("externalDiv").style.visibility = "visible";
}

var tpPlaying = false;

// handle player transitions for Windows Media, to track when playback ends
function tpWMPStateChanged(playerState)
{
	if (tpClosed) return;
	switch(playerState)
	{
		case 0:
			// "undefined";
			tpCleartpTimer();
			break;
		case 1:
			// "Stopped";
			//alert("play stopped");
			tpPlaying = false;
			tpClearTimer();
			break;
		case 2:
			// "Paused";
			tpPlaying = false;
			tpClearTimer();
			break;
		case 3:
			// "Playing";
			//alert("play started");
			if(tpStarting)
			{
				tpDelayMessage("mediaBegins");
				tpStarting = false;
			}
			else
			{
				tpDelayMessage("playing");
			}
			tpRunTimer();
			tpPlaying = true;
			clearTimeout(tpPlayTO);
			break;
		case 4:
			// "ScanForward";
			break;
		case 5:
			// "ScanReverse";
			break;
		case 6:
			// "Buffering";
			tpPlaying = true;
			tpDelayMessage("buffering");
			clearTimeout(tpPlayTO);
			break;
		case 7:
			// "Waiting";
			break;
		case 8:
			// "Playbackdone";
			tpPlaying = false;
			tpClearTimer();
			tpDelayMessage("setPlayerDone");
			break;
		case 9:
			// "Transitioning";
			tpClearTimer();
			break;
		case 10:
			// "Ready";
			tpPlayTO = setTimeout("tpTimedPlay()", 100);
			break;
		case 11:
			// "Reconnecting";
			tpClearTimer();
			break;
		default:
			tpClearTimer();
			// "invalid"
	}
	
	
}

function tpDelayMessage(message)
{
	setTimeout("tpSendDelayMessage('" + message + "')", 1);
}

function tpSendDelayMessage(message)
{
	tpController.thisMovie("playerwidget").receiveJSMessage(message);
}


function tpTimedPlay()
{	
	if (tpMP.status != 3 || tpMP.status != 6 && tpBackupUrl != "")
	{
		tpMP.URL = tpBackupUrl;
	}
}

// handle setting the URL in Windows Media Player
function tpExtPlayURL(url)
{
	tpClosed = false;
	tpBackupUrl = url;
	tpMP.URL = url;
	tpStarting = true;
	if (tpIsHidden)
	{
		tpShowDIV();
	}
}

// handle seek in Windows Media Player
function tpExtSeek(seekPos)
{
	if(!tpPlaying)
	{
		tpMP.controls.currentPosition = seekPos;
		tpMP.controls.play();
		tpPlaying = true;
		tpDelayMessage("togglePause");
	}
	else
	{
		tpMP.controls.currentPosition = seekPos;
	}
	//tpController.thisMovie("playerwidget").receiveJSMessage("startPlaylist");
	//var currDuration = MediaPlayer.currentMedia.duration;
	//alert(currDuration + "/" + currPosition);
}

function tpRunTimer()
{
	tpClearTimer();
	tpTimer = setInterval("tpReport()", 1000);
}

function tpClearTimer()
{
	clearInterval(tpTimer);
}


function tpReport()
{
	if(tpMP.currentMedia != null)
	{
		tpDuration = tpGetDuration();
		tpPosition = tpGetPosition();
		if (tpPlaying)
		{
			tpController.thisMovie("playerwidget").receiveJSMessage("synchPosition",tpDuration,tpPosition);
		}
	}
}

function tpGetDuration()
{
	if(tpMP.currentMedia.duration > 0)
	{
		return tpMP.currentMedia.duration;
	}
	
}

function tpGetPosition()
{
	return tpMP.controls.currentPosition;
}

function tpExtClose()
{
	tpClosed = true;
	tpMP.controls.close();
	tpBackupUrl = "";
	tpClearTimer();
	if (!tpIsHidden)
	{
		tpHideDIV();
	}
}

function tpExtPauseMovie(state)
{
	if(state)
	{
		tpMP.controls.pause();
	}
	else
	{
		tpMP.controls.play();
	}
}

function tpExtSetSoundLevel(level)
{
	tpMP.settings.volume = level;
}

function tpExtMutePlayer(isMute)
{
	if(!tpPlaying)
	{
		if(isMute == true){
			tpMP.settings.mute = true;
			tpMP.controls.play();
			tpMP.controls.pause();
		}else{
			tpMP.settings.mute = false;
			tpMP.controls.play();
			tpMP.controls.pause();
		}
	}
	else
	{
		if(isMute == true){
			tpMP.settings.mute = true;
			tpMP.controls.pause();
			tpMP.controls.play();
		}else{
			tpMP.settings.mute = false;
			tpMP.controls.pause();
			tpMP.controls.play();
		}	
	}
}


function tpExtFullScreen() 
{
	tpMP.fullScreen=true;
}
