/*
 INCLUDE:
  tail.js
  sidemenu.js
*/

function MsgMgr()
{
//===================================== CLASS BODY =========================================
//VARIABLES
//The current object ...
 var thisobj = this;
//The message list (it contains the strings of the messages that need to be shown!)
 this.msgtail = new Tail();
//The timer used to recall the processMessages method ...
 this.msgtimer = null;
//The slider component ...
 this.msgSlider = null;

//METHODS
 this.create = function(idocument,ioffset)
 {
//Building the msgSlider ...
  this.msgSlider = new MsgSlider(idocument,ioffset);
 }
 
 this.finalize = function()
 {
  disableMsgProc();
 }

 this.msgProcEnabled = function()
 {
  return (this.msgtimer != null);
 }

 this.enableMsgProc = function(forced,timeout)
 {
  if (forced)
  if (this.msgtimer != null)
   this.disableMsgProc();
  if (this.msgtimer == null)
   this.msgtimer = setTimeout(this.processMessages,timeout);
 }

 this.disableMsgProc = function()
 {
  if (this.msgtimer == null)
   return;
  clearTimeout(this.msgtimer);
  this.msgtimer = null;
 }

 this.setMsg = function(msg)
 {
  this.msgSlider.setText(msg);
 }

 this.showMsg = function()
 {
  this.msgSlider.slide();
 }

 this.processMessages = function()
 {
  var lmsg = null; 
 
  with (thisobj)
  {
//Consuming a message from the stack ...
   lmsg = msgtail.pop();
//If all msgs consumed then we'll stop the timer to free sys resources ...  
   if (lmsg == null)
   {
//Stop processing the messages ....
    disableMsgProc();
//Recycling the stack first ...
    msgtail.recycle();
    return;
   }
//Showing this message to the user ...
   setMsg(lmsg);
   showMsg();
//Processing the messages ....
   enableMsgProc(true,3000);
  }
 }

//This will add this message to the msglist and will be shown when all other messages have been shown!
 this.addMessage = function(msg)
 {
//Adding a new message to the stack ...
  this.msgtail.push(msg);
//New message added ... starting message processing ...
  if (!this.msgProcEnabled())
   this.processMessages();
 }
//===================================== INIT SECTION =======================================
}
