﻿// --------------------------------------------------------------------
// class ReturnedStatus : zaznam v tabulce oken
// 
function WindowRecord(windowHandle, windowId, pageName)
{
  this.windowId = windowId;
  this.windowHandle = windowHandle;
  this.pageName = pageName;
}

// --------------------------------------------------------------------
// class ReturnedStatus : zaznam v tabulce navracenych hodnot
//
function ReturnedStatus(windowId, retVal, isException)
{
  this.windowId = windowId;
  this.retVal = retVal;
  this.isException = isException;
}

// --------------------------------------------------------------------
// class GlobalManager : pristup ke sdilenym objektum
//
function GlobalManager()
{
  this.windowTable = new Array();
  
  this.SendClassMessage = function(sender, pageName, command, value)
  {
    var broadcast = (pageName == null);
    var retValArray = new Array();
    for(i=0; i<this.windowTable.length; i++)
    {
      var winRec = this.windowTable[i];
      
      if(winRec.windowHandle == null)
        continue;
        
      if(broadcast || (winRec.pageName == pageName) )
      {
        try
        {
          var retVal = winRec.windowHandle.DispatchMessage(sender, command, value);
          var status = new ReturnedStatus(winRec.windowId, retVal, false);
          retValArray.push(status);
        }
        catch(ex)
        {
          retValArray.push(new ReturnedStatus(winRec.windowId, ex, true));
        }
        
        if(!broadcast)
          break;
      }
    }
    
    return retValArray;  
  }
  
  this.SendMessage = function(sender, windowId, command, value)
  {
    var retStatus = null;
    for(i=0; i<this.windowTable.length; i++)
    {
      var winRec = this.windowTable[i];
      
      if(winRec.windowHandle == null)
        continue;
        
      if(winRec.windowId == windowId)
      {
        try
        {
          var retVal = winRec.windowHandle.DispatchMessage(sender, command, value);
          retStatus = new ReturnedStatus(winRec.windowId, retVal, false);
        }
        catch(ex)
        {
          retStatus = new ReturnedStatus(winRec.windowId, ex, true);
        }
        
        break;
      }
    }
    
    return retStatus;  
  }

  this.RegisterWindow = function(windowHandle, pageName)
  {
    if(pageName == "")
      return;
 
    var windowId = 0;  
    for(i=0; i<this.windowTable.length; i++)
    {
      if(this.windowTable[i].pageName == pageName)
        windowId++;
    }
       
    windowId = pageName + windowId;   
    windowHandle.PageID = windowId;
    var newWindowRecord = new WindowRecord(windowHandle, windowId, pageName);
    this.windowTable.push(newWindowRecord);
  }
  
  this.DeregisterWindow = function(windowHandle)
  {
    for(i=0; i<this.windowTable.length; i++)
    {
      if(this.windowTable[i].windowHandle == windowHandle)
      {
        this.windowTable.splice(i, 1);
        return;
      }
    }
  }
  
  this.GetWindowRecord = function(windowId)
  {
    for(i=0; i<this.windowTable.length; i++)
    {
      if(this.windowTable[i].windowId == windowId)
        return this.windowTable[i];
    }
    
    return null;
  }
  
  this.GetWindow = function(windowId) {
    var windowRecord = this.GetWindowRecord(windowId);
    if(windowRecord == null)
        for(i=0; i<this.windowTable.length; i++) {
            if(this.windowTable[i].pageName == windowId) {
                if (windowRecord == null) 
                    windowRecord = this.windowTable[i];
                else
                    throw "CRITICAL: Nalezeno více oken s tímto pageName: "+windowId;
            }        
        }
    if(windowRecord != null)
        return windowRecord.windowHandle;
    return null;
  }
  
  this.GetFocusedWindow = function(acceptDefault)
  {
    for(i=0; i<this.windowTable.length; i++)
    {
      if(this.windowTable[i].windowHandle.focus)
      {
        if( (this.windowTable[i].windowHandle.PageName == 'Default') && (acceptDefault == false) )
          continue;
          
        return this.windowTable[i].windowHandle;
      }
    }
    
    return null;
  }
  
  this.GetValue = function GetValue(name)
  {
    try
    {
      return this.GetWindowRecord('Default0').windowHandle[name];
    }
    catch(ex)
    {
    }
    
    return null;
  }

  this.SetValue = function SetValue(name, value)
  {
    try
    {
      return this.GetWindowRecord('Default0').windowHandle[name] = value;
    }
    catch(ex)
    {
    }
  }
  
  this.GetAxObject = function GetAxObject()
  {
    return this.GetValue('ax');
  }
}

// --------------------------------------------------------------------
// function DispatchMessage : helper pro SendMessage()
// 
function DispatchMessage(sender, command, value)
{
  // napr.: Default_OnWM_STOP
  var fnBody = 'return ' + PageName + '_On' + command + '(sender, command, value);';

  var messageFn = new Function('sender', 'command', 'value', fnBody);
  var retVal = messageFn(sender, command, value);
  return retVal;
}

function StartLoading()
{
  GetGlobalManager().SendClassMessage(this, "Default", "StartLoading", 0);
}

function StopLoading()
{
  GetGlobalManager().SendClassMessage(this, "Default", "StopLoading", 0);
}


