function displayNotification() {
  var top = 0;
  var heightNotification = '50px'

  $('.ftr-notification').each(function(index) {
    if (index == $('.ftr-notification').length - 1) {
      heightNotification = $(this).find('span').height() + 16 + 'px'
    }
    $(this).animate({
      top: top,
      height: heightNotification
    }, 300);
    top += 8;
  });
}

function notificationMsg(msg, msgType) {

  if (msgType === 'error' && $('.ftr-notification--error').length > 0) {
    $('.ftr-notification--error span').html(msg);
    return;
  }

  var $div = $('<div>');
  var $span = $('<span>');
  var $i = $('<i>');

  var id = Date.now();
  $div.attr('id', id);
  $div.addClass('ftr-grid ftr-grid--nowrap ftr-grid--middle ftr-notification--' + msgType + ' ftr-notification ftr-width--full ftr-in-space--m');
  $i.addClass('material-icons ftr-cell ftr-notification__close ftr-in-space--l-m').text('close');
  $span.addClass('ftr-cell--width-remaining').html(msg);

  $div.append($span, $i);

  switch (msgType) {
    case 'error':
      $div.addClass('svn-app-teste-error');
      $('#ftr-notification').append($div);
    break;
    case 'warning':
      $div.addClass('svn-app-teste-error');
      if ($('.ftr-notification--error').length > 0) {
        $('.ftr-notification--error').eq(0).before($div);
      } else if ($('.ftr-notification--info').length > 0) {
        $('.ftr-notification--info').eq($('.ftr-notification--info').length - 1).after($div);
      } else if ($('.ftr-notification--success').length > 0) {
        $('.ftr-notification--success').eq($('.ftr-notification--success').length - 1).after($div);
      } else {
        $('#ftr-notification').append($div);
      }
    break;
    case 'info':
      if ($('.ftr-notification--success').length > 0) {
        $('.ftr-notification--success').eq($('.ftr-notification--success').length - 1).after($div);
      } else if ($('.ftr-notification--warning').length > 0) {
        $('.ftr-notification--warning').eq(0).before($div);
      } else if ($('.ftr-notification--error').length > 0) {
        $('.ftr-notification--error').eq(0).before($div);
      } else {
        $('#ftr-notification').prepend($div);
      }
    break;
    case 'success':
      $('#ftr-notification').prepend($div);
    break;
    default:
      $('#ftr-notification').prepend($div);
  }

  var $notification = $('#' + id);
  $notification.on('click', function(){
    clearNotification($(this).closest('.ftr-notification'));
  });

  $notification.css({top: '-' + $notification.height() + 'px'});

  displayNotification();
}

function clearNotification($notification) {
  $notification = ($notification != undefined) ? $notification : $('.ftr-notification');
  $notification.animate({top: '-' + $notification.height() + 'px'}, 300, function() {
    $notification.remove();
    $('.ftr-notification:last-child').animate({height: $('.ftr-notification:last-child').find('span').height() + 16 + 'px'}, 300);
  });
}

$('.ftr-btn').on('click', function() {
  $('.ftr-notification').remove();
});
