// Specify which forms will get special treatment
var forms = ['comment_form'];
// Specify which links will become buttons
var buttons = ['a.subscribe'];
// Is tinymce enabled?
var tinymce_enabled = (typeof(tinyMCE) != 'undefined');
var isIE = (navigator.appName == "Microsoft Internet Explorer");

function toggleBlock(block) {
  block = JQ(block);
  JQ.cookie(block.id(), block.is('.collapsed') ? 'expanded' : 'collapsed', {path: '/'});
  block.toggleClass('collapsed');
  block.children('div.content').toggle();
}

// Make blocks and elements collapsible
JQ(document).ready(function() {
  // Make helptip collapsible
  block = JQ('.block-helptip');
  if (block.length) {
    if (JQ.cookie(block.id()) == 'collapsed') {
      toggleBlock(block);
    }
    block.children('h3.title a').click(function() {
      toggleBlock(JQ(this).parents('div.block'));
      return false;
    })
  }

  // Let's create some buttons
  for (button_path in buttons) {
    JQ(buttons[button_path]).each(function() {
      var button = JQ(this);
      button.html('<span>' + button.html() + '</span>');
      button.addClass('button');
    });
  }

  for (form_id in forms) {
    var form = JQ('#' + forms[form_id]);
    if (form.length) {
      // Change the label for the button that the user clicked
      form.find('input.form-submit').click(function() {
        // For whatever reason IE6/7 borks unless we get the form element again
        var form = JQ('#' + forms[form_id]);
        var oldLabel = this.value;
        var newLabel = oldLabel.split(' ')[0] + 'ing...';
        // Change the label to "[Verb]ing..."
        JQ(this).attr({value: newLabel});
        if (this.id != 'attach') {
          // Disable the buttons when the form is submitted (#2511 not for ajax file attachment submissions)
          addSubmitEvent(document.getElementById('comment_form'), function() { JQ(this).find('input.form-submit').attr('disabled', true) });
        }
        // The form hook depends on the button label so let's append that to the form
        form.append('<input type="hidden" name="op" value="' + oldLabel + '" />');
      });
    }
  }
  
  var ppbform = JQ('#search_form');
  //additional check to act only on the ppbsearch form
  var ppbform_insides = JQ('#search_form .ppbsearch');
  if (ppbform.length && ppbform_insides.length) {
    // Change the label for the button that the user clicked
    ppbform.find('input.form-submit').click(function() {
      // For whatever reason IE6/7 borks unless we get the form element again
      var ppbform = JQ('#search_form');
      var oldLabel = this.value;
      var newLabel = oldLabel.split(' ')[0] + 'ing...';
      // Change the label to "[Verb]ing..."
      JQ(this).attr({value: newLabel});
      // Disable the buttons when the form is submitted
      addSubmitEvent(document.getElementById('search_form'), function() { JQ(this).find('input.form-submit').attr('disabled', true) });
      // The form hook depends on the button label so let's append that to the form
      ppbform.append('<input type="hidden" name="op" value="' + oldLabel + '" />');
    });
  }

  JQ('div.sidebar div.block').each(function(i, block) {
    block = JQ(block);
    if (JQ.cookie(block.id()) == 'collapsed') {
      toggleBlock(block);
    }
    block.children('h3.title a').click(function() {
      toggleBlock(JQ(this).parents('div.block'));
      return false;
    })
  });
});

