var startDate = "";
var findAPITries=1;
var API = null;
var apiHandle = null;


var _Debug = false;  //debug jest wylaczony dla funkcji 1 poziomu

// Kody bledow
var _NoError = 0;
var _GeneralException = 101;
var _ServerBusy = 102;
var _InvalidArgumentError = 201;
var _ElementCannotHaveChildren = 202;
var _ElementIsNotAnArray = 203;
var _NotInitialized = 301;
var _NotImplementedError = 401;
var _InvalidSetValue = 402;
var _ElementIsReadOnly = 403;
var _ElementIsWriteOnly = 404;
var _IncorrectDataType = 405;

//////////////////////////////////////////////
///Interfejs najwyzszego poziomu dla flesza

function poczatek()
{
  initSco();
}

function odczyt(co)
{
  return get_val(co);
}

function zapis(co,wartosc)
{
  set_val(co,wartosc);
}

function zapiszstatus(jaki)
{
  stat(jaki);
}

function zatrzymajzegar()
{
  clockend();
}

function zatwierdz()
{
  commit();
}

function wynik()
{
  score();
}

function wynikraw(jaki)
{
    scormwynik(jaki);
}

function koniec()
{
    finish();
}

function sprawdzstatus()
{
var biezacystat = get_val("cmi.core.lesson_status");
	if ((biezacystat)&&(biezacystat != ""))
	{
		if ((biezacystat == "passed")||(biezacystat == "failed"))
		{
			return false;
		}		
		else
		{
			return true;
		}
	}
	else
	{
		return true;
	}
}



//////////////////////////////////////////////
function initSco()
{

	API = GetAPI();
	if( API != null )
	{
		
		var ret;
		var code;
		var diag;
		API.LMSInitialize("");
		code = API.LMSGetLastError();
		ret = API.LMSGetErrorString( code );
		diag = API.LMSGetDiagnostic( "" );
		startTimer();	
		
	}
}

function score()
{
	get_val("cmi.student_data.mastery_score");
}

function scormwynik(jaki){
	set_val("cmi.core.score.raw",jaki);

}

function stat(st)
{
	set_val("cmi.core.lesson_status",st);
	przeslijmistatus(st);
}

function clockend(){
 computeTime();
 commit();
}

function set_val( gname,gvalue )
{
	GetAPI();
	if( API != null )
	{
		var ret;
		var code;
		var diag;		

		API.LMSSetValue( gname, gvalue );		
		code = API.LMSGetLastError();
		ret = API.LMSGetErrorString( code );
		diag = API.LMSGetDiagnostic( "" );		
	}
	
};

function get_val( gname )
{	
	GetAPI();
	if( API != null )
	{
		var ret1,ret2;
		var code;
		var diag;
	
		ret1 = API.LMSGetValue( gname );		
		code = API.LMSGetLastError();
		ret2 = API.LMSGetErrorString( code );
		diag = API.LMSGetDiagnostic( "" );
		return ret1;		
		
	}
	
};

function commit()
{
	
	GetAPI();
	if( API != null )
	{
		var ret = "";
		var code;
		var diag;
		
		API.LMSCommit("");
		code = API.LMSGetLastError();
		ret = API.LMSGetErrorString( code );
		diag = API.LMSGetDiagnostic( "" );		
	}
	
};

function finish()
{	
	GetAPI();
	if( API != null )
	{
		var ret;
		var code;
		var diag;
		
		ret = API.LMSFinish("");
		code = API.LMSGetLastError();
		ret = API.LMSGetErrorString( code );
		diag = API.LMSGetDiagnostic( "" );
		
	}
	
};


function initObjectives(objRef){
	GetAPI();
	if( API != null )
	{
	var totObjCount = API.LMSGetValue("cmi.objectives._count");

	for(var i = 0; i <= totObjCount; i++ ){
		var tmp = "cmi.objectives."+i+".id";
		if(API.LMSGetValue(tmp) == objRef){
			return;
		}

		var totalObj = API.LMSGetValue("cmi.objectives._count");
	
		var request = "cmi.objectives."+totalObj+".id";
		API.LMSSetValue(request,objRef);
	
		request =  "cmi.objectives."+totalObj+".score.raw";
		API.LMSSetValue(request,"");
	
		request =  "cmi.objectives."+totalObj+".score.min";
		API.LMSSetValue(request ,"0");
	
		request =  "cmi.objectives."+totalObj+".score.max";
		API.LMSSetValue(request ,"100");
	}
	}
}


function setScore_LMS(objRef,tmpScore){
	GetAPI();
	if( API != null )
	{
	var totObjCount = API.LMSGetValue("cmi.objectives._count");
		
	for(var i = 0; i<=totObjCount; i++ ){
		var tmp = "cmi.objectives."+i+".id";
		if(API.LMSGetValue(tmp) == objRef){
			var tmp1 = "cmi.objectives."+i+".score.raw";
			tmpScore = tmpScore * 100;
			API.LMSSetValue(tmp1,tmpScore);
			break;
		}
	}
	}
}


////////////////////////////////////////////////////
////////////FUNKCJE 1 POZIOMU DLA SCORMA////////////
////////////////////////////////////////////////////

function FindAPI(win)
{
   while ((win.API == null) && (win.parent != null) && (win.parent != win))
   {
      findAPITries++;
      // Note: 7 is an arbitrary number, but should be more than sufficient
      if (findAPITries > 7) 
      {
        parent.status = "Error finding API -- too deeply nested.";
         return null;
      }
      
      win = win.parent;

   }
   return win.API;
}
function GetAPI()
{
   var API = FindAPI(window);
   if ((API == null) && (window.opener != null) && (typeof(window.opener) != "undefined"))
   {
      API = FindAPI(window.opener);
   }
   if (API == null)
   {
     parent.status = "DC-nAPI";
   }
   return API
}



/////////////////////////////////////////////////////
////FUNKCJE DODATKOWE POZA SCORM////////////////////
////////////////////////////////////////////////////

function startTimer() {

if (startDate == "") {   
startDate = new Date().getTime();
}
}


function computeTime()
{
   if ( startDate != 0 )
   {
      var currentDate = new Date().getTime();
      var elapsedSeconds = ( (currentDate - startDate) / 1000 );
      var formattedTime = convertTotalSeconds( elapsedSeconds );
   }
   else
   {
      formattedTime = "00:00:00.0";
   }

   set_val( "cmi.core.session_time", formattedTime );
}


function convertTotalSeconds(ts)
{
   var sec = (ts % 60);

   ts -= sec;
   var tmp = (ts % 3600);  //# of seconds in the total # of minutes
   ts -= tmp;              //# of seconds in the total # of hours

   // convert seconds to conform to CMITimespan type (e.g. SS.00)
   sec = Math.round(sec*100)/100;
   
   var strSec = new String(sec);
   var strWholeSec = strSec;
   var strFractionSec = "";

   if (strSec.indexOf(".") != -1)
   {
      strWholeSec =  strSec.substring(0, strSec.indexOf("."));
      strFractionSec = strSec.substring(strSec.indexOf(".")+1, strSec.length);
   }
   
   if (strWholeSec.length < 2)
   {
      strWholeSec = "0" + strWholeSec;
   }
   strSec = strWholeSec;
   
   if (strFractionSec.length)
   {
      strSec = strSec+ "." + strFractionSec;
   }


   if ((ts % 3600) != 0 )
      var hour = 0;
   else var hour = (ts / 3600);
   if ( (tmp % 60) != 0 )
      var min = 0;
   else var min = (tmp / 60);

   if ((new String(hour)).length < 2)
      hour = "0"+hour;
   if ((new String(min)).length < 2)
      min = "0"+min;

   var rtnVal = hour+":"+min+":"+strSec;

   return rtnVal;
}

///Nie chce wplywac na status elementow poprzez test - na ten czas
function przeslijmistatus(stat){
if ((parent)&&(parent.TocFrame)){
//if(parent.TocFrame.dcmagazyn){
//if (stat == "passed"){
//parent.TocFrame.dcmagazyn.zaliczaktualnystatus("1");
//}
//else {
//parent.TocFrame.dcmagazyn.zaliczaktualnystatus("0");
//}
//}
}

}


