/**
 * DOM-related utilities which build on the YUI DOM module.
 */

WOL.util.Dom = {
   /**
    * Given an element and two class names, determines which class name the element
    * actually has and replaces it with the other. Throws an exception if neither
    * class name is present on the element, or if something is passed to it which is
    * not an <code>HTMLElement</code>.
    *
    * @param {HTMLElement|String} elem An element reference or string id of an element
    * @param {String} classA One of the two class names.
    * @param {String} classB The other class name.
    */
   toggleClass: function(elem, classA, classB) {
      if(typeof(elem) == 'string') {
         elem = document.getElementById(elem);
      };
      if(!elem ||
         (!Dom.hasClass(elem, classA) && !Dom.hasClass(elem, classB))) {
         var errMsg = 'Element ' + elem + ' has neither ' + classA + ' nor ' + classB + ' as a class name';
         throw new Error(errMsg);
      };
      var newClass = Dom.hasClass(elem, classA) ? classB : classA;
      var oldClass = (newClass == classA) ? classB : classA;
      Dom.replaceClass(elem, oldClass, newClass);
   },
   
   /**
    * Given an element, switches it to <code>display: none</code> if it currently has
    * <code>display: block</code>, and vice-versa.
    * @param {String|HTMLElement} elem The element, either its string <id> or the element
    * itself.
    */
   toggleDisplay: function(elem) {
      if(typeof(elem) == 'string') {
         elem = document.getElementById(elem);
      };
      var newDisplay = (Dom.getStyle(elem, 'display') == 'block') ? 'none' : 'block';
      Dom.setStyle(elem, 'display', newDisplay);
   },

   /**
    * Given a YUI AJAX response object, parses its <code>responseText</code> into a
    * JSON object and returns it.
    * @param {Response} o The response object.
    * @type Object
    */
   parseJSONResponse: function(o) {
      var json_obj = eval( '(' + o.responseText + ')' );
      return json_obj;
   }
};