
/*global jQuery:false*/

jQuery(document).ready(function () {
    TFL_JS.init();
    // Run tab open/close event
    TFL_Tab.event();
});

// Init all fields functions (invoked from ajax)
var TFL_JS = {
    init: function () {
        // Run tab open/close
        TFL_Tab.init();
        // Load colorpicker if field exists
        TFL_ColorPicker.init();
    }
};


var TFL_ColorPicker = {
    init: function () {
        var $colorPicker = jQuery('.tfl-colorpicker');
        if ($colorPicker.length > 0) {

            $colorPicker.wpColorPicker();

        }
    }
};

var TFL_Tab = {
    init: function () {
        // display the tab chosen for initial display in content
        jQuery('.tfl-tab.selected').each(function () {
            TFL_Tab.check(jQuery(this));
        });
    },
    event: function () {
        jQuery(document).on('click', '.tfl-tab', function () {
            TFL_Tab.check(jQuery(this));
        });
    },
    check: function (elem) {
        var chosen_tab_name = elem.data('target');
        elem.siblings().removeClass('selected');
        elem.addClass('selected');
        elem.closest('.tfl-inner').find('.tfl-tab-content').removeClass('tfl-tab-show').hide();
        elem.closest('.tfl-inner').find('.tfl-tab-content.' + chosen_tab_name + '').addClass('tfl-tab-show').show();
    }
};