//TODO: Get rid of SCO.  See newListing.jsp for code that can handle acro/sco.

lobWindows = new Windows();

/**
 * Launch a Learning Object
 * 
 * @version 0.1 prototype, 2004-02-04
 * @summary             Utility function.  Calls LaunchLob with a Param
 * @author              Muness Alrubaie
 * @interface           <code>LaunchCourse(course, accessibleFlag, launchEnvironment, courseType, SCO, is3rdLMS, fullpg)</code>
 * @param params Params: Parameter list.  "acro", "access", "env", "type" and "3rd" should be set.  See LaunchLob, below.
 * @param fullpg (Optional) String: Page to launch in the course.
 * @param windowParms (Optional) String: Set of window parameters as supplied to browser's window.open() method.
 * @type void
 */
function LaunchLobParams(params, fullpg, windowParms)
{
	var TF_LAUNCH_URL="/lms/learn/launchLob.jsp";
	var ML_LAUNCH_URL="/dpec/learn/launchLob.jsp";
	var CB_LAUNCH_URL="/dpec/scripts/launchlob.exe";

	var course = params.get("acro");
	if (params.get("sco") == "null")
		params.remove("sco");
	if (params.get("sco") != null)
		course = params.get("sco");
			
	var launchEnvironment = params.get("env");

	var accessibleFlag = false;
	if (params.get("access") == "true" || params.get("access") == "1")
		accessibleFlag = true;
		
	var courseType = params.get("type");
	
	var is3rdLMS = false;
	if (params.get("3rd") == "true" || params.get("3rd") == "1")
		is3rdLMS = true;
		
	var isRcb = false;
	if (params.get("rcb") == "true" || params.get("rcb") == "1")
		isRcb = true;

	var thePage = null;
	var theLmsObject = null;

	if (launchEnvironment == "TF" && !is3rdLMS)
	{
		thePage = new MlPage();
		theLmsObject = thePage.getLmsObject();

		// This is a workaround for the special browser for Clearly Business
		var lcookie = thePage.getCookie("logincookie");
		if (lcookie != null)
			params.put("tk", lcookie);
	}

	var acro = null;
	var launchSco = false;
	var type = null;

	if (course == null)
		course = "";
		
	type = getType(course);
	if (type != "ML" && type != "AICC")
		courseType = type;
		
	//isSco and getAcroFromId require MlClientPage.js to be included
	if (isSco(course)) {
		//Launching a sco
		launchSco = true;
		SCO = course;
		acro = getAcroFromId(course);
	} else {
		acro = course;
	}

	if (typeof(launchEnvironment) == "undefined" || launchEnvironment == null) 
	{
		launchEnvironment = "TF";
	}
	if (typeof(accessibleFlag) == "undefined" || accessibleFlag == null) 
	{
		accessibleFlag = false;
	}
	if (typeof(courseType) == "undefined" || courseType == null) 
	{
	 if (type != null) {
	  courseType = type;
	 } else { 
		 courseType = "ML";
	 }
	}
	if (typeof(is3rdLMS) == "undefined" || is3rdLMS == null) {
		is3rdLMS = false;
	}

	launchEnvironment = launchEnvironment.toUpperCase();
	courseType = courseType.toUpperCase();
	
	var courseWindow = lobWindows.get(acro, launchEnvironment, courseType, is3rdLMS, isRcb, windowParms);

	if (typeof(courseWindow.coursewareHandler) != "undefined" && 
		typeof(courseWindow.coursewareHandler.wndContent) != "undefined" )
	{
		//If this coursewindow is open and its coursewareHandler is available
		var contentFrame = courseWindow.coursewareHandler.wndContent;
		if (launchSco == true) 
		{
			//if sco is passed in, retrieve its fullpg
			fullpg = courseWindow.coursewareHandler.getScoFullPage(SCO);
		}
		if (fullpg != null) 
		{
			contentFrame.location=fullpg;
			if (theLmsObject != null)
				theLmsObject.onLobLaunch(courseWindow);

			return;
		}
	}
	else if (fullpg != null)
	{
		params.put("fullpg", fullpg);
	}
	
	params.put("acro", acro);
	params.put("env", launchEnvironment);

	params.put("type", courseType);
	params.put("3rd", is3rdLMS ? "1":"0");
	params.put("access", accessibleFlag ? "1":"0");

	if (launchSco == true) {
		params.put("sco", SCO);
	}
	
	//String to pass on
	var appendToURL = params.toURLString();
	
	var url;
	if(launchEnvironment == "TF") {
		url=TF_LAUNCH_URL;
	} else if (launchEnvironment == "ML") {
		url=ML_LAUNCH_URL;
	} else if (launchEnvironment == "CB") {
		url=CB_LAUNCH_URL;
	}
	
	//alert("url=" + url + "\nqstr="+appendToURL)
	
	courseWindow.location.href= url + "?" + appendToURL;
	if (theLmsObject != null)
		theLmsObject.onLobLaunch(courseWindow);
}

/*
 * @param course String: Course acronym or SCO (<acro>_<section>).  To launch REFLIB, set this to "" and use courseType="REFL"
 * @param parms (Optional): "key1=val1&key2=val2&key3=val3", etc.
 * @param fullpg (Optional) String: If LaunchLob is called from a Course, the content frame's ("course_window") location is set to this
 * @param windowParms (Optional) String: Set of window parameters as supplied to browser's window.open() method.
*/
function LaunchLobBuildParams(parms, fullpg, windowParms)
{
	var params = new Params();
	var items = parms.split("&");
	for(var i=0;i<items.length;i++)
	{
		var term = items[i].split("=");
		params.put(term[0], term[1]);
	}
	LaunchLobParams(params, fullpg, windowParms);
}

/**
 * Launch a Learning Object
 * 
 * @version 0.2 functional, 2004-02-04
 * @summary             Starting point for launching a lob (Learning Object)
 * @author              Muness Alrubaie
 * @interface           <code>LaunchCourse(course, accessibleFlag, launchEnvironment, courseType, SCO, is3rdLMS, fullpg)</code>
 * @param course String: Course acronym or SCO (<acro>_<section>).  To launch REFLIB, set this to "" and use courseType="REFL"
 * @param accessibleFlag (Optional) Boolean: Force accessible version.  Defaults to false.
 * @param launchEnvironment (Optional) String: Launched from.  May be: ML, TF, CB.  Defaults to "TF".
 * @param courseType (Optional) String: Type of course.  May be: ML, BSV, LAB, AICC, SCORM, DUM, HIPPA, MLC, REFL, CCE. Defaults to "ML".
 * @param is3rdLMS (Optional) boolean: Launched from third party LMS.  Defaults to false.
 * @param fullpg (Optional) String: If LaunchLob is called from a Course, the content frame's ("course_window") location is set to this
 * @param windowParms (Optional) String: Set of window parameters as supplied to browser's window.open() method.
 * @type void
 */
function LaunchLob(course, accessibleFlag, launchEnvironment, courseType, is3rdLMS, fullpg, windowParms)
{
	var params = new Params();
	
	params.put("acro", course);
	if (typeof(accessibleFlag) != "undefined" && accessibleFlag != null)
		params.put("access", accessibleFlag);
	if (typeof(launchEnvironment) != "undefined" && launchEnvironment != null)
		params.put("env", launchEnvironment);
	if (typeof(courseType) != "undefined" && courseType != null)
		params.put("type", courseType);
	if (typeof(is3rdLMS) != "undefined" && is3rdLMS != null)
		params.put("3rd", is3rdLMS);

	LaunchLobParams(params, fullpg, windowParms);
}

function lobNotAvailable()
{
	alert("The course is no longer available." );
	return;
}

 function getType(id) {
 	var type = null;
		switch (id.substring(0,2)) 
		{
			case 'd_':
				type = "DUM";
				break;
			case 'c_':
				type = "MLC";
				break;
			case 'v_':
				type = "BSV";
				break;
			case 'f_':
				type = "MLF";
				break;
			default:
				type = "ML";
		}
	 if (id.toLowerCase().substring(0,4) == "aicc")
		 type = "AICC";
		 
		return type;
 }

function getScreenWidth()
{
	return screen.availWidth - 10;  // for side borders
}

function getScreenHeight(isStatus)
{
	if (typeof(isStatus) == "undefined")
		isStatus = true;

	var offset = isStatus ? 57 : 30;
	return screen.availHeight - offset; // for caption and status
}

function getLeft()
{
	var left = (getScreenWidth() - getWidth()) / 2;
	if (left < 0)
		left = 0;

	return left;
}

function getTop()
{
	var top = (getScreenHeight() - getHeight()) / 2;
	if (top < 0)
		top = 0;

	return top;
}
	
function getWidth()
{
	var width = 800;
	width = (getScreenWidth() < width) ? getScreenWidth() : width;

	return width;	
}

function getHeight()
{
	var height = 600;
	height = (getScreenHeight() < height) ? getScreenHeight() : height;

	return height;
}

/**
 * Stores a list of opened windows so that they can be reused
 *
 * @constructor
 * Constructor.
 *
 * @author              Muness Alrubaie
 * @version             0.1, prototype, 2004-02-06
 * @interface           <code>new Windows()</code>
 */
function Windows() {
	this.cache = new Array();
}

/**
 * Helper function to get a lob window
 *
 * @summary             Returns a handle to the window to use.  This may be a child window, parent window, or a new window
 * @author              Muness Alrubaie
 * @interface           <code>Windows.get(course, env, coursetype, is3rd)</code>
 * @param course String: Course acro
 * @param env String: Environment
 * @param coursetype String: Type of course
 * @param is3rdLMS boolean: Whether the launch was from a 3rd party LMS
 * @param windowParms (Optional) String: Set of window parameters as supplied to browser's window.open() method.
 * @return Handle to the Window (see DHTML spec)
 * @type Window 
 */
function Windows_get(course, env, courseType, is3rdLMS, isRcb, windowParms)
{
	if (env=="TF") 
	{
		winName=env + "_" + course.replace(/^[a-zA-Z0-9_]+\.([a-zA-Z0-9_-]+)\.[a-zA-Z0-9_]+$/,"$1");
		winName=winName.replace("-","");
	}
	else winName=env + "_" + course;
	if (courseType == "REFL") {
		winName = "reflib";
	}

	if (typeof(this.cache[winName]) != "undefined" && this.cache[winName] != null && this.cache[winName].closed == false)
	{
		return this.cache[winName];
	}

	try {
		if (typeof(window.opener) != "undefined" && window.opener.closed == false && 
			(window.opener.name == winName || window.name == winName+"_progrpt_window" || window.name == winName+"_shresults") ) {
				this.cache[winName]=window.opener;
				return window.opener;
		}
	}
	catch(e) {
		// Handling domain security exception for window.opener.name when launching from tms
	}
	
	var courseWindow;
	if (!is3rdLMS && !isRcb) 
	{
		if (courseType == "REFL")
		{
			//Reference Library gets a regular window
			courseWindow = window.open("about:blank",winName);
		}
		else if (courseType == "NCC")
		{
			courseWindow = window.open("about:blank",winName,windowParms);
		}
		else if (courseType == "DL")
		{
			courseWindow = top.worker;
		}
		else
		{
			var winParms;
			if (windowParms == "fullscreen")
				winParms = windowParms;
			else
			{
				winParms = "scrollbars=1,status=1,resizable=1";
				if (courseType == "ML" || courseType == "BSV") winParms = "scrollbars=1,status=0,resizable=1"; // turn off status for June05
				if (courseType == "MLF" && (typeof(accessibleFlag) == "undefined" || !accessibleFlag) ) winParms = "scrollbars=0,status=0,resizable=1"; // turn off scrollbars for f_ courses
				winParms += ",width=" + getWidth() + ",height=" + getHeight() + ",top=" + getTop() + ",left=" + getLeft();
			}
			courseWindow = window.open("about:blank",winName,winParms);
		}
			
	} 
	else
	{
		//For 3rd party LMS, use current window
		courseWindow = window;
		courseWindow.name = winName;
	}
	
	this.cache[winName]=courseWindow;	
	return courseWindow;
	
}
Windows.prototype.get = Windows_get;
