/*
	Copyright (c) 2003-2004 Element K LLC. All rights reserved.
	The following parameters are unique to this brand
*/
var whichLoader = "splash/loader.swf";
var whichWideLoader = "splash/loader_wide.swf";
var activityWidth = 1005;
var activityHeight = 710;
var overrideLmsBrand = true;
var startDate = new Date().getTime();

/**********************************************************
	NOTE: ALL CODE AFTER THIS POINT IS THE SAME FOR THE
	SPLASH.JS FILE AND ALL THE SPLASH_{BRAND}.JS FILES
**********************************************************/
var objSize = checkWindowSize(activityWidth,activityHeight);
activityWidth = objSize.width;
activityHeight = objSize.height;

//var scoURL =  unescape( document.location );
//******************
//** HTM LOCATION **
//******************
var docLoc = unescape( document.location );
//************************************************
//** if child course, set required variables **
//************************************************
if(typeof(parentCourse)!="undefined" && typeof(childCourse)!="undefined"){

	//*********************
	//** PARENT PATH **
	//*********************
	//** replace all references to childCourse with parent course.
	var regEx = new RegExp("("+childCourse+")","gi");
	var str_parentPath = docLoc.replace(regEx, parentCourse);

	//************************
	//** DELTA PLAYLIST PATH
	//************************
	var file_name = escape(docLoc.substring( docLoc.lastIndexOf("/")+1, docLoc.lastIndexOf( "." ) ));
	var delta_playlistPath = escape(docLoc.substring( 0, docLoc.lastIndexOf( "/" )+1 ) + file_name+".xml");

	//******************************
	// ** CHILD COURSE DIRECTORY ***
	//******************************
	var child_scoDir = docLoc.substring( 0, Math.max(docLoc.lastIndexOf( "/" ), docLoc.lastIndexOf( "\\" )) ) + "/";

	var scoURL = unescape(str_parentPath);
}else{
	var scoURL = docLoc;
}

var queryLocation = scoURL.indexOf("?");
if( queryLocation >= 0 )
{
	if( scoURL.indexOf( "boolUseDevGlobals" ) >= 0 ){
		pathToGlobal = pathToGlobal.replace( "global", "global_dev" );
		if ( typeof(brandKey) != "undefined" ){
			var strBrand = brandKey;
		}
		else{
			var strBrand = "elementk";
		}
		brandKey = prompt( "Please provide the brandkey you wish to use", strBrand );
	}
	scoURL = scoURL.substring( 0, queryLocation );
}
var playlistPath = escape(scoURL.substring( 0, scoURL.lastIndexOf( "." ) ) + ".xml");
var scoDir = scoURL.substring( 0, Math.max(scoURL.lastIndexOf( "/" ), scoURL.lastIndexOf( "\\" )) ) + "/";
if( typeof(pathToGlobal) == "undefined"  )
{
	pathToGlobal = "";
}
var strLoaderPath = pathToGlobal + whichLoader;
var boolAddStarted = false;
var strXml = "";

function setBrandKey() {
	if ( typeof(brandKey) != "undefined" )
	{
		if ( typeof(strScorm) != "undefined" )
		{
			if( overrideLmsBrand == true || strScorm.indexOf('elementk.brand_element.brand_name') < 0 )
			{
				strScorm += escape('elementk.brand_element.brand_name=' + brandKey);
			}
		}
		else
		{
			strScorm = escape('elementk.brand_element.brand_name=' + brandKey);
		}
	}
}

var strCodeBase = 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,79,0"';
if( document.location.href.indexOf("https") == 0 )
{
	strCodeBase = 'codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,79,0"';
}

function writeContent() {
	if( pathToGlobal == "" )
	{
		document.write('<body marginwidth="0" marginheight="0" leftmargin="0" topmargin="0" onUnload="closeLMSCommunication();">');
		document.write('We are sorry. A file is missing from our system preventing us from launching this content correctly. Please contact technical support to report this issue.');
		document.write('<font color="red" size="-1">developer note: pathToGlobal is undefined</font>');
		document.write('</body>');
	}
	else
	{
		var boolIsChildFrame = false;
		if( window.parent != window )
		{
			if( typeof window.parent.pathToGlobal != "undefined" )
			{
				boolIsChildFrame = true;
			}
		}
		if( window.location.search.toLowerCase().indexOf( "noscroll" ) >=  0 )
		{
			boolIsChildFrame = true;
		}

		if( !boolIsChildFrame )
		{
		        //qsUseScrolling is defined in popup.jsp in the Quality System. This variable is used to indicate whether
		        //  scroll bars should be turned on or not in the Splash content. If the Splash content is not run from within
		        //  the Quality System site, then the default value of no scrolling should be set.
		        var enableScrolling = "no";
		        if (typeof (top.qsUseScrolling) !="undefined"){
		            if(top.qsUseScrolling == true){
		                enableScrolling = "yes";
		            }
		        } 
		        
				document.write('<frameset rows="100%,*" frameborder="no" border="0">');
				document.write('<frame name="content" marginwidth="0" marginheight="0" noresize scrolling="' + enableScrolling + '" frameborder="0" src="' + window.location.href + '?noscroll">');
				//document.write('	<frame name="blank"  marginwidth="0" marginheight="0" noresize scrolling="no" frameborder="0" src="blank.htm">');
				document.write('</frameset>');
		}
		else
		{
			if (typeof (runCrawler) !="undefined")
			{
				runCrawler();
			}
			else
			{
				strScorm="";
			}
			setBrandKey();

			var apiFound = "false";
			if( typeof(apiHandle) != "undefined" )
			{
				apiFound = "true";
			}

			//************************
			//** MOVIE INPUT ***
			//************************
			if(typeof(parentCourse)!="undefined" && typeof(childCourse)!="undefined"){
				var movieInput	= 'childScoDir='+ child_scoDir + '&childPartNumber=' + childCourse + '&playlist=' + playlistPath + '&deltaList='+delta_playlistPath + '&lmsData=' + strScorm + '&apiFound=' + apiFound + '&basePath=' + escape( window.location ) + '&queryString=' + escape(window.location.search);
			}else{
				var movieInput	= 'playlist=' + playlistPath + '&lmsData=' + strScorm + '&apiFound=' + apiFound + '&basePath=' + escape( window.location ) + '&queryString=' + escape(window.location.search);
			}
			
			var swfHeight = "100%";
			var swfWidth = "100%";
			
			//The variables useCustomSize, customWidth, and customHeight can be manually defined in brandkey.js
			//  for purposes of forcing the content launched with splash.js to be a certain size. Currently, splash disables
			//  scrollbars since it is designed to auto size. The size does not affect any activities, or other pieces that launch
			//  outside the main splash wrapper.
			if( typeof(useCustomSize) != "undefined"){
			    if( useCustomSize == true && typeof( customWidth ) != "undefined" && typeof( customHeight) != "undefined" ){
			        swfHeight = customHeight;
			        swfWidth = customWidth;
			    }
			}

			document.write('<body bgcolor=#FFFFFF marginwidth="0" marginheight="0" leftmargin="0" topmargin="0" onUnload="closeLMSCommunication();" onLoad="window.document.splashLoader.focus();">');
			document.write('<object id="splashLoader" tabindex="-1" width=' + swfWidth + ' height=' + swfHeight + ' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ' + strCodeBase + '>');
			document.write('<param name=Movie value="' + strLoaderPath + '">');
			document.write('<param name="FlashVars" value="' + movieInput + '">');
			document.write('<param name="quality" value="high">');
			document.write('<param name=salign value=LT>');
			document.write('<param name="allowScriptAccess" value="always">');

			//*********************************************
			// ** SETTING THE BASE PATH TO PARENT COURSE **
			//*********************************************
			if(typeof(parentCourse)!="undefined" && typeof(childCourse)!="undefined"){
				document.write('<param name="BASE" value="'+scoDir+'">');
				document.write('<embed allowScriptAccess="always" BASE="'+scoDir+'"src="' + strLoaderPath + '" FlashVars="' + movieInput + '" name="splashLoader" salign=LT width="100%" height="100%" quality=high bgcolor=#FFFFFF swLiveconnect="True" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></embed>');
			}else{
				document.write('<embed allowScriptAccess="always" src="' + strLoaderPath + '" FlashVars="' + movieInput + '" name="splashLoader" salign=LT width="100%" height="100%" quality=high bgcolor=#FFFFFF swLiveconnect="True" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></embed>');
			}

			document.write('</object>');
			document.write('</body>');
		}
	}
}

function openExercise(file, mode, strTitle) {
    //var exerciseDir = scoDir;
    //*******************************
    //** FOR CUSTOMIZEABLE CONTENT - if xml file name already contains path to child course, dont include directory path. **
    //*******************************
    if(typeof(childCourse)!="undefined"){
                	if(file.match(childCourse)==null){
                		var exerciseDir = scoDir;
                	}else{
                 		var exerciseDir = "";
                	}
            }else{
                var exerciseDir = scoDir;
    }

    var pipeLocation = exerciseDir.indexOf("|");
    if( pipeLocation >= 0 )
    {
	    exerciseDir = exerciseDir.substring(0,pipeLocation) + ":" + exerciseDir.substring(pipeLocation+1)
    }
    activityInput = 'lmsData=' + strScorm + '&file=' + exerciseDir +file + '&mode=' + mode + '&type=handson';
    activityMode = mode;
    activityTitle = strTitle;
	var newWindow = window.open( pathToGlobal+"splash/assets/activity.htm", mode, "width=" + activityWidth  + ",height=" + activityHeight + ",screenX=0,left=0,screenY=0,top=0,alwaysRaised=0,dependent=0,directories=0,location=0,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0" )
}

function writeActivity()
{
var strDocWrt1 = '<object id="splashLoader" tabindex="-1" width="100%" height="100%" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ' + strCodeBase + '>'
var strDocWrt2 = '<param name=Movie value="' + pathToGlobal + whichWideLoader  + '">'
var strDocWrt3 = '<param name="FlashVars" value="' + activityInput + '">'
var strDocWrt4 = '<embed src="' + pathToGlobal + whichWideLoader  + '" FlashVars="' + activityInput + '" name="splashLoader" salign=LT width="100%" height="100%" quality=high bgcolor=#FFFFFF swLiveconnect="True" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></embed>'

	document.write('<body bgcolor=#FFFFFF marginwidth="0" marginheight="0" leftmargin="0" topmargin="0" onLoad="window.document.splashLoader.focus();" >');
	document.write(strDocWrt1);
	document.write(strDocWrt2);
	document.write(strDocWrt3);
	document.write('<param name="quality" value="high">');
	document.write('<param name="salign" value="LT">');
	document.write(strDocWrt4);
	document.write('</object></body>');
}

function addToExpertFeedbackXml(strAdd) {
	if (boolAddStarted == false)
	{
		strXml = "";
	}
	boolAddStarted = true;
	strXml += strAdd;
}

function openExpertFeedback(strUrl,  strWinName, strWinOptions) {
	boolAddStarted = false;
	var newWindow = window.open("", strWinName, strWinOptions);
	newWindow.document.open();
	newWindow.document.write( "<html>" );
	newWindow.document.write( "<head><title>Submit For Expert Feedback</title></head>" );
	newWindow.document.write( "<body bgcolor=#FFFFFF marginwidth='0' marginheight='0' leftmargin='0' topmargin='0' >" );
	newWindow.document.write( "<form name='hiddenForm' method='post'>" );
	newWindow.document.write( "<input type='hidden' value='" + strXml +  "'name='vparam1'>" );
	newWindow.document.write( "</form>" );
	newWindow.document.write( "</body></html>" );
	newWindow.document.hiddenForm.action = strUrl;
	newWindow.document.hiddenForm.submit();
	newWindow.document.close();
}

function openWindow(href) {
	var newWindow = window.open( href, "mindson", "width=" + activityWidth + ",height=" + activityHeight + ",screenX=0,left=0,screenY=0,top=0,alwaysRaised=0,dependent=0,directories=0,location=0,menubar=0,resizable=1,scrollbars=0,status=0,toolbar=0" );
}

function openPopup(href , winName, popWidth, popHeight) {
var newWindow = window.open( pathToGlobal + href, "Name" , "width=" + popWidth + ",height=" + popHeight + ",screenX=0,left=0,screenY=0,top=0,alwaysRaised=0,dependent=0,directories=0,location=0,menubar=0,resizable=1,scrollbars=0,status=0,toolbar=0" );
}

function checkWindowSize(intWidth, intHeight)
{
  if (screen.availWidth < intWidth || screen.availHeight < intHeight)
  {
    intHeight =  screen.availWidth * intHeight/intWidth;
    intWidth = screen.availWidth;
  }
  return {height:intHeight,width:intWidth};
}

function closeLMSCommunication()
{
	if( typeof(apiHandle) != "undefined" )
	{
		LMSSetValue( "cmi.core.exit", "" );
		recordTime();
		LMSCommit( "" );
		LMSFinish( "" );
	}
}

function recordTime() {
  var formattedTime = "00:00:00";
  if ( startDate != 0 )
  {
    var currentDate = new Date().getTime();
    var elapsedSeconds = ( (currentDate - startDate) / 1000 );
    var formattedTime = convertTotalSeconds( elapsedSeconds );
  }
  LMSSetValue( "cmi.core.session_time", formattedTime );
}

function convertTotalSeconds( ts )
{
  var remainder = ts % 3600;
  var hours = "" + ( ts - remainder ) / 3600;
  remainder = remainder % 60;
  var minutes = "" +  ( ts - hours * 3600 - remainder ) / 60;

  var seconds = "" + Math.round( remainder );
  if ( seconds == "60" ) {
    seconds = "00";
    minutes = "" + (minutes + 1);
  }
  while ( hours.length < 2 ) hours = "0" + hours;
  while ( minutes.length < 2 ) minutes = "0" + minutes;
  while ( seconds.length < 2 ) seconds = "0" + seconds;
  return hours + ":" + minutes + ":" + seconds;
}

function textVersion()
{
	window.onunload = null;
	document.write('<frameset rows="100%" frameborder="no" border="0">');
	document.write('<frame name="textVersionFrame" marginwidth="0" marginheight="0" noresize scrolling="yes" frameborder="0" src="'+pathToGlobal +'508/blank.htm">');
	document.write('</frameset>');
	document.write('<script language="JavaScript">');
	document.write('parent.textVersionTransform()');
	document.write('<\/script>\n');

}

function textVersionTransform ()
{
	var strTransform = pathToGlobal + "508/508transform.xsl";
	var xml = new ActiveXObject('Microsoft.XMLDOM');
	xml.async = false;
	xml.load(unescape(playlistPath) );

	var xsl = new ActiveXObject('Microsoft.XMLDOM');
	xsl.async = false;
	xsl.load(strTransform);
	content.textVersionFrame.document.write(xml.transformNode(xsl));
}
