$.fn.wait = function(time, type) {
    time = time || 1000;
    type = type || "fx";
    return this.queue(type, function() {
	var self = this;
	setTimeout(function() {
	    $(self).dequeue();
	}, time);
    });
};

$(function() {
  $('body').css('overflow','hidden');
  var preloadImages = ['images/gallerilogo.png','images/gallerilogo_i.png','images/gallerilogo_g.png','images/vanster.png','images/hoger.png','images/vanster_i.png','images/hoger_i.png','images/gallerisida_h.png','images/gallerisida_g.png'];
  var imgs = [];
  for (var i = 0; i < preloadImages.length; i++) {
    imgs[i] = new Image();
    imgs[i].src = preloadImages[i];
  }
  var bildtext_finns = false;
  if ($('#bildtext').length) { bildtext_finns = true; }
  var title_spacer = $('#taveltitel img');
  var images = $('#galleribild img');
  var maxheight = $('#spc').height();
  var maxwidth = $('#spc').width();
  function my_resize() {
    var height = $(window).height();
    if (bildtext_finns) { height -= 60; }
    var width = $(window).width();
    var mtop = 0;
    var mleft = 155;
    title_spacer[0].height = height - 206;
    if (height > maxheight) {
      mtop = Math.floor((height - maxheight) / 2);
      height = maxheight;
    }
    var factor = maxwidth/maxheight;
    var actual_width = factor*height;
    if ((width - 155) < actual_width) {
      var plopp = (actual_width + 155 - width)/factor;
      mtop += Math.floor(plopp/2);
      height -= Math.floor(plopp);
    } else {
      mleft += Math.floor((width - 155 - actual_width)/5);
    }
    images.each(function() {this.height = height});
    var width = Math.floor(maxwidth*height/maxheight);
    if (!jQuery.browser.msie) {
      images.each(function() {this.width = width});
    }
    $('#galleribild').css('top',mtop).css('left',mleft);
    $('#bildtext').css('top',height).css('width',width);
  }
  $(window).resize(my_resize);
  my_resize();
});

