/*
		SCO Function Library
		"SCOFunctions.js"
		
		Copyright (c) 2005.  Academic ADL Co-Laboratory.
		 
		This file is part of libSCORM 2004.  libSCORM 2004 is proprietary software for use only 
		by the Academic ADL Co-Laboratory and its clients.  Please read the full license agreement
		included with this software to learn whether you may legally use this software.
		
		libSCORM 2004 is distributed in the hope that it will be useful,
		but WITHOUT ANY WARRANTY; without even the implied warranty of
		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
		
		To obtain a copy of the license agreement, please write to:
		
		Academic ADL Co-Laboratory
		222 W. Washington Ave., Suite 470
		Madison, WI 53703
*/


var init = false;
var objective_ids = null;
var interaction_ids = null;
var loaded = false;

var timeElapsed = 0;

function incrementTime() {
	timeElapsed += 1;
}

function saveTimeElapsed() {
	var result;
	
	if(timeElapsed == 0) {
		result = "PT0S";
	}
	else {
		result = "P";
		var tmp = timeElapsed;
		var days = tmp / 86400;
		tmp %= 86400;
		var hours = tmp / 3600;
		tmp %= 3600;
		var minutes = timeElapsed / 60;
		tmp %= 60;
		var seconds = tmp;
		if(Math.floor(days) > 0 ) result += Math.floor(days) + "D";
		result += "T";
		if(Math.floor(hours) > 0 ) result += Math.floor(hours) + "H";
		if(Math.floor(minutes) > 0) result += Math.floor(minutes) + "M";
		if(seconds > 0) result += seconds + "S";
	}
	
	try {
		LMSSetValue("cmi.session_time", result);
	}
	catch(x) {}
}

function load() {
	if(!loaded) {	
		if(!init) {	
			try {
				LMSInitialize();
				init = true;
			}
			catch(x) {
			}
		
			if(init) {
				setInterval(incrementTime, 1000);
				update_objectives();
				update_interactions();
			
  				if(typeof(onInitialized) == "function") {
  					onInitialized();
  				}
  			}
		}
	
		loaded = true;
	
  		if(typeof(onLoaded) == "function") {
  			onLoaded();
  		}
  	}
}


function unload(force) {
	if(loaded) {
		loaded = false;
		if(init) {
		
			saveTimeElapsed();
		
			if(force) {
				try {
					if(LMSGetValue("cmi.completion_status") != "completed") {
						LMSSetValue("cmi.completion_status","incomplete");
					}
				}
				catch(x) {}
				
				
				try {
					LMSSetValue("cmi.exit","suspend");
				}
				catch(x) {}
				
			}
			else {
				try {
					LMSSetValue("cmi.completion_status","completed");
				}
				catch(x) {}
				
				try {
					LMSSetValue("cmi.exit","normal");
				}
				catch(x) {}
			}
			
			if(typeof(onBeforeTerminate) == "function") {
				onBeforeTerminate();
			}
		
			try {
				LMSTerminate();
				init = false;

			}
			catch(x) 
			{
			}
		}
		
		if(typeof(onUnloaded) == "function") {
			onUnloaded();
		}
	}
}

function update_objectives ( ) {

	if ( init == true ) {
		var objective_count = 0;
		
		try {
			objective_count = LMSGetValue("cmi.objectives._count");
		}
		catch(x) {}		

		if ( objective_count > 0 ) {
			objective_ids = null;
			objective_ids = new Array();

			for ( i = 0; i < objective_count; i++ ) {
				try {
					objective_ids[i] = LMSGetValue( "cmi.objectives." + i + ".id" );
				}
				catch(x) {
					objective_ids = null;
					return;
				}
			}
		}
	}
}

function update_interactions ( ) {

	if ( init ) {
		var interaction_count = 0;
		try {
			interaction_count = LMSGetValue("cmi.interactions._count");
		}
		catch(x) {}

		if ( interaction_count > 0 ) {
			interaction_ids = new Array();

			for ( i = 0; i < interaction_count; i++ ) {
				try {
					interaction_ids[i] = LMSGetValue( "cmi.interactions." + i + ".id" );
				}
				catch(x) {
					interaction_ids = null;
					return;
				}
			}
		}

	}

}

function get_objective_by_id(str_id) {
	if ( objective_ids != null ) {
		for ( i = 0; i < objective_ids.length; i++ ) {
			if ( str_id == objective_ids[i] ) {
				return i;
			}
		}
	}
	return -1;
}

function get_interaction_by_id(str_id) {
	if ( interaction_ids != null ) {
		for ( i = 0; i < interaction_ids.length; i++ ) {
			if ( str_id == interaction_ids[i] ) {
				return i;
			}
		}
	}

	return -1;	
}

function complete_activity() {
	LMSSetValue("cmi.completion_status", "completed");
}

function pass_activity() {
	LMSSetValue("cmi.success_status", "passed");
}

function fail_activity() {
	LMSSetValue("cmi.success_status","failed");
}

function LMSIsInitialized() {
	return init;
}
