<?php
/**
 * My Life functions and definitions
 *
 * @package My Life
 * @since My Life 1.0
 */

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

function my_life_content_width() {

	if ( ! is_active_sidebar( 'sidebar-1' ) && ! is_active_sidebar( 'sidebar-2' ) )
		return;

	global $content_width;

	if ( is_active_sidebar( 'sidebar-1' ) && is_active_sidebar( 'sidebar-2' ) )
		$content_width = 469;
	else
		$content_width = 599;
}

add_action( 'template_redirect', 'my_life_content_width' );


if ( ! function_exists( 'my_life_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 post thumbnails.
 *
 * @since My Life 1.0
 */
function my_life_setup() {

	/**
	 * 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/extras.php' );

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

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

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

	/**
	 * Add support for Infinite Scroll
	 */

	add_theme_support( 'infinite-scroll', array(
		'type'           => 'scroll',
		'footer_widgets' => false,
		'container'      => 'content',
		'wrapper'        => true,
		'footer'         => 'main',
	) );

	/**
	 * Enable support for Post Thumbnails
	 */
	add_theme_support( 'post-thumbnails' );
	set_post_thumbnail_size( '100', '100', true );

	/**
	 * This theme uses wp_nav_menu() in one location.
	 */
	register_nav_menus( array(
		'primary' => __( 'Header Top', 'my-life' ),
		'secondary' => __( 'Header Bottom', 'my-life' ),
		'subsidiary' => __( 'Footer', 'my-life' ),
	) );

	/**
	 * Add support for custom backgrounds
	 */
	$my_life_defaults = array(
		'default-color'          => 'f7f6f2',
		'default-image'          => get_template_directory_uri() . '/images/bg.png',
	);
	add_theme_support( 'custom-background', $my_life_defaults );

	/**
	 * Enable support for Post Formats
	 */
	add_theme_support( 'post-formats', array( 'aside', 'image', 'video', 'quote', 'link', 'audio', 'gallery', 'status' ) );
}
endif; // my_life_setup
add_action( 'after_setup_theme', 'my_life_setup' );

/**
 * Register widgetized area and update sidebar with default widgets
 *
 * @since My Life 1.0
 */
function my_life_widgets_init() {
	register_sidebar( array(
		'name' => __( 'Primary Sidebar', 'my-life' ),
		'id' => 'sidebar-1',
		'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' => __( 'Secondary Sidebar', 'my-life' ),
		'id' => 'sidebar-2',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => '</aside>',
		'before_title' => '<h1 class="widget-title">',
		'after_title' => '</h1>',
	) );
}
add_action( 'widgets_init', 'my_life_widgets_init' );


/**
 * If we've set a value of "both" in the Customizer, but removed one sidebar,
 * we need to reset the sidebar placement to "right"
 */

function my_life_reset_sidebar_placement() {

	if ( ! is_active_sidebar( 'sidebar-1' ) || ! is_active_sidebar( 'sidebar-2' ) && 'both' == get_theme_mod( 'sidebar_placement', 'right' ) )
		set_theme_mod( 'sidebar_placement', 'right' );
}
add_action( 'template_redirect', 'my_life_reset_sidebar_placement' );

/**
 * Enqueue scripts and styles
 */
function my_life_scripts() {
	wp_enqueue_style( 'my-life-style', get_stylesheet_uri() );

	wp_enqueue_script( 'small-menu', get_template_directory_uri() . '/js/small-menu.js', array( 'jquery' ), '20120123', true );
	wp_enqueue_script( 'submenu', get_template_directory_uri() . '/js/submenu.js', array( 'jquery' ), '20120123', true );


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

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

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


if ( ! function_exists( 'my_life_breadcrumbs' ) ) :
/**
 * Displays a breadcrumb navigation
*/
function my_life_breadcrumbs() {

	$cat_parents = null;
	if ( !is_home() && !is_front_page() ) {

		$sep = ' &raquo; ';
		$before = '<span class="current">';
		$after = '</span>';

		echo '<a href="' . esc_url( home_url() ) . '">' . __( 'Home', 'my-life' ) . '</a>' . $sep;

		if ( is_category() ) {
			global $wp_query;

			$cat = get_category( $wp_query->get_queried_object()->term_id );
			if ( 0 !==  $cat->parent ) {
				$cat_parents = get_category_parents( get_category( $cat->parent ), true, $sep );
			}

			if ( 0 < $cat->parent && ! is_wp_error( $cat_parents ) )
				echo $cat_parents;

			echo $before . single_cat_title() . $after;

		} elseif ( is_day() ) {
			echo '<a href="' . get_year_link( get_the_time( 'Y' ) ) . '">' . get_the_time( 'Y' ) . '</a>' . $sep;
			echo '<a href="' . get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) . '">' . get_the_time( 'F' ) . '</a>' . $sep;
			echo $before . get_the_time( 'd' ) . $after;

		} elseif ( is_month() ) {
			echo '<a href="' . get_year_link( get_the_time('Y' ) ) . '">' . get_the_time( 'Y' ) . '</a>' . $sep;
			echo $before . get_the_time( 'F' ) . $after;

		} elseif ( is_year() ) {
			echo $before . get_the_time( 'Y' ) . $after;

		} elseif ( is_single() ) {
			if ( is_attachment() ) {
				global $post;
				echo '<a href="' . get_permalink( $post->post_parent ) . '">' . get_the_title( $post->post_parent ) . '</a>' . $sep . $before . get_the_title() . $after;
			} else {
				$cat = get_the_category();
				$cat = $cat[0];

				$cat_parents = get_category_parents( $cat, true, $sep );

				if ( ! is_wp_error( $cat_parents ) )
					echo $cat_parents . $before . get_the_title() . $after;
			}

		} elseif ( is_page() ) {
			global $post;
			if ( $post->post_parent ) {
				$parent_id  = $post->post_parent;
				$parent_links = array();
				while ( $parent_id ) {
					$page = get_page( $parent_id );
					$parent_links[] = '<a href="' . get_permalink( $page->ID ) . '">' . get_the_title( $page->ID ) . '</a>';
					$parent_id  = $page->post_parent;
				}
				echo implode( $sep, array_reverse( $parent_links ) ) . $sep;
			}
			echo $before . get_the_title() . $after;

		} elseif ( is_search() ) {
			echo $before . sprintf( __( 'Search results for &#39;%s&#39;', 'my-life' ), get_search_query() ) . $after;

		} elseif ( is_tag() ) {
			echo $before . sprintf( __( 'Posts tagged &#39;%s&#39;', 'my-life' ), single_tag_title( '', false ) ) . $after;

		} elseif ( is_author() ) {
			global $author;
			echo $before . sprintf( __( 'Articles posted by %s', 'my-life' ), get_userdata( $author )->display_name ) . $after;

		} elseif ( is_404() ) {
			echo $before . __( 'Error 404', 'my-life' ) . $after;
		}

		if ( get_query_var( 'paged' ) ) {
			echo ' (' . sprintf( __( 'Page %s', 'my-life' ), get_query_var( 'paged' ) ) . ')';
		}
	}
}
endif;