var progressive;
var streaming;
var playType;
var appName;
var vidInput;
var vidTitle;
var videoWidth;
var videoHeight;
var autoPlay;
var buffLength;
var ccSource;	
var ccAtStart;
var ccPosition;
var ccTextSize;
var windowMode;
var bgColorHex;
var stageWidth;
var stageHeight;
	
// JavaScript Document
function setFocus(objID, milisecs)
{
	window.document.getElementById(objID).focus();
	clearTimeout(myTimer);
}
function embeddVideoplayer(playType, appName, vidInput, vidTitle, autoPlay, videoWidth, videoHeight, buffLength, ccSource, ccAtStart, ccPosition, ccTextSize, windowMode, bgColorHex)
{
	var stageWidth = getStageWidth(videoWidth);
	var stageHeight = getStageHeight(videoHeight);
	
	document.writeln("<script language='javascript'>");	
	document.writeln("	if (AC_FL_RunContent == 0) {");
	document.writeln("		alert('This page requires AC_RunActiveContent.js.');");
	document.writeln("	} else {");
	document.writeln("		AC_FL_RunContent(");
	document.writeln("			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0',");
	document.writeln("			'width', '" + stageWidth + "',");
	document.writeln("			'height', '" + stageHeight + "',");
	document.writeln("			'src', 'http://www.csupomona.edu/media/ezplayer_v2/swf/ez_player',");
	document.writeln("			'quality', 'high',");
	document.writeln("			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',");
	document.writeln("			'align', 'left',");
	document.writeln("			'play', 'true',");
	document.writeln("			'loop', 'true',");
	document.writeln("			'scale', 'noscale',");
	document.writeln("			'wmode', '" + windowMode + "',");
	document.writeln("			'devicefont', 'false',");
	document.writeln("			'id', 'ez_player',");
	document.writeln("			'bgcolor', '" + bgColorHex + "',");
	document.writeln("			'name', 'ez_player',");
	document.writeln("			'menu', 'true',");
	document.writeln("			'allowFullScreen', 'true',");
	document.writeln("			'allowScriptAccess','sameDomain',");
	document.writeln("			'movie', 'http://www.csupomona.edu/media/ezplayer_v2/swf/ez_player',");
	document.writeln("			'salign', 'lt',");
	document.writeln("			'flashvars', 'playType=" + playType + "&appName=" + appName + "&vidInput=" + vidInput + "&vidTitle=" + vidTitle + "&autoPlay=" + autoPlay + "&videoWidth=" + videoWidth + "&videoHeight=" + videoHeight + "&buffLength=" + buffLength + "&ccSource=" + ccSource + "&ccAtStart=" + ccAtStart + "&ccPosition=" + ccPosition + "&ccTextSize=" + ccTextSize + "'");
	document.writeln("			); //end AC code");
	document.writeln("	}");
	document.writeln("</script>");
}
function generateCodeAndPreview()
{
	initVariables();
	
	window.document.getElementById("generatedCode").value = generateEmbeddCode();	
	window.document.getElementById("movieSize").innerHTML = "This embedded Flash movie will use a space of: <strong>" + stageWidth + "px width &amp; " + stageHeight + "px height</strong> ";	
	generatePreview();
	
}
function initVariables()
{
	/*progressive = window.document.getElementById("StreamType_0").checked;
	streaming = window.document.getElementById("StreamType_1").checked;
	
	playType = streaming ? "streaming" : "progressive";*/
	for(var i=0; i<window.document.StreamType_form.StreamType.length;i++)
	{
		if(window.document.StreamType_form.StreamType[i].checked)
		{
			playType = window.document.StreamType_form.StreamType[i].value;
		}
	}
	switch(playType)
	{
		case "streaming_calpolypomona":
			appName = window.document.getElementById("appName").value;
			ccSource = "http://www.csupomona.edu/media/closedcaptions/" + appName + "/" + window.document.getElementById("ccSourceInput").value;
			break;
		case "streaming_proprietary":
			//concatinate the final app location string but if already has a trailing "/" or "\" then don't apend it
			var server = window.document.getElementById("serverAddress").value;
			appName = window.document.getElementById("serverAddress").value + "/" + window.document.getElementById("appName").value;
			if ( (server.lastIndexOf("/") == server.length-1) || (server.lastIndexOf("\\") == server.length-1) )
			{
				appName = window.document.getElementById("serverAddress").value + window.document.getElementById("appName").value;
			}
			ccSource = window.document.getElementById("ccSourceInput").value;
			break;
		default:
			appName = "";
			ccSource = window.document.getElementById("ccSourceInput").value;
			break;
	}
	
	vidInput = window.document.getElementById("vidInput").value;
	vidTitle = escape(window.document.getElementById("vidTitle").value);
	videoWidth = window.document.getElementById("videoWidth").value;
	videoHeight = window.document.getElementById("videoHeight").value;
	autoPlay = window.document.getElementById("autoPlay").value;
	buffLength = window.document.getElementById("buffLength").value;
	ccAtStart = window.document.getElementById("ccAtStart").value;
	ccPosition = window.document.getElementById("ccPosition").value;
	ccTextSize = window.document.getElementById("ccTextSize").value;
	windowMode = window.document.getElementById("windowMode").value;
	bgColorHex = window.document.getElementById("bgColorHex").value;
	
	stageWidth = getStageWidth(videoWidth);
	stageHeight = getStageHeight(videoHeight);
}
function generateSample()
{
	window.document.getElementById("StreamType_0").checked = true;
	window.document.getElementById("StreamType_1").checked = false;
	window.document.getElementById("StreamType_2").checked = false;
	
	window.document.getElementById("appName").value = "";
	window.document.getElementById("vidInput").value = "http://www.csupomona.edu/media/ezplayer_v2/swf/ezplayer_intro.m4v";
	window.document.getElementById("vidTitle").value = "Welcome to the EZPlayer v2.0";
	window.document.getElementById("videoWidth").value = "480";
	window.document.getElementById("videoHeight").value = "324";
	window.document.getElementById("autoPlay").value = "true";
	window.document.getElementById("buffLength").value = "5";
	window.document.getElementById("ccSourceInput").value = "http://www.csupomona.edu/media/ezplayer_v2/swf/ezplayer.dfxp.xml";	
	window.document.getElementById("ccAtStart").value = "false";
	window.document.getElementById("ccPosition").value = "bottom";
	window.document.getElementById("ccTextSize").value = "12";
	window.document.getElementById("windowMode").value = "window";
	window.document.getElementById("bgColorHex").value = "#FFFFFF";	
	
	toggleStreamingOption(playType)
	generateCodeAndPreview()
}
function generatePreview()
{	
	initVariables();
	
	var code = "";
	code += "	<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0' width='" + stageWidth + "' height='" + stageHeight + "' id='ez_player' align='left'>\n";
	code += "	<param name='allowScriptAccess' value='sameDomain' />\n";
	code += "	<param name='allowFullScreen' value='true' />\n";
	code += "	<param name='movie' value='http://www.csupomona.edu/media/ezplayer_v2/swf/ez_player.swf' />\n";
	code += "	<param name='quality' value='high' />\n";
	code += "	<param name='salign' value='lt' />\n";
	code += "	<param name='scale' value='noscale' />\n";
	code += "	<param name='wmode' value='" + windowMode + "' />\n";
	code += "	<param name='bgcolor' value='" + bgColorHex + "' />\n";
	code += "	<param name='flashvars' value='playType=" + playType + "&appName=" + appName + "&vidInput=" + vidInput + "&vidTitle=" + vidTitle + "&autoPlay=" + autoPlay + "&videoWidth=" + videoWidth + "&videoHeight=" + videoHeight + "&buffLength=" + buffLength + "&ccSource=" + ccSource + "&ccAtStart=" + ccAtStart + "&ccPosition=" + ccPosition + "&ccTextSize=" + ccTextSize + "' />\n";
	code += "	<embed src='http://www.csupomona.edu/media/ezplayer_v2/swf/ez_player.swf' quality='high' salign='lt' scale='noscale' wmode='" + windowMode + "' bgcolor='" + bgColorHex + "' width='" + stageWidth + "' height='" + stageHeight + "' name='ez_player' align='left' allowScriptAccess='sameDomain' allowFullScreen='true' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' flashvars='playType=" + playType + "&appName=" + appName + "&vidInput=" + vidInput + "&vidTitle=" + vidTitle + "&autoPlay=" + autoPlay + "&videoWidth=" + videoWidth + "&videoHeight=" + videoHeight + "&buffLength=" + buffLength + "&ccSource=" + ccSource + "&ccAtStart=" + ccAtStart + "&ccPosition=" + ccPosition + "&ccTextSize=" + ccTextSize + "' />\n";
	code += "	</object>\n";
	
	window.document.getElementById("preview").innerHTML = code;
	var sURL = unescape(window.location.pathname);
	window.document.location.href = sURL + "#preview";
	//window.location.reload(false);
}
function generateEmbeddCode()
{
	initVariables();
	
	var code = "";
	code += "<table border='0' width='" + stageWidth + "' height='" + stageHeight + "' cellspacing='0' cellpadding='0'>\n";
	code += "<tr><td>\n";
	code += "<script language='javascript'>AC_FL_RunContent = 0;</script>\n";
	code += "<script src='http://www.csupomona.edu/media/ezplayer_v2/js/AC_RunActiveContent.js' language='javascript'></script>\n";
	code += "<script src='http://www.csupomona.edu/media/ezplayer_v2/js/functions.js' language='javascript'></script>\n";
	code += "<script language='javascript' type='text/javascript'>\n";
	code += "embeddVideoplayer('" + playType + "','" + appName + "','" + vidInput + "','" +  vidTitle + "','" + autoPlay + "','" + videoWidth + "','" + videoHeight + "','" + buffLength + "','" + ccSource + "','" + ccAtStart + "','" + ccPosition + "','" + ccTextSize + "','" + windowMode + "','" + bgColorHex + "');\n";
	code += "</script>\n";

	code += "<noscript>\n";
	code += "	<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0' width='" + stageWidth + "' height='" + stageHeight + "' id='ez_player' align='left'>\n";
	code += "	<param name='allowScriptAccess' value='sameDomain' />\n";
	code += "	<param name='allowFullScreen' value='true' />\n";
	code += "	<param name='movie' value='http://www.csupomona.edu/media/ezplayer_v2/swf/ez_player.swf' />\n";
	code += "	<param name='quality' value='high' />\n";
	code += "	<param name='scale' value='noscale' />\n";
	code += "	<param name='wmode' value='" + windowMode + "' />\n";
	code += "	<param name='bgcolor' value='" + bgColorHex + "' />\n";
	code += "	<param name='flashvars' value='playType=" + playType + "&appName=" + appName + "&vidInput=" + vidInput + "&vidTitle=" + vidTitle + "&autoPlay=" + autoPlay + "&videoWidth=" + videoWidth + "&videoHeight=" + videoHeight + "&buffLength=" + buffLength + "&ccSource=" + ccSource + "&ccAtStart=" + ccAtStart + "&ccPosition=" + ccPosition + "&ccTextSize=" + ccTextSize + "' />\n";
	code += "	<embed src='http://www.csupomona.edu/media/ezplayer_v2/swf/ez_player.swf' quality='high' salign='lt' scale='noscale' wmode='" + windowMode + "' bgcolor='" + bgColorHex + "' width='" + stageWidth + "' height='" + stageHeight + "' name='ez_player' align='left' allowScriptAccess='sameDomain' allowFullScreen='true' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' flashvars='playType=" + playType + "&appName=" + appName + "&vidInput=" + vidInput + "&vidTitle=" + vidTitle + "&autoPlay=" + autoPlay + "&videoWidth=" + videoWidth + "&videoHeight=" + videoHeight + "&buffLength=" + buffLength + "&ccSource=" + ccSource + "&ccAtStart=" + ccAtStart + "&ccPosition=" + ccPosition + "&ccTextSize=" + ccTextSize + "' />\n";
	code += "	</object>\n";
	code += "</noscript>";
	code += "</tr></td>\n";
	code += "</table>\n";
	return code;
}
function getStageWidth(val)
{
	var stageWidth = Number(val) + 8;
	return stageWidth;
}
function getStageHeight(val)
{
	var stageHeight = Number(val) + 34
	return stageHeight;
}

/************************************************************************************************************************/
/*									toggle effects functions for the interface											*/
/************************************************************************************************************************/
function toggleStreamingOption(val)
{
	if(val == "streaming_calpolypomona")
	{
		window.document.getElementById("videoFile_text").innerHTML = "video filename:";
		window.document.getElementById("streaming_text").innerHTML = "cal poly pomona FMS 2 streaming";
		window.document.getElementById("streaming_server_field").style.display = "none";
		window.document.getElementById("streaming_server_field").style.streaming_app_field = "hidden";
		window.document.getElementById("streaming_app_field").style.display = "table-row";
		window.document.getElementById("streaming_app_field").style.streaming_app_field = "visible";
		window.document.getElementById("ccFile_text").innerHTML = "captions filename (<strong><span style=\"color:#FF0000\">NO</span></strong> URL):";
	}
	else if(val == "streaming_proprietary")
	{
		window.document.getElementById("videoFile_text").innerHTML = "video filename:";
		window.document.getElementById("streaming_text").innerHTML = "proprietary server streaming";
		window.document.getElementById("streaming_server_field").style.display = "table-row";
		window.document.getElementById("streaming_server_field").style.streaming_app_field = "visible";
		window.document.getElementById("streaming_app_field").style.display = "table-row";
		window.document.getElementById("streaming_app_field").style.streaming_app_field = "visible";
		window.document.getElementById("ccFile_text").innerHTML = "captions complete URL:";
	}
	else
	{
		window.document.getElementById("videoFile_text").innerHTML = "video path (<strong>complete url</strong>):";
		window.document.getElementById("streaming_text").innerHTML = "progressive";
		window.document.getElementById("streaming_server_field").style.display = "none";
		window.document.getElementById("streaming_server_field").style.streaming_app_field = "hidden";
		window.document.getElementById("streaming_app_field").style.display = "none";
		window.document.getElementById("streaming_app_field").style.streaming_app_field = "hidden";
		window.document.getElementById("ccFile_text").innerHTML = "captions complete URL:";
	}
}

