/**
 *  form object for the WAT desktop manager
 *
 *  @author   Tobias Hettinger
 *  @version  $Id: form.js,v 1.9 2008/06/13 13:36:35 tobias Exp $
 */
 

/**
 *  constructor of the desktop form object
 *
 *  @param string desktop  instance of the desktop object
 *  @param int    index    index of the form object in the forms array of the desktop
 */
var WATDesktopForm = function(desktop, index)
{
  //  set the member variables
  this.desktop = desktop;
  this.desktopName = desktop.name;
  this.divId = 'df' + index;
  this.index = index;
  this.used = false;
  this.key = null;
  
  //  create a new div for the form
  this.divElement = document.createElement('div');
  this.divElement.id = this.divId;
  this.divElement.zIndex = 99;
  this.divElement.style.position = 'absolute';
  this.divElement.style.display = 'none';
  document.body.appendChild(this.divElement);
  
  //  create a divEffects manager for the fading effect
  this.divEffects = new WATDivEffects(this.desktopName + '.forms[' + index + '].divEffects', this.divId);
  this.divEffects.EnableMouseMove(this.divEffects, true, false, false);
}

/**
 *  function to create the form
 *
 *  @param string key            key of the form which is used to access the form (e.g. for closing it)
 *  @param string baseClassName  css class of the form
 *  @param string caption        caption of the form (simply a string with the name of the form)
 *  @param string content        content of the form (full html code to show)
 *  @param array  buttons        array of button definitions. The structure of the array is
 *                               - onclick - onclick handler of the button
 *                               - caption - caption of the button
 */
WATDesktopForm.prototype.CreateForm = function(key, baseClassName, caption, content, buttons)
{
  //  initialize the variables
  var formContent = null;
  var buttonBar = '';
  
  //  create the button bar
  if (buttons)
  {
    //  open the button bar
    buttonBar = '<div class="buttons">';
    
    //  create the buttons
    for(var i in buttons)
    {
      var button = buttons[i];
      buttonBar += '&nbsp;&nbsp;<input type="button" value="' + button['caption'] + '" onclick="' + button['onclick'] + '" />';
    }
    
    //  close the button bar
    buttonBar += '</div>';
  }
  
  //  get the image base path
  if (this.desktop.designs[baseClassName] && this.desktop.designs[baseClassName]['formImgBasePath'] != null)
  {
    var imgBasePath = this.desktop.designs[baseClassName]['formImgBasePath'];
    var ext = WATBrowserInfo.IsIE && !WATBrowserInfo.IsIE7 ? 'gif' : 'png';
    
    //  create the form content
    formContent = '<table style="border-collapse:collapse;" cellspacing="0" cellpadding="0"><tr><td>' +
                  '<img style="display:block;" src="' + imgBasePath + 'tl.' + ext + '"></td><td onfocus="' +
                  this.desktopName + '.forms[' + this.index + '].divEffects.hasFocus = true; ' + this.desktopName +
                  '.FormFocus(\'' + key + '\');" ' + 'onblur="' + this.desktopName + '.forms[' + this.index +
                  '].divEffects.hasFocus = false;" style="vertical-align:top; background-image:url(' + imgBasePath +
                  't.' + ext + ');">' + '<div class="caption"><p>' + caption +
                  '</p></div></td><td><img style="display:block;" src="' + imgBasePath + 'tr.' + ext +
                  '"></td></tr><tr><td style="background-image:url(' + imgBasePath + 'l.' + ext +
                  ');"><img style="display:block;" src="' + imgBasePath + 'l.' + ext + '"></td><td><form><div class="body">' + 
                  content + '</div>' + buttonBar + '</form></td><td style="background-image:url(' + imgBasePath + 'r.' + ext +
                  ');"><img style="display:block;" src="' + imgBasePath + 'r.' + ext + '"></td></tr><tr><td>' +
                  '<img style="display:block;" src="' + imgBasePath + 'bl.' + ext + '"></td><td style="background-image:url(' + 
                  imgBasePath + 'b.' + ext + ');"><img style="display:block;" src="' + imgBasePath + 'b.' + ext +
                  '"></td><td><img style="display:block;" src="' + imgBasePath + 'br.' + ext + '"></td></tr></table>';
  }
  else
  {
    //  create the form content without images
    formContent = '<div class="caption" onfocus="' + this.desktopName + '.forms[' +
                  this.index + '].divEffects.hasFocus = true; ' + this.desktopName + '.FormFocus(\'' + key + '\');" ' +
                  'onblur="' + this.desktopName + '.forms[' + this.index + '].divEffects.hasFocus = false;"><p>' +
                  caption + '</p></div><form><div class="body">' + content + '</div>' + buttonBar + '</form>';
  }
                    
  //  setup the form
  this.divElement.className = baseClassName;
  watDivContent(this.divId, formContent);
  
  //  get the dimensions of the form and the screen
  var formHeight = this.divEffects.GetHeight();
  var formWidth = this.divEffects.GetWidth();
  var desktopHeight = 0;
  var scrollPos = 0;
  if (WATBrowserInfo.IsIE && !WATBrowserInfo.IsIE7)
  {
    desktopHeight = document.body.clientHeight;
    desktopWidth = document.body.clientWidth;
    scrollPos = document.body.scrollTop;
  }
  else
  {
    desktopHeight = document.documentElement.clientHeight;
    desktopWidth = document.documentElement.clientWidth;
    scrollPos = document.documentElement.scrollTop;
  }

  //  set the form position
  this.divElement.style.top = ((desktopHeight - formHeight) / 2 + scrollPos) + 'px';
  this.divElement.style.left = ((desktopWidth - formWidth) / 2) + 'px';
  
  //  activate the form
  this.key = key;
  this.used = true;  
  this.divEffects.FadeIn(100, 150);
}

/**
 *  function to close the form
 */
WATDesktopForm.prototype.CloseForm = function()
{
  //  reset the form data
  this.key = null;
  this.used = false;
  this.divEffects.FadeOut(0, 350);
}
