
var buildWhatsNew = function(){
    /*
    $('img.overlay-hero').load(function(){
        var h = $(this).height();
        if(h){
            $('.arrow').css('bottom', (h/2)-17);
        }
    });
    */
  $('.exhibition-group .next').show().removeClass('hidden').click(function() {
    var next = $(this);
    next.hide();
    var group = $(this).parent('.exhibition-group');
    group.find('.prev').hide();
    var firstItem = group.children('li.exhibition:eq(0)');
    var lastItem = group.children('li.exhibition:eq(4)');
    firstItem.find('dd.category').hide();
    firstItem.find('dd.title').hide();
    lastItem.find('dd.category').hide();
    lastItem.find('dd.title').hide();
    firstItem.animate({width:"hide", height:"200", opacity:"hide"}, "slow", function(){
      next.show();
      $('.exhibition-group .prev').show();
      $(this).insertBefore(next);
      lastItem.fadeIn("slow", function(){
        lastItem.find('dd.category').fadeIn();
        lastItem.find('dd.title').fadeIn();
      });
    });  
  });
  
  $('.exhibition-group .prev').show().removeClass('hidden').click(function() {
    var prev = $(this);
    prev.hide();
    var group = $(this).parent('.exhibition-group');
    group.find('.next').hide();
    var hideItem = group.children('li.exhibition:eq(3)');
    var length = group.find('li.exhibition').length;
    var showItem = group.children('li.exhibition:eq('+(length-1)+')');
    hideItem.find('dd.category').hide();
    hideItem.find('dd.title').hide();
    showItem.find('dd.category').hide();
    showItem.find('dd.title').hide();
    hideItem.fadeOut("slow", function(){
      prev.show();
      $('.exhibition-group .next').show();
      showItem.insertAfter(prev).animate({width:"show", marginLeft:"show", marginRight:"show", opacity:"show"}, "slow", function(){
        showItem.find('dd.category').fadeIn();
        showItem.find('dd.title').fadeIn();
      });
    });
  });
};

var transitions = {
    'next': false,
    'prev': false
};

$(document).ready(function() {

  // exhibitions
    buildWhatsNew();
  
  $('dl.gallery .next').show().click(function() {
    if(transitions['next']) return; // setTimeout(function(){$(this).click();}, 1000);
    transitions['next'] = true;
    var next = $('dl.gallery .next');
    next.hide();
    var group = $('dl.gallery');
    $('dl.gallery').each(function(index) {
      var thisg = $(this);
      thisg.children('dd.thumb:eq(0)').animate({width:"hide", marginLeft:"hide", marginRight:"hide", opacity:"hide"}, "slow", function(){
        thisg.children('dd.thumb:eq(9)').fadeIn('slow');
        var nextg = $(this).parent('dl').find('img.next').parent('dd');
        $(this).insertBefore(nextg);
        next.show();
        $('#preview dd a').unbind();
        previewBind();
        transitions['next'] = false;
      });
    });
    
  });
  
  $('dl.gallery .prev').show().click(function() {
    if(transitions['prev']) return; // setTimeout(function(){$(this).click();}, 1000);
    transitions['prev'] = true;
    var next = $('dl.gallery .next');
    var group = $('dl.gallery');
    $('dl.gallery').each(function(index) {
      var thisg = $(this);
      var length = thisg.children('dd.thumb').length;
      var prevg = $(this).find('dd img.prev').parent('dd');
      next.hide();
      thisg.children('dd.thumb:eq(8)').fadeOut("fast", function(){

        thisg.children('dd.thumb:eq('+(length-1)+')').insertAfter(prevg).animate({width:"show", marginLeft:"show", marginRight:"show", opacity:"show"}, "slow").removeClass('hidden');
        $('#preview dd a').unbind();
        previewBind();
        next.show();
        transitions['prev'] = false;
      });
    });
    
  });
});

function previewBind () {
  $('#preview dd a').each(function(index) {
    $(this).click(function() {
    
    $('embed.movie').hide(); // blackwell
    $('div.movie').show();
    
      $('#overlay').css('z-index', '100');
      $('#overlay').show();
      var selected = $('#overlay-content dd a:eq('+index+')');
      goto(selected);
      $('#overlay-content').show();
      return false;
    });
  });
}

$(document).ready(function() {
  //overlay
  repositionOverlayContent();
  $(window).resize(repositionOverlayContent);
  previewBind();
  
  $('#preview dt a').click(function() {

    $('embed.movie').hide(); // blackwell
    $('div.movie').show();
    
    $('#overlay').css('z-index', '100');
    $('#overlay').show();
    var selected = $('#overlay-content dd a:eq(0)');
    goto(selected);
    $('#overlay-content').show();
    return false;
  });
  
  $('#overlay-content dd a').click(function() {
    goto($(this));
    return false;
  });
});

function goto (selected) {
  $('#overlay-content dd a img').removeClass("on");
  selected.find('img').addClass('on');
  var imgsrc = selected.parent('dd').find('img.large').attr("src");
  var artist = selected.parent('dd').find('.artist').html();
  var title = selected.parent('dd').find('.title').html();
  var year = selected.parent('dd').find('.year').html();
  var description = selected.parent('dd').find('.description').html();
  
  with(selected.parent('dd').siblings('dt').find('img.overlay-hero')){
     //fadeOut('slow', function(){$(this).attr('src', imgsrc).fadeIn('fast');});
      attr('src', imgsrc);

  }
  
  $('span.plac').html('<strong>'+artist+'</strong><br><strong><em>'+title+'</em></strong><br>'+year);
  $('#overlay-content p').html(description);
}

/**
 * Center img.overlay-hero within it's container.
 */
function setHeroAlignment(){
    $('img.overlay-hero').each(
        function(i,e){
            var d = $(e).parents('dl.gallery').get(0);
            var W=$(d).width();
            var w=$(e).width();
            $(e).css('padding-left', (W-w)>0 ? (W-w)/2 : 0);
            $(e).show(1);
            $(e).parent('dd').css('min-height', $(e).height());
        }
    );
}

function repositionOverlayContent () {
  var WIDTH = 700;
  
  var HEIGHT = $('#overlay-content').height();
  
  $("#overlay-content").css({marginLeft: '-' + parseInt((WIDTH / 2),10)-24 + 'px', width: WIDTH + 'px'});
  $('#overlay-close').click(function() {
  
    $('div.movie').hide(); // blackwell
    $('embed.movie').show();
    

    
    $('#overlay').hide();
    $("#overlay-content").hide();
  });
}

$(document).ready(function() {
  //exhibitions detail
  setSideHeight();
  $('a.show-more').click(function() {
    $('span.more').removeClass('hidden');
    $(this).remove();
    return false;
  });
  playlister($('#video-module'));
  playlister($('#audio-module'));
  playlister($('#archive-module'));
  playlister($('#upcoming-module'));

});

function setSideHeight () {
  var sidebarHeight = 0;
  $('#related-sidebar .module').each(function(index) {
    sidebarHeight+=$(this).height();
    sidebarHeight+=30;
  });

  //$('#exhibitions-details #details').css('minHeight', sidebarHeight+'px');
}

function playlister (_mod) {
  var mod = _mod;
  var ppage = 1;
  var plength = mod.find('dl.pitem').length;
  //mod.find('span.count').html("1-3 of " + plength)
  mod.find('span.count').html((plength > 0 ? 1 : 0) + "-" + (plength < 1 ? 0 : (plength >= 3 ? 3 : plength)) + " of " + plength);
  if (plength>3) {
    mod.find('.list-controls').find('.hidden').removeClass('hidden');
    mod.find('.list-controls .next').click(function() {
      if (ppage*3>plength-1) {
        return false;
      };
      ppage = ppage+1;
      var pagenum = ((ppage-1)*3+1);
      var endnum = pagenum+3 > plength ? plength : pagenum+2;
      mod.find('span.count').html(pagenum +"-"+endnum+" of " + plength);
      mod.find('.pitem').hide();
      for (var i=pagenum-1; i < endnum; i++) {
        mod.find('.pitem:eq('+i+')').fadeIn("slow");
      };
      return false;
    });
    mod.find('.list-controls .previous').click(function() {
      if (ppage==1) {
        return false;
      };
      ppage = ppage-1;
      var pagenum = ((ppage-1)*3+1);
      mod.find('span.count').html(pagenum +"-"+(pagenum+2)+" of " + plength);
      mod.find('.pitem').hide();
      for (var i=pagenum-1; i < pagenum+2; i++) {
        mod.find('.pitem:eq('+i+')').fadeIn("slow");
      };
      return false;
    });
  };
}