
var plugin
var media_loaded
var updateTimeCodesInt
var vid
var main
var video_directory="../video/"

//videoSourceArr=new Array("fabrikam.wmv","delivery_small.wmv")



function mainCanvasLoaded(s) {
	//alert("main loaded")
		
	vid=s.findName("videoElement")
	main=s.findName("mainCanvas")
	plugin = s.getHost()

	getNews(s)
	
	
	vid.addEventListener("downloadProgressChanged", handleDownloadProgressChanged)
	vid.addEventListener("bufferingProgressChanged", handleBufferProgressChanged)
	
	plugin.content.onFullScreenChange = onFullScreenChanged
	
	
	buildImages()
}


function mainCanvasMouseLeave(s) {
	this.mouse_down=0
	clearInterval(mouseCheckInt)
	s.releaseMouseCapture()
}

function onFullScreenChanged(s, e) {	
	
    updateLayout(s,plugin.content.actualWidth, plugin.content.actualHeight)
}


function updateLayout(s,wid,hgt) {
	//alert([s,wid,hgt])
	//s.findName("msg").Text="good "+wid+ " x "+hgt+"."
	
	if(wid>700) {
		s.findName("picsPanel").opacity=0
		s.findName("rssPanel").opacity=0
		s.findName("panelSelector").opacity=0
		
		s.findName("videoPanel")["Canvas.Left"]=-20
		s.findName("videoPanel")["Canvas.Top"]=-20
	
		
		s.findName("videoElement").width=window.screen.width
		s.findName("videoElement").height=window.screen.height
		s.findName("videoElement").Stretch="Fill"
		
		s.findName("videoElementBG").width=window.screen.width
		s.findName("videoElementBG").height=window.screen.height
		
		
		s.findName("bufferMsgBG").width=window.screen.width
		s.findName("bufferMsgBG").height=window.screen.height
		
		
		
	}
	else {
		s.findName("picsPanel").opacity=1
		s.findName("rssPanel").opacity=1
		s.findName("panelSelector").opacity=1	
		
		
		s.findName("videoPanel")["Canvas.Left"]=20
		s.findName("videoPanel")["Canvas.Top"]=20
	
	
		s.findName("videoElement").width=280
		s.findName("videoElement").height=210
		s.findName("videoElement").Stretch="Uniform"
		
		s.findName("videoElementBG").width=280
		s.findName("videoElementBG").height=210
		
		s.findName("bufferMsgBG").width=280
		s.findName("bufferMsgBG").height=210
		
	}
}	



function handleDownloadProgressChanged(s, e) {
	window.status="handle dpc called"
	updateDownloadProgress(s,e)
}



function updateDownloadProgress(s,e) {

	bgBar=s.findName("ToBeLoadedPath")
	progressBar=s.findName("LoadedPath")
	window.status=s.downloadProgress
	progressBar.width = bgBar.width * s.downloadProgress
}



function handleBufferProgressChanged (s,e) {

	s.findName("bufferMsgText").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 (s,e) {

}
*/








function mediaLoaded(s){ 

	//alert("mediaLoaded")	
	media_loaded=true
	vid=s.findName("videoElement")
	main=s.findName("mainCanvas")
	
	updateTimeCodesInt=setInterval("updateTimeCodes()", 40)
}








function mediaEnd(s){ 
	s.stop()
	s.findName("playPauseText").Text="play"
	s.findName("playPauseText")["Canvas.Left"]=12
}




function videoBtn(s,e) {
	
	//click_x=e.getPosition(null).x
	media_loaded=false
	resetPlayHead(s,e)
	
	vid=s.findName("videoElement")
	vid.Source = video_directory+s.name+".wmv"
	
	
	s.findName("playPauseBtn").visibility="visible"
	s.findName("fullScreenBtn").visibility="visible"
	s.findName("LoadedPath").visibility="visible"
	s.findName("muteBtn").visibility="visible"
	
	
}


function muteBtnPress(s,e) {
	if(s.opacity==1) {
		s.opacity=0.25
		s.findName("videoElement").volume=0
	}
	else {
		s.opacity=1
		s.findName("videoElement").volume=1
	}
}


function resetPlayHead(s,e) {

	s.findName("playPauseText").Text="pause"
	s.findName("playPauseText")["Canvas.Left"]=6
	s.findName("Playhead")["Canvas.Left"]=0
	
	s.findName("TimeText").Text=""
	s.findName("TimeText2").Text=""
}




function playPauseBtnPress(sender, args) {

    if(sender.findName("playPauseText").Text=="pause") {
    	sender.findName("videoElement").pause();
    	sender.findName("playPauseText")["Canvas.Left"]=12
    	sender.findName("playPauseText").Text="play"	
    }
    else {
    	sender.findName("videoElement").play();
    	sender.findName("playPauseText")["Canvas.Left"]=6
    	sender.findName("playPauseText").Text="pause"
    }   
}


function progressBarPress(s,e) {
	
	click_x=e.getPosition(null).x-45
	s.findName("playPauseText").Text="pause"
	s.findName("playPauseText")["Canvas.Left"]=6
	
	seekTo(s,click_x)
	me=s.findName("videoElement")
	//me.pause()
	me.play()
}



function seekTo(s,xpos) {

	var me=s.findName("videoElement")
	var mediaDuration = me.naturalDuration.seconds;
	var playheadWidth = s.findName("Playhead").width;
	var playAreaWidth = s.findName("ToBeLoadedPath").width - playheadWidth;
	
	var currentPlayTime = (xpos - playheadWidth / 2) / playAreaWidth * mediaDuration;
	
	if (currentPlayTime > mediaDuration) {
		currentPlayTime = mediaDuration;
	}
	else if (currentPlayTime < 0) {
		currentPlayTime = 0;
	}
	
	var position = me.position;
	position.seconds = currentPlayTime;
	me.position = position;
}






function updateTimeCodes() { 

	if(media_loaded) {
		var currentPosition = 0;
	
	
		var position = vid.position;
	
		//window.status=position

		if (position != null) {
			currentPosition = position.seconds;
		}
	
	
		vidTime1=main.findName("TimeText")
		vidTime2=main.findName("TimeText2")
		
		vidTime1.Text = formatTime(currentPosition * 1000);
		vidTime2.Text = formatTime2(currentPosition );
		
		this.updatePlayhead(currentPosition);
	}
}


function updatePlayhead(time) {
	var offset = 0;
	
	var naturalDuration = vid.naturalDuration;
	
	if (naturalDuration != null) {
		var mediaDuration = naturalDuration.seconds;
		
		var percent = time / mediaDuration;
		
		var playhead_width = main.findName("Playhead").width;
		var playarea_width = main.findName("ToBeLoadedPath").width - playhead_width;
		
		offset = playarea_width * percent;
	}

	main.findName("Playhead")["Canvas.left"] = offset;	
}






function formatTime(time) {
	var timeString = "";
	timeString += Math.floor(time / 60000);
	if (timeString.length == 1) {
		timeString = "0" + timeString;
	}
	timeString += ":";
	var seconds = Math.floor(time / 1000.0) % 60;
	if (seconds < 10) {
		timeString += "0";
	}
	timeString += seconds;
	
	return timeString;
}


function formatTime2(time) {
	
	
	time_num=Math.ceil(vid.naturalDuration.seconds)-time
	
	elapsedM = Math.floor(time_num/60);
	remaining = time_num-(elapsedM*60);

	elapsedS = Math.floor(remaining);
	remaining = time_num-(elapsedS);

	if (elapsedM<10) {
		minutes = "0"+elapsedM.toString();
	} 
	else {
		minutes = elapsedM.toString();
	}
	if (elapsedS<10) {
		seconds = "0"+elapsedS.toString();
	} 
	else {
		seconds = elapsedS.toString();
	}
	
	return minutes+":"+seconds;
}
