$(document).ready(function() {
    //---------------------------------------------------------------------------------------------//
    // ANIMAÇÃO DA LOGO DA CAPPEN
    //---------------------------------------------------------------------------------------------//

    $('#cappen').hover(function() {
        $(this).children('.hover').stop().animate({
            'width': '86px',
            'left': '0'
        }, 500, 'easeOutSine');
        $(this).children('.hover').find('.logo').stop().animate({
            'left': '0'
        }, 500, 'easeOutSine');
    }, function() {
        $(this).children('.hover').stop().animate({
            'width': '0',
            'left': '43px'
        }, 500, 'easeOutSine');
        $(this).children('.hover').find('.logo').stop().animate({
            'left': '-43px'
        }, 500, 'easeOutSine');
    });


     $('.moreflavoreng').click(function() {
        var num;
        var div = $('div[id^="baseflavoreng"]:last');
        num = parseInt( div.prop("id").match(/\d+/g), 10) +1;
        $('<div id="baseflavoreng'+num+'"></div').appendTo('.more_flavorbaseeng');
        console.log(num);
        
        $('#dadosflavoreng1').clone().appendTo('#baseflavoreng'+num);

    });

    $('.removeflavoreng').click(function() {
        var num;
        var div = $('div[id^="baseflavoreng"]:last');
        num = parseInt( div.prop("id").match(/\d+/g), 10);
        if (num > 1) { 
            console.log(num);
            
            $('#baseflavoreng'+num).remove();
        }

    });

    $('.excluir_arquivo').click(function() {
        
        var idFile = $(this).data('id');
        var tipo = $(this).data('tipo');
        var url = '';
        if (tipo == "imagem") {
            url = SERVER_URL + '/ajax/delgallery.php'
        } else {
            url = SERVER_URL + '/ajax/delvideo.php'
        }

        $.ajax({
            url: url,
            type: 'POST',
            dataType: 'json',
            data: {id: idFile},
            success: function(json) {
                if (json.result == 'success') {
                    console.log('.file'+idFile);
                    $('.file'+idFile).remove();
                } else {
                    alert(json.msg);
                }
            },
            error:function(){
                alert('erro');
            }

        });
        return false;

     });

    $('.removeitem').click(function() {
        var num;
        var div = $('div[id^="baseflavoreng_"]:last');
        num = parseInt( div.prop("id").match(/\d+/g), 10);
        console.log(num);
        if (num > 0) { 
            console.log(num);
            
            $('#baseflavoreng_'+num).remove();
            $.ajax({
                url: SERVER_URL + '/ajax/removeitem.php',
                type: 'POST',
                dataType: 'json',
                data: {id: num},
                success: function(json) {
                    console.log(json);
                    if (json.result == 'success') {
                        $('.'+tipoArq+idiomaSupport+idSupport).remove();
                    } else {
                        alert(json.msg);
                    }    
                },
                error:function(){
                    alert('erro');
                }

            });
        }

    });

    //---------------------------------------------------------------------------------------------//
    // COMPORTAMENTO DE TABELAS
    //---------------------------------------------------------------------------------------------//
    $('.paginas li a').last().addClass('noBorder');

    //---------------------------------------------------------------------------------------------//
    // ABAS
    //---------------------------------------------------------------------------------------------//
    $('#tabs').tabs({
        cookie: {
            expires: 30
        }
    });

    //---------------------------------------------------------------------------------------------//
    // SELECT
    //---------------------------------------------------------------------------------------------//
    $('select:not(.hide-search)').select2();
    $('select.hide-search').select2({
        minimumResultsForSearch: Infinity
    });

    //---------------------------------------------------------------------------------------------//
    // MULTI-SELECT
    //---------------------------------------------------------------------------------------------//
    $(".multiselect").multiselect({
        sortable: false,
        searchable: false
    });
    $(".chosen-select").chosen();

    //---------------------------------------------------------------------------------------------//
    // DATAPICKER
    //---------------------------------------------------------------------------------------------//

    $('#datepicker').DateTimePicker({
        shortDayNames: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sáb'],
        fullDayNames: ['Domingo', 'Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sábado'],
        shortMonthNames: [
            'Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set',
            'Out', 'Nov', 'Dez'
        ],
        fullMonthNames: [
            'Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro',
            'Outubro', 'Novembro', 'Dezembro'
        ],
        dateSeparator: "/",
        dateFormat: "dd/MM/yyyy",
        dateTimeFormat: "dd/MM/yyyy hh:mm",
        setButtonContent: "Confirmar",
        clearButtonContent: "Limpar"
    });

    //---------------------------------------------------------------------------------------------//
    // Sanfona
    //---------------------------------------------------------------------------------------------//

    //menu principal
    $('nav#menuPrincipal dd').hide();

    //esconde todo o conteudo da dd
    $('nav#menuPrincipal dt').click(function() {
        var $this = $(this);

        $("nav#menuPrincipal dt").removeClass('active');
        $this.addClass('active');

        $('nav#menuPrincipal dd').slideUp();
        $this.next().slideDown();
    });

    // ------------------------------------------------------------

    $('.imagem:not(.multiple)').change(function() {
        var filename = $(this).val();
        if (filename != '') {
            var split = filename.split("\\");
            $(this).parent().parent().find('#file').html(split[split.length-1]);
        }
    });

    $('.imagem.multiple').change(function() {
        var files = $(this)[0].files;
        var length = files.length;

        $('div.showImg').html('');

        // handleFileSelect();

        if (length > 0) {
            var number = (length > 0 && length < 10) ? ("0" + length) : length;
            if (length < 2) {
                $(this).parent().parent().find('#file').html(number + " arquivo foi selecionado");
            } else {
                $(this).parent().parent().find('#file').html(number + " arquivos foram selecionados");
            }
        }
    });

    function handleFileSelect() {
        //Check File API support
        if (window.File && window.FileList && window.FileReader) {
            var files = event.target.files; //FileList object

            for (var i = 0; i < files.length; i++) {
                var file = files[i];

                //Only pics
                if ( ! file.type.match('image')) continue;

                var picReader = new FileReader();

                picReader.addEventListener("load", function (event) {
                    var picFile = event.target;
                    $('div.showImg').append('<div class="img"><img style="float: left; margin: 0 5px; width:130px; height: 130px;" src="'+ picFile.result +'"></div>');
                });

                //Read the image
                picReader.readAsDataURL(file);
            }
        } else {
            console.log("Your browser does not support File API");
        }
    }
});
