
  var allowScaleImage = true;
  var heights = new Array();
  var imageW = 0;
  var containerH = 0;
  var containerW = 0;
  var imageMargin = 0;
  var multiplyer = 1;
  var containerLeft = 0;

function startScaling() {
  i = 0;
  imageW = $('#content-center-img').width();
  containerH = $('#content-center-img').height()+3;
  containerW = $('#page-wrapper').width();
  $('#content-center-img img.large-image').each(function() {
    heights[i] = $(this).height();
    i++;
  });
}

function scaleGalleryImage() {
  if (!allowScaleImage) {
    return false;
  }
  allowScaleImage = false;
  
  if (!isMobile()) {
    //trace($(window).height() + " " + $('#content-container').height());
    fixedSpace = parseInt($('#page-wrapper').css('padding-right'))*2 + parseInt($('#top-left-text').outerWidth())*2;
    multiplyer = Math.max(0.65, Math.min(1, ($(window).width()-fixedSpace)/imageW));
    
    //alert(parseInt($('#page-wrapper').css('padding-right'))*2 + parseInt($('#top-left-text').outerWidth())*2);
    i = 0;
    $('#content-center-img img.large-image').each(function() {
      $(this).width(imageW * multiplyer);
      $(this).height(heights[i] * multiplyer);
      i++;
    });
    $('#content-center-img').width(imageW * multiplyer);
    $('#logo').width(imageW * multiplyer);
    $('#page-wrapper').width(imageW * multiplyer + fixedSpace - parseInt($('#page-wrapper').css('padding-right'))*2);
    $('#content-container').height(containerH * multiplyer);
    $('.wider-inner-wrapper').width(imageW * multiplyer + 5);
  }
  
  //Popup-föstret
  $img = $('#cover img');
  if ($(window).height()/$(window).width() > $img.height() / $img.width())  {
    aspect = $img.width() / $img.height();
    $img.width($(window).width() - 70);
    $img.height(($(window).width() - 70) / aspect);
    $img.css('margin-top',($(window).height() - $img.height())/2);
    $img.css('margin-left',($(window).width() - $img.width())/2)
  } else {
    aspect = $img.width() / $img.height();
    $img.height($(window).height() - 70);
    $img.width(($(window).height() - 70) * aspect);
    $img.css('margin-top',($(window).height() - $img.height())/2);
    $img.css('margin-left',($(window).width() - $img.width())/2)
  }
  
  /*
  var $minHeight = 340;
  $img = $('#image img:last');
  if (areaW/areaH < imageW/imageH) {
    $maxWidth = areaW;
    $maxHeight = $maxWidth / imageW * imageH;
  } else {
    $maxHeight = Math.max($minHeight, areaH);
    $maxWidth = $maxHeight / imageH * imageW;
  }
  
  //$('#content-wrapper').css('height',$maxHeight);
  
  $img.css('height',$maxHeight);
  $img.css('width',$maxWidth);
  $img.css('left', (areaW - $maxWidth)/2);
  $img.css('top', Math.max(0, (areaH - $maxHeight)));
  */
  allowScaleImage = true;
}


if (isMobile()) {
  loadCss('/css/mobile.css');
}

$(document).ready(function() {
  
  
  Cufon.replace('a', {hover: true});
  Cufon.replace('p');
  Cufon.replace('b');
  
  Cufon.now();
  
  startScaling();
  scaleGalleryImage();
  $(window).resize(scaleGalleryImage);
  
 
  $('.v-center').each(function() {
    margin = ($(this).parent().height() - $(this).height()) * 0.45;
    $(this).css('margin-top',margin);
  });

  $('.navigation-item-wrapper').click(function() {
    window.location = $(this).find('a').attr('href');
  })
  $('.navigation-item-wrapper').mouseover(function() {
    $(this).find('img').stop().fadeTo(0,0);
  })
  $('.navigation-item-wrapper').mouseout(function() {
    $(this).find('img').stop().fadeTo(500,1);
  })
  
  $('.wider-inner-wrapper').each(function() {
    $(this).width($(this).parent().width() + 5);
  });
  
  $('#content-center-img a').click(function() {
    if (isMobile()) {
      window.location = '/templates/get-large-image-wrapper.php' + $(this).attr('href');
    } else {
      $('#cover').show().fadeTo(100,1);
      $('#cover').load('/templates/get-large-image.php' + $(this).attr('href'),function() {
        $('body').css('overflow','hidden');
        // Centrerar
        $('#cover img').each(function() {
          margin = ($(this).parent().height() - $(this).height()) * 0.45;
          $(this).css('margin-top',margin);
        })
        scaleGalleryImage();
        $('#cover img').delay(250).fadeTo(250,1);
      });
    }
    return false;
  });
  
  $('#cover').click(function() {
    $('body').css('overflow','visible');
    $('#cover').fadeTo(200,0, function() {
      $('#cover').empty().hide();
      scaleGalleryImage();
    });
  })
  

});


