/**
* @package Helix3 Framework
* @author JoomShaper https://www.joomshaper.com
* @copyright Copyright (c) 2010 - 2021 JoomShaper
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
*/
jQuery(function($) {

    $('#offcanvas-toggler').on('click', function(event){
        event.preventDefault();
        $('body').addClass('offcanvas');
    });

    $( '<div class="offcanvas-overlay"></div>' ).insertBefore( '.body-innerwrapper > .offcanvas-menu' );

    $('.close-offcanvas, .offcanvas-overlay').on('click', function(event){
        event.preventDefault();
        $('body').removeClass('offcanvas');
    });

    //Mega Menu
    $('.sp-megamenu-wrapper').parent().parent().css('position','static').parent().css('position', 'relative');
    $('.sp-menu-full').each(function(){
        $(this).parent().addClass('menu-justify');
    });

    //Sticky Menu
    $(document).ready(function(){
        $("body.sticky-header").find('#sp-header').sticky({topSpacing:0})
    });

    // Select
    if($(".sp-select").length > 0){
        $(document).on('click', function(e) {
            var selector = $('.sp-select');
            if (!selector.is(e.target) && selector.has(e.target).length === 0) {
                selector.find('ul').slideUp();
            }
        });

        $('select').each(function(event) {
            $(this).hide();
            var $self = $(this);
            var spselect  = '<div class="sp-select">';
            spselect += '<div class="sp-select-result">';
            spselect += '<span class="sp-select-text">' + $self.find('option:selected').text() + '</span>';
            spselect += ' <i class="fa fa-sort-down"></i>';
            spselect += '</div>';
            spselect += '<ul class="sp-select-dropdown">';

            $self.children().each(function(event) {
                if($self.val() == $(this).val()) {
                    spselect += '<li class="active" data-val="'+ $(this).val() +'">' + $(this).text() + '</li>';
                } else {
                    spselect += '<li data-val="'+ $(this).val() +'">' + $(this).text() + '</li>';
                }
            });

            spselect += '</ul>';
            spselect += '</div>';
            $(this).after($(spselect));
        });

        $(document).on('click', '.sp-select', function(event) {
            $('.sp-select').not(this).find('ul').slideUp();
            $(this).find('ul').slideToggle();
        });

        $(document).on('click', '.sp-select ul li', function(event) {
            var $select = $(this).closest('.sp-select').prev('select');
            $(this).parent().prev('.sp-select-result').find('span').html($(this).text());
            $(this).parent().find('.active').removeClass('active');
            $(this).addClass('active');
            $select.val($(this).data('val'));
            $select.change();
        });
    }
    // End Select


    jQuery(function($){
      var date_input=$('input[name="date"]'); //our date input has the name "date"
      var options={
        format: 'mm/dd/yyyy',
        todayHighlight: true,
        autoclose: true,
      };
        date_input.datepicker(options);
    });

    //Ajax Appointment Form
    jQuery(function($) {
        $('.sppb-appointment-form').on('submit', function(event) {

            var $this = $(this);
            event.preventDefault();

            var $self   = $(this);
            var value   = $(this).serializeArray();
            var request = {
                'option' : 'com_sppagebuilder',
                'task'   : 'ajax',
                'addon'  : 'appointment_form',
                'data'   : value
            };

            $.ajax({
                type   : 'POST',
                data   : request,
                beforeSend: function(){
                    $self.find('.fa').addClass('fa-spinner fa-spin');
                },
                success: function (response) {
                    $self.find('.fa-spin').removeClass('fa-spinner fa-spin');
                    $this.find('input[type=text], input[type=email], input[type=number], textarea, input[type=date]').val("");
                    $self.find('.sppb-ajax-contact-status').html($.parseJSON(response).data).fadeIn().delay(5000).fadeOut(500);
                }
            });

            return false;
        });
    });

    //Tooltip
	var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-toggle="tooltip"]'));
	var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
		return new bootstrap.Tooltip(tooltipTriggerEl);
	});

    // rating
    $(document).on('click', '.sp-rating .star', function(event) {
        event.preventDefault();

        var data = {
            'action':'voting',
            'user_rating' : $(this).data('number'),
            'id' : $(this).closest('.post_rating').attr('id')
        };

        var request = {
                'option' : 'com_ajax',
                'plugin' : 'helix3',
                'data'   : data,
                'format' : 'json'
            };

        $.ajax({
            type   : 'POST',
            data   : request,
            beforeSend: function(){
                $('.post_rating .ajax-loader').show();
            },
            success: function (response) {
                var data = $.parseJSON(response.data);

                $('.post_rating .ajax-loader').hide();

                if (data.status == 'invalid') {
                    $('.post_rating .voting-result').text('You have already rated this entry!').fadeIn('fast');
                }else if(data.status == 'false'){
                    $('.post_rating .voting-result').text('Somethings wrong here, try again!').fadeIn('fast');
                }else if(data.status == 'true'){
                    var rate = data.action;
                    $('.voting-symbol').find('.star').each(function(i) {
                        if (i < rate) {
                           $( ".star" ).eq( -(i+1) ).addClass('active');
                        }
                    });

                    $('.post_rating .voting-result').text('Thank You!').fadeIn('fast');
                }

            },
            error: function(){
                $('.post_rating .ajax-loader').hide();
                $('.post_rating .voting-result').text('Failed to rate, try again!').fadeIn('fast');
            }
        });
    });


    //For react template
    var observer = new MutationObserver(function (mutations) {
        mutations.forEach(function (mutation) {
            var newNodes = mutation.addedNodes;
            if (newNodes !== null) {
                var $nodes = $(newNodes);

                $nodes.each(function () {
                    var $node = $(this);
                    $node.find('#slide-fullwidth').each(function () {
                        var $slideFullwidth = $("#slide-fullwidth");
                        var $autoplay = $slideFullwidth.attr("data-sppb-slide-ride");
                        if ($autoplay == "true") { var $autoplay = true; } else { var $autoplay = false };

                        var $controllers = $slideFullwidth.attr("data-sppb-slidefull-controllers");
                        if ($controllers == "true") { var $controllers = true; } else { var $controllers = false };

                        $slideFullwidth.owlCarousel({
                            margin: 0,
                            loop: true,
                            video: true,
                            autoplay: $autoplay,
                            animateIn: "fadeIn",
                            animateOut: "fadeOut",
                            autoplayHoverPause: true,
                            autoplaySpeed: 1500,
                            responsive: {
                                0: {
                                    items: 1
                                },
                                600: {
                                    items: 1
                                },
                                1000: {
                                    items: 1
                                }
                            },
                            dots: $controllers,
                        });

                        $(".sppbSlidePrev").click(function () {
                            $slideFullwidth.trigger("prev.owl.carousel", [400]);
                        });

                        $(".sppbSlideNext").click(function () {
                            $slideFullwidth.trigger("next.owl.carousel", [400]);
                        });

                        $("#slide-fullwidth .owl-controls").addClass("container");
                    });
                });

            }
        });
    });

    var config = {
        childList: true,
        subtree: true
    };
    // Pass in the target node, as well as the observer options
    observer.observe(document.body, config);
});
