// ------------------------------------------------------------------------------------------------
// Lokale Scorm-Variable, werden nach bedarf auf LMS oder Cookie gespeichert
// ------------------------------------------------------------------------------------------------
var gtick=null;  // global, wird von Timeout verwendet
var gStatus=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];


function Scormvariable(){
    this.x=[
	this.StudentName = document.Info.StudentName,
	this.TotalTime	=  document.Info.TotalTime,
	this.Status	=  document.Info.Status,
	this.Score	=  document.Info.Score,
	this.restart	=  document.Info.Restart,
	this.nSessions	=  document.Info.nSession,
	this.s7		=  document.Info.s7,
	this.data1		=  document.Info.s8
    ];
    this.SessionTime	= document.Info.SessionTime;
}

// ------------------------------------------------------------------------------------------------
// Umschalter -->> LMS - Cookie - nix
// ------------------------------------------------------------------------------------------------

function ScormParameter(win) {

    this.GetRestart = function()	{return sv.restart.value;}
    this.SetRestart = function(e)	{sv.restart.value=e;}
    this.setScore = function(e)	{sv.Score.value=e; return e;}
    this.setGesP  = function(e) {sv.s7.value=e; return e;}
    this.getGesP  = function()  {return sv.s7.value;}
    this.setStatus  = function(e) {sv.Status.value=e; return e;}
    this.getStatus  = function()  {return sv.Status.value;}
    this.setStand  = function(e) {sv.data1.value=e; return e;}
	this.setStanda = function(a,b){gStatus[a]=b; sv.data1.value=gStatus;}
    this.getStand  = function()  {return sv.data1.value;}
    this.getStanda  = function(e)  { return gStatus[e];}

    this.Finish=function() 	{sv.TotalTime.value=gtick.getTotalTime(); return LData.Finish();}
	this.Commit = function () {LData.Commit();}
	var stati = ["not attempted", "incompleted", "completed","failed","passed"];
    var sv=new Scormvariable();	
    var LData=Scorm(sv, win);
    if (!LData) LData = Cookie(sv.x);
    if (!LData) LData = Nothing();
	sv.data1.value=gStatus;
    LData.Init();
    document.Info.Speicher.value=LData.ID;
    gtick=new myTime(sv);
    sv.nSessions.value++;
	if (sv.data1.value.length>3) gStatus=sv.data1.value.split(",");
    if (_Debug) {document.Info.style.visibility='visible';  gtick.tack();}
}

// ------------------------------------------------------------------------------------------------
// **** LMS ******* LMS ******* LMS ******* LMS ******* LMS ******* LMS ***
// ------------------------------------------------------------------------------------------------

function Scorm(lDaten, win){		// ********************** LMS verfügbar

    this.Init   = function() {
	API.LMSInitialize("");
	l.StudentName.value = API.LMSGetValue("cmi.core.student_name");
	l.TotalTime.value  = API.LMSGetValue("cmi.core.total_time");
	l.Status.value = API.LMSGetValue("cmi.core.lesson_status");
	l.Score.value  = API.LMSGetValue( "cmi.core.score.raw" );
	if (l.Status.value== "not attempted")	l.Status.value="incomplete";
	l.data1.value=API.LMSGetValue("cmi.suspend_data");
	l.restart.value = API.LMSGetValue("cmi.core.lesson_location");
    }

	this.Commit = function () {API.LMSCommit();}

    this.Finish = function() {
 	API.LMSSetValue("cmi.core.lesson_status" , l.Status.value);
	API.LMSSetValue("cmi.core.lesson_location", l.restart.value);
	API.LMSSetValue("cmi.suspend_data", l.data1.value);
 	API.LMSSetValue("cmi.core.score.raw" , l.Score.value);
 	API.LMSSetValue("cmi.core.score.max" , 100);
 	API.LMSSetValue("cmi.core.score.min" , 0);
	API.LMSCommit("");
	API.LMSFinish("");
    }

    function findAPI(win) {
	while ((win.API == null) && (win.parent != null) && (win.parent != win)) { win = win.parent;}
	API = win.API;
    }

    this.ID = "SCORM-Daten --> LMS";
    var l=lDaten;
    var API=null;
    findAPI(win);
    if ((API == null) && (win.opener != null)) { findAPI(win.opener);}
    if (API) return this; else return null;
}

// ------------------------------------------------------------------------------------------------
// **** COOKIES ******* COOKIES ******* COOKIES ******* COOKIES ******* COOKIES ******* COOKIES ***
// ------------------------------------------------------------------------------------------------

function Cookie(lDaten){

    this.Init=function (){
		var dc=unescape(document.cookie).split(";");
		for (i=0; i<dc.length; i++) {
	 	   var dc1=dc[i].split("=");
		    for (j=0; j<l.length; j++) {
				if (dc1[0].search(l[j].name) > -1) { l[j].value = dc1[1]; }
		    }
		}
	}
	this.Commit = function () {}
    this.Finish=function(){
		function set(n, e) {
		    document.cookie=" "+n + "=" + escape(e) + ";expires=" + bis.toGMTString() + ";";
		}
		for (i=0; i<l.length; i++){	set(l[i].name, l[i].value);}
	}

	this.ID = "Lernerdaten --> COOKIE";
    var l=lDaten;
	var dauer=365*24*3600*1000;// Anzahl 1/1000 sec 
    var bis=new Date(new Date().getTime()+dauer); // entspricht einem Jahr (in 1/1000s)
	return null;
	if(navigator.cookieEnabled) return this; else return null;
}

function Nothing(){		// ********************** Keine Speichermöglichkeit
    this.ID = "keine Speicherung";
    this.Init   = function(){}
    this.Finish = function(){}
return this;
}

function myTime(s){

    var sv=s;
    function jetzt() {return Math.floor((new Date().getTime())/1000);}
    function diff() {return (jetzt() - Startzeit);}
    function toTime (zahl) {
	var h=0;
	h=Math.floor(zahl/3600)%3600;	ges = (h<10) ?  "0" + h : h;
	h=Math.floor(zahl/60)%60; 	ges+= (h<10) ? ":0" + h : ":"+h;
	h=zahl%60;			ges+= (h<10) ? ":0" + h : ":"+h;
	return ges;
    }

    function toValue (st) {
	st=st.split(":");// var v=st[0]*3600+st[1]*60+st[2]*1;
	return st[0]*3600+st[1]*60+st[2]*1;
    }

    var Startzeit = jetzt();
    var gesZeit=toValue(sv.TotalTime.value);

    this.tack=function() {
	sv.SessionTime.value=toTime(diff());
	sv.TotalTime.value=toTime(gesZeit+diff());
	setTimeout("gtick.tack()", 1000);
    }

    this.getTotalTime=function(){
	return toTime(gesZeit+diff());
    }

    this.getSessionTime=function(){
	return toTime(diff());
    }

    this.heute=function (l) {  // l=0-->Deutsch   l=1 --> english
	var now = new Date();
	return(dt[now.getDay()][l]+" " +now.getDate() + ". " + dm[now.getMonth()][l]+" " + now.getYear());
    }
}
