<?php
	
	// Enable widget awareness
	if (function_exists('register_sidebar'))
	{
		register_sidebar(array(
			'name' => 'Main Sidebar',
			'id' => 'main-sidebar',
			'before_widget' => '<div class="widget %2$s">',
			'after_widget' => '</div>',
			'before_title' => '<h4>',
			'after_title' => '</h4>',
		));
		
		register_sidebar(array(
			'name' => 'Page Sidebar',
			'id' => 'page-sidebar',
			'before_widget' => '<div class="widget %2$s">',
			'after_widget' => '</div>',
			'before_title' => '<h4>',
			'after_title' => '</h4>',
		));
		
		register_sidebar(array(
			'name' => 'Large Footer',
			'id' => 'large-footer',
			'before_widget' => '<div class="column small widget %2$s">',
			'after_widget' => '</div>',
			'before_title' => '<h5>',
			'after_title' => '</h5>',
		));
	}
				
	// More efficient option delivery
	function serifly_option($option)
	{
		global $seriflyOptions;
	
		if (!isset($seriflyOptions))
		{	
			$seriflyOptions = get_option('serifly_rackhost_theme_options');
		}
		
		if (isset($seriflyOptions[$option]) && $seriflyOptions[$option])
		{
			return $seriflyOptions[$option];
		}
		else
		{
			return false;
		}
	}
		
	// Catch buffer
	function callback($buffer)
	{
		// Replace default line break
		return str_replace('<p>&nbsp;</p>', '<p class="break"></p>', $buffer);
	}
	
	function buffer_start()
	{
		ob_start('callback');
	}
	
	function buffer_end()
	{
		ob_end_flush();
	}
	
	// Highlight first word
	function bold_first($string)
	{
		$stringArray = explode(' ', trim($string));
		
		if (isset($stringArray[0]))
		{
			return str_replace($stringArray[0], '<strong>' . $stringArray[0] . '</strong>', $string);
		}
		else
		{
			return '<strong>' . trim($string) . '</strong>';
		}
	}
	
	// Add buffer functions to hooks
	add_action('wp_head', 'buffer_start');
	add_action('wp_footer', 'buffer_end');
	
	// Handle theme options panel
	require(get_template_directory() . '/admin/admin-init.php');
	
	// Load shortcodes
	require(get_template_directory() . '/functions/theme-shortcodes.php');
	
	// Load custom comment
	require(get_template_directory() . '/functions/custom-comments.php');
	
	// Load custom widgets
	require(get_template_directory() . '/functions/widget-support.php');
	require(get_template_directory() . '/functions/widget-testimonial.php');
	
	// Load custom theme function
	require(get_template_directory() . '/functions/custom-theme.php');
	
	// Activate feed functions
	add_theme_support('automatic-feed-links');
			
	// Load translation domain
	load_theme_textdomain ('serifly');
	
	// Exclude pages from search
	function serifly_exclude_pages($query)
	{
		if ($query->is_search)
		{
			$query->set('post_type', 'post');
		}
		
		return $query;
	}
	
	add_filter('pre_get_posts','serifly_exclude_pages');
	
	// Register navigation
	function register_menu()
	{
		register_nav_menu('primary-menu', __('Primary Menu', 'serifly'));
	}
	
	add_action('init', 'register_menu');
	
	// Allow shortcodes in text widgets
	add_filter('widget_text', 'shortcode_unautop');
	add_filter('widget_text', 'do_shortcode');
    
    // Load required theme scripts
    function theme_scripts()
    {
    	wp_enqueue_script('jquery');
    	wp_register_script('jquery-cookie', get_template_directory_uri() . '/js/jquery.cookie.min.js');
    	wp_enqueue_script('jquery-cookie');
    	wp_register_script('rackhost', get_template_directory_uri() . '/js/rackhost.js');
    	wp_enqueue_script('rackhost');
    }
    
    add_action('wp_print_scripts', 'theme_scripts'); 
    
    // Load required theme styles
    function theme_styles()
    {
    	wp_register_style('rackhost', get_template_directory_uri() . '/style.css');
    	wp_enqueue_style('rackhost');
    }
    
    add_action('wp_print_styles', 'theme_styles'); 
    
    // Navigation description
	function description_in_nav_el($item_output, $item, $depth, $args)
	{    
		return str_replace($item->title . '</a>', bold_first($item->title) . '<br /><span>' . $item->post_content . '</span></a>', $item_output);
	}
	
	add_filter('walker_nav_menu_start_el', 'description_in_nav_el', 10, 4);
       
    // Remove paragraph tags around shortcodes
	remove_filter('the_content', 'wpautop');
	add_filter('the_content', 'wpautop' , 12);
	
	// Set content width
	if (!isset($content_width))
	{
		$content_width = 960;
	}
	
	// Adapt editor
	add_editor_style('css/custom-editor-style.css');
	
?>