<?php
/**
 * @package Radcliffe
 */


/**
 * Set the content width based on the theme's design and stylesheet.
 */
if ( ! isset( $content_width ) ) {
	$content_width = 740; /* pixels */
}


if ( ! function_exists( 'radcliffe_setup' ) ) :
/**
 * Sets up theme defaults and registers support for various WordPress features.
 *
 * Note that this function is hooked into the after_setup_theme hook, which
 * runs before the init hook. The init hook is too late for some features, such
 * as indicating support for post thumbnails.
 */
function radcliffe_setup() {
	/*
	 * Make theme available for translation.
	 * Translations can be filed in the /languages/ directory.
	 * If you're building a theme based on radcliffe, use a find and replace
	 * to change 'radcliffe' to the name of your theme in all the template files
	 */
	load_theme_textdomain( 'radcliffe', get_template_directory() . '/languages' );

	// Automatic feed
	add_theme_support( 'automatic-feed-links' );

	/*
	 * Enable support for Post Thumbnails on posts and pages.
	 *
	 * @link http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
	 */
	add_theme_support( 'post-thumbnails' );
		// Featured image sizes for resesponsive display
		add_image_size( 'radcliffe-post-image', 1440, 9999 );

	// Setup the WordPress core custom background feature.
	add_theme_support( 'custom-background', apply_filters( 'radcliffe_custom_background_args', array(
		'default-color' => 'ffffff',
		'default-image' => '',
	) ) );

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

	// Enable support for Post Formats
	add_theme_support( 'post-formats', array( 'aside', 'image', 'video', 'quote', 'link' ) );

	// This theme styles the visual editor with editor-style.css to match the theme style.
	add_editor_style( array( 'inc/editor-style.css', radcliffe_base_font_url(), radcliffe_accent_font_url() ) );

	/*
	 * Switch default core markup for search form, comment form, and comments
	 * to output valid HTML5.
	 */
	add_theme_support( 'html5', array(
		'search-form', 'comment-form', 'comment-list', 'gallery', 'caption'
	) );
}
endif; // radcliffe_setup
add_action( 'after_setup_theme', 'radcliffe_setup' );


/**
 * Register a footer widget areas.
 */
function radcliffe_widget_areas_reg() {
	register_sidebar(array(
		'name'          => __( 'Sidebar 1', 'radcliffe' ),
		'id'            => 'sidebar-1',
		'description'   => __( 'Widgets in this area will be shown in the left column in the footer.', 'radcliffe' ),
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
		'before_widget' => '<aside id="%1$s" class="widget clearfix %2$s"><div class="widget-content">',
		'after_widget'  => '</div></aside>'
	));
	register_sidebar(array(
		'name'          => __( 'Sidebar 2', 'radcliffe' ),
		'id'            => 'sidebar-2',
		'description'   => __( 'Widgets in this area will be shown in the middle column in the footer.', 'radcliffe' ),
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
		'before_widget' => '<aside id="%1$s" class="widget clearfix %2$s"><div class="widget-content">',
		'after_widget'  => '</div></aside>'
	));
	register_sidebar(array(
		'name'          => __( 'Sidebar 3', 'radcliffe' ),
		'id'            => 'sidebar-3',
		'description'   => __( 'Widgets in this area will be shown in the right column in the footer.', 'radcliffe' ),
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
		'before_widget' => '<aside id="%1$s" class="widget clearfix %2$s"><div class="widget-content">',
		'after_widget'  => '</div></aside>'
	));
}
add_action( 'widgets_init', 'radcliffe_widget_areas_reg' );


/**
 * Register Open Sans Google font for Radcliffe
 *
 * @return string
*/
function radcliffe_base_font_url() {
	$font_url = '';
	/*
	 * Translators: If there are characters in your language
	 * that are not supported by Open Sans, translate this to
	 * 'off'. Do not translate into your own language.
	 */
	if ( 'off' !== _x( 'on', 'Open Sans font: on or off', 'radcliffe' ) ) {
		$font_url = add_query_arg( 'family', str_replace( ',', '%2C', 'Open+Sans:400italic,700italic,400,300,600,700,800' ), "https://fonts.googleapis.com/css" );
	}

	return $font_url;
}


/**
 * Register Crimson Text Google font for Radcliffe
 *
 * @return string
*/
function radcliffe_accent_font_url() {
	$font_url = '';
	/*
	 * Translators: If there are characters in your language
	 * that are not supported by Crimson Text, translate this to
	 * 'off'. Do not translate into your own language.
	 */
	if ( 'off' !== _x( 'on', 'Crimson Text font: on or off', 'radcliffe' ) ) {
		$font_url = add_query_arg( 'family', str_replace( ',', '%2C', 'Crimson+Text:400,400italic,700,700italic' ), "https://fonts.googleapis.com/css" );
	}

	return $font_url;
}


/**
 * Register Abril Fatface Google font for Radcliffe
 *
 * @return string
*/
function radcliffe_header_font_url() {
	$font_url = '';
	/*
	 * Translators: If there are characters in your language
	 * that are not supported by Abril Fatface, translate this to
	 * 'off'. Do not translate into your own language.
	 */
	if ( 'off' !== _x( 'on', 'Abril Fatface font: on or off', 'radcliffe' ) ) {
		$font_url = add_query_arg( 'family', 'Abril+Fatface:400', "https://fonts.googleapis.com/css" );
	}

	return $font_url;
}


/**
 * Enqueue scripts and styles.
 */
function radcliffe_scripts() {
	// Add Open Sand font, used in the main stylesheet.
	wp_enqueue_style( 'radcliffe-base', radcliffe_base_font_url(), array(), null );

	// Add Crimson Text font, used in the main stylesheet.
	wp_enqueue_style( 'radcliffe-accent', radcliffe_accent_font_url(), array(), null );

	// Add Abril Fatface font, used in the main stylesheet.
	wp_enqueue_style( 'radcliffe-header', radcliffe_header_font_url(), array(), null );

	// Radcliffe main styles
	wp_enqueue_style( 'radcliffe-style', get_stylesheet_uri() );

	// Add picturefill.js to pollyfill the <picture> element in unsupporting browsers
	wp_enqueue_script( 'radcliffe-picturefill', get_template_directory_uri() . '/js/picturefill.js', array( 'jquery' ), '', true );

	// Add Backstretch.js for featured images
	wp_enqueue_script( 'radcliffe-backstretch', get_template_directory_uri() . '/js/jquery.backstretch.js', array( 'jquery' ), '', true );

	// Radcliffe main scripts
	wp_enqueue_script( 'radcliffe-global', get_template_directory_uri() . '/js/global.js', array( 'jquery' ), '', true );

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'radcliffe_scripts' );


/**
 * Count the number of footer sidebars to enable dynamic classes for the widget container.
 */
function radcliffe_footer_class() {
	$count = 0;

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

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

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

	$class = '';

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

	if ( $class ) {
		echo $class;
	}
}


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


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


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


/**
 * Customizer additions.
 */
require get_template_directory() . '/inc/customizer.php';


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