var videoRatioHW
var flashRatioHW=800/600
var flashWidth
var flashHeight
var flashScale
var flashY
var flashX
var videoWidth
var timeoutID 
var flashBorder_Width = 8
var PreviewFrame = false


function initVideo(){
	window.flashMovieRunning=false
	
	//read parameter from html?emisiondirecto=true/false
	if (document.location.search.substr(1).split("=")[1]=="true"){
		window.emisionEnDirecto = true;
	}else{
		window.emisionEnDirecto = false;
	}
	
	//Get ID conferencia
	strLocation = String(document.location);
	
	strSplit = strLocation.split("_Conferencias/");
	
	if (strSplit[1]!=undefined){
		strIDConference=strSplit[1].substring(0,7);
	}else{
		strIDConference="-1"
	}
		
		//Remove leading zeros
		while (strIDConference.charAt(0)=="0"){
			strIDConference=strIDConference.substr(1,strIDConference.length);
		}
		window.IDConferencia=strIDConference	
	
	//start video
	if (window.IDConferencia=="-1"){
		MediaPlayer.URL="local.asx"; //From cd/hdd
	}else{
		if (window.emisionEnDirecto==true){
			MediaPlayer.URL="conferencia.asx"; //directo
		}else{
			MediaPlayer.URL="diferido.asx"; //diferido
		}
	}
	
	MediaPlayer.controls.Play();
}

function init(){
	window.onresize=resize;
	window.setInterval("updateVideoInfoToFlash()",1000);
	window.flashMovieRunning=true
}

function resize(){

	window.clearTimeout(timeoutID);
	timeoutID = window.setTimeout("setVideoSize();", 200);
}



function setVideoSize() {
		
	var bodyWidth=document.body.clientWidth;
	var bodyHeight=document.body.clientHeight;
	var bodyRatioHW=bodyWidth/bodyHeight;

	MediaPlayer.uiMode="invisible"

	moveFlash(20,20,bodyWidth-40,bodyHeight-40);


	flashX=(bodyWidth/2)-(flashWidth/2);
	flashY=(bodyHeight/2)-(flashHeight/2);

	flashScale=flashWidth/800;
	FlashMovie.setVariable("JS_Scale",String(1/flashScale));
	FlashMovie.ReDraw(flashX,flashY);

	var targetVideoX=flashX + flashScale*flashBorder_Width;
	var targetVideoY=flashY + flashScale*65; //65=logoheight in flash
	var targetVideoWidth=videoWidth*flashScale;
	var targetVideoHeight=(targetVideoWidth/videoRatioHW);
	moveVideo (targetVideoX,targetVideoY,targetVideoWidth+1,targetVideoHeight+1);
	
	window.setTimeout("MediaPlayer.uiMode='none';", 5000);
	
}

function moveVideo(x,y,w,h){
	divVideo=document.getElementById("MediaLayer").style;
	divVideo.left=Math.round(x);
	divVideo.top=Math.round(y);
	divVideo.width=Math.round(w);
	divVideo.height=Math.round(h);
}

function moveFlash(x,y,w,h){
	divFlash=document.getElementById("FlashLayer");
	divFlash.style.left=Math.round(x);
	divFlash.style.top=Math.round(y);
	divFlash.style.width=Math.round(w);
	divFlash.style.height=Math.round(h);
		
	if ((w/h)>flashRatioHW){
		flashHeight=h;
		flashWidth=h*flashRatioHW;
	}else{
		flashWidth=w;
		flashHeight=w/flashRatioHW;
	}
}

function videoWidth_changed(newWidth){
	videoWidth=newWidth;
	setVideoSize();
}


// Control Video //

function video_play(){
	MediaPlayer.controls.Play();
	PreviewFrame=false
	showVideo();
	updateVideoInfoToFlash();
}

function video_stop(){
	MediaPlayer.controls.Stop();
	hideVideo();
	MediaPlayer.controls.currentPosition=0;
}

function video_pause(){
	MediaPlayer.controls.pause();
	updateVideoInfoToFlash();
}

function set_volume(vol){
	MediaPlayer.settings.volume=vol;
	updateVideoInfoToFlash();
}

function goto_position(pos){
	MediaPlayer.controls.currentPosition = pos;
	updateVideoInfoToFlash();
	if (MediaPlayer.playState!=3) {
		//Mediaplayer not playing. We play a bit until we get a heartbeat from the stream and then pause
		PreviewFrame=true
		MediaPlayer.controls.Play();
	}
}

function hideVideo(){
	divVideo=document.getElementById("MediaLayer").style;
	divVideo.visibility='hidden'
}

function showVideo(){
	divVideo=document.getElementById("MediaLayer").style;
	divVideo.visibility='visible'
}


function updateVideoInfoToFlash() {

	if (PreviewFrame==true){
		return;
	}

	FlashMovie.updateVideo(MediaPlayer.playState,Math.floor(MediaPlayer.controls.currentPosition),MediaPlayer.settings.Mute,Math.floor(MediaPlayer.settings.volume),MediaPlayer.network.bufferingProgress);
}

function HeartBeat(bstrparam){
	if (PreviewFrame==true){
		MediaPlayer.controls.pause();
		PreviewFrame=false;
		updateVideoInfoToFlash();
	}

	FlashMovie.HeartBeat(bstrparam);
}

function writeFlashCode(){
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="100%" height="100%" id="FlashMovie" align="middle">')
	document.write('<param name="allowScriptAccess" value="sameDomain" />')
	document.write('<param name="movie" value="ConferenceScreen.swf?mainColor=' + FlashColor + '"/>')
	document.write('<param name="quality" value="high" />')
	document.write('<param name="wmode" value="transparent">')
	document.write('<param name="bgcolor" value="#FFFFFF"/>')
	document.write('<param name="scale" value="showAll" />')
	document.write('<embed src="ConferenceScreen.swf?mainColor=' + FlashColor + '" quality="high" wmode="transparent" bgcolor="#FFFFFF" scale="showAll" width="100%" height="100%" name="FlashMovie" align="middle" allowscriptaccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />')
	document.write('</object>')
}


function writeWMPCode(){
	document.write('<object classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" id="MediaPlayer" width=100% height=100%>')
	document.write('<param name="stretchToFit" value="true">')
	document.write('<param name="autoStart" value="true">')
	document.write('<param name="uiMode" value="none">')
	document.write('<param name="URL" value="">')
	document.write('<param name="enableContextMenu" value="false">')
	document.write('</object>')
}
