/* ===========================================================*/
/* Copyright (c) 2007 by SDN AG, CH-8912 Obfelden Switzerland */
/* -----------------------------------------------------------*/
/* Project : easyLEARN skills4u                               */
/* -----------------------------------------------------------*/
/* $Id: lwassistant.js,v 1.5 2007/04/13 09:59:31 MWE Exp $  */
/* ============================================================*/
/*
History
=======
$Log: lwassistant.js,v $
Revision 1.5  2007/04/13 09:59:31  MWE
Release 4.4

Revision 1.4  2006/09/28 08:03:33  MWE
Breite/Höhe je Assistentin berücksichtigen

Revision 1.3  2006/09/27 11:48:45  MWE
Basispfad wird beim Erzeugen übergeben, wegen WO-Servern

Revision 1.2  2006/09/26 08:05:07  MWE
Neue Funktion SetAllVisited()

Revision 1.1  2006/09/15 12:35:47  MWE
Release 4.3



*/

function cAssistantCase(caseid, description, file)
{
  this.mFile = file;
  this.mDescr = description;
  this.mCaseID = caseid;
  this.mEnabled = true;
  this.mVisited = false;
}

// cLWAssistant object

function cLWAssistant(basepath)
{
	this.mID ='';
	this.mBaseURL = basepath;
	this.mCases = new Array();
	this.mPath = '';
	this.mcurrCase = 'SNACK_START';
	this.showdebugfunc = null;
	this.mAssistantState = 1;
	this.mTrackVisited = false;
	this.mInitialCase = '';
	this.mWidth = 160;
	this.mHeight = 120;
};

cLWAssistant.prototype.SetAssistant = function(aid)
{
  this.mID = aid;
  this.mPath = this.mBaseURL + this.mID.toLowerCase() + '/';
}

cLWAssistant.prototype.SetWidthHeight = function(awidth, aheight)
{
  this.mWidth = awidth;
  this.mHeight = aheight;
}

cLWAssistant.prototype.GetAssistantID = function()
{
  return this.mID;
}

cLWAssistant.prototype.GetWidth = function()
{
  return this.mWidth;
}

cLWAssistant.prototype.GetHeight = function()
{
  return this.mHeight;
}

cLWAssistant.prototype.SetInitialCase = function(acase)
{
  return this.mInitialCase = acase;
}

cLWAssistant.prototype.GetInitialCase = function()
{
  return this.mInitialCase;
}


cLWAssistant.prototype.isValid = function()
{
  return ! (this.mID == '')	;
}

cLWAssistant.prototype.SetCase = function(acase)
{
  this.mcurrCase = acase;
}

cLWAssistant.prototype.GetCase = function()
{
  return this.mcurrCase;
}

cLWAssistant.prototype.Enabled = function()
{
  return this.mAssistantState > 0;
}

cLWAssistant.prototype.SetAssistantState = function(state)
{
  switch (state)
  {
    case 0 : this.mAssistantState = 0; break;
    case 1 : this.mAssistantState = 1; break;
    case 2 : this.mAssistantState = 2; this.SetAllVisited(); break;
  }
  return this.mAssistantState;      
}

cLWAssistant.prototype.GetAssistantState = function()
{
  return this.mAssistantState;      
}

cLWAssistant.prototype.caseEnabled = function (acase)
{
  var x = this.findCase(acase);
  if (x >= 0)
    return this.mCases[x].mEnabled;
  return false;
}

cLWAssistant.prototype.caseActive = function (acase)
{
  var x = this.findCase(acase);
  if (x >= 0)
    return this.mCases[x].mEnabled  && this.mCases[x].mVisited == false;
  return false;
}

cLWAssistant.prototype.ToggleCase = function ()
{
  var x = this.findCase(this.mcurrCase);
  if (x >= 0)
    return this.mCases[x].mEnabled = !this.mCases[x].mEnabled;
  return false;
}
cLWAssistant.prototype.addCase = function(acase,file, description)
{
  this.mCases[this.mCases.length] = new cAssistantCase(acase,description,file);
}

cLWAssistant.prototype.GetCurrentAssistant = function(checkenabled)
{
  return this.GetAssistant(this.mcurrCase, checkenabled);
  
}
cLWAssistant.prototype.GetAssistant = function(caseid, checkenabled)
{
  this.mcurrCase = caseid;
  
  var x = this.findCase(caseid)
  this.showdebug('GetAssistant('+caseid+')');
  if (x >= 0)
  {
  	if (checkenabled)
  	{
  	  if (this.mCases[x].mEnabled == true && this.mCases[x].mVisited == false)
  	  {
  	  	this.showdebug('GetAssistant: checkenabled-true: found at '+x+', url is ' + this.mPath + this.mCases[x].mFile);
  	  	if (this.mTrackVisited)
  	  	  this.mCases[x].mVisited = true;
  	    return this.mPath + this.mCases[x].mFile;
  	  }
  	  return '';
  	}
  	else
  	{
  	  this.showdebug('GetAssistant: checkenabled-true: found at '+x+', url is ' + this.mPath + this.mCases[x].mFile);
  	  return this.mPath + this.mCases[x].mFile;
  	}
  }
  return '';
}


cLWAssistant.prototype.findCase = function(caseid)
{
  var id = caseid+'';
  id = id.toUpperCase();
  for (var i = 0; i < this.mCases.length;i++)	
  {
    if (this.mCases[i].mCaseID == id)
      return i;
  }
  return -1;
}

cLWAssistant.prototype.SetUserStates = function(astring)
{
  var s = astring + ''	;
  this.showdebug('SetUserStates('+s+')');
  var n = s.split(';');
  var i = 0;
  var x = 0;
  for (i = 0; i < n.length;i++)
  {
  	s2 = n[i].split(':');
  	if (s2.length == 2)
  	{
  	  x = this.findCase(s2[0]);
  	  if (x >= 0)
  	    this.mCases[x].mEnabled = s2[1] == 1 ? true: false;
  	}
  }
}

cLWAssistant.prototype.GetUserStates = function()
{
  var s = '';
  var i = 0;
  for (i = 0; i < this.mCases.length; i++)
  {
  	s = s + this.mCases[i].mCaseID + ':' + (this.mCases[i].mEnabled == true ? '1': '0') + ';';
  }
  this.showdebug('GetUserStates: result = '+s)
  return s;
}


cLWAssistant.prototype.showdebug = function(msg)
{
  if (this.showdebugfunc)
    eval(this.showdebugfunc(msg))
};

cLWAssistant.prototype.TrackVisited = function(flag)
{
  this.mTrackVisited = flag == true ? true: false;
}

cLWAssistant.prototype.SetDebugFunc = function(fnc)
{
  this.showdebugfunc = fnc;
};

cLWAssistant.prototype.SetAllVisited = function()
{
  for (var i = 0; i < this.mCases.length; i++)
    this.mCases[i].mVisited = true;
}

