(function($) {
    function page_menu() {
        var $container = $('.rad-main'),
            $menu = $('select[name="menu"]'),
            $initialMenuFields = $container.find('.field[data-name="menu_initial_description"],.field[data-name="menu-initial-state-logo"],.field[data-name="initial-menu-background-color"],.field[data-name="initial-menu-text-color"],.field[data-name="initial-menu-border-color"],.field[data-name="initial-shop-cart-text-color"]'),
            $secondMenuFields = $container.find('.field[data-name="menu_second_description"],.field[data-name="menu-second-state-logo"],.field[data-name="menu-background-color"],.field[data-name="menu-text-color"],.field[data-name="menu-border-color"],.field[data-name="shop-cart-text-color"]');

        function changeHandler() {
            var selected = $menu.find(':selected').val();
            if ('custom' == selected) {
                $initialMenuFields.removeClass('hide-completly-field');
                $secondMenuFields.removeClass('hide-completly-field');
            } else {
                $initialMenuFields.addClass('hide-completly-field');
                $secondMenuFields.addClass('hide-completly-field');
            }
        }
        $menu.change(changeHandler);
        changeHandler();
    }

    function page_header() {
        var $container = $('.rad-main'),
            $headerCustom = $container.find('select[name="page-header-custom"]'),
            $headerType = $container.find('select[name="page-header-mode"]'),
            $titleShow = $container.find('select[name="title-bar"]'),
            $headerSec = $container.find('.field[data-name="page-header-mode"]'),
            $titleSec = $container.find('.field[data-name="title-bar"]'),
            $titleColorSec = $container.find('.field[data-name="page-header-text-color"]'),
            $headerBgSec = $container.find('.field[data-name="page-header-bg-image"]'),
            $headerBgColorSec = $container.find('.field[data-name="page-header-bg"]'),
            $customTitleSec = $container.find('.field[data-name="title-text"]');
        //  Slide Up/Down Title Options
        $headerCustom.change(function() {
            var header = $headerCustom.find('option:selected').val();
            if (header === "inherit") {
                $headerSec.addClass('hide-completly-field');
                $titleSec.addClass('hide-completly-field');
                $titleColorSec.addClass('hide-completly-field');
                $headerBgSec.addClass('hide-completly-field');
                $headerBgColorSec.addClass('hide-completly-field');
                $customTitleSec.addClass('hide-completly-field');
            } else {
                $headerSec.removeClass('hide-completly-field');
                $titleSec.removeClass('hide-completly-field');
                $titleColorSec.removeClass('hide-completly-field');
                $headerBgSec.removeClass('hide-completly-field');
                $headerBgColorSec.removeClass('hide-completly-field');
                $customTitleSec.removeClass('hide-completly-field');
            }
        }).change();
        $titleShow.change(function() {
            var $titleSelected = $titleShow.find('option:selected'),
                title = $titleSelected.val();
            if (title === "1") {
                $customTitleSec.removeClass('hide-field');
            } else if (title === "0" || title === "2") {
                $customTitleSec.addClass('hide-field');
            }
        }).change();
        $headerType.change(function() {
            var type = $(this).find('option:selected').val();
            if (type === "2" || type === "3") {
                $titleSec.addClass('hide-field');
                $headerBgSec.addClass('hide-field');
                $headerBgColorSec.addClass('hide-field');
                $titleColorSec.addClass('hide-field');
            } else {
                $titleSec.removeClass('hide-field');
                $headerBgSec.removeClass('hide-field');
                $headerBgColorSec.removeClass('hide-field');
                $titleColorSec.removeClass('hide-field');
            }
        }).change();
    }

    function colorInput() {
        if (!$.fn.wpColorPicker) return;
        $('#rad_page_meta_box .colorinput').each(function() {
            $(this).wpColorPicker({
                palettes: false
            });
        });
    }

    function page_slider() {
        var $container = $('.rad-main'),
            $slider_types = $container.find('select[name="slider-type"]');
        $slider_types.on('change', function() {
            slider_type_change();
        });
        slider_type_change();
        // Home type  
        function slider_type_change() {
            var selected = $slider_types.val();
            if (selected == 'rev-slider') {
                $('.field[data-name="rev-slider"]').removeClass('hide-field');
                $('.field[data-name="slider-width"]').removeClass('hide-field');
                $('.field[data-name="slide-mode"]').addClass('hide-field');
                $('.field[data-name="builtin-slider-cat"]').addClass('hide-field');
                $('.field[data-name="slider-pagination-type"]').addClass('hide-field');
                $('.field[data-name="slider-pagination-align"]').addClass('hide-field');
                $('.field[data-name="slider-pagination-style"]').addClass('hide-field');
            } else if (selected == 'builtin-slider') {
                $('.field[data-name="rev-slider"]').addClass('hide-field');
                $('.field[data-name="slide-mode"]').removeClass('hide-field');
                $('.field[data-name="builtin-slider-cat"]').removeClass('hide-field');
                $('.field[data-name="slider-width"]').removeClass('hide-field');
                $('.field[data-name="slider-pagination-type"]').removeClass('hide-field');
                $('.field[data-name="slider-pagination-align"]').removeClass('hide-field');
                $('.field[data-name="slider-pagination-style"]').removeClass('hide-field');
            } else {
                $('.field[data-name="rev-slider"]').addClass('hide-field');
                $('.field[data-name="slide-mode"]').addClass('hide-field');
                $('.field[data-name="builtin-slider-cat"]').addClass('hide-field');
                $('.field[data-name="slider-width"]').addClass('hide-field');
                $('.field[data-name="slider-pagination-type"]').addClass('hide-field');
                $('.field[data-name="slider-pagination-align"]').addClass('hide-field');
                $('.field[data-name="slider-pagination-style"]').addClass('hide-field');
            }
        };
    }

    $(document).ready(function() {
        page_menu();
        page_header();
        colorInput();
        page_slider();
    });
})(jQuery);