<?php
/**
 * The Columnist functions and definitions
 *
 * @package The Columnist
 * @since The Columnist 1.0
 */

/**
 * Set the content width based on the theme's design and stylesheet.
 *
 * @since The Columnist 1.0
 */
if ( ! isset( $content_width ) )
	$content_width = 450; /* pixels */

if ( ! function_exists( 'the_columnist_setup' ) ):
/**
 * Sets up theme defaults and registers support for various WordPress features.
 *
 * @since The Columnist 1.0
 */
function the_columnist_setup() {

	/**
	 * Make theme available for translation
	 * Translations can be filed in the /languages/ directory
	 * If you're building a theme based on _s, use a find and replace
	 * to change 'the_columnist' to the name of your theme in all the template files
	 */
	load_theme_textdomain( 'the_columnist', get_template_directory() . '/languages' );

	/**
	 * This theme styles the visual editor with editor-style.css to match the theme style.
	 */
	add_editor_style();

	/**
	 * Add default posts and comments RSS feed links to head
	 */
	add_theme_support( 'automatic-feed-links' );

	/**
	 * Enable support for Post Thumbnails
	 */
	add_theme_support( 'post-thumbnails' );
	set_post_thumbnail_size( 610, 180, true );
	add_image_size( 'list-thumb', 140, 180, true );
	add_image_size( 'banner-image', 625, 390, true );

	/**
	 * This theme uses wp_nav_menu() in one location.
	 */
	register_nav_menus( array(
		'primary' => __( 'Primary Menu', 'the_columnist' ),
	) );

	// This theme allows users to set a custom background
	add_theme_support( 'custom-background', array( 'default-color' => 'f2f2f2' ) );

}
endif; // the_columnist_setup
add_action( 'after_setup_theme', 'the_columnist_setup' );

/**
 * Sets the post excerpt length to 20 characters.
 */
function the_columnist_excerpt_length( $length ) {
	return 20;
}
add_filter( 'excerpt_length', 'the_columnist_excerpt_length' );

/**
 * Returns a "Continue Reading" link for excerpts
 */
function the_columnist_continue_reading_link() {
	return ' <a class="more" href="'. get_permalink() . '">' . __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'yoko' ) . '</a>';
}

/**
 * Replaces "[...]" (appended to automatically generated excerpts) with an ellipsis and the_columnist_continue_reading_link().
 */
function the_columnist_auto_excerpt_more( $more ) {
	return the_columnist_continue_reading_link();
}
add_filter( 'excerpt_more', 'the_columnist_auto_excerpt_more' );

/**
 * Need to remove the widont filter b/c of limited title space
 */
function the_columnist_wido() {
    remove_filter( 'the_title', 'widont' );
}
add_action( 'init', 'the_columnist_wido' );

/**
 * Register widgetized area and update sidebar with default widgets
 *
 * @since The Columnist 1.0
 */
function the_columnist_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Sidebar 1', 'the_columnist' ),
		'id'            => 'sidebar-1',
		'description'   => __( 'Optional widget area for one column of widgets', 'the_columnist' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h1 class="widget-title">',
		'after_title'   => '</h1>',
	) );

	register_sidebar( array(
		'name'          => __( 'Sidebar 2', 'the_columnist' ),
		'id'            => 'sidebar-2',
		'description'   => __( 'Optional widget area for two columns of widgets', 'the_columnist' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	register_sidebar( array(
		'name'          => __( 'Sidebar 3', 'the_columnist' ),
		'id'            => 'sidebar-3',
		'description'   => __( 'Optional widget area for three columns of widgets', 'the_columnist' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	register_sidebar( array(
		'name'          => __( 'Sidebar 4', 'the_columnist' ),
		'id'            => 'sidebar-4',
		'description'   => __( 'Optional widget area for four columns of widgets', 'the_columnist' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );
}
add_action( 'widgets_init', 'the_columnist_widgets_init' );

/**
 * Enqueue scripts and styles
 */
function the_columnist_scripts() {

	wp_enqueue_style( 'style', get_stylesheet_uri() );

	wp_enqueue_script( 'custom-script', get_template_directory_uri() . '/js/custom-script.js', array( 'jquery' ), false, true );

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) )
		wp_enqueue_script( 'comment-reply' );

	if ( is_singular() && wp_attachment_is_image( get_post()->ID ) )
		wp_enqueue_script( 'keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20120202' );
}
add_action( 'wp_enqueue_scripts', 'the_columnist_scripts' );

/**
 * Count the number of active sidebars to enable dynamic classes
 */
function the_columnist_sidebar_class() {
	$count = 0;

	if ( is_active_sidebar( 'sidebar-1' ) )
		$count++;

	if ( is_active_sidebar( 'sidebar-2' ) )
		$count++;

	if ( is_active_sidebar( 'sidebar-3' ) )
		$count++;

	if ( is_active_sidebar( 'sidebar-4' ) )
		$count++;

	$class = '';

	switch ( $count ) {
		case '1':
			$class = 'one';
			break;
		case '2':
			$class = 'two';
			break;
		case '3':
			$class = 'three';
			break;
		case '4':
			$class = 'four';
			break;
	}

	if ( $class )
		echo 'class="' . $class . '"';
}

/**
 * Implement the Custom Header feature.
 */
require get_template_directory() . '/inc/custom-header.php';

/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/inc/template-tags.php';

/**
 * Custom functions that act independently of the theme templates.
 */
require get_template_directory() . '/inc/tweaks.php';

/**
 * Load Jetpack compatibility file.
 */
if ( file_exists( get_template_directory() . '/inc/jetpack.php' ) )
	require get_template_directory() . '/inc/jetpack.php';