var movieName = "gwAFBWindow";
var frameJump = 80;

function renderPlayerWindow()
{
	document.write('<div id="flashcontent" style="width: 320px; height: 225px; border: 4px solid"><br>Flash Player not available either because Javascript is disabled or because Flash is not installed or enabled</div></center>');
}

function renderCommandText()
{
	browsername=navigator.userAgent;
	if (browsername.indexOf("Firefox")!=-1)
		{document.write("<center><p>Quick Keys: Play/Pause (ALT-SHIFT-P), Stop (ALT-SHIFT-X), Forward (ALT-SHIFT-W), Rewind (ALT-SHIFT-Q)</p></center>");}
	else 
		{document.write("<center><p>Quick Keys: Play/Pause (ALT-P), Stop (ALT-X), Forward (ALT-W), Rewind (ALT-Q)</p></center>");}
}

function renderProgressBar()
{
	document.write('<center><DIV STYLE="width: 320px; height: 10px; border: 1px #BDB2D6 solid; text-align: left;"><DIV ID="progressBar" class="ProgressBar">&nbsp;</DIV></DIV></center><center><div><br/><INPUT name="text" TYPE="text" ID="progressText" STYLE="border: none" TITLE="Progress Text" VALUE="Downloading" READONLY><INPUT name="text" TYPE="text" ID="f_percent" STYLE="border: none; width: 60px" TITLE="Percentage Done" VALUE="0" READONLY>%</div></center>');
}

function renderPlayerControls()
{
	document.write('<IMG SRC="http://www.afb.org/media/dummy_box.gif" STYLE="position: absolute; left: -1000px; width: 0px; height: 0px;" onLoad="checkProgress();"><center><P><INPUT TYPE="BUTTON" ID="playButton" VALUE="Play" ONCLICK="playMovie()" DISABLED="true" ACCESSKEY="P"> &nbsp; <INPUT CLASS="button" TYPE="BUTTON" ID="stopButton" VALUE="Stop" DISABLED="true" ONCLICK="stopMovie()" ACCESSKEY="X"> &nbsp; <INPUT CLASS="button" TYPE="BUTTON" ID="rewindButton" DISABLED="true" VALUE="Rewind" ONCLICK="backwardMovie()" ACCESSKEY="Q"> &nbsp; <INPUT CLASS="button" TYPE="BUTTON" ID="forwardButton" DISABLED="true" VALUE="Forward" ONCLICK="forwardMovie()" ACCESSKEY="W"></P><INPUT TYPE="hidden" ID="playStatus" VALUE="0"></center>');
}

function enableButtons()
{
	document.getElementById('progressText').value="Total Movie Progress";
	document.getElementById('playButton').disabled=false;
	document.getElementById('stopButton').disabled=false;
	document.getElementById('rewindButton').disabled=false;
	document.getElementById('forwardButton').disabled=false;
}

function movieObj(movieName)
{
    // IE and Mozilla refer to the movie object differently.
    if (navigator.appName.indexOf ("Microsoft") != -1)
    {
        return window[movieName];
    }
    else
    {
        return document[movieName];
    }
}

function movieIsLoaded (theMovie)
{
    if (typeof(theMovie) != "undefined")
    {
		return theMovie.PercentLoaded() == 100;
	}
    else
    {
		return false;
    }
}

function playMovie()
{
	if (movieIsLoaded(movieObj(movieName)))
    {
       	if (document.getElementById('playStatus').value == 0)
        {
			document.getElementById('playStatus').value = 1;
            document.getElementById('playButton').value = "Pause";
			movieObj(movieName).Play();
        }
        else
        {
            document.getElementById('playStatus').value = 0;
            document.getElementById('playButton').value = "Play";
            movieObj(movieName).StopPlay();
        }

    }
}

function stopMovie()
{
    if (movieIsLoaded(movieObj(movieName)))
    {
		movieObj(movieName).StopPlay();
		movieObj(movieName).GotoFrame(0);
        document.getElementById('playStatus').value = 0;
        document.getElementById('playButton').value = "Play";		
		document.getElementById('f_percent').value = 0;
        document.getElementById('progressBar').style.width = '0%';
		movieObj(movieName).Play();
    }
}

function gotoFrame(frameNo)
{
    if (movieIsLoaded(movieObj(movieName)))
    {
        movieObj(movieName).GotoFrame(frameNo);
    }
}

function forwardMovie()
{
    if (movieIsLoaded(movieObj(movieName)))
    {
        // Get Total Frames
        if (navigator.appName.indexOf ("Microsoft") != -1)
        {
			totalFrames = movieObj(movieName).TotalFrames;
        }
        else
        {
            totalFrames = movieObj(movieName).TotalFrames();
        }
        // Get Current Frame
        currentFrame = movieObj(movieName).TCurrentFrame('_level0/');
        if (currentFrame < totalFrames - frameJump)
        {
            document.getElementById('playStatus').value = 1;
            document.getElementById('playButton').value = "Pause";
			
			movieObj(movieName).StopPlay();
            gotoFrame(currentFrame + frameJump);
            movieObj(movieName).Play();
        }

    }
}

function backwardMovie()
{
    if (movieIsLoaded(movieObj(movieName)))
    {
        // Get Current Frame
        currentFrame = movieObj(movieName).TCurrentFrame('_level0/');

        if (currentFrame > frameJump)
        {
			document.getElementById('playStatus').value = 1;
	        document.getElementById('playButton').value = "Pause";
            movieObj(movieName).StopPlay();
            gotoFrame(currentFrame - frameJump);
            movieObj(movieName).Play();
        }

    }
}

function checkProgress()
{		
    // Get Total Frames
    if (movieIsLoaded(movieObj(movieName)))
    {
		enableButtons();
						
		//Get Total Frames--diferent for IE vs. NS and FF
		if (navigator.appName.indexOf ("Microsoft") != -1)
		{totalFrames = movieObj(movieName).TotalFrames;}
		else
		{totalFrames = movieObj(movieName).TotalFrames();}
		
		// Get Current Frame
        currentFrame = movieObj(movieName).TCurrentFrame('_level0/');
        currentPercent = Math.round((currentFrame / totalFrames) * 100);
		document.getElementById('f_percent').value = currentPercent;
        document.getElementById('progressBar').style.width = currentPercent + '%';
		
		//If the player starts or stops playing on its own we need to set
		//the button text appropriately
		if(movieObj(movieName).IsPlaying())
		{ 
			if(document.getElementById('playStatus').value == 0)
			{
				document.getElementById('playStatus').value = 1;
				document.getElementById('playButton').value = "Pause";		
			}
		}
		else
		{
			if(document.getElementById('playStatus').value == 1)
			{ 
				document.getElementById('playStatus').value = 0;
				document.getElementById('playButton').value = "Play";		
			}
		}
		
		if (currentFrame < parseInt(totalFrames) - 2)
        {
	        setTimeout("checkProgress()", 100);
        }
        else
        {
           	stopMovie();
			setTimeout("checkProgress()", 100);
        }

	}
	else
	{
		//Progress meter becomes a Downloading meter until the download is complete
		percentDownloaded=document.getElementById('f_percent').value = movieObj(movieName).PercentLoaded();
    	document.getElementById('f_percent').value = percentDownloaded;
		document.getElementById('progressBar').style.width = percentDownloaded + '%';
		
		setTimeout("checkProgress()", 100);
	}
}
