/*=========================================================================
                             fs_init.js

  This file should be included in frameset.htm (and nowhere else!).  It
  contains logic that should be executed when the Class.com frameset is
  initially loaded, or is refreshed or re-loaded.
  =========================================================================*/
 var I_AM_THE_CCFS = true; // Used to uniquely identify the scope of this window object.  Don't set this in any other window except the Class.com frameset!!
 var bAgent = window.navigator.userAgent;
 var bAppName = window.navigator.appName;

 var isNS  = (bAppName.indexOf("Netscape") >= 0);
 var isIE  = (bAppName.indexOf("Explorer") >= 0);
 var isWin  = (bAgent.indexOf("Win") >= 0); 
 var isMac  = (bAgent.indexOf("Mac") >= 0);

// Set default values for certain globals, if they weren't specified in names.js:
if (typeof FRAMESET_IN_POPUP == "undefined") FRAMESET_IN_POPUP = true // put in popup by default
if (typeof FRAMESET_CONTROLS=="undefined") FRAMESET_CONTROLS = FRAMESET_IN_POPUP  
if (typeof SAVE_DATA=="undefined") SAVE_DATA = true
if (typeof USE_COOKIES=="undefined") USE_COOKIES = false

//variable for return function
return_page="";  // Used only in courses where XML_MENU is true.

// The following code was designed to let user do a refresh without causing the course to restart.
// However, we never got it to work right.
/*
switch (getCookie("loading_state"))
{
  case "loading":
    init_course_preframe()
    break
  case "loaded":
    refresh_course_preframe()
    break
}
*/
// Instead, we will always call init_course_preframe instead of refresh_course_preframe
init_course_preframe()

//-------------------------------------------------------------------
function init_course_preframe()
{
  /*
    This function is called only when the loading_state cookie's value is "loading".
    i.e. only when the course is first loaded.  In particular, it's not called when
    the page is refreshed or reloaded.  It is called BEFORE the frameset's frames
    are loaded.  It initializes the following global variables,

      useCD
      CDRoot
      STARTUP_PARMS
      bread
      ext_page_iemac
      ie_page1
      ie_page2

    It also closes the "Use CD?" popup window, if it finds it.
  */

  var lms_window
  // Make sure certain required variables from names.js are available:
  var msg = ""
  if (typeof XML_MENU == "undefined")
    msg = "Expected a variable called 'XML_MENU' in the names.js file."
  else if (typeof XML_MENU != "boolean")
    msg = "Expected the 'XML_MENU' variable to have a true/false value."
  if (msg!="") alert(msg + "\nSome features may not work correctly.")
  
  STARTUP_PARMS = getArgs()
  // Find the name of the folder enclosing the frameset.htm file that includes this document.  This is assumed to be the
  // course root folder.
  var pattern = /([\/\\])([^\/\\]+)\1[^\/\\]+$/
  var matches = pattern.exec(location.pathname)
  var course = matches[2]
  STARTUP_PARMS.course = course
  if (STARTUP_PARMS.cdroot == null)
  {
    CDRoot = ""
    useCD = false
  }
  else
  {
    CDRoot = STARTUP_PARMS.cdroot
    useCD = true
  }
  
  //====================== TEMPORARY PATCH ==============================
  // (This may end up being permanent.)
  // Lately, self.top may be in a different domain than self (likewise
  // opener.top and opener).  Therefore, to prevent cross-domain security
  // errors, we will not attempt to reference self.top or opener.top.
  
  /* ------------ COMMENTED OUT
  if (opener)
  {
    lms_window = opener.top
  }
  else
  {
    // Assume this document was inserted into the LMS's frameset:
    lms_window = self.top
  }
    ------------- END COMMENTED OUT */
  lms_window = self //(meaningless but harmless
  //==================== END TEMPORARY PATCH ===========================
  
  if (typeof lms_init_course_preframe == "function")
  {
    // platform.js contains an lms_init_course_preframe function.  Let it set LMS_PLATFORM:
    LMS_PLATFORM = lms_init_course_preframe(lms_window)
    if (LMS_PLATFORM==null || typeof LMS_PLATFORM != "object")
      alert("function init_course_preframe: LMS_PLATFORM was not correctly initialized.  Some features may not work correctly.")
  }
  else
  {
    // There is no lms_init_course_preframe function.  Assume we're in Test Mode:
    LMS_PLATFORM = new Object()
    LMS_PLATFORM.name = ""
    LMS_PLATFORM.version = ""
  }
  bread = new Array(); // Used only in courses where XML_MENU is false.
  //variable for right-hand link menu change
  ext_page_iemac="";  // Used only in courses where XML_MENU is true.
  ie_page1="";  // ?Used only in courses where XML_MENU is true.
  ie_page2="";  // ?Used only in courses where XML_MENU is true.

  if (USE_COOKIES)
  {
    setCourseStateProp("LMS_PLATFORM", LMS_PLATFORM)
    setCourseStateProp("bread", bread)
    setCourseStateProp("ext_page_iemac", ext_page_iemac)
    setCourseStateProp("ie_page1", ie_page1)
    setCourseStateProp("ie_page2", ie_page2)
  }
  else
  {
    // Kill the existing course_state cookie:
    var pattern = /(.*[\/])[^\/]+[\/][^\/]+$/
    pattern.exec(location.pathname.replace(/\\/g, "/"))
    // RegExp.$1 now contains this document's path, up to but NOT INCLUDING the document's parent folder.
    var path = RegExp.$1 + STARTUP_PARMS.course
    killLongCookie("course_state", path)
  }
}
//-------------------------------------------------------------------
function refresh_course_preframe()
{
  /*
    This function is called only when the loading_state cookie's value is "loaded".
    It's called before the frameset's frames are loaded.
    It sets the following global variables:

      useCD
      CDRoot
      STARTUP_PARMS
      bread
      ext_page_iemac
      ie_page1
      ie_page2

    NOTE: Refreshing WON'T WORK if USE_COOKIES is false (see the alert below)
  */
  var courseStateObj = getCourseState()
  if (courseStateObj)
  {
    useCD = courseStateObj.useCD
    CDRoot = courseStateObj.CDRoot
    STARTUP_PARMS = courseStateObj.STARTUP_PARMS
    bread = courseStateObj.bread
    ext_page_iemac = courseStateObj.ext_page_iemac
    ie_page1 = courseStateObj.ie_page1
    ie_page2 = courseStateObj.ie_page2
  }
  else
  {
    //This probably means USE_COOKIES is false for this course.
    alert("function refresh_course: Unable to read course state information.  Some features may not work correctly.")
  }
  // Call the lms_refresh_course_preframe function, if it exists:
  if (typeof lms_refresh_course_preframe == "function") lms_refresh_course_preframe()
}
//=============================================================================================
function start()
{
  /*
    This function is the "onload" event handler for the frameset.  Therefore, it's called after
    all the frames are finished loading.
  */
  switch (getCookie("loading_state"))
  {
    case "loaded":
      if (typeof lms_refresh_course_postframe == "function") lms_refresh_course_postframe()
      break
    default: // cookie value == "loading"; or no cookie
      if (typeof lms_init_course_postframe == "function")
      {
        // The lms_init_course_postframe function is responsible for calling the finish_initial_loading function:
        lms_init_course_postframe()
      }
      else
      {
        // There is no lms_init_course_postframe function:
        finish_initial_loading()
      }
      break      
  }
}
//-------------------------------------------------------------------------------
function finish_initial_loading()
{
  // This is the last action that's taken during the initial loading of the course.

  if (!inSafari())
  {
    // Safari cannot correctly change the loading_state cookie back to "loading" if user switches units, etc.
    // This is because the logic which sets the state to "loading" is not within the course, and Safari can't
    // set a cookie with a path that's different from the setting document's path.  In other words, when user
    // switches units, we'll end up with two loading_state cookies, one saying "loading" and the other (older)
    // one saying "loaded".  But it's dangerous to start the course while the "loaded" version exists, because
    // things then won't get initialized properly.  THEREFORE, we shall not set the state to "loaded" (ever)
    // in Safari, and stuff will just have to re-initialize every time.  Probably not a bad idea in any case.
    
    // UPDATE -- We're abandoning the loading_state cookie for all browsers.
    // setCookie("loading_state", "loaded")
  }
  if (typeof load_pages == "function") load_pages()
}
//---------------------------------------------------------------
function getArgs()
{
  // Parses the querystring passed to this document, and returns an object
  // whose property names and property values match the name/value pairs
  // in the querystring.  Every property value will be a string (possibly
  // an empty string).  If there is no querystring, the function returns
  // an object with no properties.
  // NOTE: All property names in the returned object will be forced to lower case.
  var args = new Object()
  var query = location.search.substring(1) //strip the "?"
  var pairs = query.split("&")
  for (var i=0; i<pairs.length; i++)
  {
    var one_pair = pairs[i].split("=")
    var name = one_pair[0]
    if (name.length > 0)
    {
      if (one_pair.length > 1) var value = unescape(one_pair[1].replace(/\+/g," ")); else value = ""
      args[name.toLowerCase()] = value
    }
  }
  return args
}