jQuery(document).ready(function() {
  if (document.images) {
    image1 = new Image();
    image1.src="/images/common/menu_top.gif";
    image2 = new Image();
    image2.src="/images/common/menu_center.gif";
    image3 = new Image();
    image3.src="/images/common/menu_center_hl.gif";
    image4 = new Image();
    image4.src="/images/common/menu_bottom.gif";
    image5 = new Image();
    image5.src="/images/common/tab_left.gif";
    image6 = new Image();
    image6.src="/images/common/tab_right.gif";
  }

  if ($(".mail").length > 0) {
    var mail = $(".mail");
    mail.html(mail.attr("a") + "@" + mail.attr("b") + "." + mail.attr("c"));
  }

  // initialize open menus
  $("li.parent").each(function(index) {
    var state = $.crumb('menus', $(this).identify());
    
    if (state == "open") {
      $(this).addClass("open");
      $(this).parents("ul").children("li.child_menu").show();
    }
  });

  // toggle menus
  $("li.parent .arrow").click(function(eventObj) {
    var target = $(eventObj.target);

    var node = target.parents("li");    
    var ul   = target.parents("ul");

    if (node.hasClass("open")) {
      node.removeClass("open");
      ul.children("li.child_menu").hide();
      $.crumb('menus', node.identify(), null);

    } else {
      node.addClass("open");
      ul.children("li.child_menu").show();
      $.crumb('menus', node.identify(), "open");
    }
  });

  // put content for image in the edit box
  $(".thumb p.cap a").click(function(eventObj) {
      eventObj.preventDefault();
      var imageId = $(eventObj.target).attr("rel");

      $('#img_id').val(imageId);
      $('#img_title').val($('#img' + imageId + "_title").val());
      $('#img_desc').val($('#img' + imageId + "_desc").val());
      $('#img_year').val($('#img' + imageId + "_year").val());
    }
  );

  // Make sure that there is an image selected
  $("#img_edit").submit(function(eventObj) {

    if ($('#img_id').val() == "") {
      alert("You must choose an image to edit. \n Do this by clicking on the caption");
    }
  });
});
