<?php
/**
* Avoid direct call of the file
*/
if (!function_exists('add_action')) {
    header('Status: 403 Forbidden');
    header('HTTP/1.1 403 Forbidden');
    exit();
}

define('BLN_TEXTDOMAIN', 'bluelines');
define('BLN_URL', get_template_directory_uri());
define('BLN_DIR', get_template_directory());
define('BLN_OPTION_NAME', 'blue_lines_theme_options');
define('BLN_TPLUS_NAME', 'bln_text_widget_plus');
define('BLN_VERSION', '1.0');

global $bln_default_options;

$bln_default_options = array(
    'version' => BLN_VERSION,
    'font' => array(
        'body' => 'Palatino',
        'inputs' => 'Trebuchet',
        'code' => 'Monaco',
    ),
    'page_comments' => true,
    'sidebar' => 'right',
    'slider' => array(
        'slides' => array(),
        'timeout' => 8000,
        'speed' => 1000,
        'fx' => 'tileBlind',
    ),
    'navbar_search' => false,
);

if ('' == session_id()) {
	session_start();
}

if ( ! isset($content_width) ) {
    $content_width = 414;
}

require_once(BLN_DIR . '/incl/helper.class.php');

global $bln_help;
$bln_help = new BLN_HELPER();

if (is_admin()) {
    require_once(BLN_DIR . '/incl/admin.class.php');
    global $bln_admin;
    $bln_admin = new BLN_ADMIN();
} else {
    require_once(BLN_DIR . '/incl/phone_menu_walker.class.php');
    require_once(BLN_DIR . '/incl/main_menu_walker.class.php');
    global $bln_help;    
    /**
    * Register front scripts (and styles)
    */
    if (!function_exists('bln_front_script')) {
        function bln_front_script() {
            global $bln_help;
            $options = $bln_help->get_theme_options();
            
            if (is_singular() && get_option('thread_comments')) {
                wp_enqueue_script('comment-reply');
            }
            wp_enqueue_style('bln_bootsrap', BLN_URL . '/bootstrap/css/bootstrap.css');
            wp_enqueue_style('bln_responsive', BLN_URL . '/bootstrap/css/bootstrap-responsive.css', array('bln_bootsrap'));
            
            /**
            * NOTE FOR THEME DEVELOPERS:
            *
            * Use this filter to include your own css in child theme. It needs to be included right after the Bootstrap's css files.
            * Otherwise, some things (such as the main navbar) will be broken under certains conditions.
            */
            $main_css = apply_filters('bln_main_css', BLN_URL . '/css.css');
            wp_enqueue_style('bln_main_css', $main_css, array('bln_responsive'));
            
            wp_enqueue_script('jquery');
            wp_enqueue_script('bln_bootstrap_js', BLN_URL . '/bootstrap/js/bootstrap.min.js', array('jquery'));
            if ($options['slider']['slides'] != array()) {
                wp_enqueue_script('bln_cycle2', BLN_URL . '/scripts/jquery.cycle2.min.js', array('jquery'));
                wp_enqueue_script('bln_cycle2_caption', BLN_URL . '/scripts/jquery.cycle2.caption2.min.js', array('bln_cycle2'));
                wp_enqueue_script('bln_cycle2_tile', BLN_URL . '/scripts/jquery.cycle2.tile.min.js', array('bln_cycle2'));
            }
            wp_enqueue_script('bln_front_js', BLN_URL . '/scripts/front.js', array('jquery'));
        }
    }
    add_action('wp_enqueue_scripts', 'bln_front_script');
    add_action('wp_head', array($bln_help, 'wp_head'));
}

if (!function_exists('bln_front_title')) {
    function bln_front_title() {
     global $page, $paged;
        $title = "";
        if (is_home() || is_front_page()) {
            $blog_desc = get_bloginfo('description');
            $title .= get_bloginfo('name');
            if ($blog_desc) $title .= ' &#171; ' . $blog_desc;
        } else {
            $title .= get_the_title();
            $title .= ' &#171; ' . get_bloginfo('name');
        }
        if ($paged >= 2 || $page>= 2) 
           $title .= ' - ' . sprintf(__('Page %s', BLN_TEXTDOMAIN), max($page, $paged)); 
        return $title;
    }
}
add_filter('wp_title', 'bln_front_title');

if (!function_exists('bln_setup_theme')) {
    function bln_setup_theme() {
        register_nav_menu('top', __('Top Menu', BLN_TEXTDOMAIN));
        register_nav_menu('main', __('Main Menu', BLN_TEXTDOMAIN));
        
        $default_header_args = array(
            'default-image'          => '',
            'width'                  => 0,
            'height'                 => 0,
            'flex-height'            => true,
            'flex-width'             => true,
            'default-text-color'     => '333',
            'header-text'            => true,
            'uploads'                => true,
            'wp-head-callback'       => '',
            'admin-head-callback'    => 'bln_admin_head_cb',
            'admin-preview-callback' => '',
       );
        add_theme_support('custom-header', $default_header_args);
        add_theme_support('custom-background', array(
            'default-color' => '',
            'default-image' => '',
       ));
        add_theme_support('post-thumbnails');
        add_theme_support( 'automatic-feed-links' );
        add_editor_style();
    }
}
add_action('after_setup_theme', 'bln_setup_theme');

if (!function_exists('bln_admin_head_cb')) {
    function bln_admin_head_cb() {
    ?>
    <style type="text/css">
        #name {font-size: 52px;text-decoration: none;}
        #name: hover {text-decoration: underline;}
        #desc {font-style: italic;font-size: 24px;font-weight: normal;}
        #name,#desc {margin: 20px 10px 10px 45px;}
    </style>
    <?php
    }
}

if (!function_exists('bln_widget_init')) {
    function bln_widget_init() {
        global $bln_help;
        $footer_class = $bln_help->horizontal_widget_class('sidebar2');
        register_sidebar(array(
                'name' => __('Main Widget Area', BLN_TEXTDOMAIN),
                'id' => 'sidebar2',
                'description' => __('Appears in the footer section of the site.', BLN_TEXTDOMAIN),
                'before_widget'	=> '<div id="%1$s" class="widget ' . $footer_class . ' %2$s">',
                'after_widget'	=> '</div>',
                'before_title'	=> '<h3 class="widget-title">',
                'after_title'	=> '</h3>'  
               )
        );
        register_sidebar(array(
                'name' => __('Secondary Widget Area', BLN_TEXTDOMAIN),
                'id' => 'sidebar1',
                'description' => __('Appears on posts and pages in the sidebar.', BLN_TEXTDOMAIN),
                'before_widget'	=> '<aside id="%1$s" class="widget well %2$s">',
                'after_widget'	=> '</aside>',
                'before_title'	=> '<h3 class="widget-title">',
                'after_title'	=> '</h3>'  
               )
        );
        $horz_widget_class = $bln_help->horizontal_widget_class('sidebar3');
        register_sidebar(array(
                'name' => __('Alternative Templates Widget Area', BLN_TEXTDOMAIN),
                'id' => 'sidebar3',
                'description' => __('Appears on alternative page templates above the footer.', BLN_TEXTDOMAIN),
                'before_widget'	=> '<div id="%1$s" class="widget horz-widget ' . $horz_widget_class . ' %2$s">',
                'after_widget'	=> '</div>',
                'before_title'	=> '<h3 class="widget-title">',
                'after_title'	=> '</h3>'  
               )
        );
    }
}
add_action('widgets_init', 'bln_widget_init');

if (!function_exists('bln_excerpt_more')) {
    function bln_excerpt_more($more) {
        global $post;
        return '<small><a class="moretag muted" href="'. get_permalink($post->ID) . '">' . __('Continue reading', BLN_TEXTDOMAIN) . ' &raquo;</a></small>';
    }
}
add_filter('excerpt_more', 'bln_excerpt_more');
