/**
 *  frame definition for the desktop
 *
 *  @author   Tobias Hettinger
 *  @version  $Id: frame.js,v 1.13 2009/05/06 11:22:39 tobias Exp $
 */
 

//=====================================================================================================================
//  frame events
//=====================================================================================================================

/**
 *  constructor of the OnDesktopFrameChange event
 */
function WATDesktopFrameChangeEvent()
{
  //  set the member variables
  this.eventHandlers = new Array();
}

/**
 *  function to register the passed event handler
 */
WATDesktopFrameChangeEvent.prototype.AddHandler = function(eventHandler)
{
  this.eventHandlers.push(eventHandler);
}

/**
 *  function to raise the event
 *
 *  @param object frame  instance of the affected frame
 *  @param object sheet  instance of the affected sheet or null if no sheet is affected
 *  @param int    flags  flags of the event
 *                       - 1 - a sheet has been shown
 */
WATDesktopFrameChangeEvent.prototype.Raise = function(frame, sheet, flags)
{
  for(var i=0; i<this.eventHandlers.length; i++)
    this.eventHandlers[i](frame, sheet, flags);
}

//  create an instance of the OnDesktopFrameChange event
WATOnDesktopFrameChange = new WATDesktopFrameChangeEvent();


//=====================================================================================================================
//  desktop frame
//=====================================================================================================================

/**
 *  constructor
 *
 *  alignments:
 *    0: absolute (top, left, width, height required)
 *    1: top (only the height is used)
 *    2: right (only the width is used)
 *    3: left (only the width is used)
 *    4: bottom (only the height is used)
 *    5: middle frame (no property is used)
 */
var WATDesktopFrame = function(name, cssClassName, align, top, left, width, height, visible)
{
  //  set the member variables
  this.name = name;
  this.align = align;
  this.top = top;
  this.left = left;
  this.width = width;
  this.height = height;
  this.visible = visible;
  this.sheetSelect = null;
  
  //  create a new div
  this.divElement = document.createElement('div');
  this.divElement.id = 'df' + name;
  this.divElement.className = cssClassName;
  this.divElement.style.position = 'absolute';
  if (!visible) this.divElement.style.display = 'none';
  
  //  add the frame to the document
  document.body.appendChild(this.divElement);
  
  //  initialize the sheets
  this.sheets = new Array();
  this.activeSheet = null;
  
  //  grids that can be used with the frame
  this.grids = new Array();
}


//=====================================================================================================================
//  frame management
//=====================================================================================================================

/**
 *  function to show or hide the frame
 *
 *  @param bool visible  if true, the frame is shown, if false the frame is hidden
 */
WATDesktopFrame.prototype.SetVisible = function(visible)
{
  this.visible = visible;
  if (visible) this.divElement.style.display = 'block';
    else this.divElement.style.display = 'none';
}

/**
 *  function to enable auto scrolling for the frame
 */
WATDesktopFrame.prototype.EnableAutoScroll = function()
{
  //  enabe auto scrolling
  this.divElement.style.overflow = 'auto';
}

/**
 *  function to disable auto scrolling for the frame
 */
WATDesktopFrame.prototype.DisableAutoScroll = function()
{
  //  disable auto scrolling
  this.divElement.style.overflow = 'hidden';
}


//=====================================================================================================================
//  sheet management
//=====================================================================================================================

/**
 *  function to add a new sheet to the frame
 *
 *  @param string name         name of the sheet
 *  @param bool   activate     true if the sheet should be immediately activated
 *  @param string caption      caption of the sheet
 *  @param string content      content of the sheet or null if there is no content yet
 *  @param string contentHash  hash value that can be used to detect if an update is required
 *
 *  @return object  the function returns the created sheet
 */
WATDesktopFrame.prototype.AddSheet = function(name, activate, caption, content, contentHash)
{
  //  create a new sheet
  var sheet = new WATDesktopSheet(name, caption ? caption : name, this.divElement);
  sheet.SetContent(content, contentHash);
  
  //  register the new sheet
  this.sheets.push(sheet);
  if (this.sheetSelect) this.sheetSelect.AddSheet(name);
  if (activate) this.ShowSheet(sheet.name);
  return sheet;
}

/**
 *  function to add a new sheet to the frame by using an existing div
 *
 *  @param string name      name of the sheet
 *  @param bool   activate  true if the sheet should be immediately activated
 *  @param string caption   caption of the sheet
 *  @param string divId     id of the div that should be used for the sheet
 *
 *  @return object  the function returns the created sheet
 */
WATDesktopFrame.prototype.AddSheetFromDiv = function(name, activate, caption, divId)
{
  //  create a new sheet
  var sheet = new WATDesktopSheet(name, caption, this.divElement, divId);
  
  //  register the new sheet
  this.sheets.push(sheet);
  if (activate) this.ShowSheet(sheet.name);
  return sheet;
}

/**
 *  function to get the sheet with the passed name
 *
 *  @param string name  name of the sheet
 *
 *  @return object  the function returns the selected sheet or null if the sheet has not been found
 */
WATDesktopFrame.prototype.GetSheet = function(name)
{
  for (var i in this.sheets)
    if (this.sheets[i] && this.sheets[i].name == name) return this.sheets[i];
  
  //  the sheet has not been found
  return null;
}

/**
 *  function to show the sheet with the passed name
 *
 *  @param string name  name of the sheet to show
 */
WATDesktopFrame.prototype.ShowSheet = function(name)
{
  var result = null;
  
  for (var i in this.sheets)
    if (this.sheets[i])
    {
      if (this.sheets[i].name == name)
      {
        //  show the selected sheet ...
        this.activeSheet = this.sheets[i];
        this.sheets[i].divElement.style.display = 'block';
        result = this.sheets[i];
      }
      //  ... remove all the other ones
      else this.sheets[i].divElement.style.display = 'none';
    }
  
  if (result)
  {
    WATOnDesktopFrameChange.Raise(this, result, 1);
  }
  
  return result;
}

/**
 *  function to remove the sheet with the passed name
 *
 *  @param string name  name of the sheet to remove
 */
WATDesktopFrame.prototype.RemoveSheet = function(name)
{
  for (var i in this.sheets)
    if (this.sheets[i] && this.sheets[i].name == name)
    {
      this.divElement.removeChild(this.sheets[i].divElement);
      this.sheets[i] = null;
    }
}

WATDesktopFrame.prototype.RemoveAllSheets = function()
{
  for (var i in this.sheets)
    if (this.sheets[i]) this.RemoveSheet(this.sheets[i].name);
}

/**
 *  function to get the active sheet
 *
 *  @return object  the function returns the active sheet or null if no sheet is active
 */
WATDesktopFrame.prototype.GetActiveSheet = function()
{
  return this.activeSheet;
}


WATDesktopFrame.prototype.AddGrid = function(grid)
{
  this.grids.push(grid);
}

WATDesktopFrame.prototype.ResizeGrids = function()
{
  if (this.grids.length)
  {
    for (var i in this.grids)
      this.grids[i].Resize(this.width);
  }
}

