//======================================================================
//  Knowledge Force Accelerator ELearning SCORM Environment
//======================================================================


//======================================================================
//  Public Objects
//======================================================================

var	API = null;



//======================================================================
//  Public Functions
//======================================================================

Object.extend( TTPortal, {
	
	SCOEnvironment : {
	
		options : {},	
		
		initEnvironment : function( options ) {
			
			this.options = {
				scoIdParam : "sco_id",
				scoUrlParam : "sco_url",
				storageSystemParam : "storage_system",
				acceleratorScormQueryParam : "acc_sco_query",
				wbtFrameName :	"wbtViewport"
			};
			Object.extend(this.options, options || { });
			
			if(window.frames.length > 0) {
			
				var objWBTFrame = window.frames[ this.options.wbtFrameName ];
				if( objWBTFrame ) {
					
					var sScoId = decodeURIComponent( TTPortal.Url.getParam( this.options.scoIdParam, null) );
					
					var sScoUrl = decodeURIComponent( TTPortal.Url.getParam( this.options.scoUrlParam, null ) );
					if(top == window) {
						// KF-15834
						sScoUrl += "&resizetop=true";
					}
					if( (sScoUrl.length == 0) || (sScoUrl == "undefined") ) {
						return( false );
					}
					
					var courseWindow = this.CourseDetector.getCourseWindow();
					if( courseWindow && courseWindow.API ) {
						var bResult = this.initLinkedEnvironment( courseWindow.API, sScoUrl, objWBTFrame )
						return( bResult );
					}

					var courseApi = null;
					var courseApiType = null;
					if( courseWindow && courseWindow.COURSE_DATA_API ) {
						courseApi = courseWindow.COURSE_DATA_API;
						if( typeof( courseApi.TTGetApiType ) != "undefined" ) {
							courseApiType = courseApi.TTGetApiType();
						}
					}
					
					if( TTPortal.MobileLearning.isRunningInMobileAppWithDataHandling() ) {
						var mobileAppCourseApi = ( courseApiType == "mobile_app_course" ) ? courseApi : null;
						var bResult = this.initMobileEnvironment( sScoId, sScoUrl, objWBTFrame, mobileAppCourseApi );
						return( bResult );
					}

					var sStorageSystem = TTPortal.Url.getParam( this.options.storageSystemParam, null );
					if( sStorageSystem.toLowerCase() == "tt_accelerator" ) {
						var acceleratorCourseApi = ( courseApiType == "accelerator_course" ) ? courseApi : null;
						var bResult = this.initAcceleratorEnvironment( sScoId, sScoUrl, objWBTFrame, acceleratorCourseApi );
						return( bResult );
					}
				}
			}

			return( false );
		},
		
		initLinkedEnvironment : function( objLinkedAPI, sScoUrl, objWBTFrame ) {

			this.linkScormAPI( objLinkedAPI );

/*			var sStorageSystem = TTPortal.Url.getParam( this.options.storageSystemParam, null );
			if( sStorageSystem.toLowerCase() == "scorm_lms" ) {
				sScoUrl = "../../../../" + sScoUrl;
			}
*/
			objWBTFrame.location.replace( sScoUrl );
			
			return( true );
		},
		
		initMobileEnvironment : function( sScoId, sScoUrl, objWBTFrame, objMobileAppCourseApi ) {

			var objMobileAPI = TTPortal.MobileLearning.lookupMobileApi();
			if( !objMobileAPI ) {
				return( false );
			}
			
			if( this.createMobileAppAPI( sScoId, objMobileAPI, objMobileAppCourseApi ) ) {
				objWBTFrame.location.replace( sScoUrl );
				return( true );
			}
			
			return( false );
		},
				
		initAcceleratorEnvironment : function( sScoId, sScoUrl, objWBTFrame, objAcceleratorCourseApi ) {
	
			var sAcceleratorScormQuery = decodeURIComponent( TTPortal.Url.getParam( this.options.acceleratorScormQueryParam, null) );
			if( (sAcceleratorScormQuery.length == 0) || (sAcceleratorScormQuery == "undefined") ) {
				return( false );
			}
			
			if( this.createAcceleratorAPI( sScoId, sAcceleratorScormQuery, objAcceleratorCourseApi ) ) {
				objWBTFrame.location.replace( sScoUrl );
				return( true );
			}
			
			return( false );
		},

		createMobileAppAPI : function( sScoId, objMobileAPI, objMobileAppCourseApi ) {
			API = new CMobileAppScormApi( sScoId, objMobileAPI, objMobileAppCourseApi );
			return( API != null );
		},

		createAcceleratorAPI : function( sScoId, sAcceleratorScormQuery, objAcceleratorCourseApi ) {
			API = new CAcceleratorScormApi( sAcceleratorScormQuery, sScoId, false, objAcceleratorCourseApi );
			return( API != null );
		},

		linkScormAPI : function( objAPI ) {
	
			if( objAPI ) {
				API = objAPI;
				return( true );
			}
			
			return( false );
		},

		getPortalApi : function() {

			var courseWindow = this.CourseDetector.getCourseWindow();
			if( !courseWindow || !courseWindow.document ) {
				return( null );
			}
						
			if( courseWindow.TTPortalAPI && (typeof(courseWindow.TTPortalAPI) != "undefined") ) {
				return( courseWindow.TTPortalAPI );
			}

			return( null );
		},
		
		CourseDetector : {
	
			nDepth : 0,
			nMaxDepth : 4,
	
			objCourseWindow : null,
			
			getCourseWindow : function() {
				
				if( this.objCourseWindow ) {
					return( this.objCourseWindow );
				}

				this.objCourseWindow = this.findCourseWindow( window );
				if( this.objCourseWindow ) {
					return( this.objCourseWindow );
				}
				
				this.objCourseWindow = this.findCourseWindow( window.opener );
				return( this.objCourseWindow );
			},
			
			findCourseWindow : function( win ) {
			
				if( !win ) {
					return( null );
				}
				
				if( win && win.TTPortal && win.TTPortal.Course && win.TTPortal.Course.Instance ) {
					this.nDepth = 0;
					return( win );
				}

				if( win && win.TTPortalCourse && win.TTPortalCourse.Instance ) {
					this.nDepth = 0;
					return( win );
				}
				
				this.nDepth++;
				if( this.nDepth >= this.nMaxDepth ) {
					this.nDepth = 0;
					return( null );
				}
				
				if( win.parent ) {
					return( this.findCourseWindow( win.parent ) );
				}
				
				return( null );
			}
		}
	}
});

/***********************************************************************
  this API provide the main portal functions to a launched sco
***********************************************************************/
var TTPortalAPI = {
		
	Course : {	
		
		getStructureInfo : function() {

			var objApi = TTPortal.SCOEnvironment.getPortalApi();
			if( !objApi || !objApi.Course ) {
				return( null );
			}

			return( objApi.Course.getStructureInfo() );
		},
	
		processAction : function( sAction ) {

			var objApi = TTPortal.SCOEnvironment.getPortalApi();
			if( !objApi || !objApi.Course ) {
				return( null );
			}
			
			return( objApi.Course.processAction( sAction ) );
		},

		getCertificateUrl : function() {
			var objApi = TTPortal.SCOEnvironment.getPortalApi();
			if( !objApi || !objApi.Course ) {
				return("");
			}

			return( objApi.Course.getCertificateUrl() );
		},
		
		terminateSco : function() {

			var objApi = TTPortal.SCOEnvironment.getPortalApi();
			if( !objApi || !objApi.Course ) {
				return;
			}

			objApi.Course.terminateSco();
		},

		resizeScoWindow : function( nWidth, nHeight ) {

			var nWindowWidth = Number(nWidth),
				nWindowHeight = Number(nHeight)

			// KF-17973
//			jQuery("#viewportWbt").css({
//				"width" : nWindowWidth + "px",
//				"height" : nWindowHeight + "px"
//			});
			
			TTPortal.Browser.resizeBrowserWindow( window, nWindowWidth, nWindowHeight );

			return true;
		},
	
		closeScoWindow : function() {
			this.terminateSco();
			TTPortal.Browser.closeBrowserWindow();
			return true;
		}
	}
};


//======================================================================
//TTPlayer events
//======================================================================

var OnTTELearningResize = function( nWidth, nHeight ) {
	TTPortalAPI.Course.resizeScoWindow( nWidth, nHeight );
};

var OnTTELearningClose = function() {
	TTPortalAPI.Course.closeScoWindow();
};


//======================================================================
//  Page events
//======================================================================

Event.observe( document, "dom:loaded", function(e) {
	
	window.moveTo(0,0);
	
	if( !TTPortal.SCOEnvironment.initEnvironment() ) {
		alert("!!! ERROR WHILE CREATING SCORM-API !!!");
	}
});

Event.observe( window, "unload", function(e) {
	TTPortalAPI.Course.terminateSco();
});