<?php
/**
 * Functions
 *
 * @package WordPress
 * @subpackage Fudo
 * @since Fudo 3.0
 */

/**
 * Set the content width based on the theme's design and stylesheet.
 *
 * Used to set the width of images and content. Should be equal to the width the theme
 * is designed for, generally via the style.css stylesheet.
 */
if ( ! isset( $content_width ) )
	$content_width = 640;

/** Tell WordPress to run fudo_setup() when the 'after_setup_theme' hook is run. */
add_action( 'after_setup_theme', 'fudo_setup' );

if ( ! function_exists( 'fudo_setup' ) ):

/**
 * Sets up theme defaults and registers support for various WordPress features.
 *
 * @since Fudo 2.0
 */
function fudo_setup() {

	// This theme has some pretty cool theme options
	require_once ( get_template_directory() . '/theme-options.php' );

	// This theme styles the visual editor with editor-style.css to match the theme style.
	add_editor_style();

	// This theme uses post thumbnails
	add_theme_support( 'post-thumbnails' );

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

	// Make theme available for translation
	// Translations can be filed in the /languages/ directory
	load_theme_textdomain( 'fudo', TEMPLATEPATH . '/languages' );

	$locale = get_locale();
	$locale_file = TEMPLATEPATH . "/languages/$locale.php";
	if ( is_readable( $locale_file ) )
		require_once( $locale_file );

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

	// This theme allows users to set a custom background
	add_custom_background();

	// Your changeable header business starts here
	define( 'HEADER_TEXTCOLOR', '666' );
	// No CSS, just IMG call. The %s is a placeholder for the theme template directory URI.
	//You can define image for all pages or remove it.
	define( 'HEADER_IMAGE', '%s/images/path.jpg' );

	// The height and width of your custom header. You can hook into the theme's own filters to change these values.
	// Add a filter to fudo_header_image_width and fudo_header_image_height to change these values.
	define( 'HEADER_IMAGE_WIDTH', apply_filters( 'fudo_header_image_width', 960 ) );
	define( 'HEADER_IMAGE_HEIGHT', apply_filters( 'fudo_header_image_height', 300 ) );

	// We'll be using post thumbnails for custom header images on posts and pages.
	// We want them to be 940 pixels wide by 200 pixels tall.
	// Larger images will be auto-cropped to fit, smaller ones will be ignored. See header.php.
	set_post_thumbnail_size( HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, true );

	// Don't support text inside the header image.
	define( 'NO_HEADER_TEXT', true );

	// Add a way for the custom header to be styled in the admin panel that controls
	// custom headers. See fudo_admin_header_style(), below.
	add_custom_image_header( '', 'fudo_admin_header_style' );

	// ... and thus ends the changeable header business.

	// Default custom headers packaged with the theme. %s is a placeholder for the theme template directory URI.
	register_default_headers( array(
		'path' => array(
			'url' => '%s/images/path.jpg',
			'thumbnail_url' => '%s/images/path-thumbnail.jpg',
			'description' => __( 'Path', 'fudo' )
		)
	) );
}
endif;

/**
 * Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
 *
 * @since Fudo 2.0
 */
function fudo_page_menu_args( $args ) {
	$args['show_home'] = true;
	return $args;
}
add_filter( 'wp_page_menu_args', 'fudo_page_menu_args' );

/**
 * Sets the post excerpt length to 40 characters.
 *
 * @since Fudo 2.0
 * @return int
 */
function fudo_excerpt_length( $length ) {
	return 40;
}
add_filter( 'excerpt_length', 'fudo_excerpt_length' );

/**
 * Returns a "Continue Reading" link for excerpts
 *
 * @since Fudo 2.0
 * @return string "Continue Reading" link
 */
function fudo_continue_reading_link() {
	return ' <a href="'. get_permalink() . '">' . __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'fudo' ) . '</a>';
}

/**
 * Replaces "[...]" (appended to automatically generated excerpts) with an ellipsis and fudo_continue_reading_link().
 *
 * @since Fudo 2.0
 * @return string An ellipsis
 */
function fudo_auto_excerpt_more( $more ) {
	return ' &hellip;' . fudo_continue_reading_link();
}
add_filter( 'excerpt_more', 'fudo_auto_excerpt_more' );

/**
 * Adds a pretty "Continue Reading" link to custom post excerpts.
 *
 * @since Fudo 2.0
 * @return string Excerpt with a pretty "Continue Reading" link
 */
function fudo_custom_excerpt_more( $output ) {
	if ( has_excerpt() && ! is_attachment() ) {
		$output .= fudo_continue_reading_link();
	}
	return $output;
}
add_filter( 'get_the_excerpt', 'fudo_custom_excerpt_more' );

/**
 * Remove inline styles printed when the gallery shortcode is used.
 *
 * @since Fudo 2.0
 * @return string The gallery style filter, with the styles themselves removed.
 */
function fudo_remove_gallery_css( $css ) {
	return preg_replace( "#<style type='text/css'>(.*?)</style>#s", '', $css );
}
add_filter( 'gallery_style', 'fudo_remove_gallery_css' );

if ( ! function_exists( 'fudo_comment' ) ) :

/**
 * Template for comments and pingbacks.
 *
 * Used as a callback by wp_list_comments() for displaying the comments.
 *
 * @since Fudo 2.0
 */
function fudo_comment( $comment, $args, $depth ) {
	$GLOBALS['comment'] = $comment;
	switch ( $comment->comment_type ) :
		case '' :
	?>
	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
		<div id="comment-<?php comment_ID(); ?>">
		<div class="comment-author vcard">
			<?php echo get_avatar( $comment, 40 ); ?>
			<?php printf( __( '%s <span class="says">says:</span>', 'fudo' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
		</div><!-- .comment-author .vcard -->
		<?php if ( $comment->comment_approved == '0' ) : ?>
			<em><?php _e( 'Your comment is awaiting moderation.', 'fudo' ); ?></em>
			<br />
		<?php endif; ?>

		<div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
			<?php
				/* translators: 1: date, 2: time */
				printf( __( '%1$s at %2$s', 'fudo' ), get_comment_date(),  get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)', 'fudo' ), ' ' );
			?>
		</div><!-- .comment-meta .commentmetadata -->

		<div class="comment-body"><?php comment_text(); ?></div>

		<div class="reply">
			<?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
		</div><!-- .reply -->
	</div><!-- #comment-##  -->

	<?php
			break;
		case 'pingback'  :
		case 'trackback' :
	?>
	<li class="post pingback">
		<p><?php _e( 'Pingback:', 'fudo' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __('(Edit)', 'fudo'), ' ' ); ?></p>
	<?php
			break;
	endswitch;
}
endif;

/**
 * Register widgetized areas, including two sidebars and four widget-ready columns in the footer.
 *
 * @since Fudo 2.0
 * @uses register_sidebar
 */
function fudo_widgets_init() {

	// Left Sidebar Widget Area

	register_sidebar( array(
		'name' => __( 'Top Left Primary Widget Area', 'fudo' ),
		'id' => 'top-left-primary-widget-area',
		'description' => __( 'The top left primary widget area width 210px', 'fudo' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );
	
	register_sidebar( array(
		'name' => __( 'Top Left Secondary Widget Area', 'fudo' ),
		'id' => 'top-left-secondary-widget-area',
		'description' => __( 'The top left secondary widget area width 90px', 'fudo' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );
	
	register_sidebar( array(
		'name' => __( 'Left Feature Widget Area', 'fudo' ),
		'id' => 'left-feature-widget-area',
		'description' => __( 'The left feature widget area width 310px', 'fudo' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	register_sidebar( array(
		'name' => __( 'Left Primary Widget Area', 'fudo' ),
		'id' => 'left-primary-widget-area',
		'description' => __( 'The left primary widget area width 150px', 'fudo' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	register_sidebar( array(
		'name' => __( 'Left Secondary Widget Area', 'fudo' ),
		'id' => 'left-secondary-widget-area',
		'description' => __( 'The left secondary widget area width 150px', 'fudo' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	// Right Sidebar Widget Area

	register_sidebar( array(
		'name' => __( 'Top Right Primary Widget Area', 'fudo' ),
		'id' => 'top-right-primary-widget-area',
		'description' => __( 'The top right primary widget area width 210px', 'fudo' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );
	
	register_sidebar( array(
		'name' => __( 'Top Right Secondary Widget Area', 'fudo' ),
		'id' => 'top-right-secondary-widget-area',
		'description' => __( 'The top right secondary widget area width 90px', 'fudo' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );
	
	register_sidebar( array(
		'name' => __( 'Right Feature Widget Area', 'fudo' ),
		'id' => 'right-feature-widget-area',
		'description' => __( 'The right feature widget area width 310px', 'fudo' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	register_sidebar( array(
		'name' => __( 'Right Primary Widget Area', 'fudo' ),
		'id' => 'right-primary-widget-area',
		'description' => __( 'The right primary widget area width 150px', 'fudo' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	register_sidebar( array(
		'name' => __( 'Right Secondary Widget Area', 'fudo' ),
		'id' => 'right-secondary-widget-area',
		'description' => __( 'The right secondary widget area width 150px', 'fudo' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	// Footer Widget Area
	
	register_sidebar( array(
		'name' => __( 'First Footer Widget Area', 'fudo' ),
		'id' => 'first-footer-widget-area',
		'description' => __( 'The first footer widget area width 220px', 'fudo' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	register_sidebar( array(
		'name' => __( 'Second Footer Widget Area', 'fudo' ),
		'id' => 'second-footer-widget-area',
		'description' => __( 'The second footer widget area width 220px', 'fudo' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	register_sidebar( array(
		'name' => __( 'Third Footer Widget Area', 'fudo' ),
		'id' => 'third-footer-widget-area',
		'description' => __( 'The third footer widget area width 220px', 'fudo' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	register_sidebar( array(
		'name' => __( 'Fourth Footer Widget Area', 'fudo' ),
		'id' => 'fourth-footer-widget-area',
		'description' => __( 'The fourth footer widget area width 220px', 'fudo' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	register_sidebar( array(
		'name' => __( 'Fifth Footer Widget Area', 'fudo' ),
		'id' => 'fifth-footer-widget-area',
		'description' => __( 'The fifth footer widget area width 50%', 'fudo' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	register_sidebar( array(
		'name' => __( 'Sixth Footer Widget Area', 'fudo' ),
		'id' => 'sixth-footer-widget-area',
		'description' => __( 'The sixth footer widget area width 50%', 'fudo' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	register_sidebar( array(
		'name' => __( 'Seventh Footer Widget Area', 'fudo' ),
		'id' => 'seventh-footer-widget-area',
		'description' => __( 'The seventh footer widget area width 100%', 'fudo' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

}

/** Register sidebars by running fudo_widgets_init() on the widgets_init hook. */
add_action( 'widgets_init', 'fudo_widgets_init' );

/**
 * Removes the default styles that are packaged with the Recent Comments widget.
 *
 * @since Fudo 2.0
 */
function fudo_remove_recent_comments_style() {
	global $wp_widget_factory;
	remove_action( 'wp_head', array( $wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style' ) );
}
add_action( 'widgets_init', 'fudo_remove_recent_comments_style' );

if ( ! function_exists( 'fudo_posted_on' ) ) :

/**
 * Prints HTML with meta information for the current post—date/time and author.
 *
 * @since Fudo 2.0
 */
function fudo_posted_on() {
	printf( __( '<span class="%1$s">Posted on</span> %2$s <span class="meta-sep">by</span> %3$s', 'fudo' ),
		'meta-prep meta-prep-author',
		sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
			get_permalink(),
			esc_attr( get_the_time() ),
			get_the_date()
		),
		sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
			get_author_posts_url( get_the_author_meta( 'ID' ) ),
			sprintf( esc_attr__( 'View all posts by %s', 'fudo' ), get_the_author() ),
			get_the_author()
		)
	);
}
endif;

if ( ! function_exists( 'fudo_posted_in' ) ) :

/**
 * Prints HTML with meta information for the current post (category, tags and permalink).
 *
 * @since Fudo 2.0
 */
function fudo_posted_in() {
	// Retrieves tag list of current post, separated by commas.
	$tag_list = get_the_tag_list( '', ', ' );
	if ( $tag_list ) {
		$posted_in = __( 'This entry was posted in %1$s and tagged %2$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'fudo' );
	} elseif ( is_object_in_taxonomy( get_post_type(), 'category' ) ) {
		$posted_in = __( 'This entry was posted in %1$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'fudo' );
	} else {
		$posted_in = __( 'Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'fudo' );
	}
	// Prints the string, replacing the placeholders.
	printf(
		$posted_in,
		get_the_category_list( ', ' ),
		$tag_list,
		get_permalink(),
		the_title_attribute( 'echo=0' )
	);
}
endif;

/**
 *  Returns the current fudo layout as selected in the theme options
 *
 * @since Fudo 1.0
 */
function fudo_current_layout() {
	$options = get_option( 'fudo_theme_options' );
	$current_layout = $options['theme_layout'];

	return $current_layout;
}

/**
 *  Adds fudo_current_layout() to the array of body classes
 *
 * @since Fudo 2.0
 */
function fudo_body_class($classes) {
	$classes[] = fudo_current_layout();
		
	return $classes;
}
add_filter( 'body_class', 'fudo_body_class' );
