<?php
/**
 * Novus functions and definitions
 *
 * @package Novus
 */

/**
 * Set the content width based on the theme's design and stylesheet.
 */
if ( ! isset( $content_width ) )
	$content_width = get_theme_mod( 'site_width', 600 ) - 30; /* pixels */

if ( ! function_exists( 'novus_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.
 */
function novus_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' );

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

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

	add_theme_support( 'custom-background', array(
		'default-color' => '#f563d8',
		'default-image' => '',
	) );

	/**
	 * Add theme support for Infinite Scroll.
	 * See: http://jetpack.me/support/infinite-scroll/
	 */
	add_theme_support( 'infinite-scroll', array(
		'container' => 'content',
		'footer'    => 'page',
	) );
}
endif; // novus_setup
add_action( 'after_setup_theme', 'novus_setup' );

/**
 * Register widgetized area and update sidebar with default widgets
 */
function novus_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Footer', 'novus' ),
		'id'            => 'sidebar-1',
		'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', 'novus_widgets_init' );

/**
 * Enqueue scripts and styles
 */
function novus_scripts() {
	wp_enqueue_style( 'Novus-style', get_stylesheet_uri() );

	wp_enqueue_script( 'Novus', get_template_directory_uri() . '/js/novus.js', array( 'jquery' ), '1.0', 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( 'Novus-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20120202' );
	}
}
add_action( 'wp_enqueue_scripts', 'novus_scripts' );

function novus_customize_register( $customizer ) {
	$customizer->add_setting( 'logo' );
	$customizer->add_control( new WP_Customize_Image_Control( $customizer, 'logo', array(
		'label' => __( 'Logo', 'novus' ),
		'section' => 'title_tagline',
	) ) );
	$customizer->add_section( 'layouts', array(
		'title' => __( 'Layout', 'novus' ),
		'priority' => 40
	) );
	$customizer->add_setting( 'site_width', array(
		'default' => 600
	) );
	$customizer->add_control( 'site_width', array(
		'label' => __( 'Site width', 'novus' ),
		'section' => 'layouts',
		'type' => 'text'
	) );
}
add_action( 'customize_register', 'novus_customize_register' );

function novus_inline_styles() {

	/* set the logo */
	$logo = get_theme_mod( 'logo' );
	if( '' == $logo ) $logo = trailingslashit( get_template_directory_uri() ) . 'images/logo.png';
	list( $width, $height ) = @getimagesize( $logo );
	$css = ".site-title a { display: block; background: url('{$logo}') no-repeat; overflow: hidden; text-indent: -9999px; width: {$width}px; height: {$height}px; }";

	/* adjust the page shadow position */
	$height += 20;
	$css .= ".site{ background-position: 45px {$height}px }";

	/* use the icons from the admin dashboard for post formats 0-o */
	$css .= '.post-icon { background-image: url("' . admin_url( 'images/post-formats32.png' ) . '"); }';

	$css .= '.site { width: ' . get_theme_mod( 'site_width', 600 ) . 'px; }';

	echo '<style>' . $css . '</style>';
}
add_action( 'wp_head', 'novus_inline_styles' );

/**
 * Returns number of widgets in a widget position
 *
 * @since 1.0
 * @return int
 */
function novus_position_count( $position ) {
	$sidebar_widgets = wp_get_sidebars_widgets();
	if( isset( $sidebar_widgets[$position] ) ) {
		return count( $sidebar_widgets[$position] );
	}
	return 0;
}