// $Id: DSCollapse.js,v 1.41.2.2 2008/02/27 19:44:44 goba Exp $

var DSCollapse = DSCollapse || { 'settings': {}, 'behaviors': {}, 'themes': {}, 'locale': {} };

/**
 * Set the variable that indicates if JavaScript behaviors should be applied
 */
DSCollapse.jsEnabled = document.getElementsByTagName && document.createElement && document.createTextNode && document.documentElement && document.getElementById;


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

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

/**
 * Scroll a given fieldset into view as much as possible.
 */
DSCollapse.collapseScrollIntoView = function (node) {
  var h = self.innerHeight || document.documentElement.clientHeight || $j('body')[0].clientHeight || 0;
  var offset = self.pageYOffset || document.documentElement.scrollTop || $j('body')[0].scrollTop || 0;
  var posY = $j(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);
    }
  }
};

DSCollapse.behaviors.collapse = function (context) {
  $j('fieldset.collapsible > legend:not(.collapse-processed)', context).each(function() {
    var fieldset = $j(this.parentNode);
    // Expand if there are errors inside
    if ($j('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
    if (fieldset.attr("class") == 'collapsible collapsed') {
//       var text = this.innerHTML;
      var text = this.innerHTML.replace(/Hide /,"");
    } else {
//       var text = this.innerHTML;
      var text = 'Hide '+this.innerHTML.replace(/Show /,"");
    }

      $j(this).empty().append($j('<a href="#">'+ text +'</a>').click(function() {
        var fieldset = $j(this).parents('fieldset:first')[0];
        // Don't animate multiple times
        if (!fieldset.animating) {
          fieldset.animating = true;
          DSCollapse.toggleFieldset(fieldset);
        }
        return false;
      }))
      .after($j('<div class="fieldset-wrapper"></div>')
      .append(fieldset.children(':not(legend):not(.action)')))
      .addClass('collapse-processed');
  });
};

DSCollapse.attachBehaviors = function(context) {
  context = context || document;
  if (DSCollapse.jsEnabled) {
    // Execute all of them.
    jQuery.each(DSCollapse.behaviors, function() {
      this(context);
    });
  }
};

// Global Killswitch on the <html> element
if (DSCollapse.jsEnabled) {
  // Global Killswitch on the <html> element
  document.documentElement.className = 'js';
  // 'js enabled' cookie
  document.cookie = 'has_js=1; path=/';
  // Attach all behaviors.
  $j(document).ready(function() {
    DSCollapse.attachBehaviors(this);
  });
}