function setWidthHeightRatio(){
  $('.main-image').data('data-ratio',$('.main-image-size').width()/$('.main-image-size').height());
}
function fitImage(){
  var widthDifference = $('.main-image img').width() - $('#page-wrapper').width();
  var heightDifference = $('.main-image img').height() - $('#page-wrapper').height();
  if(widthDifference <= heightDifference){
    $('.main-image img').height($('#page-wrapper').height());
    $('.main-image img').width($('#page-wrapper').height()*$('.main-image').data('data-ratio'))
  };
  if(widthDifference > heightDifference){
    $('.main-image img').width($('#page-wrapper').width());
    $('.main-image img').height($('#page-wrapper').width()/$('.main-image').data('data-ratio'))
  };
}
function centerImage(){
  $('.main-image').width($('.main-image img').width());
  $('.main-image').height($('.main-image img').height())
}
function hackFakeTextShadow(){
  $('.text .fake-text-shadow, .text .more-fake-shadow, .text .some-more, .text .even-more-shadows-LOL').remove();
  $('.no-textshadow .cast-text-shadow').clone().toggleClass('cast-text-shadow fake-text-shadow').prependTo('.overview');
  $('.no-textshadow .cast-text-shadow').clone().toggleClass('cast-text-shadow more-fake-shadow').prependTo('.overview');
  $('.no-textshadow .cast-text-shadow').clone().toggleClass('cast-text-shadow some-more').prependTo('.overview');
  $('.no-textshadow .cast-text-shadow').clone().addClass('even-more-shadows-LOL').prependTo('.overview');
  $('.fake-text-shadow img, .more-fake-shadow img, .even-more-shadows-LOL img').attr('z-index','-7')
}
function attachSwapImageFunction(){
  $('.post.with-swappable-image a img[src*="wp-content"]').each(function(){
    $(this).bind('click',function(){
      var previousImage = {
        src: $('.main-image img').attr('src'),
        title: $('.main-image img').attr('title'),
        alt: $('.main-image img').attr('alt')
      };
      $('.main-image').empty();
      $('<img>',{
        src: $(this).attr('src'),
        title: $(this).attr('title'),
        alt: $(this).attr('alt')
      }).appendTo($('.main-image'));
      $('.main-image-size').empty();
      $('<img>',{
        src: $(this).attr('src'),
      }).appendTo($('.main-image-size'));
      $(this).removeAttr('width').attr({
        src: previousImage.src,
        title: previousImage.title,
        alt: previousImage.alt
      });
      setWidthHeightRatio();
      fitImage();
      centerImage();
      hackFakeTextShadow()
    })
  })
}
function showLoadingAnimation(){
  $('<div class="loading-animation"></div>').appendTo('#page-wrapper');
}
$(function moreTextShadowHacks(){
  $('.no-textshadow .title').clone().addClass('fake-text-shadow').prependTo('.post');
  $('.no-textshadow .title').clone().addClass('more-fake-shadow').prependTo('.post');
  $('.no-textshadow .title').clone().addClass('some-more').prependTo('.post');
  $('.no-textshadow .title').clone().addClass('even-more-shadows-LOL').prependTo('.post');
});
$(function makeCrossBrowserColumns(){
  $('.no-csscolumns .secondary-navigation').columnize({ columns: 3 });
});
$(function(){
  setWidthHeightRatio();
  fitImage();
  centerImage();
  hackFakeTextShadow();
  $('.main-image img').load(function(){
    $('.main-image').removeClass('loading-animation');
  })
  $('a.main-image, .prev a, .next a, button, .wp-paginate a').bind('click',function(){
    showLoadingAnimation();
  })
});
$(window).resize(function(){
  fitImage();
  centerImage();
  $('#tinyscrollbar').tinyscrollbar()
});
window.onload = function loadLargeImages(){
  $('.post.with-swappable-image a:has(img[src*="wp-content"])').each(function(){
    $(this).find($('img')).attr('src',$(this).attr('href'));
    $(this).removeAttr('href');
  });
  attachSwapImageFunction();
  $('.stories a:has(img[src*="wp-content"])').each(function(){
    $(this).removeAttr('href');
  });
  $('.main-image').removeClass('loading-animation');
  $('#tinyscrollbar').tinyscrollbar()
};
$(function navigate(){
  $(document).bind('keyup',function(e){
    if($('.prev, .next').css('display') == 'block'){ //make sure the footer isn't extended
      if(e.which == 37 && ($('.prev a').attr('href'))){ //make sure there's a 'previous'-button present
        showLoadingAnimation();
        window.location = $('.prev a').attr('href');
      }
      if(e.which == 39 && ($('.next a').attr('href'))){
        showLoadingAnimation();
        window.location = $('.next a').attr('href');
      }
      if(e.which == 37 && ($('a.prev').attr('href'))){ //make sure wp-paginate created a 'previous'-button
        showLoadingAnimation();
        window.location = $('a.prev').attr('href');
      }
      if(e.which == 39 && ($('a.next').attr('href'))){
        showLoadingAnimation();
        window.location = $('a.next').attr('href');
      }
    };
  });
  $('.next, .prev').bind('click',function(e){
    if($(this).find($('a')).length){
      showLoadingAnimation();
      window.location = $(this).find($('a')).attr('href');
    };
  });
  $('.category-story .next:not(:has("a"))').hide();
  $('.category-story .prev:not(:has("a"))').hide();
});
$(function expandFooter(){
  $('#footer a[title="Contact"]').one('click',function(e){
    //Craft a link plus div that contains the contact form to load.
    var contactLink = $(this).attr('href');
    var insertThisDiv = 'div.overview';
    e.preventDefault();
    $('#footer').addClass('loading-animation');
    $('<div class="contact-form"></div>').prependTo('#footer');
    $('.contact-form').load( contactLink+' '+insertThisDiv,function(){
      $(this).trigger('complete');
    }).bind('complete',function(){$('#footer').removeClass('loading-animation')})
  });
  $('.main-image-wrapper').bind('click',function(){
    if($('#footer').hasClass('expanded')){
      $('#footer').toggleClass('expanded contracted');
      $('.prev, .next').toggle();
    };
  });
  $('a[title="Contact"]').bind('click',function(e){
    e.preventDefault();
    $('#footer').toggleClass('expanded contracted');
    $('.prev, .next').toggle();
  });
});
$('.contact-form').submit(function(){
  $(":submit",this).prop('disabled', true);
});
