<?php

// Theme Options

include( 'inc/meta-fields.php' );
include( 'inc/related.php' );
include( 'inc/review.php' );
include( 'inc/super-menu.php' );
include( 'inc/t1t-customize.php' );
include( 'inc/live-style.php' );
include( 'inc/t1t-help.php' );
include( 'inc/widget-presets.php' );
include( 'inc/gallery.php' );
include( 'inc/image-sizes.php' );
include( 'inc/archive-styles.php' );
include( 'inc/header-posts.php' );

//Theme Javascript Files

function titan_scripts() {
		wp_enqueue_script( 'jquery' );
		wp_enqueue_script('jquery-masonry');
		wp_enqueue_script('flexslider', get_template_directory_uri() . '/js/jquery.flexslider-min.js', array('jquery'));
		wp_enqueue_script('titan', get_template_directory_uri() . '/js/t1t-scripts.js', array('jquery'));
		wp_enqueue_script('respond', get_template_directory_uri() . '/js/respond.min.js', array('jquery'));	
		wp_enqueue_script('smoothscroll', get_template_directory_uri() . '/js/smoothscroll.js', array('jquery'));	
}
add_action('wp_enqueue_scripts', 'titan_scripts');


// Title

function t1t_wp_title($title) {
	global $page, $paged, $s;

	// vars
	$the_title = get_bloginfo( 'name', 'display' );
	$site_description = get_bloginfo( 'description', 'display' );
	
	//specific page title
	if (is_home() || is_front_page()) {
		$title = "$the_title - $site_description";
	}elseif( is_single() || is_page() ){
		$title = single_post_title();
	}elseif(is_search()) { 
		$title ="Search results for $s";
	} elseif ( is_404() ){
		$title ="$the_title - not found";
	} elseif(is_archive()){
		if(is_month()){$archive_type=single_month_title();}
		elseif(is_category()){$archive_type=single_cat_title();}
		elseif(is_author()){$archive_type=get_the_author();}
		elseif(is_search()){$archive_type=the_search_query();}
		elseif(is_tag()){$archive_type=single_tag_title();}	
		else{$archive_type ="$the_title";}			
	$title ="$archive_type";	
	} else {$title ="$the_title"; }
	
	if (class_exists('Woocommerce')) {
		if(is_shop()) { 
			$title ="Shop - $the_title";	
	}}
	// Add a page number if necessary:
	if ($paged >= 2 || $page >= 2 ) {
		$title .= " - " . sprintf( __( 'Page %s', '_s' ), max( $paged, $page ) );
	}
	return $title;
}
add_filter( 'wp_title', 't1t_wp_title', 10, 2 );

// Add RSS links to <head> section

add_theme_support( 'automatic-feed-links' );

//Content Width

if ( ! isset( $content_width ) ){ $content_width = ((get_option('t1t_site_width') - 30) * 0.7) - 30; }

//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'         => 367,
	'height'        => 76,
	'default-image' => get_template_directory_uri() . '/images/logo.png',
	'header-text'   => false,
	'random-default' => false,

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

//Editor-style

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

//Post Formats

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

//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' => 'Homepage',
		'id' => 'Homepage',
		'before_widget' => '<div class="home-widget"><div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div></div>',
		'before_title' => '<div class="widget-title">',
		'after_title' => '</div>',
	));	
	

	
	register_sidebar(array(
		'name' => 'Category Sidebar',
		'id' => 'catsidebar',
		'before_widget' => '<div class="home-widget"><div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div></div>',
		'before_title' => '<div class="widget-title">',
		'after_title' => '</div>',
	));
	
	register_sidebar(array(
		'name' => 'Post Sidebar',
		'id' => 'postsidebar',
		'before_widget' => '<div class="home-widget"><div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div></div>',
		'before_title' => '<div class="widget-title">',
		'after_title' => '</div>',
	));
	
	register_sidebar(array(
		'name' => 'Page Sidebar',
		'id' => 'pagesidebar',		
		'before_widget' => '<div class="home-widget"><div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div></div>',
		'before_title' => '<h3><div class="widget-title">',
		'after_title' => '</div></h3>',
	));
	
	if (class_exists('Woocommerce')) {		
	register_sidebar(array(
		'name' => 'Woocommerce Sidebar',
		'id' => 'woocommercesidebar',		
		'before_widget' => '<div class="home-widget"><div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div></div>',
		'before_title' => '<h3><div class="widget-title">',
		'after_title' => '</div></h3>',
	));
	}
	
	if(function_exists('bp_is_active')){		
	register_sidebar(array(
		'name' => 'Buddypress Sidebar',
		'id' => 'bpsidebar',		
		'before_widget' => '<div class="home-widget"><div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div></div>',
		'before_title' => '<h3><div class="widget-title">',
		'after_title' => '</div></h3>',
	));
	}
	if ( class_exists('bbPress') ) {
	register_sidebar(array(
		'name' => 'bbPress Sidebar',
		'id' => 'bbpress_sidebar',		
		'before_widget' => '<div class="home-widget"><div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div></div>',
		'before_title' => '<h3><div class="widget-title">',
		'after_title' => '</div></h3>',
	));		
	}
	
	
}

// Dynamic Widget area for alternative homepage	
function alternative_homepage(){
if ( function_exists('register_sidebar') ) {
	$pageposts = get_posts(array('post_type' => 'page', 'post_status' => 'publish', 'meta_query' => array(array('key' => '_wp_page_template', 'value' => 'alternative homepage.php')),));
	foreach ( $pageposts as $q ){
	$id = 'sidebar-'.$q->ID;
	$page_title = 'page-'.$q->post_title;
	if ($page_title && function_exists('register_sidebar')){
	register_sidebar(array(
		'id' => $id, 
		'name' => $page_title ,
		'before_widget' => '<div class="home-widget"><div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div></div>',
		'before_title' => '<div class="widget-title">',
		'after_title' => '</div>',
	));
	}
	}
}
}
add_action( 'after_setup_theme', 'alternative_homepage' );

		//Widgets

include("widgets/small-featured-images.php");
include("widgets/big-featured-images.php");
include("widgets/featured-categories.php");
include("widgets/blogroll1.php");
include("widgets/slider.php");
include("widgets/carousel.php");
include("widgets/jumping-posts.php");
include("widgets/blogroll2.php");
include("widgets/tabber.php");
include("widgets/video.php");
include("widgets/tv-widget.php");
include("widgets/about-us.php");
include("widgets/ad-widget.php");
include("widgets/thumbnails.php");
include("widgets/most-commented.php");
include("widgets/ticker.php");
include("widgets/super-slider.php");
include("widgets/video-list-ajax.php");
include("widgets/shortcode-widget.php");
include("widgets/trending-posts.php");
include("widgets/social-widget.php");

		//Widgets style
		
function t1t_widgets_style(){
	echo 
"<style type='text/css'>
	div.widget[id*=_t1t] .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*=_t1t] .widget-title h4{color: #0F7BB8;}
	div.widget[id*=_t1t] input[type=radio]{height:30px;border-radius:0;width:22%;margin: 0 1% 0 0;text-indent:0;font-size:12px;line-height:30px;color:#747474;font-weight:700;font-family: Open Sans;background-color: #D1D1D1;text-shadow: 1px 1px 0px #FFF;box-shadow: inset 1px 1px 1px #AAA;}
	div.widget[id*=_t1t] input[type=radio]:checked:before{border-radius:0;padding:0;margin:0;height:100%;width:100%;background-color: #0DA000;text-indent:0;font-size:12px;line-height:30px;color:#FFF;font-weight:700;font-family: Open Sans;text-shadow: 1px 1px 0px #000;box-shadow: none;}
	div.widget[id*=_t1t] .one-part:before{content: '1/4';}
	div.widget[id*=_t1t] .two-parts:before{content: '2/4';}
	div.widget[id*=_t1t] .three-parts:before{content: '3/4';}
	div.widget[id*=_t1t] .four-parts:before{content: '4/4';}
</style>"
;}
add_action('admin_print_styles-widgets.php', 't1t_widgets_style');



// Register Custom Menus

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

add_action( 'init', 'reg_menus' );




// Excerpt Limit

function excerpt($limit) {
  $excerpt = explode(' ', get_the_excerpt(), $limit);
  if (count($excerpt)>=$limit) {
    array_pop($excerpt);
		if(!empty($excerpt)) {
			$excerpt = implode(' ',$excerpt).'...';
		}else{
			$excerpt = '';
		}
  } elseif ( strpos( get_the_excerpt(), 'more-link' ) === false ) {
  	$excerpt = implode(' ',$excerpt).'...';  
  } else {
    $excerpt = implode(' ',$excerpt);
  }
  $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
  return $excerpt;

}

function t1t_excerpt_length($length) {return 120;}
add_filter('excerpt_length', 't1t_excerpt_length');


//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 t1t_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,
  ) );	
}

// Video Page number of posts
function video_page_queries( $query ) {
	$t1t_tv_widget_style = get_option('t1t_tv_widget_style'); 
	if ($t1t_tv_widget_style == 'one') {
			if(is_tax() && $query->is_main_query()){
      $query->set('posts_per_page', 50);
    }
	}elseif($t1t_tv_widget_style == 'two'||$t1t_tv_widget_style == 'three') {
			if(is_tax() && $query->is_main_query()){
      $query->set('posts_per_page', 8);
    }
	}
  }
add_action( 'pre_get_posts', 'video_page_queries' );

// category archive and search number of posts
function archive_page_queries( $query ) {
	$t1t_category_number = get_option('t1t_category_number'); 
	$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
	$t1t_category_big_post = get_option('t1t_category_big_post');
	
	if (class_exists('Woocommerce')) {
	if(is_archive() && ! is_tax() && $query->is_main_query() && ! is_woocommerce()){
		  $query->set('posts_per_page', $t1t_category_number);
    }}else{
	if(is_archive() && ! is_tax() && ! is_category() && $query->is_main_query()){
		  $query->set('posts_per_page', $t1t_category_number);
    }
	if(is_category() && $query->is_main_query() && $page == 1 && $t1t_category_big_post == 'true'){
			$query->set('posts_per_page', $t1t_category_number);
			$query->set('offset', '1' );	
	}
	}

}
add_action( 'pre_get_posts', 'archive_page_queries' );


//wrap embed
function t1t_responsive_embed( $the_embed ) {
    return '<div class="embed-wrapper">' . $the_embed . '</div>';
}
add_filter( 'embed_oembed_html', 't1t_responsive_embed', 10, 3 );
function t1t_oembed_filter( $return, $data, $url ) {
 	$return = str_replace('frameborder="0"', 'style="border: none"', $return);
	return $return;
}
add_filter('oembed_dataparse', 't1t_oembed_filter', 90, 3 );


//woocommerce
add_theme_support( 'woocommerce' );

function wp_enqueue_woocommerce_style(){
    wp_register_style( 'woocommerce', get_template_directory_uri() . '/inc/woocommerce.css' );
	if ( class_exists( 'woocommerce' ) ) {
		wp_enqueue_style( 'woocommerce' );
	}
}
add_action( 'wp_enqueue_scripts', 'wp_enqueue_woocommerce_style' );

add_filter( 'woocommerce_output_related_products_args', 'jk_related_products_args' );
  function jk_related_products_args( $args ) {
 
	$args['posts_per_page'] = 4; // 4 related products
	$args['columns'] = 4; // arranged in 2 columns
	return $args;
}

remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_upsells', 15 );
 
if ( ! function_exists( 'woocommerce_output_upsells' ) ) {
	function woocommerce_output_upsells() {
	    woocommerce_upsell_display( 4,4 ); // Display 4 products in rows of 4
	}
}

//add_filter('loop_shop_per_page', create_function('$cols', 'return 16;') );


	//buddypress images

	define ( 'BP_AVATAR_THUMB_WIDTH', '75' );
	define ( 'BP_AVATAR_THUMB_HEIGHT', '75' );
	define ( 'BP_AVATAR_FULL_WIDTH', '250' );
	define ( 'BP_AVATAR_FULL_HEIGHT', '250' );
	

?>