<?php
/**
 * functions and definitions
 *
 * @package doo
 */

/**
 * 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 post thumbnails.
 */
function doo_theme_setup(){
	global $content_width;
	if(!isset($content_width)){$content_width = 780;}
	
	// Add default posts and comments RSS feed links to head.
	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'title-tag' );
	
	//Style the Tiny MCE editor
	add_editor_style();
	
	/*
	 * Make theme available for translation.
	 * Translations can be filed in the /languages/ directory.
	 * If you're building a theme based on Madre, use a find and replace
	 * to change 'doo' to the name of your theme in all the template files
	 */
	load_theme_textdomain('doo', get_template_directory() . '/languages' );
}
add_action('after_setup_theme', 'doo_theme_setup');

function doo_theme_scripts(){
	wp_enqueue_style( 'doo_style', get_stylesheet_uri(), array() );
	wp_enqueue_style( 'doo_font-awesome', get_template_directory_uri() . '/font-awesome/css/font-awesome.min.css', array() );
	wp_enqueue_style( 'doo_google-fonts', '//fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic,700,700italic', array() );
	
	wp_enqueue_script( 'doo_fitvids', get_template_directory_uri() . '/js/jquery.fitvids.js', array('jquery'), '' );
	wp_enqueue_script( 'doo__fitvids_doc_ready', get_template_directory_uri() . '/js/fitvids-doc-ready.js', array( 'jquery' ), '' );
	
	wp_enqueue_script( 'doo_base', get_template_directory_uri() . '/js/base.js',array('jquery'), '' );
	if(is_singular()&&comments_open()&&get_option('thread_comments')){wp_enqueue_script('comment-reply');}
}
add_action('wp_enqueue_scripts','doo_theme_scripts');

function doo_register_menus(){
	register_nav_menus(array('menu' => 'Menu'));
}
add_action('init','doo_register_menus');

function doo_theme_title($title,$sep) {
	global $paged, $page;
	if ( is_feed() )
		return $title;
	$title .= get_bloginfo( 'name', 'display' );
	$site_description = get_bloginfo( 'description', 'display' );
	if ( $site_description && ( is_home() || is_front_page() ) )
		$title = "$title";
	if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() )
		$title = "$title $sep " . sprintf( __( 'Page %s', 'doo' ), max( $paged, $page ) );
	return $title;
}
if ( ! function_exists( '_wp_render_title_tag' ) ) {
	add_filter('wp_title','doo_theme_title',10,2);
}

function doo_theme_widgets() {
	register_sidebar( array(
		'name'          => __( 'Footer', 'doo'),
		'id'            => 'footer',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<div class="widget-title">',
		'after_title'   => '</div>',
	) );
}
add_action( 'widgets_init', 'doo_theme_widgets' );

if(!function_exists('doo_footer_widget_params')):
/**
 * Set the widths of the footer widgets
 *
 * @param $params
 * @return mixed
 * 
 * @filter dynamic_sidebar_params
 */
function doo_footer_widget_params($params){
	// Check that this is the footer
	if($params[0]['id'] != 'footer') return $params;

	$sidebars_widgets = wp_get_sidebars_widgets();
	$count = count($sidebars_widgets[$params[0]['id']]);
	$params[0]['before_widget'] = preg_replace('/\>$/', ' style="width:'.round(100/$count,4).'%" >', $params[0]['before_widget']);

	return $params;
}
endif;
add_filter('dynamic_sidebar_params', 'doo_footer_widget_params');

/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/inc/template-tags.php';

/**
 * Load Jetpack compatibility file.
 */
require get_template_directory() . '/inc/jetpack.php';
?>