
jQuery.fn.fadeToggle = function(speed, easing, callback) {
  return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

$(document).ready(function() {

  // Search box
  $('#site-search').val('SEARCH').css({ marginTop: '-5px' }).focus(function() {
    if ($(this).val() == 'SEARCH') {
      $(this).val('');
    }
  }).blur(function() {
    if (!$(this).val()) {
      $(this).val('SEARCH');
    }
  });

  // Blur links on focus
  $('a').bind('focus', function() { if (this.blur) this.blur(); });

  $('.dropdown-box').hover(
    function() {
      $(this).find('.dropdown-list-outer').show();
    },
    function() {
      $(this).find('.dropdown-list-outer').hide();
    }
  );

  // Toggle block controls.
  $('div.block').hover(
    function() { $(this).find('div.block-controls').fadeIn(140); },
    function() { $(this).find('div.block-controls').fadeOut(140); }
  );

  // Toggle comment controls.
  $('div.comment').hover(
    function() { $(this).find('div.comment-controls').fadeIn(140); },
    function() { $(this).find('div.comment-controls').fadeOut(140); }
  );

  // Email form functions.
  $('a.email-form-cancel').click(function() {
    $('.article-email-form').fadeOut(150);
  });

  // Strip colons from labels.
  $('#comment_form label').each(function() {
      var html = $(this).html().replace(/:/g, '');
      $(this).html(html);
  });

  // Remove "Anonymous" username field
  if ($('#edit-name-1').val() == 'Anonymous') $('#edit-name-1').val('');

  // Remove empty comments div
  if ($('#comments').length && !$('#comments').html().replace(/\s/g, '').length) $('#comments').remove();

  // Replace Comments "Save" button with "Submit"
  $('#comment_form').each(function() {
      var submit = $('input.form-submit', this);
      submit.hide();
      $('<div class="article-continue large-link clearfix"><a href="#">Submit</a></div>').click(function() {
          submit.click();
          return false;
      }).appendTo(this);
  });

  $('#show_comments').click(function() {
      if ($('.show_comments:visible').length > 0) {
        $('.show_comments').hide();
      } else {
        $('.show_comments').show();
      }
      if ($(this).attr('href') == '#comments') {
        return false;
      }
  });

});

function toggleTarget(target) {
  $(target).fadeToggle(150);
}

function selectDropDown(type, val) {
  $('#selected-' + type).html(val);
  $('#' + type + '-input input').val(val);
  $('#form-' + type + '-dropdown .dropdown-list-outer').hide();
}

function submitNewsletterForm() {
  $('#mc-embedded-subscribe-form').submit();
}

