$('.w3-modal').on('click', '.modal-close', function(e) {
	$('.w3-modal').css("display", "none");
});

$('.modal-opener-container').on('click', '.modal-opener', function(e) {
	e.preventDefault();
	var modal = $('#' + $(this).attr('data-modal'));

	var id = $(this).attr('data-post');
	var content = $('#post-content[data-post="' + id + '"]').html();

	$(modal).find('#post-html').html(content);

	var url = $(this).attr('href');
	var urlform = $(this).attr('url-comment');
	var loading = $(modal).find('#modal-loading:first');
	var content = $(modal).find('#modal-content:first');
	var form = $(modal).find("#modal-form:first");
	$(modal).css("display", "block");
	$(loading).css("display", "block");
	$(content).css("display", "none");
	$(form).css("display", "none");

	var post = $(this).attr('data-post');
	$.get(urlform, function(data) {
		$(form).html(data);
		$(form).css("display", "block");
	});
	$.get(url, function(data) {
		$(content).html(data);
		$(content).css("display", "block");
		$(loading).css("display", "none");
	});
});

$('#replies-modal').on('submit', '#modal-form form', function(e) {
	e.preventDefault();
	var $this = $(this);
	$(this).css("display", "none");

	var form_url = $this.attr('action');
	var modal = $('#replies-modal');

	var loader = $(modal).find('#modal-loading');
	var content = $(modal).find('#modal-content');
	var form = $(modal).find("#modal-form");
	$(loader).css("display", "block");

	$.ajax({
        url: form_url, 
        type: $this.attr('method'), 
        data: $this.serialize(),
        success: function(html) { 
            $(content).prepend(html);
            $(content).find('.no-replies').css("display", "none");
			$(loader).css("display", "none");

			$(form).html("");
			$.get(form_url, function(data) {
				$(form).html(data);
			});
        }
    });
});

$(document).ready(function() {
	if($('#timeline-container').length) {
		(function checkFreshTimelinePost() {
		    $('.timeline-post.fresh').each(function(index) {
		    	var content = $(this).find('.timeline-post-content');

		    	if($(content).height() > 300) {
		    		$(content).css('max-height', 250);
				    $(content).css('overflow', 'hidden');
				    $(content).after('<p class="see-more-button w3-text-theme">Afficher plus...</p>');
		    	}

		    	$(this).removeClass('fresh');
		    });

		    setTimeout(checkFreshTimelinePost, 1000);
		})();

		$('#timeline-container').on('click', '.see-more-button', function(e) {
			$(this).prev().css('max-height', '');
			$(this).remove();
		});
	}
});