jQuery.fn.extend({
  toggleMenuImage: function() {
    if (this.length) {
      new_letter = this.attr("src").match(/a\.gif$/) || this.attr("name") == getCurrentPage() ? 'b' : 'a';
      return this.attr("src", "/images/navi_" + this.attr("name") + "_" + new_letter + ".gif");
    }
  },
  deselectMenuImage: function() {
    return this.attr("src", this.attr("src").replace(/[ab]\.gif$/, "a.gif"));
  }
});

function getCurrentPage() {
  return document.location.hash == "" ? "start" : document.location.hash.match(/#(\w+)/i)[1];
}

function loadLinkByAnchor(link) {
  $('#main').load(link.href.match(/#(\w+)/)[1]+".htm");
}

jQuery(document).ready(function () {
   $('#main').load(getCurrentPage() + ".htm");
   $("#navigation img[name="+ getCurrentPage() +"]").toggleMenuImage();

   $("a.menulink").bind('click', function() {
    $('ul.subnavigation').hide();
    $(this).parent().find('ul.subnavigation').show();
    $("#navigation img").each(function() {
      $(this).deselectMenuImage();
    });
    $(this).find("img:first").toggleMenuImage();
    loadLinkByAnchor(this);
  });

  $("a.ajaxlink").bind('click', function() {
    $('ul.subnavigation').hide();
    loadLinkByAnchor(this);
    $("#navigation img").each(function() {
      $(this).deselectMenuImage();
    });
    $("#navigation img[name=" + this.href.match(/#(\w+)/)[1] + "]").toggleMenuImage();
  });

  $('#navigation img').bind('mouseover mouseout', function() {
    $(this).toggleMenuImage();
  });

  $('#blogbutton img').bind('mouseover mouseout', function() {
    this.src = "images/blog" + (this.src.match(/blog\.gif$/) ? "_mouseover" : "") + ".gif";
  });
}); 