document.contentLaunched = false; //describes if an actual sco_content file has been launched. Need this so we know if we have to call commit/finish on the LMS.
document.menuSelectFile = "none"; // first page of topic selected on the menu page -- kgc 11/27/08

var snifferPagePreferences = {
	"closePanels": false,
	"snifferBookmark" : false,
	"langPreference" : "en",
	"htmlOrFlashVersionChosen" : "", /* can be "h" or "f" */
	"startTime" : new Date() /*not a preference - but a convenient place to store this for the purposes of time tracking*/	
}

// ADL routines to locate the API adapter
//  
var findAPITries = 0;

function findAPI(win)
{
   // Check to see if the window (win) contains the API
   // if the window (win) does not contain the API and
   // the window (win) has a parent window and the parent window  
   // is not the same as the window (win)
   while ( (win.API == null) && 
           (win.parent != null) && 
           (win.parent != win) )
   {	
      // increment the number of findAPITries
      findAPITries++;

      // Note: 7 is an arbitrary number, but should be more than sufficient
      if (findAPITries > 7) 
      {
         alert("Error finding API -- too deeply nested.");
         return null;
      }
      
      // set the variable that represents the window being 
      // being searched to be the parent of the current window
      // then search for the API again
      win = win.parent;
   }
   return win.API;
}

//  
// Function: getAPI()
// Parameters: none 
// Returns: null if API could not be located, 
//		a handle to API adapter is returned if the API was located.
//
// Description: Locate the SCORM API Adaptor
//
function getAPI()
{
   // start by looking for the API in the current window
   var theAPI = findAPI(window);

   // if the API is null (could not be found in the current window)
   // and the current window has an opener window
   if ( (theAPI == null) && 
        (window.opener != null) && 
        (typeof(window.opener) != "undefined") )
   {
      // try to find the API in the current window's opener
      theAPI = findAPI(window.opener);
   }

   // if the API is null (could not be found in the current window,
   // or the current windows opener) see if the opener of the topmost
   // window contains the api
   // and the current window has an opener window
   if ( (theAPI == null) && 
        (window.top.opener != null) && 
        (typeof(window.top.opener) != "undefined") )
   {
      // try to find the API in the current window's opener
      theAPI = findAPI(window.top.opener);
   }

   // if the API has not been found
   if (theAPI == null)
   {
      // Alert the user that the API Adapter could not be found
      //Remove below as this is now taken care of by versioning pages - IR 1816
      //alert("Unable to find an API adapter");
   }
   return theAPI;
}
//get a handle on the LMS' API. 
document.scormAPI = getAPI();
if(document.scormAPI != null){
	document.scormAPI.LMSInitialize("");
}

/**
 * called by the main window onload
 */
function init(){
}

function end(){
	//tg 10/01/07 - only call these if the actual content has not been launched and we have only visited the sniffer page.
	if(document.contentLaunched == true){
		if(document.scormAPI){
			// ensure that the document in the frame has a scorm object defined - *EAC 10/01/08
			// don't do commit action if sco_content.html has been replaced with 'powered by intuition' or 'menu' page
			if (typeof (frames["index"].document.scorm) != "undefined") {
				//reach inside the content and call its scorm commit code.
				frames["index"].document.scorm.end();
			}
		}
	}
	//finish up with the LMS.
	if(document.scormAPI){
		document.scormAPI.LMSCommit("")
		document.scormAPI.LMSFinish("");
	}
}
function loadCourse(folder){
	
	var tmp = frames["index"].document.location.href;
	tmp = tmp.substring(tmp.lastIndexOf("/")+1,tmp.length);

	// handles the Start and Resume on the sniffer page or menu page and launching from the redirect page -- kgc 12/11/08
	if ((document.menuPgEnabled) && (tmp != "menu.html")) {
		if (snifferPagePreferences.snifferBookmark) {
			document.path = "data/" + folder + "/sco_content.html";
			document.interv = setInterval(loadCourseWA,100); //ie wont change the page without this work-around
		}
		else {
			document.path = "menu.html";
			document.interv = setInterval(loadCourseWA,100); //ie wont change the page without this work-around
		}				
	}
	else {
		document.path = "data/" + folder + "/sco_content.html";
		document.interv = setInterval(loadCourseWA,100); //ie wont change the page without this work-around
	}
}

function loadCourseWA(){
	clearInterval(document.interv);
	document.contentLaunched = true;
	frames["index"].document.location.href = document.path;
}

// removed resize functionality from sco.js as was not being used and added resize functionality 
// from index.js and adjusted for sco.js level so that any first page (index, menu, redirect) can use it -- kgc 11/28/08

/*
function getWidth(theWindow) {
  if (typeof(theWindow.innerWidth) == 'number') {
	//Non-IE
	return theWindow.innerWidth;
  } else {
	if (theWindow.document.documentElement && theWindow.document.documentElement.clientWidth) {
	  //IE 6+ in 'standards compliant mode'
	  return theWindow.document.documentElement.clientWidth;
	} else {
	  if (theWindow.document.body && theWindow.document.body.clientWidth) {
		//IE 4 compatible
		return theWindow.document.body.clientWidth;
	  }
	}
  }
}

function getHeight(theWindow) {
  if (typeof(theWindow.innerHeight) == 'number') {
	//Non-IE
	return theWindow.innerHeight;
  } else {
	if (theWindow.document.documentElement && theWindow.document.documentElement.clientHeight) {
	  //IE 6+ in 'standards compliant mode'
	  return theWindow.document.documentElement.clientHeight;
	} else {
	  if (theWindow.document.body && theWindow.document.body.clientWidth) {
		//IE 4 compatible
		return theWindow.document.body.clientHeight;
	  }
	}
  }
}


function resizeWindow(requiredWidth, requiredHeight) {
	
	var windowWidth = getWidth(window.top);
	var windowHeight = getHeight(window.top);
	var contentWidth = getWidth(this);
	var contentHeight = getHeight(this);

	var lmsWidth = windowWidth - contentWidth;
	var lmsHeight = windowHeight - contentHeight;
	
	var newWindowWidth = lmsWidth + requiredWidth;
	var newWindowHeight = lmsHeight + requiredHeight;

	if ((newWindowWidth > windowWidth) ||
		(newWindowHeight > windowHeight)) {
		window.top.resizeBy(newWindowWidth - windowWidth, newWindowHeight - windowHeight);
	}
}*/

// checkBrowser: Checks if the browser is IE5. And sets a document level variable accordingly.
// A much better way to do this would be to somehow introduce conditional behavious using conditional comments 
// (The way MS advises doing IE browser detection)
function checkBrowser(){
	document.isIE5x = false;
	if (navigator.appName.indexOf("Explorer")!=-1 && navigator.appVersion.indexOf("MSIE 5")!=-1) document.isIE5x = true;
}
//getViewPortHeight: Sets a global variable with the height available.
function getViewPortHeight(){
	checkBrowser();
	document.viewPort_h = (document.isIE5x) ? window.frames['index'].document.body.clientHeight : window.frames['index'].document.documentElement.clientHeight;
}
// getViewPortHeight: Resizes the window.
function resizeWindow(){
	if(screen.availWidth <=800){
		window.moveTo(-4,-4);
		window.resizeTo(802,602);
	} else if(screen.availWidth <=1024){
		window.moveTo(0,0);
		window.resizeTo(1024, screen.height-32);
	} else {
		var windowW = 1024; var windowH = 768;
		// center winow on the screen
		window.moveTo((screen.width-windowW)/2,(screen.height-windowH)/2);
		window.resizeTo(windowW,windowH);
	}	
}
// resizePageContents: Resizes the pageContents div so it shows a scroll bar properly. Scrollbars are supressed on the HTML/BODY elements.
function resizePageContents(){
	getViewPortHeight();
	var ePC = window.frames['index'].$('pageContents');
	ePC.style.height = document.viewPort_h - 10 +"px"; //the scroll bar on the page is suppressed with overflow:hidden. This line, makes sure the scroll bar on the pageContents div kicks in.
}