﻿var QTMovieNum = 0;
var QTMovieNumMax = 0;
var QTVideoDir = "";
var QTFileNames = "";
var QTFilesExt = "";
var QTObjId = "";
var QTEmbId = "";
var QTElem = null;
var QTActive = false;


function OnLoad()
{
   	if (QTActive == true) 
	   RegQTListener("qt_ended", QTObjId, QTEmbId, videoEndedQt);
}


function RegQTListener(eventName, objID, embedID, listenerFcn)
{
        var obj = document.getElementById(objID);

        if (!obj)
        	obj = document.getElementById(embedID);


	if (obj)
	{
        	if (document.addEventListener)
            		obj.addEventListener(eventName, listenerFcn, false);
        	else
            		obj.attachEvent("on" + eventName, listenerFcn);
	}
}


function videoEndedQt()
{
	LoadNextQTVideo(-1);
}


function PlayItem( ItemNum, ElemId)
{
   if (QTActive == true) 
   {
	LoadNextQTVideo(ItemNum);
   }
   else
   {
   	var player = document.getElementById(ElemId); 

   	if(player.playState == undefined)
       	   player = GetEmbedPlayer(ElemId); 

   	if(player.playState != 3 )
       	   player.controls.play();

      	var media = player.currentPlaylist.item(ItemNum);

      	// Play the media item.
      	player.controls.playItem(media);
   }		
}


function GetEmbedPlayer(p_ElemId)
{
    var embeds = document.getElementsByTagName("embed");

    for (var i = 0; i < embeds.length; i++)
    {
      var embed = embeds[i];

      if (embed.getAttribute("id") != null && embed.getAttribute("id") == p_ElemId) 
      	return embed;
    }
    
    return null;
}



function InitPlayer(p_VideoDir, p_VideoFileList, p_VideoNameFiles, p_VideoExt, p_Width, p_Height, p_MaxIdx)
{
	var l_VideoFile = "";

	if (-1 != navigator.userAgent.indexOf("Firefox")) 
	{
		l_VideoFile = GetVideoLink(p_VideoDir, p_VideoFileList);

		document.write("<object id=\"videoPlayer\" ");
		document.write("type=\"application/x-ms-wmp\" ");
		document.write("width=\"" + p_Width + "\" ");
		document.write("height=\"" + p_Height + "\" ");
		document.write("url=\"" + l_VideoFile + "\">");
		document.write("<param name=\"autoStart\" value=\"true\">");
		document.write("<param name=\"url\" value=\"" + l_VideoFile + "\">");
		document.write("<param name=\"wmode\" value=\"transparent\">");
		document.write("<param name=\"uiMode\" value=\"full\">");
		document.write("<param name=\"loop\" value=\"false\">");
		document.write("</object>");
	}
	else if (-1 != navigator.userAgent.indexOf("MSIE"))
	{
		l_VideoFile = GetVideoLink(p_VideoDir, p_VideoFileList);

		document.write("<object id=\"videoPlayer\" ");
		document.write("classid=\"CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6\" ");
		document.write("width=\"" + p_Width + "\" ");
		document.write("height=\"" + p_Height + "\" ");
		document.write("codebase=\"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701\" ");
		document.write("standby=\"Loading Microsoft Windows Media Player components...\" ");
		document.write("type=\"application/x-oleobject\">");
		document.write("<param name=\"autoStart\" value=\"true\">");
		document.write("<param name=\"url\" value=\"" + l_VideoFile + "\">");
		document.write("<param name=\"wmode\" value=\"transparent\">");
		document.write("<param name=\"uiMode\" value=\"full\">");
		document.write("<param name=\"loop\" value=\"false\">");

		document.write("<embed id=\"videoPlayer\" ");
		document.write("name=\"videoPlayer\" ");
		document.write("type=\"application/x-mplayer2\" ");
		document.write("pluginspage=\"http://www.microsoft.com/Windows/MediaPlayer/\" ");
		document.write("autoStart=\"true\" ");
		document.write("controls=\"ImageWindow\" ");
		document.write("src=\"" + l_VideoFile + "\" ");
		document.write("width=\"" + p_Width + "\" ");
		document.write("height=\"" + p_Height + "\">");
		document.write("</embed>");

		document.write("</object>");
	}
	else
	{
		QTMovieNum  = 0;
		QTMovieNumMax = p_MaxIdx;
		QTVideoDir = p_VideoDir;
		QTFileNames = p_VideoNameFiles;
		QTFilesExt = p_VideoExt;
		QTObjId = "videoPlayer";
		QTEmbId = "videoPlayerEmbed";

		l_VideoFile = GetQTDir();
		
		document.write("<object id=\"qt_event_source\" ");
		document.write("classid=\"clsid:CB927D12-4FF7-4a9e-A169-56E4B8A75598\" ");
		document.write("codebase=\"http://www.apple.com/qtactivex/qtplugin.cab#version=7,2,1,0\">");
		document.write("</object>");

		document.write("<object id=\"" + QTObjId + "\" ");
		document.write("classid=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\" ");
		document.write("type=\"video/quicktime\" ");
		document.write("codebase=\"http://www.apple.com/qtactivex/qtplugin.cab\" ");
		document.write("style=\"behavior:url(#qt_event_source);\" ");
		document.write("width=\"" + p_Width + "\" ");
		document.write("height=\"" + p_Height + "\">");
		document.write("<param name=\"src\" value=\"" + l_VideoFile + "\">");
		document.write("<param name=\"postdomevents\" value=\"true\">");
		document.write("<param name=\"autoplay\" value=\"true\">");


		document.write("<embed id=\"" + QTEmbId + "\" ");
		document.write("name=\"videoPlayer\" ");
		document.write("src=\"" + l_VideoFile + "\" ");
		document.write("type=\"video/quicktime\" ");
		document.write("pluginspage=\"http://www.apple.com/quicktime/download/\" ");
		document.write("autoplay=\"true\" ");
		document.write("postdomevents=\"true\" ");
		document.write("width=\"" + p_Width + "\" ");
		document.write("height=\"" + p_Height + "\">");
		document.write("</embed>");

		document.write("</object>");
		
		QTElem = document.videoPlayer;
		QTActive = true;
	}
}



function GetVideoLink(p_VideoDir, p_VideoFile)
{

	//////////////////////////////////
	// the subfolder and file name - these are used only on the mozilla browser
	var videosubdirectory = p_VideoDir;
	var videofile = p_VideoFile;

	//this will be the final video link (absolute path)
	var videolink = location.href;

	//remove the file:// because windows media doesn't understand that
	var file = videolink.indexOf( "file://");

	if ( file > -1 )
	{
		// THIS IS A LOCAL PATH
		videolink = videolink.substring( file + 8 );

		//remove the %20 because media player doesn't understand those
		while( videolink.lastIndexOf( "%20" ) != -1 )
		{
			videolink = videolink.replace( "%20", " " );
		}

		//remove the html file name now
		var lastslash = videolink.lastIndexOf( "/");

		if ( lastslash > -1 )
			videolink = videolink.substring( 0, lastslash );

		//add the subfolder (if there is one) and then the video file name
		if ( videosubdirectory.length > 0 )
			videolink = videolink + "/" + videosubdirectory + "/" + videofile;
		else
			videolink = videolink + "/" + videofile;
		}
	else
	{
		////////////////////////////
		// THIS IS NOT A LOCAL FILE - give relative path

		//no need to give an absolute path
		if ( videosubdirectory.length > 0 )
			videolink = videosubdirectory + "/" + videofile;
		else
			videolink = videofile;
	}


	return videolink;
}


function GetQTDir()
{
   var l_Rslt = "";
   var l_href = location.href;
   var l_lastslash = l_href.lastIndexOf( "/");

   if (l_lastslash > -1 )
      l_href = l_href.substring(0, l_lastslash);

   l_Rslt = l_href + "/" + QTVideoDir + "/" + QTFileNames + "-" + QTMovieNum + "." + QTFilesExt;

   return l_Rslt; 
}


function LoadNextQTVideo(p_VideoNum)
{
   var l_Stop = false;

   if (p_VideoNum != -1)
      QTMovieNum = p_VideoNum;
   else
      QTMovieNum++;
  
   if (QTMovieNumMax < QTMovieNum)
   {
      QTMovieNum = 0;
      l_Stop = true;

   }

   var l_VideoFile = GetQTDir();

   if (QTElem)
   {
      QTElem.SetURL(l_VideoFile);
      QTElem.SetAutoPlay(!l_Stop);

      if (QTElem.GetAutoPlay() == false)
         QTElem.Stop();
   }
}
