<?php

// Theme Options

include( 'inc/meta-fields.php' );
include( 'inc/related.php' );
include( 'inc/review.php' );
include( 'inc/super-menu.php' );
include( 'inc/trendingnews-customize.php' );
include( 'inc/live-style.php' );
include( 'inc/trendingnews-help.php' );
include( 'inc/home-slider.php' );
include( 'inc/ticker.php' );


//Theme Javascript Files

function trendingnews_scripts() {
		wp_enqueue_script( 'jquery' );
		wp_enqueue_script('flexslider', get_template_directory_uri() . '/js/jquery.flexslider-min.js', array('jquery'));
		wp_enqueue_script('trendingnews', get_template_directory_uri() . '/js/tn-scripts.js', array('jquery'));
		wp_enqueue_script('respond', get_template_directory_uri() . '/js/respond.min.js', array('jquery'));	
}
add_action('wp_enqueue_scripts', 'trendingnews_scripts');


// Add RSS links to <head> section

add_theme_support( 'automatic-feed-links' );

//Content Width

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

//Background enable

$args = array(
	'default-color' => 'ebebeb',
	'default-image' => get_template_directory_uri() . '/images/background.jpg',
);
add_theme_support( 'custom-background', $args );

//Header-image enable

$args = array(
	'width'         => 370,
	'height'        => 75,
	'default-image' => get_template_directory_uri() . '/images/logo.png',
	'header-text'   => false,
	'random-default' => false,

);
add_theme_support( 'custom-header', $args );

//Editor-style

function tn_editor_styles() {
    add_editor_style( 'inc/tn-editor-style.css' );
}
add_action( 'init', 'tn_editor_styles' );

//Post Formats

add_theme_support( 'post-formats', array('video','aside'));

//Rename Post Format

function rename_post_formats( $rename_format ) {
    if ( $rename_format == 'Aside' )
        return 'Review';

    return $rename_format;
}
add_filter( 'esc_html', 'rename_post_formats' );

function live_rename_formats() { 
    global $post;
    if ( $post == 'post-new.php' || $post == 'post.php' ) { ?>
        <script type="text/javascript">
        jQuery('document').ready(function() {
            jQuery("span.post-state-format").each(function() { 
                if ( jQuery(this).text() == "Aside" )
                    jQuery(this).text("Review");             
            });
        });      
        </script>
<?php }
}
add_action('admin_head', 'live_rename_formats');

//Widgets and Areas

		//Areas
if ( function_exists('register_sidebar') ) {
	register_sidebar(array(
		'name' => 'Body',
		'before_widget' => '<div class="home-widget"><div class="%2$s">',
		'after_widget' => '</div></div>',
		'before_title' => '<h3><div class="widget-title">',
		'after_title' => '</div></h3>',
	));

	register_sidebar(array(
		'name' => 'Sidebar',
		'before_widget' => '<div class="home-widget"><div class="widget %2$s">',
		'after_widget' => '</div></div>',
		'before_title' => '<h3><div class="widget-title">',
		'after_title' => '</div></h3>',
	));

	register_sidebar(array(
		'name' => 'Footer',
		'before_widget' => '<div class="home-widget"><div class="widget %2$s">',
		'after_widget' => '</div></div>',
		'before_title' => '<h3><div class="widget-title">',
		'after_title' => '</div></h3>',
	));

	register_sidebar(array(
		'name' => 'Category Sidebar',
		'before_widget' => '<div class="home-widget"><div class="widget %2$s">',
		'after_widget' => '</div></div>',
		'before_title' => '<h3><div class="widget-title">',
		'after_title' => '</div></h3>',
	));
	
	register_sidebar(array(
		'name' => 'Post Sidebar',
		'before_widget' => '<div class="home-widget"><div class="widget %2$s">',
		'after_widget' => '</div></div>',
		'before_title' => '<h3><div class="widget-title">',
		'after_title' => '</div></h3>',
	));
	
		register_sidebar(array(
		'name' => 'Page Sidebar',
		'before_widget' => '<div class="home-widget"><div class="widget %2$s">',
		'after_widget' => '</div></div>',
		'before_title' => '<h3><div class="widget-title">',
		'after_title' => '</div></h3>',
	));
}


		//Widgets

include("widgets/feat-category.php");
include("widgets/feat-category-square.php");
include("widgets/Image-featured-category.php");
include("widgets/multiple-categories.php");
include("widgets/big-featured-category.php");
include("widgets/slider.php");
include("widgets/carousel.php");
include("widgets/latest-news.php");
include("widgets/sidebar-latest-news.php");
include("widgets/blog-widget.php");
include("widgets/tabber.php");
include("widgets/video.php");
include("widgets/tv-widget.php");
include("widgets/about-us.php");
include("widgets/thumbnails.php");

		//Widgets style
		
function tn_widgets_style(){
	echo 
"<style type='text/css'>
	div.widget[id*=_tn] .widget-title h4:before {content: '';background: url(".get_template_directory_uri()."/images/stepfox-tiny-logo-widgets.png)no-repeat;width: 16px;height: 16px;float: left;margin-right: 5px;}
	div.widget[id*=_tn] .widget-title h4{color: #0F7BB8;}
</style>"
;}
add_action('admin_print_styles-widgets.php', 'tn_widgets_style');



// Register Custom Menus

function reg_menus() {
	register_nav_menus(
		array(
			'main-menu' =>'Main Menu',
			'top-menu' => 'Top Menu', )
	  	);
	  }

add_action( 'init', 'reg_menus' );

// Register Thumbnails

if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
add_image_size( 'slider-img', 802, 499, true );
add_image_size( 'solo-slider-img', 636, 395, true );
add_image_size( 'slider-thumb', 165, 99, true );
add_image_size( 'feat-cat-thumb', 305, 191, true );
add_image_size( 'small-thumb', 75, 75, true );
add_image_size( 'tiny-thumb', 30, 30, true );
add_image_size( 'carousel-thumb', 141, 141, true );
add_image_size( 'image-featured', 305, 185, true );
add_image_size( 'tv-widget-image', 110, 110, true );
add_image_size( 'review-img', 230, 160, true );

}


// Excerpt Limit

function excerpt($limit) {
  $excerpt = explode(' ', get_the_excerpt(), $limit);
  if (count($excerpt)>=$limit) {
    array_pop($excerpt);
    $excerpt = implode(' ',$excerpt).'...';  
  } elseif ( strpos( get_the_excerpt(), 'more-link' ) === false ) {
  	$excerpt = implode(' ',$excerpt).'...<a href="'.get_permalink().'">Read More</a>';  
  } else {
    $excerpt = implode(' ',$excerpt);
  }
  $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
  return $excerpt;
}

//Page view counter

function count_views($postID) {
	if (is_single()) {
		$count_key = 'post_views_count';
		$count = get_post_meta($postID, $count_key, true);
		if($count==''){
			$count = 0;
			delete_post_meta($postID, $count_key);
			add_post_meta($postID, $count_key, '0');
		}else{
			$count++;
			update_post_meta($postID, $count_key, $count);
		}
	}
}

function get_views($postID) {
		$count_key = 'post_views_count';
		$count = get_post_meta($postID, $count_key, true);
		if($count==''){
			delete_post_meta($postID, $count_key);
			add_post_meta($postID, $count_key, '0');
			return "0";
		}
		return $count;
}
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

// Get the page number

function get_page_number() {
    if ( get_query_var('paged') ) {
        print ' | ' . 'Page ' . get_query_var('paged');
    }
}

function tn_pagination() {
  global $wp_query;
  $big = 999999999; 
  echo paginate_links ( array (
		  'base' => str_replace ( $big, '%#%', get_pagenum_link ( $big ) ),
		  'format' => '?paged=%#%',
		  'current' => max ( 1, get_query_var ( 'paged' ) ),
		  'total' => $wp_query->max_num_pages,
  ) );	
}

function tn_responsive_embed( $the_embed ) {
    return '<div class="video-container">' . $the_embed . '</div>';
}
add_filter( 'embed_oembed_html', 'tn_responsive_embed', 10, 3 );
?>