//***************************************************************************
//
// atgss_solutionToolbar.js File
//
//
// Copyright (C) 2005 Art Technology Group, Inc.
// All Rights Reserved.  No use, copying or distribution of this
// work may be made except in accordance with a valid license
// agreement from Art Technology Group.  This notice must be
// included on all copies, modifications and derivatives of this
// work.
// 
// Art Technology Group (ATG) MAKES NO REPRESENTATIONS OR WARRANTIES
// ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. ATG SHALL NOT BE
// LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
// MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
// 
//
// This page is the encapsulates the soltution toolbar object.
// This element is implemented as a js object due to its complex
// interface and behavior.
//
//*****************************************************************************


// strIconOrButton = 1- for icon and  2 - for button

function solutionToolbar(id, strCloseIcon ,strCloseText,strIconOrButton)
{
  //****************************************************************************
  // Initialize Element
  //
  this.toolbar	= document.createElement("div");
  this.toolbar.width = "100%";

  //********************************************************************
  // Initialize Properties
  //
  this.toolbar.buttons	         = new Array();
  this.toolbar.dropDisplay       = null;
  this.toolbar.leftButtonCell	 = null;
  this.toolbar.rightButtonCell	 = null;
  this.toolbar.closeIcon	 = strCloseIcon;
  this.toolbar.closeButtonText   =  strCloseText;
  this.toolbar.flagIconOrButton  =  strIconOrButton;

  //*********************************************************************
  // Initialize Methods
  //
  this.toolbar.display           = ToolbarDisplay;
  this.toolbar.addButton         = ToolbarAddButton;
  this.toolbar.closeDisplay      = ToolbarCloseDisplay;
  this.toolbar.openDisplay       = ToolbarOpenDisplay;
  this.toolbar.setToolbar        = setToolbar;

  this.toolbar.display();
}

function ToolbarDisplay()
{
  var toolbar = this;
  var mainTable = toolbar.appendChild(document.createElement("table"));

  mainTable.cellspacing   = 0;
  mainTable.cellsadding   = 0;
  mainTable.width         ="100%";

  var mainRow             = mainTable.insertRow(0);
  var mainToolbarCell     = mainRow.insertCell(0);
  var toolbarTable        = mainToolbarCell.appendChild(document.createElement("table"));
  toolbarTable.width      ="100%";
  toolbarTable.cellpadding = "0";
  toolbarTable.cellspacing = "0";
  toolbarTable.border      = "0";
  toolbarTable.id          = "topBarMenu";

  var toolbarTableRow      = toolbarTable.insertRow(0);
  toolbar.leftButtonCell   = toolbarTableRow.insertCell(0);
  toolbar.rightButtonCell  = toolbarTableRow.insertCell(1);

  toolbar.leftButtonCell.align   = "left";
  toolbar.leftButtonCell.vAlign  = "middle";

  toolbar.rightButtonCell.align  = "right";
  toolbar.rightButtonCell.vAlign = "middle";

  var rowtableLeft = toolbar.leftButtonCell.appendChild(document.createElement("table"));
  var rowtableRight = toolbar.rightButtonCell.appendChild(document.createElement("table"));

  rowtableLeft.cellPadding = 0;
  rowtableLeft.cellSpacing = 0;
  rowtableLeft.border      = 0;

  rowtableRight.cellPadding = 0;
  rowtableRight.cellSpacing = 0;
  rowtableRight.border      = 0;

  toolbar.leftRow           = rowtableLeft.insertRow(0);
  toolbar.rightRow          = rowtableRight.insertRow(0);

  //************************************************************************
  // Create display div area
  //
  var displayCell     = mainTable.insertRow(1).insertCell(0);
  displayCell.align   = "left";
  displayCell.colSpan = 2;

  var displayTable    = displayCell.appendChild(document.createElement("table"));
  displayTable.width  = "100%";
  displayTable.cellpadding = "0";
  displayTable.cellspacing = "0";
  displayTable.style.display = "none";
  displayTable.className = "bgWhite";

  toolbar.displayTable = displayTable;
  toolbar.currentActiveButton = null;

  // Insert close icon or button
  
  var closeCell = displayTable.insertRow(0).insertCell(0);
  closeCell.align = "right";

  if(toolbar.flagIconOrButton=="1") 
    {
      // Insert close icon
   
  var closeIcon	= document.createElement("img");
  closeIcon.src	= toolbar.closeIcon;
  closeIcon     = closeCell.appendChild(closeIcon);
  closeIcon.onclick = toolbar.closeDisplay;
  closeIcon.toolbar = toolbar;
   } 
  else if(toolbar.flagIconOrButton=="2")
  {
     // Insert close button
     
      var closeButton = document.createElement("input");
      closeButton.type = "button";
      closeButton.value = toolbar.closeButtonText;
      closeButton.className = "buttonSmallinsert";
      closeButton = closeCell.appendChild(closeButton);  
      closeButton.onclick = toolbar.closeDisplay;
      closeButton.toolbar = toolbar;  
      
     }

  displayTable.contentCell = displayTable.insertRow(1).insertCell(0);
  displayTable.contentCell.height = "100%";
}

function ToolbarAddButton(strId, strLabel, strIconOff, strIconOn, objContentDiv, action, strPosition)
{
  var toolbar = this;
  var button = document.createElement("table");

  button.cellPadding = 0;
  button.cellSpacing = 0;
  // bug correction
  var buTTon = document.createElement("input");
  buTTon.type = "image";
  buTTon.src = strIconOff;
  // 

  var buttonMainRow = button.insertRow(0);
  button.iconCell = buttonMainRow.insertCell(0);
  // bug correction
  button = button.iconCell.appendChild(buTTon);
  //
  button.toolbar = toolbar;

  if (strIconOff != "")
  {
    button.iconCell = buttonMainRow.insertCell(0);
    var iconOff	= document.createElement("img");
    iconOff.src	= strIconOff;
    button.iconOff = button.iconCell.appendChild(iconOff);

    if (strIconOn != "")
    {
      var iconOn = document.createElement("img");
      iconOn.src = strIconOn;
      button.iconOn = button.iconCell.appendChild(iconOn);
      button.iconOn.style.display = "none";
    }
  }

  button.labelCell           = buttonMainRow.insertCell(buttonMainRow.cells.length);
  button.labelCell.vAlign    = "middle";
  button.labelCell.className = "blueSmall"
  button.labelCell.innerHTML = strLabel;

  if (objContentDiv != null)
  {
    button.contentDiv               = toolbar.displayTable.contentCell.appendChild(objContentDiv);
    button.contentDiv.style.display = "none";
  }
  else
  {
    button.contentDiv   = null;
    button.onmousedown  = function () { button.iconOff.style.display = "none";  button.iconOn.style.display = ""; };
    button.onmouseup    = function () { button.iconOn.style.display = "none";  button.iconOff.style.display = ""; };
  }

  button.action    = action;
  button.className = "cursorToolBar";
  button.onclick   = toolbar.openDisplay;

  if (strPosition == "right")
    var insertCell = toolbar.rightRow.insertCell(toolbar.rightRow.cells.length);
  else
    var insertCell = toolbar.leftRow.insertCell(toolbar.leftRow.cells.length);

  toolbar.buttons[toolbar.buttons.length] = insertCell.appendChild(button);
}

function ToolbarOpenDisplay()
{
  var button       = this;
  var toolbar      = button.toolbar;
  var displayTable = toolbar.displayTable;
  var contentDiv   = button.contentDiv;

  if (button.action != null)
    button.action();

  if (contentDiv == null)
    return;

  if (toolbar.currentActiveButton == button)
    return;

  if (toolbar.currentActiveButton != null)
  {
    toolbar.currentActiveButton.iconOff.style.display = "";
    toolbar.currentActiveButton.iconOn.style.display = "none";
    toolbar.currentActiveButton.contentDiv.style.display = "none";
  }
  button.iconOff.style.display = "none";
  button.iconOn.style.display  = "";
  toolbar.currentActiveButton  = button;

  displayTable.style.display   = "";
  contentDiv.style.display     = "";
}

function ToolbarCloseDisplay()
{
  var toolbar                = this.toolbar;
  var displayTable           = toolbar.displayTable;
  displayTable.style.display = "none";

  if (toolbar.currentActiveButton != null)
  {
    toolbar.currentActiveButton.iconOff.style.display = "";
    toolbar.currentActiveButton.iconOn.style.display  = "none";
    toolbar.currentActiveButton.contentDiv.style.display = "none";
    toolbar.currentActiveButton = null;
  }
  document.getElementById("content").style.height = "1%";
}

function setToolbar(toolbar)
{
  this.toolbar = toolbar;
}
