<?php function serpentsoft_widgets_init() {
    
// Wide Home Page Sidebar
    register_sidebar(array(
        'name' => __('Homepage Wide Sidebar', 'serpentsoft'),
        'id' => 'hp-wide-sidebar',
        'description' => __('Appears on posts and pages except the optional Front Page template, which has its own widgets', 'serpentsoft'),
        'before_widget' => '<aside id="%1$s" class="widget %2$s"><div class="widget-content clearfix padding20">',
        'before_title' => '</div><div class="widget-title"><h4>',
        'after_title' => '</h4></div><div class="widget-content clearfix">',
        'after_widget' => '</div></aside>',
    ));

// Narrow Home Page Sidebar
    $page_layout = serpentsoft_get_data('serpentsoft_pagelayout');
    
    if( $page_layout != '2cols-left' && $page_layout != '2cols-right' ){
    
        register_sidebar(array(
            'name' => __('Homepage Narrow Sidebar', 'serpentsoft'),
            'id' => 'hp-narrow-sidebar',
            'description' => __('Appears on posts and pages except the optional Front Page template, which has its own widgets', 'serpentsoft'),
            'before_widget' => '<aside id="%1$s" class="widget %2$s"><div class="widget-content clearfix padding20">',
            'before_title' => '</div><div class="widget-title"><h4>',
            'after_title' => '</h4></div><div class="widget-content clearfix">',
            'after_widget' => '</div></aside>',
        ));

    }
    
    /*
    // Wide Top Menu Sidebar
    register_sidebar(array(
        'name' => __('Top Menu Wide Sidebar', 'serpentsoft'),
        'id' => 'tm-wide-sidebar',
        'description' => __('Appears on Top Menu', 'serpentsoft'),
        'before_widget' => '<div id="%1$s" class="clearfix %2$s">',
        'before_title' => '<div class="title"><h3>',
        'after_title' => '</h3><hr /></div><div class="widget-content clearfix">',
        'after_widget' => '</div></div>'
    ));

    // Narrow Top Menu Sidebar
    register_sidebar(array(
        'name' => __('Top Menu Narrow Sidebar', 'serpentsoft'),
        'id' => 'tm-narrow-sidebar',
        'description' => __('Appears on Top Menu', 'serpentsoft'),
        'before_widget' => '<div id="%1$s" class="clearfix %2$s">',
        'before_title' => '<div class="title"><h3>',
        'after_title' => '</h3><hr /></div><div class="widget-content clearfix">',
        'after_widget' => '</div></div>',
    ));
    */

    serpentsoft_sidebars_topMenu_dynamic();
    
    // Get Sidebars from (sidebars-dynamic.php)
    serpentsoft_sidebars_footer_dynamic();
}
add_action('widgets_init', 'serpentsoft_widgets_init');

// ==========================================================
// Page Layout Settings
// ==========================================================
function serpentsoft_draw_headerPageLayout($page_layout = '') {
    if ($page_layout == '') {
        $page_layout = serpentsoft_get_data('serpentsoft_pagelayout');
    }

    switch ($page_layout) {
        case 'left':
            // Wide sidebar
            get_sidebar('wide');

            // Narrow sidebar
            get_sidebar('narrow');
            break;

        case 'middle':
            // Wide sidebar
            get_sidebar('wide');
            break;
        
        case '2cols-left':
            // Wide sidebar
            get_sidebar('wide');
            break;
        
    } // Switch End
}

function serpentsoft_draw_footerPageLayout($page_layout = '') {
    if ($page_layout == '') {
        $page_layout = serpentsoft_get_data('serpentsoft_pagelayout');
    }

    switch ($page_layout) {
        case 'middle':
            // Right sidebar
            get_sidebar('narrow');
            break;

        case 'right':
            // Right sidebar
            get_sidebar('narrow');

            // Left sidebar
            get_sidebar('wide');
            break;
        
        case '2cols-right':
            // Wide sidebar
            get_sidebar('wide');
            break;
        
    } // Switch End	
}

function serpentsoft_correct_layout($post_id){
    
    $page_layout_options_panel = serpentsoft_get_data('serpentsoft_pagelayout');
    $post_page_layout = get_post_meta($post_id, 'serpentsoft_page_layout', true);
    
    if( ($page_layout_options_panel == 'left' || $page_layout_options_panel == 'middle' || $page_layout_options_panel == 'right') && ( $post_page_layout == '2cols-left' || $post_page_layout == '2cols-right' ) ){
        return $page_layout_options_panel;
    }
    
    elseif( ($page_layout_options_panel == '2cols-left' || $page_layout_options_panel == '2cols-right') && ($post_page_layout == 'left' || $post_page_layout == 'middle' || $post_page_layout == 'right' ) ){
        return $page_layout_options_panel;
    }
    
    else{
        return $post_page_layout;
    }
}

function serpentsoft_header_page_layout() {
    global $post;

    if (!is_single() && !is_page()):
        serpentsoft_draw_headerPageLayout();

// Single or Page
    else:

        $post_page_layout = get_post_meta($post->ID, 'serpentsoft_page_layout', true);

        // Old Post
        if (!isset($post_page_layout) || empty($post_page_layout) || $post_page_layout == 'default'):
            // Get Page Layout from Options Panel
            serpentsoft_draw_headerPageLayout();

        // New Post	
        else:
            serpentsoft_draw_headerPageLayout( serpentsoft_correct_layout($post->ID) );
        endif;

    endif;
}

function serpentsoft_footer_page_layout() {
    global $post;

    if (!is_single() && !is_page()):
        serpentsoft_draw_footerPageLayout();

// Single or Page
    else:
        $post_page_layout = get_post_meta($post->ID, 'serpentsoft_page_layout', true);

        //print_r($post_page_layout);
        
        // Old Post
        if (!isset($post_page_layout) || empty($post_page_layout) || $post_page_layout == 'default'):
            // Get Page Layout from Options Panel
            serpentsoft_draw_footerPageLayout();

        // New Post	
        else:
            serpentsoft_draw_footerPageLayout( serpentsoft_correct_layout($post->ID) );
        endif;

    endif;
} ?>