/home/crealab/pueblito.brainware.com.co/wp-content/plugins/scroll_magic/assets/admin/js/script.js
;(function ($) {
    "use strict"
    $('li#toplevel_page_scrollmagic > ul > li:nth-last-child(1) > a, li#toplevel_page_scrollmagic > ul > li:nth-last-child(2) > a').attr('target', '_blank');

    function hbGetFontCSS(font) {
        if (
            font != "" &&
            $.inArray(
                font,
                "Arial|Helvetica+Neue|Courier+New|Times+New+Roman|Comic+Sans+MS|Impact".split(
                    "|"
                )
            ) >= 0
        ) {
            return
        }
        var api = "//fonts.googleapis.com/css?family="
        $("<link>")
            .appendTo("head")
            .attr({
                type: "text/css",
                rel: "stylesheet",
                href: api + font,
            })
    }

    function hbGetFontName(font) {
        font = font.replace(/\+/g, " ")
        font = font.split(":")
        return font[0]
    }

    $(document).ready(function () {
        if ($('#wpsg-container[ng-app="wpsgApp"]').length > 0) {
            $('#wpsg-container[ng-app="wpsgApp"]').removeAttr("style")
        }

        $("#wpsg-close-previewframe").on("click", function () {
            $('#wpsg-preview-frame').addClass('wpsg-hide-frame');
        })

        // import scene
        $(".import-from-template").on("click", function () {
            function inTemplates(scenes, number = 15) {
                var dataCode = SCROLLMAGIC.LICENSE_KEY;
                var buttomImport = ` <svg class="${dataCode ? "bb-import-act" : "bb-enter-license"}" title="${dataCode ? "Import the scene" : "Please Enter License Key"}" xmlns="http://www.w3.org/2000/svg" height="20" viewBox="0 96 960 960" width="40"><path d="M284.332 855q-35.057 0-58.695-23.787Q202 807.425 202 772.385V723h58v49.385q0 9.23 7.692 16.923Q275.385 797 284.615 797h390.77q9.23 0 16.923-7.692Q700 781.615 700 772.385V723h58v49.385q0 35.04-23.799 58.828Q710.401 855 675.344 855H284.332ZM481 696.231 307.461 522.692l42.308-38.769L452 586.154v-357h58v357l102.231-102.231 42.308 38.769L481 696.231Z"></path></svg>
                        <input type="hidden" class="wpsg-scene-inport" onchange="angular.element(this).scope().importTemplate(this)">`

                $(".wpsg-import-footer-pagination-container").pagination({
                    dataSource: scenes,
                    pageSize: number,
                    showPrevious: false,
                    showNext: false,
                    callback: function (data, pagination) {
                        // template method of yourself
                        var html = ""
                        $.each(data, function (key, scene) {
                            html += `
                                <li class="wpsg-scene">
                                    <div class="wpsg-scene-avater" style="background-image: url('${SCROLLMAGIC.CLOUD_ASSETS}/imgs/${scene.group}/${scene.id}.png');">
                                        <span class="wpsg-scene-tag">SCENE</span>
                                    </div>
                                    <div class=" wpsg-scene-info">
                                        <div class="wpsg-scene-import">
                                            <div class="wpsg-scene-import-function">
                                                <a class="preview-demo" target="_blank" href="${SCROLLMAGIC.CLOUD_DEMO}/${scene.group}">
                                                    <svg class="svg" width="40" height="40" viewBox="0 0 40 40" xmlns="http://www.w3.org/2000/svg"><path d="M17 14.098l.765.478 8 5 .678.424-.678.424-8 5-.765.478V14.098zm1 1.804v8.196L24.557 20 18 15.902z" fill-rule="evenodd" fill-opacity="1" fill="#fff" stroke="none"></path></svg>
                                                </a>
                                                <a class="import-demo" data-id="${scene.id}" data-group="${scene.group}">
                                                    ${buttomImport}
                                                </a>
                                                <h2 class="wpsg-scene-name">${scene.name}</h2>
                                            </div>
                                        </div>
                                    </div>
                                </li>
                            `
                        });
                        
                        $("ul.wpsg-scenes").html(html)
                        //import data demo
                        $(".import-demo .bb-import-act").on(
                            "click",
                            function () {
                                $(".wpsg-all-scene").append(`
                                <div class="wpsg-loading">
                                    <h1>
                                        Processing ...
                                    </h1>
                                </div>
                            `)
                                var seft = this
                                $.ajax({
                                    method: "POST",
                                    url: SCROLLMAGIC.API,
                                    data: {
                                        action: "scene",
                                        id: $(this).closest(".import-demo").attr("data-id"),
                                        group: $(this).closest(".import-demo").attr("data-group"),
                                        license: SCROLLMAGIC.LICENSE_KEY,
                                        url: window.location.hostname,
                                    },
                                })
                                    .done(function (sceneJson) {
                                        try {
                                            JSON.parse(sceneJson)
                                        } catch (e) {
                                            $(".wpsg-loading h1").html(
                                                `Please <a href="${settings_slug}" >Verify License Key</a> again or contact Docodev!`
                                            )
                                            console.log(e)
                                            console.log(sceneJson);
                                            return false
                                        }
                                        $(seft)
                                            .closest(".import-demo")
                                            .find(".wpsg-scene-inport")
                                            .val(sceneJson)
                                            .trigger("change")
                                        $(".wpsg-all-scene .wpsg-loading").remove()
                                        $(".wpsg-import-scene").removeClass("active")
                                    })
                                    .fail(function (e) {
                                        console.log(e)
                                        $(".wpsg-loading h1").html(
                                            `Please <a href="${settings_slug}" >Verify License Key</a> again or contact Docodev!`
                                        )
                                    })
                            }
                        )
                        $(".import-demo .bb-enter-license").on(
                            "click",
                            function () {
                                swal({
                                    title: "You need to enter your License Key",
                                    text: "Do you want to go Settings Page?",
                                    buttons: true,
                                    dangerMode: true,
                                    position: "top",
                                }).then(function (will) {
                                    if (will) {
                                        document.location = settings_slug
                                    }
                                })
                                return
                            }
                        )
                    },
                })
            }
            function filter(scenes) {
                var filter1 = $('input[type="radio"][name="category-scene"]:checked').val().toLowerCase();
                var filter2 = $(".wpsg-search-scene").val().toLowerCase();
                var filter3 = $(".wpsg-import-footer-pagination-number select").val();
                var dataReturn = $.map(scenes, function (val, ind) {
                    if ( (filter1 == "all" || val.group.toLowerCase() == filter1) && ( !filter2.trim() || val.name.toLowerCase().indexOf(filter2) >= 0) ) {
                        return val;
                    }
                })
                inTemplates(dataReturn, filter3);
            }
            $(".wpsg-import-scene").addClass("active")
            $(".wpsg-all-scene").append(`
                <div class="wpsg-loading">
                    <h1>
                        Processing...
                    </h1>
                </div>
            `)
            $.ajax({
                method: "POST",
                url: SCROLLMAGIC.API,
                data: {
                    action: "list",
                },
            }).done(function (res) {
                $(".wpsg-all-scene .wpsg-loading").remove()
                var res = JSON.parse(res),
                    groups = res.groups,
                    scenes = [],
                    $group = $(".wpsg-import-right-bar .right-bar-filter-categiry");

                    $.each(res.data, function (idGroup, scenesData) {
                        $.each(scenesData, function (id, name) {
                            scenes.push({
                                id: id,
                                name: name,
                                group: idGroup,
                            });
                        });
                    });

                inTemplates(scenes);
                $group.html(`
                    <li>
                        <label>
                            <input type="radio" name="category-scene" checked="checked" value="all">
                            <span>All</span>
                        </label>
                    </li>
                `);
                $.each(groups, function (groupId, groupName) {
                    $group.append(`
                            <li>
                                <label>
                                    <input type="radio" name="category-scene" value="${groupId}">
                                    <span>${groupName}</span>
                                </label>
                            </li>`)
                })
                $(".wpsg-search-scene").on("input change", function () {
                    filter(scenes)
                })
                $('input[type="radio"][name="category-scene"]').on(
                    "change",
                    function () {
                        filter(scenes)
                    }
                )
                $(".wpsg-import-footer-pagination-number select").on(
                    "input change",
                    function () {
                        filter(scenes)
                    }
                )
            })
        })

        // close tab import
        $(".wpsg-import-top-bar .bb-close-frame").on("click", function (e) {
            $(".wpsg-import-scene").removeClass("active")
        })

        $(document).on("click", ".wpsg-hide-setting", function () {
            $(this).toggleClass("bb-toggle-expanded")
            $("#BBScrollmagic").toggleClass("bb-toggle-expanded")
        })

        // rulerjs
        function printRulerTopLeft() {
            $(".wpsg-ruler-top").html("")
            var widthAll = $(window).width();
            $(".wpsg-ruler-top")
                .css("width", widthAll)
                .css("left", $(".wpsg-rows").offset().left)
            for (var i = 0; i < $(".wpsg-rows").offset().left / 10; i++) {
                var style = `left:${-i * 10}px;`
                var value = ""
                if (i % 10 == 0 && i !== 0) {
                    style += " height:10px;"
                    value = `<span>${-i * 10}</span>`
                }
                $(".wpsg-ruler-top").prepend(`
                    <div style="${style}">${value}</div>
                `)
            }
            for (var i = 0; i < widthAll / 10; i++) {
                var style = `left:${i * 10}px;`
                var value = ""
                if (i % 10 == 0) {
                    style += " height:10px;"
                    value = `<span>${i * 10}</span>`
                }
                $(".wpsg-ruler-top").append(`
                    <div style="${style}">${value}</div>
                `)
            }
            var HeightAll = $(window).height();
            $(".wpsg-ruler-left, .wpsg-ruler-right").css("height", HeightAll)
            for (var i = 0; i < HeightAll / 10; i++) {
                var style = `top:${i * 10}px;`
                var value = ""
                if (i % 10 == 0) {
                    style += " width:10px;"
                    value = `<span>${i * 10}</span>`
                }
                $(".wpsg-ruler-left, .wpsg-ruler-right").append(`
                    <div style="${style}">${value}</div>
                `)
            }
            let parent = $(".content-container")[0]
            parent.onmouseover =
                parent.onmouseout =
                parent.onmousemove =
                    handler
            function handler(event) {
                let type = event.type
                while (type < 11) type += " "
                $(".wpsg-ruler-top-position-mouse").css(
                    "left",
                    event.pageX - $(".content-container").offset().left
                )
                $(".wpsg-ruler-left-position-mouse, .wpsg-ruler-right-position-mouse").css(
                    "top",
                    event.pageY - $(".content-container").offset().top
                )
                return false
            }
        }

        // scrollbar
        if ($("body").hasClass("scrollmagic_page_wpsg_add_scrollmagic")) {
            printRulerTopLeft()

            // new PerfectScrollbar('.content-container');
            // new PerfectScrollbar('.content-all-element');
            // new PerfectScrollbar('#wpsg-popup-settings');

            // contextMenu/
            // $.contextMenu({
            //     selector: ".scrollmagic-builder-element",
            //     callback: function (key, options) {
            //         $(this).trigger("click")
            //         switch (key) {
            //             case "content":
            //                 $("#scroll-content-setting").trigger("click")
            //                 break
            //             case "beforeScroll":
            //                 $("#scroll-before-setting").trigger("click")
            //                 break
            //             case "afterScroll":
            //                 $("#scroll-after-setting").trigger("click")
            //                 break
            //             case "duplicate":
            //                 $(
            //                     ".wpsg-element .scrollyicon-content_copy"
            //                 ).trigger("click")
            //                 break
            //             case "delete":
            //                 $(".wpsg-element .scrollyicon-delete").trigger(
            //                     "click"
            //                 )
            //                 break
            //             case "lookUnlock":
            //                 $(".wpsg-element .wpmg-topbar-lock label").trigger(
            //                     "click"
            //                 )
            //                 break
            //             case "showHide":
            //                 $(
            //                     ".wpsg-element .wpmg-topbar-display label"
            //                 ).trigger("click")
            //                 break

            //             default:
            //                 break
            //         }
            //     },
            //     items: {
            //         edit: {
            //             name: "Edit",
            //             icon: "fa-pencil",
            //             items: {
            //                 content: {
            //                     name: "Content",
            //                     icon: "fa-pencil",
            //                 },
            //                 beforeScroll: {
            //                     name: "Before scroll",
            //                     icon: "fa-pencil",
            //                 },
            //                 afterScroll: {
            //                     name: "After scroll",
            //                     icon: "fa-pencil",
            //                 },
            //             },
            //         },
            //         duplicate: { name: "Duplicate", icon: "fa-clone" },
            //         delete: { name: "Delete", icon: "fa-trash" },
            //         lookUnlock: { name: "Lock/Unlock", icon: "fa-lock" },
            //         showHide: { name: "Show/Hide", icon: "fa-eye" },
            //     },
            // })
        }

        //open settingAll
        setTimeout(function () {
            $("#scroll-all-setting").trigger("click")
        }, 100)

        var mousewheelCounter = 100
        document.addEventListener("keydown", function (e) {

            if(e.key == 'Escape') {
                $('.bb-ajax-loading').css({display: 'none'});
            }
            if (
                e.ctrlKey &&
                (e.key == "61" ||
                    e.key == "48" ||
                    e.key == "107" ||
                    e.key == "173" ||
                    e.key == "109" ||
                    e.key == "187" ||
                    e.key == "189")
            ) {
                if (
                    (e.key == "187" || e.key == "61") &&
                    mousewheelCounter < 200
                ) {
                    mousewheelCounter += 5
                } else if (
                    (e.key == "189" || e.key == "173") &&
                    mousewheelCounter > 25
                ) {
                    mousewheelCounter -= 5
                } else if (e.key == "48") {
                    mousewheelCounter = 100
                    console.log("hihi")
                }
                // $("#bb-content-container .wpsg-rows").css(
                //     "transform",
                //     "scale(" + mousewheelCounter / 100 + ")"
                // )
                $("#bb-content-container .wpsg-rows").css(
                    "zoom",
                    mousewheelCounter / 100
                )
                $("#zoom-content-title").text(mousewheelCounter + "%")
                e.preventDefault()
            }
        })
        document.addEventListener(
            "wheel",
            function (e) {
                if (e.ctrlKey) {
                    if (e.deltaY < 0 && mousewheelCounter < 200) {
                        mousewheelCounter += 5
                    } else if (e.deltaY > 0 && mousewheelCounter > 25) {
                        mousewheelCounter -= 5
                    }
                    // $("#bb-content-container .wpsg-rows").css(
                    //     "transform",
                    //     "scale(" + mousewheelCounter / 100 + ")"
                    // )
                    $("#bb-content-container .wpsg-rows").css(
                        "zoom",
                        mousewheelCounter / 100
                    )
                    $("#zoom-content-title").text(mousewheelCounter + "%")
                    e.preventDefault()
                }
            },
            {
                passive: false,
            }
        )

        // Dynamic size for input scene title
        function resizable(el, factor) {
            var int = Number(factor) || 7.7
            function resize() {
                el.style.width = (el.value.length + 1) * int + "px"
            }
            var e = "keyup,keypress,focus,blur,change".split(",")
            for (var i in e) el.addEventListener(e[i], resize, false)

            setTimeout(function () {
                resize()
            }, 300)
        }
        if ($("#bbtxt-title-scene").length > 0) {
            resizable(document.getElementById("bbtxt-title-scene"), 5.8)
        }

        // Expand device settings
        if ($(".btn-device .bb-dropdown-icon").length > 0) {
            $(".btn-device").on("click", function (e) {
                if (
                    $(e.target).hasClass("bb-dropdown-icon") ||
                    $(e.target).closest(".bb-dropdown-icon").length > 0
                ) {
                    if ($(this).closest(".btn-device").hasClass("expanded")) {
                        $(this).closest(".btn-device").removeClass("expanded")
                    } else {
                        $(this)
                            .closest(".btn-device")
                            .siblings()
                            .removeClass("expanded")
                        $(this).closest(".btn-device").addClass("expanded")
                    }
                } else {
                    $(this)
                        .closest(".btn-device")
                        .siblings()
                        .removeClass("expanded")
                }
            })
        }

        // resize list of elemant
        // if ($(".wpsg-wrapper-action").length > 0) {
        //     $(".wpsg-wrapper-action").resizable({
        //         handles: "s",
        //         maxHeight: 860,
        //         minHeight: 400,
        //         stop: function (event, ui) {
        //             if (ui.size.height >= 820) {
        //                 $(".active-list-of-element").removeClass(
        //                     "scrollyicon-clearclose"
        //                 )
        //                 $(".active-list-of-element").addClass(
        //                     "scrollyicon-keyboard_arrow_up"
        //                 )
        //             } else {
        //                 $(".active-list-of-element").removeClass(
        //                     "scrollyicon-keyboard_arrow_up"
        //                 )
        //                 $(".active-list-of-element").addClass(
        //                     "scrollyicon-clearclose"
        //                 )
        //             }
        //         },
        //     })
        // }
        if ($(window).width() <= 1130) {
            $(".wpsg-elements .dropbtn span").text("Elements")
        } else {
            $(".wpsg-elements .dropbtn span").text("Add Element")
        }
        $(window).resize(function () {
            if ($(window).width() <= 1130) {
                $(".wpsg-elements .dropbtn span").text("Elements")
            } else {
                $(".wpsg-elements .dropbtn span").text("Add Element")
            }
        })

        // $(".active-list-of-element").on("click", function () {
        //     if ($(this).hasClass("scrollyicon-clearclose")) {
        //         $(".wpsg-wrapper-action").css("height", "860px")
        //     } else {
        //         $(".wpsg-wrapper-action").css("height", "77.7%")
        //     }
        //     $(this)
        //         .toggleClass("scrollyicon-keyboard_arrow_up")
        //         .toggleClass("scrollyicon-clearclose")
        // })

        $(".wp-list-table").on("click", ".bb-button-delete", function () {
            var $self = $(this),
                id = $self.data("id"),
                $table = $self.closest("table").DataTable(),
                $row = $self.closest("tr")

            swal({
                title: "Are you sure?",
                text: "Once deleted, you will not be able to recover this scene!",
                icon: "",
                buttons: true,
                dangerMode: true,
            }).then(function (willDelete) {
                if (willDelete) {
                    $(".bb-ajax-loading").css({ display: "flex" })
                    $.post(
                        ajaxurl,
                        { action: "wpsg_delete_scrollmagic", id: id },
                        function (response) {
                            response = JSON.parse(response)

                            if (typeof response.status != "undefined") {
                                $.growl({
                                    title: '',
                                    message: response.message,
                                    location: "br",
                                    style: response.status,
                                })

                                if (response.status == "notice") {
                                    $table.row($row).remove()
                                    $table.draw()
                                }
                            }

                            $(".bb-ajax-loading").css({ display: "none" })
                        }
                    )
                }
            })
            return
        })

        setTimeout(function () {
            $(".bb-toolbar-link.home").on("click", function () {
                $("#adminmenumain").addClass("open")
            })

            //     $(function (event, ui) {
            //         var screen = $(window).height()
            //         if ($("#wpsg-app-element").length > 0) {
            //             $("#wpsg-app-element").resizable({
            //                 handles: "n",
            //                 maxHeight: 735,
            //                 minHeight: 55,
            //                 resize: function (event, ui) {
            //                     if (ui.size.height < 180) {
            //                         $(".wpsg-wrapper-action").addClass("full")
            //                         $("#wpsg-app-element .title-setting i").hide()
            //                     }
            //                     if (ui.size.height > 180) {
            //                         $(".wpsg-wrapper-action").height(
            //                             screen - ui.size.height - 55
            //                         )
            //                         if (
            //                             $(".wpsg-wrapper-action").hasClass("full")
            //                         ) {
            //                             $(".wpsg-wrapper-action").removeClass(
            //                                 "full"
            //                             )
            //                             $(
            //                                 "#wpsg-app-element .title-setting i"
            //                             ).show()
            //                             $("#wpsg-app-element .title-setting i")
            //                                 .addClass("fa-times")
            //                                 .removeClass("fa-chevron-up")
            //                         }
            //                     }
            //                 },
            //             })
            //         }
            //     })
        }, 500)
    })
    if (
        $("body").hasClass("toplevel_page_wpsg_scrollmagics") ||
        $("body").hasClass("visual-scrollmagic_page_wpsg_settings") ||
        $("body").hasClass("scrollmagic_page_wpsg_add_scrollmagic") ||
        $("body").hasClass("magic-content_page_smb_wpbb_about")
    ) {
        $("#wpwrap").addClass("wpsg_active")
        $("html.wp-toolbar").addClass("wpsg_active")
    }
    $(document).on("click", function (e) {
        if (
            $(e.target).closest("#adminmenumain").length == 0 &&
            $(e.target).closest(".bb-toolbar-link.home").length == 0
        ) {
            if ($("#adminmenumain").hasClass("open")) {
                $("#adminmenumain").removeClass("open")
            }
        }
    })
})(window.jQuery)