/**
* @package Helix3 Framework
* @author JoomShaper http://www.joomshaper.com
* @copyright Copyright (c) 2010 - 2016 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(){

$("#pesquisa").keyup(function(){
var pesquisa = $(this).val();
  
	if(pesquisa != ''){
      var dados = {
        palavra : pesquisa
      }
      
      $.post('/busca.php',dados,function(retorna){
        $("#resultado").html(retorna);
      });
    }
});
     


        $("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.next('.sppb-ajax-contact-status').html($.parseJSON(response).data).fadeIn().delay(5000).fadeOut(500);
                }
            });

            return false;
        });
    });

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

    $(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');
            }
        });
    });

});
