var messagesOpen = false;
var premiumOptionsOpen = false;

$(document).ready(function() {

    $(".ads-close").click(function(e) {
        e.preventDefault();

        var $this = $(this);

        $this.parent().fadeOut(200, function() {
            $this.parent().html("");
        });
    });

    $("#posts_container").on('click', '.fav-button', function(e) {
        e.preventDefault();

        var $this = $(this);
        var $bookmarksCount = $(".bookmarks_counter");
        var postID = $this.closest('.new_post_prev').attr('data-post-id');
        var activeClass = 'btn-active';

        $.ajax({
            type: "POST",
            global: false,
            url: "/ajax.php",
            data: ({
                type: 27,
                post: postID
            }),
            dataType: "json",
            success: function(data) {
                if(data.status == "removed") $this.removeClass(activeClass);
                else $this.addClass(activeClass);

                if(data.count == 0) $bookmarksCount.hide();
                else $bookmarksCount.show();
                $bookmarksCount.html(data.count);
            }
        });
    });

    $(".top_menu_dropdown").dropdown();
    $(".top_menu_dropdown").tooltip();

    $(".post-actions").dropdown();
    $(".post-actions").tooltip();

    var tmp1=$(window).scrollTop()-50;
    var tmp2=$(window).scrollTop()+50;
    var $dropMoreBlock = $('#headline').find('.header_menu_right__more');
    var $dropUserBlock = $('#headline').find('.header_menu_right__user');
    //if($(window).height()>=768){
        $(window).scroll(function(){
            if(!messagesOpen && !premiumOptionsOpen && !$('.header_menu_right__notifications').hasClass('active')) {
                if($(window).scrollTop()>=tmp2){
                    tmp1=$(window).scrollTop()-50;
                    tmp2=$(window).scrollTop()+50;
                    $('#headline').addClass('nav-up');
                    if($dropMoreBlock.hasClass('open')) $dropMoreBlock.click();
                    if($dropUserBlock.hasClass('open')) $dropUserBlock.click();
                    if($('#more_link button').hasClass('active')) $('#more_link button').click();
                }else if($(window).scrollTop()<=tmp1){
                    tmp1=$(window).scrollTop()-50;
                    tmp2=$(window).scrollTop()+50;
                    $('#headline').removeClass('nav-up');
                }
            }
        });
    //}

	$('[data-toggle="tooltip"]').tooltip();

	$('.navmenu').on('show.bs.offcanvas', function() {
		$('button[data-toggle="offcanvas"]').html('<i class="fa fa-times" aria-hidden="true"></i>');
	});

	$('.navmenu').on('hide.bs.offcanvas', function() {
		$('button[data-toggle="offcanvas"]').html('<i class="fa fa-bars" aria-hidden="true"></i>');
	});

	$('#search_link button').click(function() {
		$('.search').fadeToggle('fast', function() {
		     var $searchicon = $('#search_link button');
		     $('.search').is(':visible') ? $searchicon.html('<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>') : $searchicon.html('<span class="glyphicon glyphicon-search" aria-hidden="true"></span>');
		});
	});

	$('#more_link button').click(function() {
		$('.header_menu_more').fadeToggle('fast', function() {
		     $('.header_menu_more').is(':visible') ? $('#more_link button').addClass('active') : $('#more_link button').removeClass('active');
		});
	});

	$('#sub-menu3-link').click(function(event){
		event.stopPropagation();
		event.preventDefault();
		$('.header_menu_more').hide();
		$('#more_link button').removeClass('active')
		$('#sub-menu5').fadeToggle('fast');
		$('#sub_menu3_arrow').fadeToggle('fast');

	});

    $(".premium-sub-info-button").click(function(event) {
        event.preventDefault();
        $('.premium-sub-info-block').fadeToggle();
        $('.premium-sub-button').toggleClass('goldpulse');
    });

    $(".premium-sub-info-block .close").click(function(event) {
        $(this).parent().fadeOut();
        $('.premium-sub-button').removeClass('goldpulse');
    });
});




$(document).click(function(e){
    var elem = $("#sub-menu");
    if(e.target!=elem[0]&&!elem.has(e.target).length) elem.hide();
    var elem = $("#sub-menu2");
    if(e.target!=elem[0]&&!elem.has(e.target).length) elem.hide();
	elem = $("#sub-menu5").add($("#sub_menu3_arrow"));
	if(e.target!=elem[1]&&elem[0]&&!elem.has(e.target).length) elem.hide();



})

function getNoun(number, one, several, many) {
    number = Math.abs(number);
    number %= 100;
    if (number >= 5 && number <= 20) {
        return many;
    }
    number %= 10;
    if (number == 1) {
        return one;
    }
    if (number >= 2 && number <= 4) {
        return several;
    }
    return many;
}


$.fn.textWidth = function(text, font) {
    if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>')
        .css({visibility: "hidden", "pointer-events": "none", "position": "absolute", top: 0, left: 0}).appendTo(document.body);

    var elemFontCss = {
        "font-family": $(this).css("font-family"),
        "font-size": $(this).css("font-size"),
        "line-height": $(this).css("line-height"),
        "font-stretch": $(this).css("font-stretch"),
        "font-weight": $(this).css("font-weight"),
        "font-style": $(this).css("font-style"),
        "font-variant": $(this).css("font-variant"),
    }

    $.fn.textWidth.fakeEl.text(text || $(this).val() || $(this).text() || $(this).attr('placeholder')).css(elemFontCss);

    return $.fn.textWidth.fakeEl.width();
};