//Handing accordion interfaces
(function($) {
  $.fn.accordion = function(){
    $(this).each(function() {
      var $this = $(this),
        numItems = $this.children().length,
        openItem = 1;

      if(numItems > 1) {
        openItem = 2;
      }

      $this.children(':not(:nth-child('+openItem+'))').addClass('closed');
      $this.children(':nth-child('+openItem+')').addClass('open');

      $this.find('.contents').prev('a').click(function(e) {
        $(this).addClass('no-outline');
        var $parEl = $(this).closest('li');

        if(!$parEl.hasClass('open')) {
          var $openItem = $this.find('.open');
          $openItem.toggleClass('open').find('.contents').slideUp(300);
          $parEl.toggleClass('open').find('.contents').slideDown(300, function() {
            $parEl.trigger("animation-finished");
          });
        }

        e.preventDefault();
      });

    });
    return $(this);
  };
})(jQuery);
