

$(document).ready(function () {

    /**sticky menu**/
    var header = $('#header-id');
    var headerSticky = false;
    $(window).scroll(function () {
	if (this.scrollY > 0) {
	    if (!headerSticky) {
		header.addClass('sticky-menu');
		headerSticky = true;
	    }
	} else {
	    if (headerSticky) {
		header.removeClass('sticky-menu');
		headerSticky = false;
	    }
	}
    });



    //preparation steps


//    var stepLength = $('.preparation-steps .step').length;

    var steps = document.querySelectorAll('.preparation-steps .step');
    var seeMoreSteps = document.querySelector('.see-more-steps');

    var stepsArray = Array.from(steps);


    if (steps.length > 5)
    {
	seeMoreSteps.style.display = 'block';

	stepsArray.slice(5).forEach(function (item) {
	    item.style.display = 'none';
	});
    }

    if (seeMoreSteps)
    {
	seeMoreSteps.addEventListener('click', function () {
	    stepsArray.slice().forEach(function (item) {
		item.style.display = 'block';
	    });

	    seeMoreSteps.style.display = 'none';
	});
    }



    var postGallery = new Swiper(".gallery__swiper", {
	spaceBetween: 20,
	slidesPerView: 1,
	loop: true,

	navigation: {
	    nextEl: ".swiper-button-next",
	    prevEl: ".swiper-button-prev",
	},

	breakpoints: {
	    0: {
		slidesPerView: 1,
	    },
	    768: {
		slidesPerView: 3,
	    },
	    1024: {
		slidesPerView: 4,
	    },

	    1200: {
		slidesPerView: 4,
	    },
	},

    });


    const lightbox = GLightbox({
	selector: '.glightbox',
	touchNavigation: true,
    });


    /** close when click outside div **/

    $(document).mouseup(function (e)
    {

	var navbar = $('.navbar-collapse');
	var headerRecipes = $('.recipes-list-wrapper');
	var navTogler = $('.navbar-toggler')

	// if the target of the click isn't the container nor a descendant of the container


	if (!navbar.is(e.target) && navbar.has(e.target).length === 0 && !headerRecipes.is(e.target) && headerRecipes.has(e.target).length === 0 && !navTogler.is(e.target) && navTogler.has(e.target).length === 0)
	{
	    if ($(window).width() < 1200)
	    {
		navbar.removeClass('navbar-visible');
	    }
	    navTogler.removeClass('active');
	    $('body').removeClass('body-overlay');
	    $('body').removeClass('overlay');
	}

    });


    $(".cover-btn").click(function () {
	$('html, body').scrollTop($("#home-description").offset().top - 83);
    });


    var prevScrollpos = window.pageYOffset;

    window.onscroll = function () {
	var currentScrollPos = window.pageYOffset;
	if (prevScrollpos > currentScrollPos) {
	    document.getElementById("mobile-nav-id").style.bottom = "0";
	} else {
	    document.getElementById("mobile-nav-id").style.bottom = "-100px";
	}
	prevScrollpos = currentScrollPos;
    }


    $('.password-field').each(function () {

	var passField = $(this).find('input[type=password]');

	$(this).find($('.toggle-visibility')).click(function () {

	    (passField.attr('type') === 'password') ? passField.attr('type', 'text') : passField.attr('type', 'password');

	    $(this).find($('.see-password')).toggleClass('visible');

	}.bind(this));
    });



    //check if uploaded photo - comment

    $(".leave-comment-field").each(function () {
	$(this).find('.attach-photo .form-control').change(function () {
	    $(this).parent().addClass('photo-added');
	    console.log(this);
	})
    })


//    $(".leave-comment-field .attach-photo .form-control").change(function () {
//	$(this).parent().addClass('photo-added');
//    });


    $('.text-area .form-control').on('input', function () {
	this.style.height = 'auto';

	this.style.height =
		(this.scrollHeight) + 'px';
    });




    //timer - challenge recipe

    $.syotimerLang.ro = {
	second: ['secunda', 'secunde'],
	minute: ['minut', 'minute'],
	hour: ['ora', 'ore'],
	day: ['zi', 'zile']
    };


    $('.timer').syotimer({
	year: 2023,
	month: 12,
	day: 6,
	hour: 10,
	lang: 'ro',
	layout: "dhm",

	afterDeadline: function () {
	    // ...
	}


    });

    //upload challenge recipe

    $('.upload-photo .input-file input[type="file"]').change(function (e) {
	var fileName = e.target.files[0].name;
	$(this).parent().find('.file-name').html(fileName);
    });


    $('.recipe-menu .btn').each(function () {

	$(this).click(function () {

	    var hrefValue = $(this).attr('href');

	    $([document.documentElement, document.body]).animate({
		scrollTop: $(hrefValue).offset().top - 100
	    }, 500);
	});
    });


    $('#searchModal').on('shown.bs.modal', function () {
	$('.search-modal .search input').focus();
    });


    var categoriesHolder = $('.categories-holder').outerHeight();
    var categoriesHolderTrigger = $('.categories-holder-trigger');


    if ($(window).width() > 576)
    {
	categoriesHolder > 109 ? categoriesHolderTrigger.show() : categoriesHolderTrigger.hide();
    }

    if ($(window).width() > 767)
    {
	categoriesHolder > 112 ? categoriesHolderTrigger.show() : categoriesHolderTrigger.hide();
    }

    categoriesHolderTrigger.click(function () {
	$('.categories-holder').toggleClass('show-all');

	$(this).find('.icon').toggleClass('rotate-180');

	var btnText = $(this).find('.btn-text');

	btnText.text(function (i, text) {
	    return text === 'Vezi mai mult' ? 'Vezi mai putin' : 'Vezi mai mult';
	});

    });


});


function fpro() {
    $('.form-newsletter, .form-review').submit(function (e) {
	if (e.originalEvent) {
	    e.preventDefault();
	    $(this).append('<input type="hidden" name="aprot" value="1" />');
	    this.submit();
	}
    });
}
fpro();