// JavaScript Document
// By Christof Daetwyler, M.D.
// Drexel University College of Medicine
// 2006
// contact: christof.daetwyler@drexelmed.edu

myArray = new Array();
var myMovieCell;
var randomName;
var randomNumber;
var movieNumber;
var myWindowWidth;
var myWindowHeight;
var myMovieWidth;
var myMovieHeight;
var myAssessment;


function endMovie()
{
	  if (myAssessment = "no asessment")
	  {
	  alert("You have reached the end of this on-line video - please consider giving your appreciated feed-back to time@drexel.edu - thank you!");
	  resetAll()
	  }
	  else
	  {
	  alert("You have reached the end of this on-line video - the next step is a short assessment..");
      resetAll()
	  }
}

  function initializeInterAct(Assessment)
{
	if (Assessment == undefined)
	{
		myAssessment = "no assessment"
	}
	//myMovieCell = document.getElementById("movieCell").innerHTML;
 initializeMyArray();
 movieNumber=myArray.length-1;
 parent.ValueFrame.document.getElementById("movieNumber").innerHTML = movieNumber;
 setMovieWidth();
 resetAll();
 document.getElementById("mover_watch").style.verticalAlign = "top";
 showSystemRequirements();
}

function setMovieWidth()
{
  getWindowSize();
  myMovieWidth = eval(myWindowWidth) - 240;
  myMovieHeight = Math.round(eval(myMovieWidth*0.75));
  if ((myWindowHeight - myMovieHeight) < 2) {
  	myMovieHeight = eval(myWindowHeight) - 2;
  	myMovieWidth = Math.round(eval(myMovieHeight*1.33));
	}
//	alert(myMovieWidth+"*"+myMovieHeight);
  document.getElementById("MovieColumn").setAttribute("width",eval(myMovieWidth)+10);
    parent.ValueFrame.document.getElementById("myMovieWidth").innerHTML = myMovieWidth;
    parent.ValueFrame.document.getElementById("myMovieHeight").innerHTML = myMovieHeight;
}

function getWindowSize() 
{
// I found this script at http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  myWindowWidth = myWidth;
  myWindowHeight = myHeight;
}

  function pausecomp(millis)
{
date = new Date();
var curDate = null;
do { var curDate = new Date(); }
while(curDate-date < millis);
}

 function initializeMyArray()
{
 var myTable = document.getElementById("tableText").innerHTML;
 var watch_table = document.getElementById("table_watch");
 var myTable = myTable.toLowerCase();
 var myLine;
 var WatchStr = "";
 lastReturn = 0;
   for (i=0; i > -1; i++)
   {
   nextReturn = myTable.indexOf("<br>",lastReturn);
   if (nextReturn < 0) {break}
   myLine = myTable.substring(lastReturn + 3,nextReturn - 1);
   myArray[i] = new Array();
   lastTab = 0;
   	for (j=0; j > -1; j++)
  	 {
  	 nextTab = myLine.indexOf(",",lastTab);
 	 if (nextTab < 0) {
	 break;
	 }
 	 myArray[i][j] = myLine.substring(lastTab,nextTab);
	  if (j == 1)
	  {
	  // write the tables
	  var watch_row = watch_table.insertRow(watch_table.rows.length);
	  var watch_cell = watch_row.insertCell(0);
	  watch_cell.setAttribute("id", eval(myArray[i][0]));
      var watch_link = document.createElement('a');
      watch_link.href= "javascript:loadMovie(" + eval(myArray[i][0])+ ");";
      watch_link.innerHTML = myArray[i][1];
  	  watch_cell.appendChild(watch_link);	    
	  }	 
      lastTab = nextTab + 1;
	 }
   lastReturn = nextReturn + 1;
   }    
}

function resetAll()
{
if (document.getElementById("movieCell").style.display != "none"){
   pausecomp(100);
   document.main_movie.Stop();
   pausecomp(100);
   }
document.getElementById("tableText").style.display = "none";
document.getElementById("movieCell").style.display = "none";
document.getElementById("introCell").style.display = "inline";
}


function initializeWatch()
{
resetAll();
document.getElementById("mover_watch").style.display = "inline";
document.getElementById("mover_watch").style.verticalAlign = "top";
}

function editTableText()
{
document.getElementById("tableText").style.display = "block";
}


function id2title(myID)
{
myID=eval(myID);
  for (i=0; i < myArray.length; i++)
  {
	  if (myID == myArray[i][0])
	  {
	  return(myArray[i][1]);
	  }
   }
}


 function writeMyArray()
{
 var myStr = "";
   for (i=0; i < (myArray.length); i++) 
   {
   myStr += myArray[i] +"<br>" ;
   }
   document.write(myStr,"<br>");
 }
 
function stripSpaces(x) {
     while (x.substring(0,1) == ' ') x = x.substring(1);
    while (x.substring(x.length-1,x.length) == ' ') x = x.substring(0,x.length-1);
    return x;
}
 

function loadMovie(movie) {
  var movie = eval(movie);//identifies which movie to show
  var nextmovie = eval(movie+1);//identifies which movie to show next
  var myMovieWidth = parent.ValueFrame.document.getElementById("myMovieWidth").innerHTML;
  var myMovieHeight = parent.ValueFrame.document.getElementById("myMovieHeight").innerHTML;
  var movieNumber = parent.ValueFrame.document.getElementById("movieNumber").innerHTML;

  i=0;
  if (nextmovie	> (movieNumber)) 
      {
		endMovie()
	  }
  //set the bg color of the current selection
  for (i==0; i<movieNumber; i++)
  {
   parent.DrexloFrame.document.getElementById(i).style.backgroundColor="#000000";
  }
  p = parent.DrexloFrame.document.getElementById(movie);
  p.style.backgroundColor='#666666';
  
  //show the movie
myMovie = "movies/" + movie + ".mov";
myStr = "";
myStr = '<OBJECT CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab" WIDTH="'+myMovieWidth+'" HEIGHT="'+myMovieHeight+'" id="main_movie"  CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B">';
myStr = myStr + '<PARAM NAME="SRC" VALUE="'+myMovie+'">';
myStr = myStr + '<PARAM name="SCALE" VALUE="aspect">';
myStr = myStr + '<PARAM name="CONTROLLER" VALUE="TRUE">';
myStr = myStr + '<PARAM name="BGCOLOR" value="#000000">';
myStr = myStr + '<EMBED SRC="'+myMovie+'" name="main_movie" WIDTH="'+myMovieWidth+'" SCALE="aspect" HEIGHT="'+myMovieHeight+'" align="center" CONTROLLER="TRUE" type="video/quicktime" BGCOLOR="#000000" BORDER="0" PLUGINSPAGE="http://www.apple.com/quicktime/download/indext.htm"></EMBED><\/OBJECT>';
parent.DrexloFrame.document.getElementById("movieCell").innerHTML = myStr;
parent.DrexloFrame.document.getElementById("movieCell").style.display = "inline";
parent.DrexloFrame.document.getElementById("introCell").style.display = "none";

myQTNext=nextmovie
window.clearInterval(myTimer);
var myTimer=window.setInterval("LoadNexMovietWhenEnded()",400);
}

var myQTNext;

function LoadNexMovietWhenEnded(nextmovie)
// this function is called every 400ms while the movie is running
// it determines if if the movie has reached its end, and if so, it loads the next movie
{
     if ((document.main_movie.GetPluginStatus() == "Playable" || document.main_movie.GetPluginStatus() == "Complete") && (document.main_movie.GetDuration() == document.main_movie.GetTime()))
    {
	 loadMovie(myQTNext);
	}
}


function playMyMovie(movie)
{
myMovie = "movies/" + movie + ".mov";
myStr = "";
myStr = '<OBJECT CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab" WIDTH="'+myMovieWidth+'" HEIGHT="'+myMovieHeight+'" id="main_movie"  CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B">';
myStr = myStr + '<PARAM NAME="SRC" VALUE="'+myMovie+'">';
myStr = myStr + '<PARAM name="SCALE" VALUE="tofit">';
myStr = myStr + '<PARAM name="CONTROLLER" VALUE="TRUE">';
myStr = myStr + '<PARAM name="BGCOLOR" value="#000000">';
myStr = myStr + '<EMBED SRC="'+myMovie+'" name="main_movie" WIDTH="'+myMovieWidth+'" SCALE="aspect" HEIGHT="'+myMovieHeight+'" align="center" CONTROLLER="TRUE" type="video/quicktime" BGCOLOR="#000000" BORDER="0" PLUGINSPAGE="http://www.apple.com/quicktime/download/indext.htm" enablejavascript="true"></EMBED><\/OBJECT>';
document.getElementById("movieCell").innerHTML = myStr;
document.getElementById("movieCell").style.display = "inline";
document.getElementById("introCell").style.display = "none";}

function getAttributes(obj){
		for(var i=0;i<obj.attributes.length;i++){
			if(obj.attributes[i].specified){
				alert(obj.attributes[i].nodeName + "=" + obj.attributes[i].nodeValue);
			}
		}
	}

/*******************************************************
QUICKTIME DETECT
All code by Ryan Parman, unless otherwise noted.
(c) 1997-2003, Ryan Parman
http://www.skyzyx.com
Distributed according to SkyGPL 2.1, http://www.skyzyx.com/license/
*******************************************************/



function doquicktimestuff()
{

var quicktime=new Object();

// Set some base values
quicktime.installed=false;
quicktime.version='0.0';


if (navigator.plugins && navigator.plugins.length)
{
	for (x=0; x<navigator.plugins.length; x++)
	{
		if (navigator.plugins[x].name.indexOf('QuickTime Plug-in') != -1)
		{
			quicktime.installed=true;
			quicktime.version=navigator.plugins[x].name.split('QuickTime Plug-in ')[1].split(' ')[0];
			break;
		}
	}
}
else if (window.ActiveXObject)
{
	try
	{
		oQTime=new ActiveXObject('QuickTimeCheckObject.QuickTimeCheck.1');
		if (oQTime)
		{
			quicktime.installed=oQTime.IsQuickTimeAvailable(0);
			quicktime.version=parseInt(oQTime.QuickTimeVersion.toString(16).substring(0,3))/100;
		}
	}
	catch(e) {}
}



quicktime.ver2=(quicktime.installed && parseInt(quicktime.version) >= 2) ? true:false;
quicktime.ver3=(quicktime.installed && parseInt(quicktime.version) >= 3) ? true:false;
quicktime.ver4=(quicktime.installed && parseInt(quicktime.version) >= 4) ? true:false;
quicktime.ver5=(quicktime.installed && parseInt(quicktime.version) >= 5) ? true:false; 
quicktime.ver6=(quicktime.installed && parseInt(quicktime.version) >= 6) ? true:false;
quicktime.ver7=(quicktime.installed && parseInt(quicktime.version) >= 7) ? true:false; 
quicktime.ver8=(quicktime.installed && parseInt(quicktime.version) >= 8) ? true:false; 
quicktime.ver9=(quicktime.installed && parseInt(quicktime.version) >= 9) ? true:false;

var myVar='';
if (quicktime.ver7)
	{
	// if QuickTime 7.0 or newer is installed, do QuickTime 7.0 stuff.
	
	myVar=''
	myVar='<font face="Verdana" size="-1">Your system meets the requirements for GEM</font><br>'
	myVar=myVar+'<font face="Verdana" size="-1">The videos should run smoothly.</font><br>'
	document.write(myVar);
	}
	else if (quicktime.installed)
	{
	// do older QuickTime stuff
	myVar=''
	myVar='<font face="Verdana" color="red" size="-1"><b>SYSTEM REQUIREMENTS ARE NOT MET!</b></font><br>'
	myVar=myVar+'<font face="Verdana" size="-1"><b><blink>You have an older version of Quicktime!</blink></font></b><br>';
	myVar=myVar+'<font face="Verdana" size="-1">Your version is: </font>' + quicktime.version;
	myVar=myVar+'<br><font face="Verdana" size="-1">Please go <a href="http://www.apple.com/quicktime/" target="_blank"> here</a> and get the most recent version of Quicktime</font><br>';
	document.write(myVar);
	parent.DrexloFrame.document.getElementById("systemrequirements").style.backgroundColor='FFCC33';
	}
	else
	{
	// QuickTime is NOT installed. Do something else.
	
	myVar=''
	myVar='<font face="Verdana" color="red" size="-1"><b>SYSTEM REQUIREMENTS ARE NOT MET!</b></font><br>'
	myVar=myVar+'<font face="Verdana" size="-1"><b><blink>You do NOT have Quicktime installed!</blink></b></font><br>';
	myVar=myVar+'<font face="Verdana" size="-1">Please go <a href="http://www.apple.com/quicktime/" target="_blank"> here</a> and get Quicktime</font><br>'; 
	document.write(myVar);
	parent.DrexloFrame.document.getElementById("systemrequirements").style.backgroundColor='FFCC33';
	}



}