// $Id: collapse.js,v 1.17 2008/01/29 10:58:25 goba Exp $

/**
 * Toggle the visibility of a fieldset using smooth animations
 */
Drupal.toggleFieldset = function(fieldset) {
  if ($(fieldset).is('.collapsed')) {
    // Action div containers are processed separately because of a IE bug
    // that alters the default submit button behavior.
    var content = $('> div:not(.action)', fieldset);
    $(fieldset).removeClass('collapsed');
    $('> legend > a > img', fieldset).replaceWith(Drupal.theme('collapseImage', false));
    content.hide();
    content.slideDown( {
      duration: 'fast',
      easing: 'linear',
      complete: function() {
        Drupal.collapseScrollIntoView(this.parentNode);
        this.parentNode.animating = false;
        $('div.action', fieldset).show();
      },
      step: function() {
        // Scroll the fieldset into view
        Drupal.collapseScrollIntoView(this.parentNode);
      }
    });
  }
  else {
    $('div.action', fieldset).hide();
    var content = $('> div:not(.action)', fieldset).slideUp('fast', function() {
      $(this.parentNode).addClass('collapsed');
      $('> legend > a > img', this.parentNode).replaceWith(Drupal.theme('collapseImage', true));
      this.parentNode.animating = false;
    });
  }
};

/**
 * Scroll a given fieldset into view as much as possible.
 */
Drupal.collapseScrollIntoView = function (node) {
  var h = self.innerHeight || document.documentElement.clientHeight || $('body')[0].clientHeight || 0;
  var offset = self.pageYOffset || document.documentElement.scrollTop || $('body')[0].scrollTop || 0;
  var posY = $(node).offset().top;
  var fudge = 55;
  if (posY + node.offsetHeight + fudge > h + offset) {
    if (node.offsetHeight > h) {
      window.scrollTo(0, posY);
    } else {
      window.scrollTo(0, posY + node.offsetHeight - h + fudge);
    }
  }
};

Drupal.behaviors.collapse = function (context) {
  $('fieldset.collapsible > legend:not(.collapse-processed)', context).each(function() {
    var fieldset = $(this.parentNode);
    // Expand if there are errors inside
    if ($('input.error, textarea.error, select.error', fieldset).size() > 0) {
      fieldset.removeClass('collapsed');
    }

    // Turn the legend into a clickable link and wrap the contents of the fieldset
    // in a div for easier animation
    var text = this.innerHTML;
    var show = fieldset.hasClass('collapsed') ? true : false;
    $(this).empty().append($('<a href="#">' + Drupal.theme('collapseImage', show) + ' <span class="collapser">' + text + '</span>' + '</a>').click(function () {
	var fieldset = $(this).parents('fieldset:first')[0];
        // Don't animate multiple times
        if (!fieldset.animating) {
          fieldset.animating = true;
          Drupal.toggleFieldset(fieldset);
        }
        return false;
      }))
      .after($('<div class="fieldset-wrapper"></div>')
      .append(fieldset.children(':not(legend):not(.action)')))
      .addClass('collapse-processed');
  });
};

/**
 * Provide the source URL of the collapse and expand image
 * Overrirde this in a theme to apply different images.
 *
 * Parameters:
 *     collapsed: true if collapsed, flase if expanded
 *
 * Returns the src attribute for the image.
 */

Drupal.theme.prototype.collapseImageSrc = function(collapsed) {
  return Drupal.settings.basePath + ((collapsed) ? 'misc/menu-collapsed.png' : 'misc/menu-expanded.png');
}

/**
 * Construct the image for the collapse/expand link.
 *
 * Parameters:
 *   collapsed: true if collapsed, false if expanded
 *
 * Returns the HTML of the img element.
 */
Drupal.theme.prototype.collapseImage = function (collapsed) {
  var alt = (collapsed) ? Drupal.t('Show') : Drupal.t('Hide');
  return '<img src="' + Drupal.theme('collapseImageSrc', collapsed) + '" alt="' + alt + '" />';
};



