<?php
/**
 * @package Structure
 */

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

/**
 * Sets up theme defaults and registers support for WordPress features.
 */
if ( ! function_exists( 'structure_setup' ) ):
	function structure_setup() {

		// This theme uses post thumbnails
		add_theme_support( 'post-thumbnails', array( 'post' ) );
		add_image_size( 'structure-small', 440, 240, true ); // Used on the home page for featured posts
		add_image_size( 'structure-medium', 620, 380, true ); // Used on the home page for latest feature post
		add_image_size( 'structure-large', 640, 392, true ); // Used on single pages and posts

		//Make theme available for translation
		load_theme_textdomain( 'structuretheme', get_template_directory() . '/languages' );

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

		// This theme uses wp_nav_menu()
		register_nav_menus( array(
			'primary' => __( 'Primary Navigation', 'structuretheme' ),
		) );

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

		// This theme enables the choice of a custom background
		add_theme_support( 'custom-background' );

	}
endif;
add_action( 'after_setup_theme', 'structure_setup' );

// Add ID and CLASS attributes to the first <ul> occurence in wp_page_menu
// so we can get it to match up to wp_nav_menu() when we fall back to it
function add_menuclass($ulclass) {
	return preg_replace('/<ul>/', '<ul class="ot-menu">', $ulclass, 1);
}
add_filter('wp_page_menu','add_menuclass');

// Get our fallback, wp_page_menu(), to look even more like wp_nav_menu()
function new_menu_args( $args ) {
	$args = $args + array(
		'sort_column' => 'menu_order, post_title',
		'menu_class' => 'navbar',
		'echo' => true,
		'link_before' => '',
		'link_after' => '',
		'show_home' => true,
	);

	return $args;
}
add_filter('wp_page_menu_args', 'new_menu_args');

// Filter the excerpt [...]
function structure_excerpt_more( $more ) {
	return '&nbsp;&hellip; <a href="'. esc_url( get_permalink() ) . '">' . __('Continue reading <span class="meta-nav">&rarr;</span>', 'structuretheme') . '</a>';
}
add_filter( 'excerpt_more', 'structure_excerpt_more' );

// Add theme scripts
function structure_scripts() {
	wp_enqueue_style( 'structure', get_stylesheet_uri() );

	if ( st_option( 'dark_scheme' ) )
		wp_enqueue_style('dark', get_template_directory_uri() . '/dark.css');

	wp_enqueue_script( 'structure-superfish', get_template_directory_uri() . '/js/superfish/superfish.js', array( 'jquery' ), '201300418', true );
	wp_enqueue_script( 'structure-hoverintent', get_template_directory_uri() . '/js/superfish/hoverIntent.js', array( 'jquery' ), '201300418', true );
	wp_register_script('iepngfix', get_template_directory_uri() . '/js/iepngfix_tilebg.js');

	wp_enqueue_script('iepngfix');
}
add_action( 'wp_enqueue_scripts', 'structure_scripts' );

// enable threaded comments
function enable_threaded_comments(){
	if (!is_admin()) {
		if (is_singular() AND comments_open() AND (get_option('thread_comments') == 1))
			wp_enqueue_script('comment-reply');
		}
}
add_action('get_header', 'enable_threaded_comments');

//Turn a category ID to a Name
function cat_id_to_name($id) {
	foreach((array)(get_categories()) as $category) {
		if ($id == $category->cat_ID) { return $category->cat_name; break; }
	}
}

//	Pull theme options from database
function st_option($key) {
	$option = get_option( 'structure-options' );

	if( isset( $option[$key] ) ) :
		return $option[$key];
	else :
		return FALSE;
	endif;
}


if ( function_exists('register_sidebars') )
	register_sidebar(array('name'=>'Right Sidebar','before_title'=>'<h4>','after_title'=>'</h4>'));
	register_sidebar(array('name'=>'Left Sidebar','before_title'=>'<h4>','after_title'=>'</h4>'));
	register_sidebar(array('name'=>'Homepage Top Right','before_title'=>'<h4 class="widget-title">','after_title'=>'</h4>'));
	register_sidebar(array('name'=>'Footer Left','before_title'=>'<h4>','after_title'=>'</h4>'));
	register_sidebar(array('name'=>'Footer Mid Left','before_title'=>'<h4>','after_title'=>'</h4>'));
	register_sidebar(array('name'=>'Footer Mid Right','before_title'=>'<h4>','after_title'=>'</h4>'));
	register_sidebar(array('name'=>'Footer Right','before_title'=>'<h4>','after_title'=>'</h4>'));

//	Use a div ID, not CLASS, for wp_page_menu
function menu_class_to_div($menu) {
	$menu = str_replace('<div class', '<div id', $menu);

	return $menu;
}
add_filter('wp_page_menu', 'menu_class_to_div');

function structuretheme_list_pages($output) {
	$include_pages = st_option('include_pages');
	if(in_array('feed', (array)$include_pages))
		$output .= '<li class="feed"><a href="'.get_bloginfo('rss2_url').'">RSS Feed</a></li>';

	return $output;
}
add_filter('wp_list_pages', 'structuretheme_list_pages');

// Get the URL of the next image in the gallery
function theme_get_next_attachment_url() {
	$k = null;
	$attachments = [];
	global $post;
	$post = get_post($post);

	if ( $post->post_parent ) {
		$attachments = array_values(get_children( array('post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') ));

		foreach ( $attachments as $k => $attachment )
			if ( $attachment->ID == $post->ID )
				break;

		$k = $k + 1;
	}

	if ( isset($attachments[$k]) ) {
		return get_attachment_link($attachments[$k]->ID);
	} else {
		return get_permalink($post->post_parent);
	}
}

/**
 * Filters wp_title to print a neat <title> tag based on what is being viewed.
 *
 * @since Structure 1.0
 */
function structure_wp_title( $title, $sep ) {
	global $page, $paged;

	if ( is_feed() )
		return $title;

	// Add the blog name
	$title .= get_bloginfo( 'name' );

	// Add the blog description for the home/front page.
	$site_description = get_bloginfo( 'description', 'display' );
	if ( $site_description && ( is_home() || is_front_page() ) )
		$title .= " $sep $site_description";

	// Add a page number if necessary:
	if ( $paged >= 2 || $page >= 2 )
		$title .= " $sep " . sprintf( __( 'Page %s', 'structure' ), max( $paged, $page ) );

	return $title;
}
add_filter( 'wp_title', 'structure_wp_title', 10, 2 );

// include the theme options
require get_template_directory() . '/includes/theme-options.php';

// Grab the custom header bits
require get_template_directory() . '/includes/wpcom-header.php';