<?php

/***** Include Custom Functions *****/ 

require_once('includes/theme-options.php');
require_once('includes/mh-widgets.php');

/***** Set Content Width *****/	

if (!isset($content_width)) $content_width = 622;

/***** Custom Theme Support	*****/

if (function_exists('add_theme_support')) {
    add_theme_support('automatic-feed-links');
    add_theme_support('post-thumbnails');
    add_theme_support('custom-background');
    $header = array(
    	'default-image'	=> get_template_directory_uri() . '/images/logo.png',
    	'width'         => 400,
    	'height'        => 100,
    	'header-text'   => false,
    );
    add_theme_support('custom-header', $header);
}
if (function_exists('add_image_size')) {
	add_image_size('content', 622, 253, true);
	add_image_size('loop', 174, 131, true);
	add_image_size('cp_small', 70, 53, true);
}
if (function_exists('add_editor_style')) {
	add_editor_style();
}

/***** Register Navigation *****/

if (function_exists('register_nav_menus')) {
    register_nav_menus(array('main_nav' => __('Main Navigation', 'mh')));
}

/***** Register Widget Areas / Sidebars	*****/

if (function_exists('register_sidebar')) {
    register_sidebar(array('name' => __('Sidebar', 'mh'), 'id' => 'sidebar', 'description' => __('Sidebar', 'mh'), 'before_widget' => '<div class="sb-widget">', 'after_widget' => '</div>', 'before_title' => '<h6 class="widget-title">', 'after_title' => '</h6>'));
    register_sidebars(2, array('name' => __('Posts %d', 'mh'), 'description' => __('Widget area on posts', 'mh'), 'before_widget' => '<div class="sb-widget">', 'after_widget' => '</div>', 'before_title' => '<h6 class="widget-title">', 'after_title' => '</h6>'));
    register_sidebar(array('name' => __('Footer 1', 'mh'), 'id' => 'footer-1', 'description' => __('Footer widget area', 'mh'), 'before_widget' => '<div class="sb-widget">', 'after_widget' => '</div>', 'before_title' => '<h6 class="widget-title">', 'after_title' => '</h6>'));
    register_sidebar(array('name' => __('Footer 2', 'mh'), 'id' => 'footer-2', 'description' => __('Footer widget area', 'mh'), 'before_widget' => '<div class="sb-widget">', 'after_widget' => '</div>', 'before_title' => '<h6 class="widget-title">', 'after_title' => '</h6>'));
    register_sidebar(array('name' => __('Footer 3', 'mh'), 'id' => 'footer-3', 'description' => __('Footer widget area', 'mh'), 'before_widget' => '<div class="sb-widget">', 'after_widget' => '</div>', 'before_title' => '<h6 class="widget-title">', 'after_title' => '</h6>'));
    register_sidebar(array('name' => __('Footer 4', 'mh'), 'id' => 'footer-4', 'description' => __('Footer widget area', 'mh'), 'before_widget' => '<div class="sb-widget">', 'after_widget' => '</div>', 'before_title' => '<h6 class="widget-title">', 'after_title' => '</h6>'));
}

/***** Custom Excerpts *****/

function custom_excerpt($text = '') {
	$raw_excerpt = $text;
	if ('' == $text) {
		$options = get_option('mh_options'); 
		$custom_length = empty($options['excerpt_length']) ? '45' : $options['excerpt_length'];
		$text = get_the_content('');
		$text = do_shortcode($text);
		$text = apply_filters('the_content', $text);
		$text = str_replace(']]>', ']]>', $text);
		$excerpt_length = apply_filters('excerpt_length', $custom_length);
		$excerpt_more = apply_filters('excerpt_more', ' ' . '...');
		$text = wp_trim_words($text, $excerpt_length, $excerpt_more);
	}
	return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'custom_excerpt');

/***** Custom Comment List *****/

if (!function_exists('mh_comments')) :
	function mh_comments($comment, $args, $depth) {
		$GLOBALS['comment'] = $comment; ?>
		<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
			<div id="comment-<?php comment_ID(); ?>">
				<div class="vcard meta">	
					<?php echo get_avatar($comment->comment_author_email, 30); ?>			
					<?php echo get_comment_author_link() ?> // 
					<a href="<?php echo esc_url(get_comment_link($comment->comment_ID)) ?>"><?php printf(__('%1$s at %2$s', 'mh'), get_comment_date(),  get_comment_time()) ?></a> // 
					<?php if (comments_open() && $args['max_depth']!=$depth) { ?>		
					<?php comment_reply_link(array_merge($args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
					<?php } ?>
					<?php edit_comment_link(__('(Edit)', 'mh'),'  ','') ?>
				</div>
				<?php if ($comment->comment_approved == '0') : ?>
					<div class="comment-info"><?php _e('Your comment is awaiting moderation.', 'mh') ?></div>
				<?php endif; ?>
				<div class="comment-text">	
					<?php comment_text() ?>	
				</div>
			</div>
<?php }
endif;

/***** Custom Comment Fields *****/

function comment_fields($fields) {
$commenter = wp_get_current_commenter();
	$req = get_option('require_name_email');
	$aria_req = ($req ? " aria-required='true'" : '');
	$fields =  array(
		'author'	=>	'<p class="comment-form-author"><label for="author">' . __('Name ', 'mh') . '</label>' . ($req ? '<span class="required">*</span>' : '') . '<br/><input id="author" name="author" type="text" value="' . esc_attr($commenter['comment_author']) . '" size="30"' . $aria_req . ' /></p>',
		'email' 	=>	'<p class="comment-form-email"><label for="email">' . __('Email ', 'mh') . '</label>' . ($req ? '<span class="required">*</span>' : '' ) . '<br/><input id="email" name="email" type="text" value="' . esc_attr($commenter['comment_author_email']) . '" size="30"' . $aria_req . ' /></p>',
		'url' 		=>	'<p class="comment-form-url"><label for="url">' . __('Website', 'mh') . '</label><br/><input id="url" name="url" type="text" value="' . esc_attr($commenter['comment_author_url']) . '" size="30" /></p>'
	);
return $fields;
}
add_filter('comment_form_default_fields', 'comment_fields');

/***** Enable Shortcodes inside Widgets	*****/

add_filter('widget_text', 'do_shortcode');

/***** Load JavaScript *****/

if (!function_exists('mh_scripts')) :
	function mh_scripts() {
		wp_deregister_script('jquery');
		wp_enqueue_script('modernizr', get_template_directory_uri() . '/js/modernizr.js');
		wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js');
		wp_enqueue_script('flexslider', get_template_directory_uri() . '/js/jquery.flexslider-min.js', array('jquery'));
		wp_enqueue_script('custom-js', get_template_directory_uri() . '/js/custom.js', array('jquery'));
		wp_enqueue_script('jquery-powertip', get_template_directory_uri() . '/js/jquery.powertip-1.1.0.min.js', array('jquery'));
		wp_enqueue_script('prettyphoto', get_template_directory_uri() . '/js/jquery.prettyPhoto.js', array('jquery'));
		wp_enqueue_style('prettyphoto', get_template_directory_uri() . '/css/prettyPhoto.css');
	}
endif;
add_action('wp_enqueue_scripts', 'mh_scripts');

/***** Theme Translations *****/

load_theme_textdomain('mh', get_template_directory() . '/languages');

/***** Subheading Meta Box *****/

add_action('add_meta_boxes', 'mh_add_meta_boxes');
add_action('save_post', 'mh_save_meta_boxes', 10, 2 );

function mh_add_meta_boxes() {
	add_meta_box('mh_subheading', __('Subheading', 'mh'), 'mh_subheading_meta_box', 'post', 'normal', 'default');
}
function mh_subheading_meta_box() {
	global $post;
	wp_nonce_field('mh_meta_box_nonce', 'meta_box_nonce'); 
	echo '<p>';
	echo '<label for="mh-subheading">' . _e("Add a subheading to display below post title.", 'mh') . '</label>';
	echo '<br />';
	echo '<input class="widefat" type="text" name="mh-subheading" id="mh-subheading" value="' . esc_attr(get_post_meta($post->ID, 'mh-subheading', true)) . '" size="30" />';
	echo '</p>';
}
function mh_save_meta_boxes($post_id, $post) {
	if (!isset($_POST['meta_box_nonce']) || !wp_verify_nonce($_POST['meta_box_nonce'], 'mh_meta_box_nonce')) {
		return $post->ID;
	}
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post->ID;
    }
	if ('page' == $_POST['post_type']) {
		if (!current_user_can('edit_page', $post_id)) {
			return $post->ID;
		}
	} 
	elseif (!current_user_can('edit_post', $post_id)) {
		return $post->ID;
	}
	$meta_data['mh-subheading'] = $_POST['mh-subheading'];
	foreach ($meta_data as $key => $value) {
		if ($post->post_type == 'revision') return;
		$value = implode(',', (array)$value);
		if (get_post_meta($post->ID, $key, FALSE)) {
			update_post_meta($post->ID, $key, $value);
		} else {
			add_post_meta($post->ID, $key, $value);
		}
		if (!$value) delete_post_meta($post->ID, $key);
	}
}

?>