(function ($) {
    "use strict";
    $(document).ready(function ($) {
        $(".fancybox").on("click", function () {
            $.fancybox({
                href: this.href,
                type: $(this).data("type")
            }); // fancybox
            return false;
        }); // on
    });
    //blog
    var blog_paged = $('#blog-loadmore').data('paged');
    var blog_page = blog_paged ? blog_paged + 1 : 2;
    var Blog = {
        _initialized: false,
        init: function () {
            if (this._initialized)
                return false;
            this._initialized = true;
            this.blogMasonry();
            this.blogLoadmore();
        },
        blogMasonry: function () {
            $(window).load(function() {
                $('.blog-entries-wrap').isotope({
                    itemSelector: '.grid-item',
                    percentPosition: true,
                });
            });    
        },
        blogLoadmore: function () {
            $('#blog-loadmore').click(function (event) {
                event.preventDefault();
                var el = $(this);
                var blog_wrap = $('.blog-entries-wrap.has-loadmore');
                var url = $(this).attr('href');
                $('.blog-loadmore').append('<i class="fa fa-spinner fa-spin"></i>');
                el.addClass('hide-loading');
                $.ajax({
                    type: 'GET',
                    url: url,
                    data: {paged: blog_page},
                    success: function (response) {
                        $('.blog-loadmore').find('i').remove();
                        el.removeClass('hide-loading');
                        var result = $(response).find('.blog-entries-wrap.has-loadmore').html();
                        if ($().isotope) {
                            $(result).imagesLoaded(function () {
                                if (blog_wrap.data('isotope')) {
                                    blog_wrap.isotope('insert', $(result));
                                }
                            });
                        }
                        blog_page++;
                        if (blog_page > parseInt(el.data('totalpage'))) {
                            el.parent().remove();
                        }
                    }
                });
            });
        }
    };
    $(document).ready(function () {
        Blog.init();
    });
    //view post
    $('body').on('click', '.akira-post-like', function (event) {
        event.preventDefault();
        var heart = $(this);
        var post_id = heart.data("post_id");
        heart.html("<i class='fa fa-spinner fa-spin'></i>");
        $.ajax({
            type: "post",
            url: ajax_var.url,
            data: "action=akira-post-like&nonce=" + ajax_var.nonce + "&akira_post_like=&post_id=" + post_id,
            success: function (count) {
                if (count.indexOf("already") !== -1)
                {
                    var lecount = count.replace("already", "");
                    heart.prop('title', 'Like');
                    heart.removeClass("liked");
                    heart.html("<i id='icon-unlike' class='fa fa-heart'></i>");
                }
                else
                {
                    heart.prop('title', 'Unlike');
                    heart.addClass("liked");
                    heart.html("<i id='icon-like' class='fa fa-heart'></i>");
                }
            }
        });
     });
    //Scroll to top
    $(window).load(function () {
        var wd = $(window).width();
        if ($('.scroll-to-top').length) {
            $(window).scroll(function () {
                if ($(this).scrollTop() > 1) {
                    $('.scroll-to-top').css({bottom: "30px"});
                        if(akira_params.header_sticky == 1) {
                            $('.site-header').addClass("is-sticky");
                        }
                } else {
                    $('.scroll-to-top').css({bottom: "-100px"});
                    $('.site-header').removeClass("is-sticky");
                }
            });
            $('.scroll-to-top').click(function () {
                $('html, body').animate({scrollTop: '0px'}, 800);
                return false;
            });
        }
    });
    //Scroll bar
    $(document).ready(function() {
        var wd = $(window).width();
            $('.scrollbar-inner').scrollbar();   
    });    
    $(document).ready(function(){
     var heightScroll = $('.header-v1').height();
     $('.scroll-wrapper').css('max-height', heightScroll - 66 + 'px'); 
    });
    $(window).resize(function () {
     var heightScroll = $('.header-v1').height();
     $('.scroll-wrapper').css('max-height', heightScroll - 66 + 'px'); 
    });
    //hamburger toogle icon
    $(document).ready(function () {
        $(".menu-toggle").click(function () {
            $(".mega-menu").slideToggle("slow");
        });
        $(".wppb-user-forms input#username").attr("placeholder", "Username");
        $(".wppb-user-forms input#email").attr("placeholder", "Email");
        $(".wppb-user-forms input#passw1").attr("placeholder", "Password");
        $(".wppb-user-forms input#passw2").attr("placeholder", "Comfirm Password");
        var text = $(".wppb-user-forms label[for='user_login']").text();
        if(text =='Email'){
            $(".wppb-user-forms label[for='user_login'] + input#user_login").attr("placeholder", "Email");
        }

        $(".main-navigation a[href*='#']").click(function(e) {
            var val = $(this).attr('href');
            var id = val.substring(val.indexOf("#"));
            var top = $(id).offset().top;
            var scroll = top -78;
            $('html, body').animate({
                scrollTop: scroll
            }, 700);
        });
    });
    $('.button-footer').click(function(){

        var WH = $(window).height();  
        var SH = $('body')[0].scrollHeight;
        var wd = $(window).width();
        if(wd > 767){
            $('html, body').stop().animate({scrollTop: SH-WH}, 1000);
        console.log( SH+' '+WH );
        }
        else{
            $('html, body').stop().animate({scrollTop: SH-WH-1150}, 1000);
        console.log( SH+' '+WH );
        }
    }); 
    //footer toogle icon
    $('.button-footer').click(function (e) {
        var wd = $(window).width();
        e.preventDefault();
        $(".footer").toggleClass('footer-open');
        $(".footer-office").toggleClass('open');
    });
    if (screen.width >= 992) {
        $(".header-v1 ul.mega-menu .menu-item-has-children > a").on("click", function (e) {
            e.preventDefault();
            $(this).parent().toggleClass('active');
        });
    }
    $('.about-faqs').click(function (e) {
        var wd = $(window).width();
        e.preventDefault();
        $(".about-faqs").addClass('active');
        $(".about-ourteam").removeClass('active');
        $(".about-contact").removeClass('active');
    });
    $('.about-ourteam').click(function (e) {
        var wd = $(window).width();
        e.preventDefault();
        $(".about-ourteam").addClass('active');
        $(".about-faqs").removeClass('active');
        $(".about-contact").removeClass('active');
    });
    $('.about-contact').click(function (e) {
        var wd = $(window).width();
        e.preventDefault();
        $(".about-contact").addClass('active');
        $(".about-ourteam").removeClass('active');
        $(".about-faqs").removeClass('active');
    });
    
    $(document).ready(function ($) {
        $(".fancybox").on("click", function () {
            $.fancybox({
                href: this.href,
                type: $(this).data("type")
            }); // fancybox
            return false;
        }); // on
    });
    //tooltip
    $(document).ready(function(){
        $('[data-toggle="tooltip"]').tooltip();
    });
    //Search toogle
    $('.btn-search').click(function (e) {
        e.preventDefault();
        $(".header-form-search").addClass('open');
    });
    $('.header-form-search .btn-close').click(function (e) {
        e.preventDefault();
        $(".header-form-search").removeClass('open');
    });
    $('#user_login').attr( 'placeholder', 'Username' );
    $('#user_pass').attr( 'placeholder', 'Password' );
    var w = $(window).width(); 
    $('.header-form-search').css('width', w + 'px');
    // fix placeholder IE 9
    $('[placeholder]').focus(function () {
        var input = $(this);
        if (input.val() === input.attr('placeholder')) {
            input.val('');
            input.removeClass('placeholder');
        }
    }).blur(function () {
        var input = $(this);
        if (input.val() === '' || input.val() === input.attr('placeholder')) {
            input.addClass('placeholder');
            input.val(input.attr('placeholder'));
        }
    }).blur().parents('form').submit(function () {
        $(this).find('[placeholder]').each(function () {
            var input = $(this);
            if (input.val() === input.attr('placeholder')) {
                input.val('');
            }
        });
    });

})(jQuery);
