var plugin
var main

var updateTimeCodesInt
var controlsTimer
var checkMediaInt

var objsArr=new Array()
var _root_xmouse=0
var _root_ymouse=0

var media_fully_downloaded=false

window.onresize = resize;

checkMediaInt=setInterval("checkMedia()",100);

function mainCanvasLoaded(s) {

	//alert("main loaded")
	
	main=s.findName("mainCanvas");
	main.opacity=0 //hide while resizing
	plugin = s.getHost();
	
	objsArr["Playhead"]=new Object();
	objsArr["Playhead"].el=s.findName("Playhead");
	
	objsArr["volumeSlider"]=new Object();
	objsArr["volumeSlider"].el=s.findName("volumeSlider");
	
	vid=main.findName("videoElement");
	dpcToken=vid.addEventListener("downloadProgressChanged", updateDownloadProgress);
	vid.addEventListener("bufferingProgressChanged", handleBufferProgressChanged);
		
	plugin.content.onResize = resize;
}


function updateDownloadProgress(s,e) {
	bgBar=s.findName("ToBeLoadedPath");
	progressBar=s.findName("LoadedPath");
	//window.status=s.downloadProgress
	if (progressBar) progressBar.width = bgBar.width * s.downloadProgress;
	if(s.downloadProgress==1) {
		media_fully_downloaded=true;
	}
}

function checkMedia() {

	if(media_fully_downloaded) {
		
		var ph=main.findName("Playhead");
		if (ph) {
			ph.addEventListener("mouseLeftButtonDown", PlayheadPress);
			ph.addEventListener("mouseLeftButtonUp", PlayheadRelease);
			ph.addEventListener("mouseEnter", PlayheadEnter);
		}
		
		var lp=main.findName("LoadedPath");
		if (lp) {
			lp.addEventListener("mouseLeftButtonDown", progressBarPress);
			lp.addEventListener("mouseEnter", PlayheadEnter);
		}
		
		clearInterval(checkMediaInt);
	}
}

function handleBufferProgressChanged (s,e) {
	var bufMsg=s.findName("bufferMsgText");
	if (bufMsg) {
		bufMsg.Text="Buffering Video: "+Math.floor(s.bufferingProgress*100)+"%";
		if(s.bufferingProgress >0 && s.bufferingProgress <1) {
			s.findName("bufferMsg").opacity=0.8;
		}
		else {
			s.findName("bufferMsg").opacity=0;
		}
	}		
}

/*
function mediaLoaded(s){ 

	alert("mediaLoaded")	
	
	vid=s.findName("videoElement")
	resize()
	
	updateTimeCodesInt=setInterval("updateTimeCodes()", 100)
}
*/

/*
function mediaEnd(s){ 
	s.stop()
	togglePlayPauseBtn(s.findName("thePlayBtn"))
}
*/

function mainCanvasMouseLeave(s) {
	clearInterval(objsArr["Playhead"].playHeadInt);
	clearInterval(mouseCheckerVolumeInt);
	setControlsInvisible();
	s.releaseMouseCapture();
}


function whenMouseMoves(s,e) {
	var pt = e.getPosition(null);
	_root_xmouse = pt.x;
	_root_ymouse = pt.y;	
	//putDebug('Mouse: '+_root_xmouse+','+_root_ymouse);
	s.findName("PlayControls").opacity = 1;
	//clearTimeout ( controlsTimer );
	//controlsTimer = setTimeout("setControlsInvisible()",1200)
}


function setControlsInvisible() {
	clearTimeout ( controlsTimer );
	//if(_root_ymouse<plugin.content.actualHeight-100) {
		main.findName("PlayControls").opacity = 0;
	//}
	//else main.findName("PlayControls").opacity = 1
}



