$(function () {
    Website.init();
});

Website = {
    init: function () {
        Website.banner();
        Website.modal();
        Website.bairros();
        Website.resize();
        Website.overflow();
        Website.hoverHome();
        Website.navegacao();
        Website.recebaNovidades();
        Website.mask();
        Website.compartilhamento();
    },

    mask: function () {
        var SPMaskBehaviorFiltro = function (val) {
            var length = val.replace(/\D/g, '').length;
            if (length < 9)
                return '0000-00009';
            if (length === 9)
                return '00000-00009';
            if (length === 10)
                return '(00)0000-00009';
            return '(00)00000-0000';
        },
            spOptions = {
                onKeyPress: function (val, e, field, options) {
                    field.mask(SPMaskBehaviorFiltro.apply({}, arguments), options);
                },
                clearIfNotMatch: true
            };
        if ($('.mask-fone').length > 0)
        {
            $('.mask-fone').mask(SPMaskBehaviorFiltro, spOptions);
        }
    },

    overflow: function () {
        $(document).ready(function () {
            var windowWidth = $(window).width();
            var width = (windowWidth - 960) / 2;
            $(".header-overflow").css("width", width);

            var absolute = width * (-1)
            $(".overflow-right").css("right", absolute);
            $(".overflow-left").css("left", absolute + 1);
        });
    },

    hoverHome: function () {

    },

    resize: function () {
        $(window).resize(function () {
            Website.banner();
            var windowWidth = $(window).width();
            var width = (windowWidth - 960) / 2;

            $(".header-overflow").css("width", width);

            var absolute = width * (-1)
            $(".overflow-right").css("right", absolute);
            $(".overflow-left").css("left", absolute + 1);
        })
    },

    banner: function () {
        $(".banner-container ul").carouFredSel({
            responsive: true
            , auto: 5000
            , prev: $(".left")
            , next: $(".right")
            , scroll: { items: 1, duration: 1000 }
        });
    },

    bairros: function () {
        $(".bairros").carouFredSel({
            responsive: true
            , auto: false
            , items: 1
            , prev: $(".left-bairros")
            , next: $(".right-bairros")
        });
    },

    modal: function () {
        $(document).ready(function () {
            $('#modal-servicos').on('show.bs.modal', function () {
                var url = $('#id-modal').data("href");
                $.get(url, {}, function (result) {
                    $("#modal-servicos .modal-body").html(result);
                });
            });
            $(document).on("click", "#btn-enviar", function (e) {
                e.preventDefault();
                var url = $('#id-modal').data("href-enviar");
                $.post(url, $("#modal-servicos form").serialize(), function (result) {
                    $("#modal-servicos .modal-body").html(result);
                    $("#modal-servicos #Nome").focus();
                });
            });
        });
        $('#modal-servicos').on('shown.bs.modal', function () {
            $("#modal-servicos #Nome").focus();
        });
    },

    mapa: function () {
        function initialize() {
            var mapCanvas = document.getElementById('map');
            var mapOptions = {
                scrollwheel: false,
                center: latLgn,
                zoom: 17,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                icon: pin,
            }
            var map = new google.maps.Map(mapCanvas, mapOptions);

            var marker = new google.maps.Marker({
                position: latLgn,
                map: map,
                icon: pin,
                title: 'Concisa'
            });
        }
        google.maps.event.addDomListener(window, 'load', initialize);
    },

    whatsapp: function () {
        if ($(this).data("value"))
            $("#id-numero-whatsapp").css("display", "none");
        else
            $("#id-numero-whatsapp").css("display", "");

        $(this).data("value", !$(this).data("value"));
    },

    navegacao: function () {
        $("#cabecalho-click").click(function () {
            var status = $(this).data('status');
            if (status) {
                $(this).data('status', false).find("span").text("Expandir formulario para receber novidades sobre vagas");
                $(".receba-vagas").removeClass("visivel").addClass("oculto");
            } else {
                $(this).data('status', true).find("span").text("Esconder formulario para receber novidades sobre vagas");
                $(".receba-vagas").removeClass("oculto").addClass("visivel");
            }
        });
    },

    recebaNovidades: function () {
        $("#AreaId").change(function () {
            var url = ROOT_URL + 'Interesse/CarregarCargos';
            var areaId = $(this).val();
            $.get(url, { areaId }, function (result) {
                $("#cargo").html(result);
            });
        });

        $("#remover-cadastro").click(function () {
            var url = ROOT_URL + 'Interesse/Cancelamento';
            $.get(url, {}, function (result) {
                $("#conteudo-vaga").html(result);
                Website.recebaNovidades();
                $(".form-modal #Nome").focus();
            });
        });

        $("#inscricao").click(function () {
            var url = ROOT_URL + 'Interesse/Inscricao';
            $.get(url, {}, function (result) {
                $("#conteudo-vaga").html(result);
                Website.recebaNovidades();
                $(".form-modal #areaId").focus();
            });
        });

        $("#interesse-vaga-salvar").click(function () {
            var url = ROOT_URL + 'Interesse/Salvar';
            $.get(url, $("#interesse-vaga").serialize(), function (result) {
                $("#conteudo-vaga").html(result);
                Website.recebaNovidades();
                $(".form-modal .message").focus();
            });
        });

        $("#cancelamento-vaga-salvar").click(function () {
            var url = ROOT_URL + 'Interesse/Cancelar';
            $.get(url, $("#cancelamento-vaga").serialize(), function (result) {
                $("#conteudo-vaga").html(result);
                Website.recebaNovidades();
                $(".form-modal .message").focus();
            });
        });
    },

    compartilhamento: function () {
        if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
            $(".whatsappMobile").show();
            $(".whatsappDesktop").hide();
        } else {
            $(".whatsappMobile").hide();
            $(".whatsappDesktop").show();
        }
    }
}
