/**
 ******************************************************************************
 * Copyright © 2004-2006. EMC Corporation.  All Rights Reserved.
 ******************************************************************************
 *
 * Project        WDK
 * File           controls.js
 * Description    WDK Controls Javascript Library
 * Created on     11th August 2006
 * Tab width      3
 *
 ******************************************************************************
 *
 * PVCS Maintained Data
 *
 * Revision       $Revision: 2$
 * Modified on    $Date: 2/24/2008 11:43:29 PM$
 *
 * Log at EOF
 *
 ******************************************************************************
 */

if (typeof wdk.control == "undefined")
{ // support portal

/**
 * Create control namespace
 */
wdk.control = new Object();

/**
 * Client-side model for dmf:button
 */
wdk.control.Button = function (btnId)
{
   this.m_button = document.getElementById(btnId);
}

/**
 * Disables or enables a WDK button control. This method understands icon buttons and how to
 * switch between the two images for enabled and disabled state.
 * If it's an icon button, there are assumed to be two child images, one for enabled, one for disabled.
 */
wdk.control.Button.prototype.setDisabled = function (isDisabled)
{
   if (this.m_button == null)
      return;

   // The DOM property on IHTMLButton
   this.m_button.disabled = isDisabled;

   if (wdk.dom.hasClass(this.m_button, "image"))  // Icon buttons
   {
      var imgs = this.m_button.getElementsByTagName("IMG");

      if (imgs.length == 2)
      {
         imgs[0].style.display = (isDisabled) ? "none" : "inline";
         imgs[1].style.display = (!isDisabled) ? "none" : "inline";
      }
   }
}

/**
 * Client-side model for dmf:panel
 */
wdk.control.Panel = new function()
{
   /**
    * Based on the isVisible flag passed in, show or hide the dom elements whose class attribute values
    * match the panel name.
    */
   this.setVisible = function (panelName, isVisible)
   {
      if (typeof panelName == undefined || panelName == null)
         return;

      // get elements classed by panel name
      var elems = wdk.dom.getElementsByClassName(null, panelName, null);
      for (var i=0; i<elems.length; i++)
      {
         var elem = elems[i];
         elem.style.display = (isVisible) ? '' : 'none';
      }
   }
}

/**
 * Access the WDK client control model for the given element.
 * Based on the element's control class, we will return the appropriate client model object.
 */
wdk.control.get = function (id)
{
   var elem = document.getElementById(id);

   if (elem == null)
      return null;

   if (elem.m_ctrl == null)
   {
      // TODO: Memory leak here, since button references node, and node now references button?
      if (wdk.dom.hasClass(elem, "button"))
         elem.m_ctrl = new wdk.control.Button(id);
   }

   return elem.m_ctrl;
}

} // end if (typeof wdk.control == "undefined")
