/* *****************************************************

Project: WHMCS + HostLab - Responsive Hosting Service With WHMCS Template
Description: Hostlab Responsive Premium Template Designed for all web hosting providers
Author: inebur (Rúben Rodrigues)
Author URI: http://inebur.com/
Author Envato: https://themeforest.net/user/inebur
Copyright: 2019 inebur
Version: 2.8

[Main Javascript]
***************************************************** */
document.addEventListener('DOMContentLoaded', function() {
    "use strict";
    loadWindowSettings();
    loadWindowEvents();
    loadMenu();
    loadTabs();
    accordion();
    loadTooltips();
    initSliderUI();
    nav();
    loader();
});
/*----------------------*/
/*       Loader         */
/*----------------------*/
function loader() {
    $(window).on('load', function() {
        $("#spinner-area").fadeOut("slow");
    })
}
/*----------------------*/
/*       NAVBAR         */
/*----------------------*/
function nav(){
    document.querySelector( "#nav-toggle" )
      .addEventListener( "click", function() {
        this.classList.toggle( "active" );
    });
} 
/*-------------------------*/
/*        TOOLTIPS         */
/*-------------------------*/
function loadTooltips() {
    $('#element').tooltip('show')
    $(function() {
        $('[data-toggle="tooltip"]').tooltip()
    })
}
function initSliderUI() {
    var initIterator = 0;
    if ($(".slider-ui").length) {
        $(".slider-ui").each(function() {
            var self = $(this),
                sliderUI = self.find('.slider-line'),
                sliderInp = self.find('.slider-inp'),
                sliderUniqueId = 'sliderUI' + initIterator,
                inputUniqueId = 'inputUI' + initIterator,
                start = parseInt(sliderInp.attr('data-start'), 10),
                count_step = parseInt(sliderInp.attr('data-count-step'), 10);
            // console.log(count_step);
            sliderUI.attr('id', sliderUniqueId);
            sliderInp.attr('id', inputUniqueId);
            initIterator++;
            count_step = count_step ? count_step : 300;
            var keypressSlider = document.getElementById(sliderUniqueId),
                input = document.getElementById(inputUniqueId);

            noUiSlider.create(keypressSlider, {
                start: start ? start : 0,
                step: 1,
                connect: "lower",
                format: {
                    to: function(value) {
                        return value;
                    },
                    from: function(value) {
                        return value;
                    }
                },
                tooltips: true,
                format: {
                    to: function(value) {
                        return parseInt(value, 10);
                    },
                    from: function(value) {
                        return value;
                    }
                },
                range: {
                    'min': 1,
                    'max': count_step
                }
            });
            ////////////////////////////////
            // Calculate Docker Product second and third  diagram
            function getValue2(values, handle, unencoded, tap) {
                // console.log("value of slider2: " + unencoded);

                var circle = $(this.target).closest('.sidebar').find('.circle');
                circle.attr('data-percent', parseInt(unencoded,10) / count_step * 100);

                circle.empty().circliful();

            }
            keypressSlider.noUiSlider.on('change', getValue2);
            ////////////////////////////////////////////////////////////
            keypressSlider.noUiSlider.on('update', function(values, handle) {
                input.value = values[handle];
            });

            input.addEventListener('change', function() {
                keypressSlider.noUiSlider.set([null, this.value]);

            });
            // Listen to keydown events on the input field.
            input.addEventListener('keydown', function(e) {
                // Convert the string to a number.
                var value = Number(keypressSlider.noUiSlider.get()),
                    sliderStep = keypressSlider.noUiSlider.steps()

                // Select the stepping for the first handle.
                sliderStep = sliderStep[0];
                // 13 is enter,
                // 38 is key up,
                // 40 is key down.
                switch (e.which) {
                    case 13:
                        keypressSlider.noUiSlider.set(this.value);
                        break;
                    case 38:
                        keypressSlider.noUiSlider.set(value + sliderStep[1]);
                        break;
                    case 40:
                        keypressSlider.noUiSlider.set(value - sliderStep[0]);
                        break;
                }
            });
        });
    }

}
/*-------------------------*/
/*     MENU                */
/*-------------------------*/
function loadMenu() {
    $(".nav-menu .menu-toggle").on("click", function() {
        $(this).closest(".menu-wrap").toggleClass("active");
    });
    $('.btn-scroll').on("click", function() {
        $('html, body').animate({
            scrollTop: $($.attr(this, 'href')).offset().top - 10
        }, 500);
        return false;
    });
    $(".menu-item").on('click',
        function() {
            if ($(".sub-menu", this).css('display') === 'none') {
                $(".sub-menu", this).css("display", "block");
            } else {
                $(".sub-menu", this).css("display", "none");
            }
        }
    );
}
/*-------------------------*/
/*            TABS         */
/*-------------------------*/
function loadTabs() {
    $('.tabs-header').on('click', 'li:not(.active)', function() {
        var index_el = $(this).index();
        $(this).addClass('active').siblings().removeClass('active');
        $(this).closest('.tabs').find('.tabs-item').removeClass('active').eq(index_el).addClass('active');
    });
}
/*----------------------*/
/*       ACCORDION      */
/*----------------------*/
function accordion() {
    $('.accordion').on('click', '.panel-title', function() {
        var self = $(this);
        var panelWrap = self.parent();
        panelWrap.find('.panel-collapse').slideToggle('200');
        self.toggleClass('active');
        panelWrap.siblings().find('.panel-collapse').slideUp('200');
        panelWrap.siblings().find('.panel-title').removeClass('active');
    });
    accordHeight();
}
function accordHeight() {
    $(".accordion.faq .square").each(function() {
        $(this).css({
            "height": $(this).parent().outerHeight(true),
            "padding-top": $(this).parent().outerHeight(true) / 2 - 20
        });
    });
}
/*-------------------------*/
/*          SKILLS         */
/*-------------------------*/
function loadSkills() {
    //Circle skills
    $('.circle').not('.animated').each(function() {
        if ($(window).scrollTop() >= $(this).offset().top - $(window).height() * 0.7) {
            $(this).addClass('animated').circliful();
        }
    });
    //Progress bar skills
    if ($('.skill-progress-box').length) {
        $('.skill-progress-box').not('.animated').each(function() {
            if ($(window).scrollTop() >= $(this).offset().top - $(window).height() * 0.7) {
                var $skill_number = $(this).addClass('animated').find('.block-title span[data-skill_percent]');
                var skill_percent = $skill_number.data('skill_percent');
                $skill_number.animateNumber({
                    number: skill_percent
                }, 2000);
                $(this).find('.progress-bar').each(function() {
                    $(this).css({
                        'width': skill_percent + '%'
                    });
                });
            }
        });
    }
}
/*-------------------------*/
/*           SELECT        */
/*-------------------------*/
function selectInit() {
    $('.selectpicker').each(function() {
        var self = $(this);
        var selectStyle = self.attr('data-class'); //additional style attribute, not required
        self.selectpicker({
            style: 'cst-select ' + selectStyle //add classes to customize select field
        });
    });
}
function loadWindowEvents() {
    /*-------------------------*/
    /*  RUN RESIZE FUNCTIONS   */
    /*-------------------------*/
    $(window).on("resize", function() {
        offheight();
        accordHeight();
    });
    /*-------------------------*/
    /*  RUN SCROLL FUNCTIONS   */
    /*-------------------------*/
    $(window).on("scroll", function() {
        loadSkills();
    });


    $(window).on('scroll', function(){
      if ($(window).scrollTop() >= 150) {
          $('.menu-wrap').addClass('fixed');
      } else {
          $('.menu-wrap').removeClass('fixed');
      }
    });
}
function offheight() {
    if ($(window).width() > 750) {
        var offerHeight = $(".offers").outerHeight(true);
        $(".offers.light").css("height", offerHeight + 1);
    }
}
function loadWindowSettings() {
    offheight();
    /*-------------------------*/
    /*       FIXED MENU        */
    /*-------------------------*/
    if ($(window).width() < 750) {
        $(".nav-menu .main-menu > .menu-item-has-children > a").on("click", function() {
            if ($(this).attr('href') !== '#') {
                $(this).next().slideToggle(0);
                return false;
            }
        });
    }
}

