function showMenu() {
  var max = 535;
  var h = $('#menu').height();
  var new_h = 535 - h - 20;
  
  if (new_h > 0) {
    $('#menuSpacer').height(new_h);
  }
  
  $('#menu').SlideInDown(500);
}

function hideMenu() {
  if ($('#menu').css('display') != 'none') {
    $('#menu').SlideOutDown(500);
  }
  $('#menuSpacer div').hide();
}

function changeImage(src, indicatorSrc, base) {
  $('#mainImage').css({background: 'transparent url(' + indicatorSrc + ') no-repeat center center'});
  hideMenu();
  $.getJSON(src + '?' + Date.parse(new Date()) , function(json){
    var img = json.image; //id, src, info, position, next, prev
    var gal = json.gallery; //id, name, count
    
    //image preload
    var pic = new Image(); 
    pic.src = img.src;
    //pic.onload = function () {
      $('#mainImage').css({background: 'transparent url(' + pic.src + ') no-repeat top center'});
      $('#mainImage').fadeIn(1000);
      showInfoContainers();
    //}
    
    $('#infoContainer').html((img.info) ? '<b>Info:</b> ' + img.info : '&nbsp;');
    $('#galleryInfoContainer').html('&nbsp;' + gal.name);
    
    var prev = '';
    if (img.prev != "0") {
      prev = '<a href="' + base + 'images/get/' + img.prev + '" id="prevImage">&lt;</a>';
    }
    
    var next = '';
    if (img.next != "0") {
      next = '<a href="' + base + 'images/get/' + img.next + '" id="nextImage">&gt;</a>';
    }
    
    $('#imagePositionContainer').html(prev + ' ' + img.position + ' / ' + gal.count + ' ' + next);
    
    $('#nextImage').click(function(){
      changeImage(this.href, iSrc, base);
      return false;
    });
    $('#prevImage').click(function(){
      changeImage(this.href, iSrc, base);
      return false;
    });
    
  });
}

function showInfoContainers() {
  $('#infoContainer').show();
  $('#infoContainer2').show();
}

