/***********************************************************************
 ** video.js **
 -----------------
 Author: msteffen
 Description: Several functions to open and display videos using HTML 5.
 Use With: video.css
 ***********************************************************************/
 
var videoIsLoaded = false;
var videoIsHTML = false;
var videoDivSelected = false;

function supportsHTML5Video(videoTag)
{
/*
	// First, see if the video tag is even supported
	if (!videoTag.canPlayType)
		return false;
	
	// Check whether the browser supports HTML5 compatible video formats
	if (videoTag.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"') == "probably")
		return true;
	else if (videoTag.canPlayType('video/ogg; codecs="theora, vorbis"') == "probably")
		return true;
	else
		return false;
			
	return false;
	*/
	
	return false;
}

function openVideoDiv(videoFilename)
{
	if (videoIsLoaded)
	{
		return;	
	}
	
/*	// Create the object tag for VPlayer.swf (Flash fallback)
	var objectTag = document.createElement('object');
	objectTag.id = "FLV_Video";
	objectTag.width = 448;
	objectTag.height = 376;
	objectTag.type = "application/x-shockwave-flash";
	objectTag.data = "VPlayer.swf" + "?src=" + videoFilename + ".flv";
	objectTag.innerHTML = '<param name="movie" value="VideoPlayer.swf" />';
	
	// Create the param tag
	var paramTag = document.createElement('param');
	paramTag.name = "movie";
	paramTag.value = "VPlayer.swf" + "?src=" + videoFilename + ".flv";
	objectTag.appendChild(paramTag); */
	
	// Since IE 8 has problems with the above method of creating an object tag and param tag using DOM scripting,
	// just create as innerHTML within a DIV
	var filePath = 'VPlayer.swf' + '?src=' + videoFilename + '.flv';
	var objText = '<object id="FLV_Video" width="448" height="376" type="application/x-shockwave-flash" data=' + filePath + '>';
	var paramText = '<param name="movie" value="' + filePath + '" />';
	
	var objectDiv = document.createElement('div');
	objectDiv.id = "Object_Div";
	objectDiv.innerHTML = objText + paramText + '</object>';
	
	// Create the video tag for the HTML video player
	var videoTag = document.createElement('video');
	if(supportsHTML5Video(videoTag))
	{
		// This browser can play HTML5 video.
		videoIsHTML = true;
		
		// Set the attributes of the video tag
		videoTag.id = "HTML_Video";
		videoTag.width = "448";
		videoTag.height = "336";
		videoTag.preload = true;
		videoTag.controls = true;
		videoTag.autoplay = true;
		
		// Create source tags for the relevant video format
		var sourceMP4 = document.createElement('source');
		sourceMP4.id = "mp4_src";
		sourceMP4.src = videoFilename + ".mp4";
		sourceMP4.type = 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"';
		videoTag.appendChild(sourceMP4);
		
		var sourceOGV = document.createElement('source');
		sourceOGV.id = "ogv_src";
		sourceOGV.src = videoFilename + ".ogv";
		sourceOGV.type = 'video/ogg; codecs="theora, vorbis"';
		videoTag.appendChild(sourceOGV);
		
		// Append the object tag to the videoTag, as a Flash fallback
		// (In case this browser cannot play any of the sources above)
		videoTag.appendChild(objectDiv);
		
		// Append the videoTag to the videoArea
		var videoArea = document.getElementById("divVideoArea");
		if (videoArea)
		{
			videoArea.appendChild(videoTag);
		}
	}
	else
	{
		// This browser cannot play HTML5 video.
		videoIsHTML = false;
		
		// Simply append the object tag directly to the videoArea
		var videoArea = document.getElementById("divVideoArea");
		if (videoArea)
		{
			videoArea.appendChild(objectDiv);
		}
	}
		
/*	// Create a paragraph tag for the filename
	var fNameText = document.createElement('span');
	fNameText.innerHTML = videoFilename;
	fNameText.id = "filenameText";
	
	// Append the video filename to the top of the video div
	var videoDiv = document.getElementById("divVideoWindow");
	if (videoDiv)
	{
		videoDiv.appendChild(fNameText);
	}
	*/
	
	// Set divVideoWindow to visible
	MM_showHideLayers('divVideoWindow','','show');
	
	// Set the videoIsLoaded flag
	videoIsLoaded = true;
}

function closeVideoDiv()
{
	if (!videoIsLoaded)
	{
		return;	
	}
	
	// Hide the divVideoWindow
	MM_showHideLayers('divVideoWindow','','hide','divCloseBox','','inherit');
	
	// Get a reference to the videoArea
	var videoArea = document.getElementById("divVideoArea");
	if (!videoArea)
		return;
	
	// Remove the video, depending on whether it is HTML5 or Flash
	if (videoIsHTML)
	{
		var videoTag = document.getElementById("HTML_Video");
		videoArea.removeChild(videoTag);	
	}
	else
	{
		var objectDiv = document.getElementById("Object_Div");
		videoArea.removeChild(objectDiv);
	}
		
	/* Remove the text from the title bar
	var videoDiv = document.getElementById("divVideoWindow");
	var fNameText = document.getElementById("filenameText");
	if (videoDiv)
		videoDiv.removeChild(fNameText);
	*/
	
	// Clear the videoIsLoaded flag
	videoIsLoaded = false;
}

// Load an XML file
function loadXML(xmlFilename)
{
	var xml_http;
	
	// On modern browsers, the window should have an XMLHttpRequest object
	if (window.XMLHttpRequest)
	{
		xml_http = new XMLHttpRequest();
	}
	// For older browsers (IE6, IE5), use an ActiveXObject
	else
	{
		xml_http = new ActiveXObject("Microsoft.XMLHTTP");	
	}
	
	// Create a request to open an XML file, and send it
	xml_http.open("GET", xmlFilename, false);
	xml_http.send();
	
	// Create an object for the resulting XML object
	var xmlFile = xml_http.responseXML;
	
	// Return the open XML file
	return xmlFile;
}

