$( function() {

  var accordion = $('#accordion-wrapper');

  var matchedPane = $("#" + $(accordion).find('a.matched').eq(0).attr('rel'));
  $(accordion).children('.accordion-pane').not($(matchedPane)).css('display', 'none');
  $(accordion).find('.collapse').hide();

  $(accordion).children(".accordion-key").click( function(e) {
          var targetPane = document.getElementById($(this).attr('rel'));
          var visible = $(targetPane).css('display');
          $(accordion).children('a.active').children('.collapse').hide();
          $(accordion).children('a.active').children('.expand').show();
          $(accordion).children('a.active').not(this).removeClass('active');

          if (visible == "none") {
                  $(this).addClass('active');
                  $(this).children('.expand').hide();
                  $(this).children('.collapse').show();
                  $(targetPane).slideDown();
          } else {
                  $(targetPane).slideUp();
                  $(this).removeClass('active');
                  $(this).children('.expand').show();
                  $(this).children('.collapse').hide();
          }
          $(accordion).children('.accordion-pane').not(targetPane).slideUp();
          e.preventDefault();
  });

});
