// JavaScript Document
// By Christof Daetwyler, M.D.
// Drexel University College of Medicine
// 2006
// contact: christof.daetwyler@drexelmed.edu

var myMedia_array = new Array();
var myMovieCell;
var movieNumber;
var myWindowWidth;
var myWindowHeight;
var myMovieWidth;
var myMovieHeight;
var myAssessment;
var myOldID = 0;
var mediaType;
var slideDuration = 4;
var slideTime;
var myTimer=window.setInterval("CheckStatus()",1000);

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)
{
 initializeMyArray();
 movieNumber=myMedia_array.length-1;
 //setMovieWidth();
// document.getElementById("mover_watch").style.verticalAlign = "top";
}

function setMovieWidth()
{
  getWindowSize();
  myMovieWidth = eval(myWindowWidth) - 240;
  myMovieHeight = Math.round(eval(myMovieWidth*0.5));
  if ((myWindowHeight - myMovieHeight) < 50) {
  	myMovieHeight = eval(myWindowHeight) - 20;
  	myMovieWidth = Math.round(eval(myMovieHeight*2));
	}
//	alert(myMovieWidth+"*"+myMovieHeight);
  this.document.getElementById("MovieColumn").setAttribute("width",eval(myMovieWidth)+10);
}

function getWindowSize() 
{
// I found this script at http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
  var myWidth = 0, myHeight = 0;
  if( typeof( top.window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = top.window.innerWidth;
    myHeight = top.window.innerHeight;
  } else if( top.document.documentElement && ( top.document.documentElement.clientWidth || top.document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = top.document.documentElement.clientWidth;
    myHeight = top.document.documentElement.clientHeight;
  } else if( top.document.body && ( top.document.body.clientWidth || top.document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = top.document.body.clientWidth;
    myHeight = top.document.body.clientHeight;
  }
  myWindowWidth = myWidth;
  myWindowHeight = myHeight;
}

function initializeMyArray()
{
 var myTable = top.document.getElementById("tableText").innerHTML;
 var watch_table = frames['MediaShowiFrame'].document.getElementById("table_watch");
 //alert(watch_table);
 var myLine;
 var WatchStr = "";
 var myMediaField = "";
 
 myMedia_array=myTable.split(";");
 for(i = 0; i < myMedia_array.length-1; i++)
 {
	  myMedia_array[i]=myMedia_array[i].split(":"); 
	  
	  var watch_row = watch_table.insertRow(watch_table.rows.length);
      var watch_cell = watch_row.insertCell(0);
	  watch_cell.setAttribute("id", i);
      var watch_link = frames['MediaShowiFrame'].document.createElement('a');
	  if (myMedia_array[i][1] == "") {myMedia_array[i][1] = myMedia_array[i][0]} 
      watch_link.href= "javascript:showMedia('" +  myMedia_array[i][0]+ "'," + i + ")";
	  
      watch_link.innerHTML = "<img src='thumbs/" + myMedia_array[i][1] + "'>" + myMedia_array[i][1];
  	  watch_cell.appendChild(watch_link);	
 
      var watch_anchor = frames['MediaShowiFrame'].document.createElement('a');
      watch_anchor.href= "name = " + i ;
      watch_cell.appendChild(watch_anchor);
	  
	 myMediaField = myMediaField + "*" + i + "*" + myMedia_array[i][0] + "*";
  } 
  top.document.getElementById("myMediaArray").innerHTML = myMediaField;
}


function showMedia(name,ID)
{
    document.getElementById(myOldID).style.backgroundColor="#000000";
    document.getElementById(ID).style.backgroundColor='#666666';
    myOldID = ID;
	top.document.getElementById("myID").innerHTML = ID;
	//name=name.toLowerCase();
	
    slideTime = 0
	
	if (name.indexOf("mov") > 0) 
	{mediaType="movie"; loadMovie(name,ID)}
	else
	{mediaType="picture";
	showPicture(name,ID);
	}
}

function loadNextMedia()
{
	slideTime = 0;
    ID = parseInt(top.document.getElementById("myID").innerHTML);
 	if (ID == movieNumber-1){ID=-1}
    ID = (ID + 1);
	myBackUpArray = top.document.getElementById("myMediaArray").innerHTML;
	var start = myBackUpArray.indexOf("*"+ID+"*");
	var end = myBackUpArray.indexOf("**",start);
	name = myBackUpArray.substring(start+1,end);
	name = name.substring(name.indexOf("*")+2);
	document.getElementById(myOldID).style.backgroundColor="#000000";
    document.getElementById(ID).style.backgroundColor='#666666';
    myOldID = ID;
	top.document.getElementById("myID").innerHTML = ID;
	//name=name.toLowerCase();
	if (name.indexOf("mov") > 0) 
	{mediaType="movie"; loadMovie(name,ID)}
	else
	{mediaType="picture";
     showPicture(name,ID);
	}
}

function showPicture(name,ID)
{
	myStr = '<img src="media/'+name+'">';
	top.document.getElementById("movieCell").innerHTML = myStr;
    top.document.getElementById("movieCell").style.display = "inline";
    top.document.getElementById("introCell").style.display = "none";
}


function CheckStatus()
// this function is called once every second
{
	if (mediaType=="movie"){
		 if ((top.document.main_movie.GetPluginStatus() == "Playable" || top.document.main_movie.GetPluginStatus() == "Complete") && (top.document.main_movie.GetDuration() == top.document.main_movie.GetTime()))
		{	 
		 loadNextMedia();
		}
	}
	else if (mediaType=="picture"){
		if (slideTime < slideDuration) {slideTime = slideTime + 1}
		else
		{loadNextMedia()}
	}
}

function initializeWatch()
{
resetAll();
top.document.getElementById("mover_watch").style.display = "inline";
top.document.getElementById("mover_watch").style.verticalAlign = "top";
}
 

function loadMovie(movie,ID) {
  
  //show the movie
	myMovie = "media/" + movie;
	myStr = "";
	myStr = '<OBJECT CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab" id="main_movie" width="848" height="500" 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" align="center" CONTROLLER="TRUE" type="video/quicktime" BGCOLOR="#000000" BORDER="0" PLUGINSPAGE="http://www.apple.com/quicktime/download/indext.htm" enablejavascript="true" scale="aspect" width="848" height="500"></EMBED><\/OBJECT>';
	top.document.getElementById("movieCell").innerHTML = myStr;
	top.document.getElementById("movieCell").style.display = "inline";
	top.document.getElementById("introCell").style.display = "none";
}