$(function () {
    "use strict";
    if($('.tooltipster-top').length>0){
        $('.tooltipster-top').tooltipster({
            position: "top"
        });
    } 
});

$(function () {
    "use strict";
    if($('.chosenSelect').length>0){
        $('.chosenSelect').chosen();
    }  
});

$(function () {
    "use strict";
    if($('#blockList').length>0){
        $('#blockList').dataTable({
            ordering: false,
            searching: false,
            paging: false,
            info: false,
            dom: '<f<t><"bottom"ilp>><"clear">',
            language: {
                url: language,
            }
        });
    }
});

$(function () {
    "use strict";
    if($('.itemHTMLTable').length>0){
            $('.itemHTMLTable').dataTable(
                {
                    ordering: false,
                    searching: true,
                    paging: false,
                    info: false,
                    "dom": '<f<t><"bottom"ilp>><"clear">',
                    language: {
                        url: language,
                    }
                }
            );

        $('.collapseAll').on('click', function () {
            $('.boxPagesGroup').removeClass('collapsed-box');
            $('.collapseExpand').click();
        });
        $('.expandAll').on('click', function () {
            $('.collapsed-box .collapseExpand').click();
        });

    }
});

$(function () {
    "use strict";
    // wyświetlanie stron
    $('.selectCategory').change(function () {
        $('#selectCategoryForm').submit();
    });
    //wysyłanie formularza paginacji
    $('select[name=per_page]').change(function () {
        $(this).parent().submit();
    })
})


$(function () {
    "use strict";
    // usuwanie pytania
    $('#deleteBanner').change(function () {
        if (this.checked) {
            $('#BannerDeleteInput').removeAttr('disabled');
        } else {
            $('#BannerDeleteInput').attr('disabled', 'disabled');
        }


    });
});

$(function () {
    "use strict";
    // usuwanie pytania
    $('#deleteBannerSet').change(function () {
        if (this.checked) {
            $('#BannerSetDeleteInput').removeAttr('disabled');
        } else {
            $('#BannerSetDeleteInput').attr('disabled', 'disabled');
        }


    });
});

// Chosen
$(function () {
    "use strict";
    // Włączenie chosen - dodawanie i edycja newsa - wybór kategorii
    if($('.chosenSelectGroup').length>0){
        $('.chosenSelectGroup').chosen();
    }
});

$(function() {
    if($('.dateStart').length>0){
        $('.dateStart').datetimepicker({
            format: 'd-m-Y H:i',
            onShow: function (ct) {
                this.setOptions({
                    maxDate: jQuery('.dateEnd').val() ? jQuery('.dateEnd').val() : false
                });
            },
            timepicker: true,
            closeOnDateSelect: true,
            scrollInput: false
        });
    }
    if($('.dateEnd').length>0){
        $('.dateEnd').datetimepicker({
            format: 'd-m-Y H:i',
            onShow: function (ct) {
                this.setOptions({
                    minDate: jQuery('.dateStart').val() ? jQuery('.dateStart').val() : false
                });
            },
            timepicker: true,
            closeOnDateSelect: true,
            scrollInput: false
        });
    }
});

// Cropper
$(function () {
    "use strict";
    if($('#cropperImage').length>0){
        var image = document.getElementById('cropperImage');
        var paramWidth = $('#imageDataFromParameter').attr('cropper-width');
        var paramHeight = $('#imageDataFromParameter').attr('cropper-height');


        var originalImageURL = image.src;
        var uploadedImageURL;
        var cropper = new Cropper(image);

        var inputImage = document.getElementById('wi_admin_bannerbundle_banner_file');
        if (URL) {
            inputImage.onchange = function () {
                var files = this.files;
                var file;

                if (cropper && files && files.length) {
                    file = files[0];

                    if (/^image\/\w+/.test(file.type)) {
                        if (uploadedImageURL) {
                            URL.revokeObjectURL(uploadedImageURL);
                        }

                        image.src = uploadedImageURL = URL.createObjectURL(file);
                        cropper.destroy();
                        cropper = new Cropper(image, {
                            aspectRatio: paramWidth / paramHeight,
                            maxCanvasWidth: paramWidth,
                            maxCanvasHeight: paramHeight,
                            responsive: false,
                            autoCropArea: 0.9,
                            scalable: false,
                            cropBoxResizable: true,
                            dragMode: 'move',
                            rotatable: false,
                            toggleDragModeOnDblclick: true,
                            viewMode: 1,
                            crop: function(e) {
                                var x = e.detail.x;
                                var y = e.detail.y;
                                var width = e.detail.width;
                                var height = e.detail.height;
                                $('#wi_admin_bannerbundle_banner_crop_x').val(x);
                                $('#wi_admin_bannerbundle_banner_crop_y').val(y);
                                $('#wi_admin_bannerbundle_banner_crop_width').val(width);
                                $('#wi_admin_bannerbundle_banner_crop_height').val(height);
                            }
                        });

                        // inputImage.value = inputImage;
                    } else {
                        window.alert('Please choose an image file.');
                    }
                }
            };
        } else {
            inputImage.disabled = true;
            inputImage.parentNode.className += ' disabled';
        }
        document.getElementById('Wyslijfoto').onclick = function () {
            var varDataUrl = cropper.getCroppedCanvas().toDataURL('image/jpeg');
            //document.getElementsByName("file")[0].setAttribute("name", varDataUrl);

            document.getElementById('cropperPreview').setAttribute("src", varDataUrl);
            $('#wi_admin_bannerbundle_banner_cropSave').val(true);

        };
    }
    
    // Sprawdza czy zdjęcie zostało dodane, jeśli nie to zamyka cropper

    /* Tested on Google Chrome */
    $('.InputFileHidden').on("change", function(ev) {
        $('#windowModalCropper').foundation('open');
    });
});

//$(function imageValidation() {
//     var x = $('#wi_admin_bannerbundle_banner_imageURL').value;
//     if (x == 0) {
//        alert("Image must be filled out");
//        return false;
//    }
//});

//$(function(){
//    $('form').submit(function(){
//        var x = document.getElementByName('wi_admin_bannerbundle_banner_imageURL');
//        alert(x);
//        return false;
//     
//    })
//})

$("body").on("submit", "#wi_admin_bannerbundle_banner", function(evt) {

    // Disable things that we don't want to validate.
    $(["input:hidden, textarea:hidden, select:hidden"]).attr("disabled", true);

    // If HTML5 Validation is available let it run. Otherwise prevent default.
    if (this.el.checkValidity && !this.el.checkValidity()) {
        // Re-enable things that we previously disabled.
        $(["input:hidden, textarea:hidden, select:hidden"]).attr("disabled", false);
        return true;
    }
    evt.preventDefault();

    // Re-enable things that we previously disabled.
    $(["input:hidden, textarea:hidden, select:hidden"]).attr("disabled", false);

    // Whatever other form processing stuff goes here.
});