//
// Function: SCOFind()
// Parameters: none
// Returns: handle to the SCO frame.
//			null if the frame cound not be found.
//
// Description: Locate the SCO framework. Code searches for the frame named "SCOFrame".
//
function SCOFind() {
  	
  	var SCOWindow = null;
  	var ancestorWindow = parent;
		if (document.location.href.indexOf("index.html") > 0 ) {
			// index page, go to opener parent before starting to look for 
			// API
			ancestorWindow = window.opener.parent
		}

    if (window.frames.length == 1) { 
      if (window.frames[0].name == "SCOFrame") {
        SCOWindow = window;
      }
    }  

  if (SCOWindow == null) {
    if (ancestorWindow.frames.length == 1) { 
      if (ancestorWindow.frames[0].name == "SCOFrame") {
        SCOWindow = ancestorWindow;
      } else {
        SCOWindow = do_find(ancestorWindow);
      }
    } else {
      SCOWindow = do_find(ancestorWindow);
    }
  }

	if ((SCOWindow == null) && (window.opener != null) && (typeof(window.opener) != "undefined")) {
		SCOWindow = do_find(window.opener.parent)
	}
	
	if (SCOWindow == null) {
		//alert("SCO frame could not be found");
	}

	if (SCOWindow == null){
		SCOWindow = new Object();
		SCOWindow.SCOSetLessonLocation = new Function();
		SCOWindow.SCODisplayResumeStudy = new Function();
		SCOWindow.SCONotifyPageTurn = new Function();
		SCOWindow.SCORecordPercentComplete = new Function();
		SCOWindow.DisplayIndexLink = new Function();	
		document.location.replace("accessdenied.html")
	}	

	return SCOWindow;
}

function do_find(ancestorWindow) {
	var SCOWindow = null;

	do {  // while we're not at the top level and we've not found it
		if (ancestorWindow.frames.length > 0) {
			if (ancestorWindow.name == "SCOFrame") { 
				// found the SCOFrame frame, it's parent is the SCOWindow
				SCOWindow = ancestorWindow.parent;
			} else {
				ancestorWindow = ancestorWindow.parent;
			}
		}
	} while ((ancestorWindow != ancestorWindow.parent) && (SCOWindow == null))

	return SCOWindow;
}
