var styles = [{"featureType":"water","elementType":"geometry","stylers":[{"color":"#e9e9e9"},{"lightness":17}]},{"featureType":"landscape","elementType":"geometry","stylers":[{"color":"#999"},{"lightness":20}]},{"featureType":"road.highway","elementType":"geometry.fill","stylers":[{"color":"#ffffff"},{"lightness":17}]},{"featureType":"road.highway","elementType":"geometry.stroke","stylers":[{"color":"#ffffff"},{"lightness":29},{"weight":0.2}]},{"featureType":"road.arterial","elementType":"geometry","stylers":[{"color":"#ffffff"},{"lightness":18}]},{"featureType":"road.local","elementType":"geometry","stylers":[{"color":"#ffffff"},{"lightness":16}]},{"featureType":"poi","elementType":"geometry","stylers":[{"color":"#f5f5f5"},{"lightness":21}]},{"featureType":"poi.park","elementType":"geometry","stylers":[{"color":"#dedede"},{"lightness":21}]},{"elementType":"labels.text.stroke","stylers":[{"visibility":"on"},{"color":"#ffffff"},{"lightness":16}]},{"elementType":"labels.text.fill","stylers":[{"saturation":36},{"color":"#333333"},{"lightness":40}]},{"elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"transit","elementType":"geometry","stylers":[{"color":"#f2f2f2"},{"lightness":19}]},{"featureType":"administrative","elementType":"geometry.fill","stylers":[{"color":"#fefefe"},{"lightness":20}]},{"featureType":"administrative","elementType":"geometry.stroke","stylers":[{"color":"#fefefe"},{"lightness":17},{"weight":1.2}]}];

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

$.extend(true, $.magnificPopup.defaults, {
    tClose: 'Cerrar (Esc)', // Alt text on close button
    tLoading: 'Cargando...', // Text that is displayed during loading. Can contain %curr% and %total% keys
    gallery: {
        tPrev: 'Anterior (Izquierda)', // Alt text on left arrow
        tNext: 'Siguiente (Derecha)', // Alt text on right arrow
        tCounter: '%curr% de %total%' // Markup for "1 of 7" counter
    },
    image: {
        tError: '<a href="%url%">La imagen</a> no se pudo cargar.' // Error message when image could not be loaded
    },
    ajax: {
        tError: '<a href="%url%">El contenido</a> no se pudo cargar.' // Error message when ajax request failed
    }
});

new WOW().init();

function zeroFill( number, width )
{
    width -= number.toString().length;
    if ( width > 0 )
    {
        return new Array( width + (/\./.test( number ) ? 2 : 1) ).join( '0' ) + number;
    }
    return number + ""; // always return a string
}

$(function() {
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    // $('.nav > ul > li > a').each(function() {
    //     var text = $(this).text().trim().split(" ");
    //     if(text.length > 1 && text[0].length > 5) {
    //         html = text.slice(0, -1).join(" ") + " <br />" + text.pop();
    //         $(this).html(html);
    //     }
    // });

    $('.btn-close').on('click', function() {
        $('body').removeClass('menu-active');
        $('.mobile-menu').removeClass('active');
        return false;
    });

    $('.btn-menu').on('click', function() {
        $('body').addClass('menu-active');
        $('.mobile-menu').addClass('active');
        return false;
    });

    $(window).scroll(function() {
        if($(this).scrollTop() >= $('.header').outerHeight()) {
            $('.header').addClass('fixed');
        } else {
            $('.header').removeClass('fixed');
        }
    });

    $('.scroll-to').on('click', function() {
        var href = $(this).attr('href');
        if($(href).length) {
            $('html, body').animate({
                scrollTop: $(href).offset().top - $('.header').outerHeight() + 1
            }, 500);
            $('.btn-close').trigger('click');
        }
        return false;
    });

    // $('.nav, .mobile-nav').onePageNav({
    //     currentClass: 'active',
    //     changeHash: false,
    //     scrollSpeed: 750,
    //     scrollThreshold: 0.5,
    //     filter: '',
    //     easing: 'swing',
    //     begin: function() {
    //         $('.mobile-menu').removeClass('active');
    //         $('body').removeClass('menu-active');
    //     },
    //     end: function() {
    //     },
    //     scrollChange: function($currentListItem) {
    //     }
    // });

    $('.mobile-nav .dropdown > a').on('click', function(event) {
        event.stopPropagation();
        var menu = $(this).siblings('.dropdown-menu');
        if(!menu.is(':visible')) {
            menu.slideDown();
        } else {
            menu.slideUp();
        }
        return false;
    });

    $('.nav .dropdown > a').on('click', function() {
        var parent = $(this).parents('.dropdown');
        parent.toggleClass('active');
        $('.dropdown').not(parent).removeClass('active');
        $('.nav .search-form input').first().focus();
        return false;
    });

    // $('.nav > ul > li:not(:first-child) > a').each(function() {
    //     var html = $(this).html().split(" ");
    //     if(html.length > 1) {
    //         html = html.slice(0, -1).join(" ") + " <br />" + html.pop();
    //         $(this).html(html);
    //     }
    // });


    $('.ajax-form').on('submit', function() {
        var $this = $(this);
        var button = $this.find('button[type="submit"]').html();
        var form = $($this)[0];
        var formData = new FormData(form);
        var errors = 0;
        $this.find('.alert-container').addClass('d-none');
        $this.find('[data-required]').each(function() {
            if(!$(this).val()) {
                $(this).addClass('has-error');
                errors++;
            } else {
                $(this).removeClass('has-error');
            }
        }).promise().done(function() {
            if(errors == 0) {
                $this.find('button[type="submit"]').text('...').attr('disabled', 'disabled');
                $.ajax({
                    url: $this.attr('action'),
                    type: 'POST',
                    data: formData,
                    contentType: false,
                    processData: false
                }).done(function(response) {
                    if(response) {
                        $this.trigger('reset');
                        $this.find('button').html(button).removeAttr('disabled');
                        $this.find('.alert').html(response).addClass('alert-success').removeClass('alert-danger');
                        $this.find('.alert-container').removeClass('d-none');
                    }
                });
            } else {
                $this.find('.alert').html('Por favor completá todos los campos requeridos').addClass('alert-danger');
                $this.find('.alert-container').removeClass('d-none');
            }
        });
        return false;
    });

    $('.mzoom').magnificPopup({
        type: 'image',
        midClick: true,
        closeBtnInside: true,
        callbacks: {
            open: function() {
                $('.mfp-content').addClass('zoomIn animated');
            }
        },
        gallery: {
            enabled:true
        }
    });

    $(window).on('load', function() {
        $('body').removeClass('preload');
        $(window).scroll();
    });
});
