// From http://markmail.org/message/w7zqx4kpv3avvtea
function viewport_height() {
  var windowWidth, windowHeight;
  if (self.innerHeight) {    // all except Explorer
    windowWidth = self.innerWidth;
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }
  
  return windowHeight;
};

$(function() {
  $('li a.thumbnail').click(function(){
    var height = 700;
    var max_height = viewport_height() - 75; // Some buffer area
    if(height > max_height) height = max_height;
    
    tb_show('Article images', '#TB_inline?height=' + height + '&width=500&inlineId=article-gallery', false);
    this.blur();
    
    loadCarousel();

    // Workaround for jCarousel bug. For Safari jCarousel defers calling its
    // setup() method to a window.load event. Since we're loading the carousel
    // after the event has been fired it is never called. Also the carousel
    // can't be initialised whilst off screen as it doesn't have element
    // dimensions to calculate things from. See jquery.jcarousel.js:185 v0.2.3
    if (jQuery.browser.safari) {
      $(window).trigger('load');
    }

    $("#carousel a[href = " + $(this).attr("href") + "]").click(); // display the clicked on image
    
    return false;
  });
});
