// courseVideo.js

/****************************************************************************************
 *  Windows Media Player 6.4 ClassID -> clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95   	*
 * 	Windows Media Player 7.1+ ClassID -> clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6		*
 * 																						*
 * 	Original Video Dimensions: 720x540													*
 * 	Scaled Video Dimensions:   460x345													*
 * 	Video player controls: +65 pixels in height.										*
 ****************************************************************************************/
var videoWidth=460;			// video player width
var videoHeight=410;		// video player height with the height offset added
var images=null;			// array of image objects for caching image buttons
var windowlessVideo=false; 	// if true, will allows CSS style menus to overlap the video (slower)
var messageShown=false;		// whether or not the message for the media player version issue has alreaddy been shown once.

var reloadPosition=false;
var playerState=null;

// broswer detection (poor way of doing this, but IE thinks that an object of type 'video/x-ms-wmv' is a dangerous activex control)
var IE = (navigator.appName == "Microsoft Internet Explorer" ? true : false);

// get exact IE version number
ieVersion = function()
{
	var pattern = "MSIE ";
	var ua = navigator.userAgent;
	var b_version = ua.substr(ua.indexOf(pattern)+pattern.length, 3);
	
	var result = parseFloat(b_version);
	
	// if brower in question isn't IE, just return 10 for the version number.
	if(isNaN(result)) return 10;
	
	return result;
}

/****************************************************************************************
 *  DisplayVideo	-	places the video object on the webpage and then loads the 		*
 *						the corresponding course module's video file playlist (*.ASX)	*
 *	@var courseModule	-	the course module object containing the filename used in 	*
 *							loading the video content									*
 ****************************************************************************************/
DisplayVideo = function( courseModule )
{	
	if( courseModule == null ) {
		alert("DisplayVideo :: No course module specified.");
		return;		
	}
	
	videoPath = "videos/";
		
	courseContentDiv = document.getElementById('course-content');
	
	if( courseContentDiv == null ) {
		alert("DisplayVideo :: Error locating document element 'course-content'.");
		return;		
	}
	
	
	Player = document.getElementById('MediaPlayer');
	
	/* create video object*/	
	if( Player == null )
	{			
		var IE_Players = 
			'<object id="MediaPlayer" name="MediaPlayer" title="Double-click on the video, while it is playing, to view it in full-screen on your computer" ' +
				'classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" ' +
				'width="'+videoWidth+'" height="'+videoHeight+'">' +
				'<param name="URL" value="'+ videoPath + courseModule.fileName +'">';
				

		var nonIE_Players = 
			'<object id="MediaPlayer" name="MediaPlayer" title="Double-click on the video, while it is playing, to view it in full-screen on your computer" ' +
				'type="'+courseModule.mimeType+'" data="'+videoPath+courseModule.fileName+'" ' +
				'width="'+videoWidth+'" height="'+videoHeight+'">' +
				'<param name="controller" value="true">';
		
		var ALL_Players = 
				'<param name="src" value="'+ videoPath + courseModule.fileName +'">' +		/* a fix for safari */
				'<param name="windowlessVideo" value="'+(windowlessVideo?'true':'false')+'">' +
				'<param name="transparentatStart" value="true">'+
				'<param name="stretchToFit" value="true">' +
				'<param name="enableContextMenu" value="false">' +
				'<param name="autoStart" value="true">' +
				'<param name="uiMode" value="full"></object>';
		
		courseContentDiv.innerHTML = (IE?IE_Players:nonIE_Players) + ALL_Players +
			'<p><a href="javascript:;" onclick="ExpandVideo();" id="viewModeAnchor">[click to view full screen]</a></p>';
				
		// Both IE and Mozilla FireFox handle the events using the JScipt event handler
		if( navigator.appName == "Netscape" && navigator.appCodeName != "Mozilla" ) {
			navigator.plugins.refresh();
			courseContentDiv.innerHTML += '<object src="NPDS.npDSEvtObsProxy.Class" name="appObs" data="NPDS.npDSEvtObsProxy.Class" classid="java:NPDS.npDSEvtObsProxy.Class"></object>';
			RegisterEventObservers();
			//"\x3C" + "applet MAYSCRIPT Code=NPDS.npDSEvtObsProxy.class width=5 height=5 name=appObs\x3E \x3C/applet\x3E";
		}		
	}
	
	/* reset the text alignment */
	courseContentDiv.style.textAlign='center';
		
	/* update the loading div */
	document.getElementById('loading').style.display = "block";
	
	// if the player object has already been created, change the media clip instead of creating a new object
	//	multiple nested try-catch statements to handle the different players and their methods.
	if(Player != null) {
		/* Windows Media Player 11 */
		try { Player.Open(videoPath+courseModule.fileName);	} 
		catch(openException) {
			/* Windows Medit Player 7.1+ */
			try { Player.URL = videoPath+courseModule.fileName;	} 
			catch (UrlException) {
				/* Windows Media Player 6.4 and below AND other common players */
				try {Player.filename(videoPath+courseModule.fileName);	} 				
				catch(filenameException) { alert("DisplayVideo :: Error. Unable to change the video content."); }
			}
		}		
	} else {
		Player = document.getElementById('MediaPlayer');
	}
	
	// only show the message once per page load.
	if(!messageShown) {
		messageShown=true;
		
		// check user's player version and warn them if it is unsupported.
		if(isNaN(parseInt(Player.versionInfo))) {
			alert("This site was unable to verify the version of Microsoft® Windows® Media Player you\n"+
				  "are using. Although Version 11 or higher is required for all video playback, other\n"+
				  "players may work fine but they are not supported.");
			
		} else if(parseInt(Player.versionInfo) < 11) {
			alert("This site requires that you have Microsoft® Windows® Media Player 11 or higher,\n"+
				  "whereas version "+parseInt(Player.versionInfo)+" was found on your computer.\n"+
				  "This configuration may work, however if you experience any problems with the\n"+
				  "video, you should update your version of Microsoft® Windows® Media Player.");
		}
	}
}


/****************************************************************************************
 *  saveCurrentState	-	records the currnet position in the video playing file	 	*
 ****************************************************************************************/
saveCurrentState = function(Player)
{
	playerState = new Object();
	
	// save the currnet position in the list item
	playerState.position = Player.controls.currentPosition;
	
}

/****************************************************************************************
 *  loadPreviousState	-	load the previously recorded video file postion				*
 *							Fix for IE6 and Firefox										*
 ****************************************************************************************/
loadPreviousState = function(Player)
{
	//alert("Position: " + Player.controls.currentPosition + " item: " + Player.controls.currentItem);
	if(playerState != null) {
		// load the previous position in the list item
		Player.controls.currentPosition = playerState.position;
	}
	
}

/****************************************************************************************
 *  ExpandVideo		-	controls the video size - expand to fullscreen or shrink to 	*
 *						regular size.													*
 ****************************************************************************************/
ExpandVideo = function()
{
	objVideo = document.getElementById('MediaPlayer');
	
	if(objVideo != null) {
		modifyVideoObject(objVideo);
		saveCurrentState(objVideo);
		reloadPosition = true;
	}
	
	// modify background
	backDiv = document.getElementById('course-content');
	if(backDiv == null) return;
	
	// set video to full-screen
	if(backDiv.style.position != 'absolute') {
		document.getElementById('viewModeAnchor').innerHTML = "[click to reduce video size]";		
		
		backDiv.style.position='absolute';
		backDiv.style.zIndex='3';
		backDiv.style.top='0px';
		backDiv.style.left='0px';
		
		// handle ie6 issue
		if(ieVersion() < 7) {		
			backDiv.style.width=document.body.clientWidth;
			backDiv.style.height=document.body.clientHeight;
		} else {
			backDiv.style.width='100%';
			backDiv.style.height='100%';
		}
		backDiv.style.backgroundColor='#003173';
		setObjectOpacity(backDiv, 90);		
	
	// set video to regular size screen
	}else {
		document.getElementById('viewModeAnchor').innerHTML = "[click to view full screen]";
		
		backDiv.style.position='relative';
		backDiv.style.zIndex='0';
		backDiv.style.top='0';
		backDiv.style.left='0';
		backDiv.style.width='auto';
		backDiv.style.height='5in';
		backDiv.style.backgroundColor='transparent';
		setObjectOpacity(backDiv, 100);		
	}	
}

/****************************************************************************************
 *  setObjectOpacity	-	sets the opacity of an HTML object in multiple broswers 	*
 ****************************************************************************************/
setObjectOpacity = function(obj, opacity)
{
	obj.style.opacity=(opacity/100); 				//Opera
	obj.style.MozOpacity=(opacity/100); 			//Mozilla+Firefox
	obj.style.KhtmlOpacity=(opacity/100); 			//Konqueror
	obj.style.filter="alpha(opacity="+opacity+")"; 	//IE
}

/****************************************************************************************
 *  modifyVideoObject	-	modifies the video object to expand or shrink accordingly	*
 ****************************************************************************************/
modifyVideoObject = function(videoObject)
{
	if(videoObject.style.width != '630px') {
		videoObject.style.height='536px';
		videoObject.style.width='630px';
		videoObject.style.position='relative';
		videoObject.style.margin='0.5in auto auto auto';
	} else {
		videoObject.style.height=videoHeight+'px';
		videoObject.style.width=videoWidth+'px';
		videoObject.style.position='relative';
		videoObject.style.margin='0';
	}
}

/****************************************************************************************
 *  reposition	-	repositions the 'loading' div when in fullscreen mode to allow for 	*
 *					brower window resizing												*
 ****************************************************************************************/
reposition = function()
{
	var loading =  document.getElementById('loading');
		
	// if object is null it hasn't been created yet.
	if(loading != null) {	
		// calculate the horizontal position
		loading.style.left=(document.body.clientWidth/2) - (loading.offsetWidth/2);
		loading.style.top=(document.body.clientHeight/2) - (loading.offsetHeight/8);
	}	
}


// end of file