activEvent = null;
$(document).ready(function(){
  var wizard = $('#accordion').accordion({
    active: false,
    alwaysOpen: false,
    autoheight: false,
    event: false,
    header: 'div.accHeader'
  });
  
  $("img.accControl", wizard).each(function(index) {
    $(this).click(function(){
      $.accChange(this,index,'slide');
    });
  });
  
  $("a.accLink").click(function() {
    var i = $("a.accLink").index(this);
    var elem = $("img.accControl:eq(" + i + ")");
    $.accChange(elem[0],i,false);
    var hash = this.href.split("#");
    hash = "#" + hash[hash.length - 1];
    window.location.hash = hash;
    return false;
  });
  
  /*
   * @param e - referenz to the selected item of the accordion
   * @param i - the index of the item
   * @param a - animate the accordion
   */
  $.extend({
    accChange: function (e,i,a) {
      var s = e.src;
      if (s.match(/plus.gif$/)) {
        s = s.replace(/plus.gif$/, 'minus.gif');
        if (null != activEvent) {
          activEvent.src = s.replace(/minus.gif$/, 'plus.gif');
        }
        activEvent = e;
      } else {
        s = s.replace(/minus.gif$/, 'plus.gif');
        activEvent = null;
      }
      e.src = s;
      $('#accordion').data('ui-accordion').options.animated = a;
      wizard.accordion("activate", i);
    }
  });              
});
