'use strict';

jQuery(document).ready(function () {


    jQuery(function (jQuery) {

        if (jQuery(".toggle-Enable").length > 0) {

            if (document.getElementById('toggle-Enable').checked) {
                document.getElementById('milo_header_options_table').className = 'form-table';
            } else {
                document.getElementById("milo_header_options_table").className = 'form-table milo-hide-options';
            }
              }
               if (jQuery("#milo_layout_options_table").length > 0) {
            if (document.getElementById('milo_page_boxed').checked) {
                document.getElementById('milo_layout_options_table').className = 'form-table';
            } else {
                document.getElementById("milo_layout_options_table").className = 'form-table milo-hide-boxed';
            }

            jQuery('.custom_upload_image').each(function (i, obj) {
                if (this.value == '') {
                    document.getElementById(this.id + '_clear').style.display = 'none';
                }
            });
        }

        if (jQuery("#nav-menu-header").length > 0) {

            jQuery('.menu-item-settings').each(function (i, obj) {
                // Aquire the current menu item's id number
                var current_id = jQuery(this).attr('id').substr(19);
                var status_text = '';
                if (document.getElementById('edit-menu-item-classes-' + current_id).value.search('megamenu') != -1) {
                    status_text = "<span>Enabled</span>";
                }

                jQuery(this).prepend('<label>Mega Menu/Section : </label><button class="milo-nav-mega-menu-class" \n\
                    onclick="return milo_toggle_nav_mega(\'' + current_id + '\')">Enable/Disable</button> <div class="milo-nav-mega-status" id="milo-nav-mega-status-' + current_id + '">' + status_text + '</div><br>');
            });
        }

    });
    
    jQuery(function () {
        jQuery('.custom_header_color').wpColorPicker();
    });

});

function milo_toggle_nav_mega(current_id) {
    var target_class = document.getElementById('edit-menu-item-classes-' + current_id);
    var object_class = document.getElementById('menu-item-' + current_id).className;
    var nav_class = object_class.substr(0, 28);
    var menu_item = document.getElementById('edit-menu-item-classes-' + current_id);
    if (nav_class == 'menu-item menu-item-depth-0 ' || nav_class == 'menu-item menu-item-depth-1 ') {
        if (target_class.value.search(' megamenu') != -1) {
             menu_item.value=menu_item.value.replace(' megamenu', '');
            document.getElementById('milo-nav-mega-status-' + current_id).innerHTML = '';
        }else if (target_class.value.search('megamenu') != -1){
             menu_item.value=menu_item.value.replace('megamenu', '');
            document.getElementById('milo-nav-mega-status-' + current_id).innerHTML = '';
        }else {
            menu_item.value = menu_item.value+' megamenu';
            document.getElementById('milo-nav-mega-status-' + current_id).innerHTML = "<span>Enabled</span>";
        }
    }
    return false;
}

function milo_upload_trigger(target_id) {
    var formfield = document.getElementById(target_id + '_upload_input');
    var preview = document.getElementById(target_id + '_preview_image');

    // Show the media image selection WordPress frame
    tb_show('', 'media-upload.php?type=image&TB_iframe=true');
    window.send_to_editor = function (html) {
        var imgurl = jQuery('img', html).attr('src');
        var classes = jQuery('img', html).attr('class');
        var id = classes.replace(/(.*?)wp-image-/, '');
        formfield.value = id;
        preview.src = imgurl;
        if (document.getElementById(target_id + '_upload_input').value != '') {
            document.getElementById(target_id + '_upload_input_clear').style.display = 'inline-block';
        }
        tb_remove();

    }
    return false;
}
function milo_clear_image_trigger(target_id,image_none) {
    // Removes the uploaded header image from the preview and the input fields
    document.getElementById(target_id + '_upload_input').value = '';
    document.getElementById(target_id + '_preview_image').src = image_none;
    document.getElementById(target_id + '_upload_input_clear').style.display = 'none';
    return false;
}

function milo_header_override() {
    if (document.getElementById('toggle-Enable').checked) {
        document.getElementById('milo_header_options_table').className = 'form-table';
    } else {
        document.getElementById("milo_header_options_table").className = 'form-table milo-hide-options';
    }
}

function milo_boxed_toggle() {
    if (document.getElementById('milo_page_boxed').checked) {
        document.getElementById('milo_layout_options_table').className = 'form-table';
    } else {
        document.getElementById("milo_layout_options_table").className = 'form-table milo-hide-boxed';
    }
}

function milo_style_select() {
    if (document.getElementById('toggle-on').checked) {
        document.getElementById("milo_style_container").style.display = '';
    } else {
        document.getElementById("milo_style_container").style.display = 'none';
    }
}

// Changes the font icon display based on the selection
function milo_icon_change(obj) {
    document.getElementById("milo_selected_icon").className = obj.className;
    document.getElementById('milo_icon_input').value = obj.className;
}

// Removes the icon the selected icon
function milo_icon_clear_selection() {
    document.getElementById("milo_selected_icon").className = "";
    document.getElementById('milo_icon_input').value = "";
    return false;
}

// Displays the selected icon
function milo_selected_icon_display() {
    document.getElementById("milo_selected_icon").className = document.getElementById('milo_icon_input').value;
}

// Radio style selection for icon box
function milo_radio_style_option(obj) {
    document.getElementById('milo_selected_radio_style').value = obj.value;
}

// Changes the font icon display based on the selection
function milo_icon_change(obj) {
    document.getElementById("milo_selected_icon").className = obj.className;
    document.getElementById('milo_icon_input').value = obj.className;
}

// Removes the icon the selected icon
function milo_icon_clear_selection() {
    document.getElementById("milo_selected_icon").className = "";
    document.getElementById('milo_icon_input').value = "";
    return false;
}

// Displays the selected icon
function milo_selected_icon_display() {
    document.getElementById("milo_selected_icon").className = document.getElementById('milo_icon_input').value;
}

// Radio style selection for icon box
function milo_radio_style_option(obj) {
    document.getElementById('milo_selected_radio_style').value = obj.value;
}
