$(function() {
  $('.resizePicture img').waitForImages({
    each: function() {
      resizePicture($(this).parents('.resizePicture'));
    },
    waitForAll: true
  });

  $('.resizePicture video').each(function() {
    resizePicture($(this).parents('.resizePicture'));
  });
  
  $('header ul.currencies > li > a').on('click', function(event) {
    event.preventDefault();
    event.stopPropagation();
    var $this = $(this);
    $.ajax({
      method: 'POST',
      url: $this.attr('href'),
      dataType: 'json',
      success: function(data) {
        if (data.status == 'success')
          document.location.reload();
      }
    });
    
  });
  
});

$(window).resize(function() {
  resizePictures();
});

/** Scroll to an anchor in page */
function scrollToAnchor(to, speed)
{
  var speed = speed ? speed : 750;
  var offset = 0;

  if(!$(to).is(':visible'))
  {
    $(to).show(0, function() {
      offset = $(to).offset();
      $(to).hide();
    });
  }
  else
  {
    offset = $(to).offset();
  }

  $('html, body').animate({ scrollTop: (offset.top - 20) }, speed);
  return false;
}

/** Launch pictures resizing on page. */
function resizePictures()
{
  // pictures resizing
  $('.resizePicture').each(function() {
    resizePicture($(this));
  });
}

/** Resize picture to fit into a container */
function resizePicture(container)
{
  // make image not overflow container
  var overflow = typeof container.data('overflow') !== 'undefined' ? container.data('overflow') : true;

  // no overflow for portrait pictures
  var preservePortrait = typeof container.data('preserveportrait') !== 'undefined' ? container.data('preserveportrait') : true;

  // if true not enlarge images smaller than container
  var enlarge = typeof container.data('enlarge') !== 'undefined' ? container.data('enlarge') : true;

  // if true, make image visible after resizing
  var opacity = typeof container.data('opacity') !== 'undefined' ? container.data('opacity') : true;

  var elemWidth = container.innerWidth();
  var elemHeight = container.innerHeight();
  var elemRatio = elemWidth / elemHeight;

  container.find('img, video').each(function() {
    // get original picture size.
    $(this).css({ width:'auto', height: 'auto' });

    var pictureWidth = $(this).innerWidth();
    var pictureHeight = $(this).innerHeight();
    var pictureRatio = pictureWidth / pictureHeight;

    if (isNaN(pictureRatio)) // fix when item is in display none
      $(this).css({ width:'100%', 'min-height': '100%' });
    else if (enlarge == false && pictureWidth < elemWidth && pictureHeight < elemHeight) // picture smaller than container
      $(this).css({ width:'auto', height: 'auto' });
    else if (overflow == false)
    {
      // No overflow
      if (pictureRatio < elemRatio)
        $(this).css({ width: 'auto', height:'100%' });
      else
        $(this).css({ width:'100%', height: 'auto' });
    }
    else if (pictureRatio < 1 && preservePortrait) // Portrait picture with no overflow
      $(this).css({ width: 'auto', height:'100%' });
    else if (pictureRatio < elemRatio) // Landscape
      $(this).css({ width:'100%', height: 'auto' });
    else // Portrait
      $(this).css({ width: 'auto', height:'100%' });

    if (opacity) $(this).animate({'opacity': 1}, 800);
  });
}

/** Update pictures size.
 * @param (object) container : pictures container
 * @param (bool) animate : if true, resizing will be animated
 * @param (bool) preservePortrait : if true, portrait picture will be set as 100% height. */
function updatePicturesSize(container, animate, preservePortrait)
{
  var animate = typeof animate !== 'undefined' ? animate : false;
  var preservePortrait = typeof preservePortrait !== 'undefined' ? preservePortrait : false;
  var elemRatio = container.width() / container.height();

  container.find('img').each(function() {
    $(this).css({ width:'auto', height: 'auto' }); // get original picture size.
    var pictureRatio = $(this).width() / $(this).height();

    if (pictureRatio < elemRatio && (preservePortrait == false || pictureRatio > 1))
    {
      if (animate)
        $(this).animate({ width:'100%', height: 'auto' });
      else
        $(this).css({ width:'100%', height: 'auto' });
    }
    else
    {
      if (animate)
        $(this).animate({ width: 'auto', height:'100%' });
      else
        $(this).css({ width: 'auto', height:'100%' });
    }
  });
}

function hasClass(element, cls) {
  return (' ' + element.getAttribute('class') + ' ').indexOf(' ' + cls + ' ') > -1;
}