jQuery.fn.sameHeight = function() {
  var $this = $(this);
  var maxHeight = 0;
  var max = null;
  $($this).each(function(i, el) {
    if($(el).height () > maxHeight) {
      maxHeight = $(el).height();
      max = el;
    }
  });
  $this.not(max).css("min-height", maxHeight);
};

jQuery.fn.showoff = function() {
  var interval = 7000; // 7 seconds
  var $container = $(this);
  var $images = $container.find("img");
  var currentIndex = 0;
  var transition = $.showoff.fade;
  i = 0;
  setTimeout(function() {
    var anonymousSelf = arguments.callee;
    var nextIndex = (currentIndex + 1) % $images.length;
    var next = $images.get(nextIndex);
    var current = $images.get(currentIndex % $images.length);
    // let's reset the positioning for the image about to be shown
    $(next).css($.showoff._default_position);
    
    transition(current, next, function() {
      $(current).hide(); // not current anymore!
      setTimeout(anonymousSelf, interval);
    });
    
    currentIndex += 1;
  }, interval);
};

jQuery.showoff = {
  _default_position: {
    "position": "absolute",
    "top": "0px",
    "left": "0px",
    "right": "auto",
    "bottom": "auto",
    "float": "none",
    "margin": "0px",
    "padding": "0px",
    "opacity": 1,
    "width": "100%",
    "height": "100%",
    "display": "none"
  },
  
  _multiCallback: function(wait, callback) {
    this.ready = function() {
      wait -= 1;
      if(wait <= 0) {
        callback();
      }
    }
  },
  
  fade: function(from, to, callback) {
    var multiCallback = new $.showoff._multiCallback(2, callback);
    
    $(to).css({"opacity": 0, "display": "block"});
    $(to).animate({"opacity": 1}, 1000, multiCallback.ready);
    $(from).animate({"opacity": 0}, 1000, multiCallback.ready);
  }
};

(function (){
  $(function() {
    $("#exhibition .window").sameHeight();
    
    $("#mainnav ul").each(function() {
      var ul = $(this);
      ul.data('su-orig-height', ul.height());
      ul.data('su-orig-padding-top', ul.css('padding-top'));
      ul.data('su-orig-padding-bottom', ul.css('padding-bottom'));
      ul.height(0);
      ul.css('padding-top', 0);
      ul.css('padding-bottom', 0);
      ul.css('visibility','visible');
    });
    
    var bar = "up";
    $("#mainnav").click(function(e) {
      e.preventDefault();
      if(bar === "up") {
        $("#mainnav ul").each(function() {
          var ul = $(this);
          ul.animate({
            paddingTop: ul.data('su-orig-padding-top')
          }, 50, function() {
            ul.animate({
              height: ul.data('su-orig-height'),
              paddingBottom: ul.data('su-orig-padding-bottom')
            });
          });
        });
        bar = "down";
      } else if(bar === "down") {
        $("#mainnav ul").each(function() {
          var ul = $(this);
          ul.animate({
            height: 0,
            paddingBottom: 0
          }, function() {
            ul.animate({paddingTop: 0}, 50);
          });
        });
        bar = "up";
      }
      return false;
    })
    
    $("#mainnav form").click(function(e) { if($(e.target).val() != "") { e.stopPropagation(); } });
    $("#mainnav ul a").click(function(e) { e.stopPropagation(); });
    $("#showoff").showoff();
  });
})();


