﻿// --------------------------------------------------------------------
// 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.CleanWindowRecords = function() {
    for (i = this.windowTable.length - 1; i >= 0; i--) {
      if (this.windowTable[i].windowHandle.closed === true) 
        this.windowTable.splice(i, 1);
    }
  }

  this.RegisterWindow = function(windowHandle, pageName) {
    if (pageName == "")
      return;
    this.CleanWindowRecords(); 
    var windowId = 0;
    for (i = 0; i < this.windowTable.length; i++) {
      if (this.windowTable[i].pageName == pageName)
        windowId = Math.max(windowId, parseInt(this.windowTable[i].windowId.substr(pageName.length))) + 1;
    }

    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)
  {
    this.CleanWindowRecords(); 
    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)
  {
    this.CleanWindowRecords(); 
    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.GetWindowSettings = function(ID, win) {
    var list = this.GetValue("WindowSettings"); 
    if (!list) list = new Object(); 
    var o = new WindowSettingObject(); 
    if (list[ID]) { 
      o.LoadFromSerialization(list[ID]);
      if (win) o.SetToWindow(win); 
    }  
    return o; 
  } 
  
  this.SetWindowSettings = function(ID, win) {
    var o = win;  
    if (!WindowSettingObject.prototype.isPrototypeOf(win)) {
      o = new WindowSettingObject(); 
      o.LoadFromWindow(win); 
    } 

    var list = this.GetValue("WindowSettings");
    if (!list) list = new Object(); 
    list[ID] = o.SaveForSerialization(); 
    this.SetValue("WindowSettings", list);
    this.SetValue("WindowSettingsSaved", false)
  }
  
  this.SaveWindowSettingsToServer = function () {
    var list = this.GetValue("WindowSettings"); 
    if (!list || this.GetValue("WindowSettingsSaved")===true) return false; 
    
    var query = "";
    for (key in list)
        query += "&" + key + "=" + list[key]; 

    var l_sUrl = TranslateUrlForCall("~/Gin/SaveConfiguration.aspx?key=WindowSettings"+encodeURI(query));
    
    var serverReq = null;
    if (window.XMLHttpRequest) serverReq = new XMLHttpRequest();
    else if (window.ActiveXObject) { // IE6
      try { serverReq = new ActiveXObject("Msxml2.XMLHTTP"); } 
      catch (e) {
        try { serverReq = new ActiveXObject("Microsoft.XMLHTTP"); } 
        catch (e) {}
      }
    }
    if (!serverReq) return false;

    serverReq.onreadystatechange = function () { };
    serverReq.open('GET', l_sUrl, false);
    serverReq.send(null);
    this.SetValue("WindowSettingsSaved", true)
    return true;
  }
  
  this.GetAxObject = function GetAxObject()
  {
    return this.GetValue('ax');
  }
}

// --------------------------------------------------------------------
// trida pro uchovani nastaveni okna
// 
function WindowSettingObject(aLeft, aTop, aWidth, aHeight) {
  if (typeof(aLeft)!="undefined" && aLeft) this.Left = aLeft; 
  if (typeof(aTop)!="undefined" && aTop) this.Top = aTop;  
  if (typeof(aWidth)!="undefined" && aWidth) this.Width = aWidth; 
  if (typeof(aHeight)!="undefined" && aHeight) this.Height = aHeight; 
}

WindowSettingObject.prototype.Left = 0; 
WindowSettingObject.prototype.Top = 0; 
WindowSettingObject.prototype.Width = 0;
WindowSettingObject.prototype.Height = 0; 

WindowSettingObject.prototype.LoadFromSerialization = function (SerInfo) {
  var as = SerInfo.split("|");
  this.Left=parseInt(as[0]);
  this.Top=parseInt(as[1]);
  this.Width=parseInt(as[2]);
  this.Height=parseInt(as[3]);
} 

WindowSettingObject.prototype.SaveForSerialization = function () {
  return this.Left+"|"+this.Top+"|"+this.Width+"|"+this.Height;
} 

WindowSettingObject.prototype.LoadFromWindow = function (win) {
  this.Left = (navigator.appVersion.indexOf('MSIE')>=0 ? win.screenLeft : win.screenX); 
  this.Top = (navigator.appVersion.indexOf('MSIE')>=0 ? win.screenTop : win.screenY);
  this.Width = win.document.body.clientWidth;
  this.Height = win.document.body.clientHeight;
}

WindowSettingObject.prototype.SetToWindow = function (win) {
  if (win.dialogHeight && win.dialogWidth && win.dialogTop && win.dialogLeft) {
    // IE6 pri posunu okna nemeni velikost dialogLeft/top, proto je nutne nejprive nastavit fixni velikost, pak zkontrolovat, kam se okno
    // posunulo a pak doladit podle zadanych rozmeru
    if ((navigator.appVersion.indexOf('MSIE 6')>=0)) {                 
      win.dialogTop = this.Top+"px"; 
      win.dialogLeft = this.Left+"px"; 
    }
    var o = new WindowSettingObject(); 
    o.LoadFromWindow(win);
    var dt = parseInt(win.dialogTop) + this.Top - o.Top;
    var dl = parseInt(win.dialogLeft) + this.Left - o.Left;
    var dy = parseInt(win.dialogHeight) + this.Height - o.Height;
    var dx = parseInt(win.dialogWidth) + this.Width - o.Width;
    win.dialogTop = dt+"px"; 
    win.dialogLeft = dl+"px"; 
    win.dialogHeight = dy+"px";
    win.dialogWidth = dx+"px";
  } else {
    win.moveTo(this.Left, this.Top); 
    win.resizeBy(this.Width - win.document.body.clientWidth, this.Height - win.document.body.clientHeight);
  }  
}

// --------------------------------------------------------------------
// function DispatchMessage : helper pro SendMessage()
// 
function DispatchMessage(sender, command, value)
{
  // napr.: Default_OnWM_STOP
  var fnName = PageName + '_On' + command;
  var fnBody = 'if (typeof('+fnName+')=="function") return ' + fnName + '(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);
}


