<?php
/**
 * @package Marketer
 * @author DigitalCourt
 * @link http://wordpress.org/themes/marketer
 */

define('THEME', 'Marketer');
define('THEME_URI', get_template_directory_uri());
define('THEME_VERSION', 1.0);

require_once('inc/Mobile_Detect.php');
require_once('inc/sidebars.php');
require_once('inc/admin.php');
require_once('translation/translation.php');
require_once('inc/plugins/plugins-included.php');
require_once('inc/customize/customize.php');
require_once('inc/breadcrumbs.php');
require_once('inc/side-navigation.php');

if ( !isset( $content_width ) ) { 
    $content_width = 1200;
}

// we're firing all out initial functions at the start
add_action('after_setup_theme','DS_start', 15);

function DS_start() {
    // enqueue base scripts and styles
    add_action('wp_enqueue_scripts', 'DS_scripts_and_styles', 999);
    // ie conditional wrapper
    add_filter( 'style_loader_tag', 'DS_ie_conditional', 10, 2 );
    // launching this stuff after theme setup
    DS_theme_support();
    // adding sidebars to Wordpress (these are created in functions.php)
    add_action( 'widgets_init', 'DS_register_sidebars' );
    // adding the DS search form (created in functions.php)
    add_filter( 'get_search_form', 'DS_wpsearch' );
    // cleaning up excerpt
    add_filter('excerpt_more', 'DS_excerpt_more');
    // Add editor style
    add_action( 'init', 'ds_add_editor_styles' );
}

//add page excerpt support
add_action( 'init', 'my_add_excerpts_to_pages' );
function my_add_excerpts_to_pages() {
    add_post_type_support( 'page', 'excerpt' );
}

/*********************
SCRIPTS & ENQUEUEING
*********************/

// loading modernizr and jquery, and reply script
function DS_scripts_and_styles() {
  if (!is_admin()) {

    // enqueue main stylesheet
    wp_enqueue_style( 'DS-stylesheet', THEME_URI . '/css/style.css', array(), '', 'all' );

    // ie-only style sheet
    wp_enqueue_style( 'DS-ie-only', THEME_URI . '/css/ie.css', array(), '' );
     
    // comment reply script for threaded comments
    if ( is_singular() AND comments_open() AND (get_option('thread_comments') == 1)) {
        wp_enqueue_script('comment-reply');
    }

    // enqueue scripts
    wp_enqueue_script( 'DS-modernizr', THEME_URI . '/js/modernizr.js', array( 'jquery' ), '2.7.1' );
    wp_enqueue_script( 'scrolltofixed', THEME_URI . '/js/jquery-scrolltofixed.js', array( 'jquery' ), THEME_VERSION );
    wp_enqueue_script( 'rtables-js', THEME_URI . '/js/responsive-tables.js', array( 'jquery' ), THEME_VERSION );
    wp_enqueue_script( 'meanmenu', THEME_URI . '/js/jquery.meanmenu.js', array( 'jquery' ), THEME_VERSION );
    wp_enqueue_script( 'cssua', THEME_URI . '/js/cssua.js', array( 'jquery' ), THEME_VERSION );
    wp_enqueue_script( 'ie', THEME_URI . '/js/ie.js', array( 'jquery' ), THEME_VERSION );
    wp_enqueue_script( 'jquery.textfill', THEME_URI .'/js/jquery.textfill.js', array( 'jquery' ), THEME_VERSION ); 
    wp_enqueue_script( 'jquery.scrollUp', THEME_URI . '/js/jquery.scrollUp.js', array( 'jquery' ), THEME_VERSION );
    wp_enqueue_script( 'fastclick', THEME_URI . '/js/fastclick.js', array( 'jquery' ), THEME_VERSION );    
    wp_enqueue_script( 'DS-js', THEME_URI . '/js/scripts.js', array( 'jquery' ), THEME_VERSION );
 
  }
}

//Touch device fixes 
function browser_styles() {

$return = NULL;
$detect = new Mobile_Detect();

    //drop down menu compatibility for touch
    if($detect->isTablet()){
       $return .= '.cssmenu ul ul ul, .cssmenu > ul > li > ul {display: none;} ';
    }

    if($return) {
        echo '<style type="text/css">' . $return . '</style>';
    }
}

add_action( 'wp_enqueue_scripts', 'browser_styles' );

// adding the conditional wrapper around ie stylesheet
// source: http://code.garyjones.co.uk/ie-conditional-style-sheets-wordpress/
function DS_ie_conditional( $tag, $handle ) {
	if ( 'DS-ie-only' == $handle )
		$tag = '<!--[if lt IE 9]>' . "\n" . $tag . '<![endif]-->' . "\n";
	return $tag;
}

/*********************
THEME SUPPORT
*********************/

// Adding WP 3+ Functions & Theme Support
function DS_theme_support() {

	// wp thumbnails (sizes handled in functions.php)
	add_theme_support('post-thumbnails');

	// default thumb size
	set_post_thumbnail_size(1000, 400, true);

	// wp custom background (thx to @bransonwerner for update)
	add_theme_support( 'custom-background',
	    array(
	    'default-image' => '',  // background image default
	    'default-color' => '', // background color default (dont add the #)
	    'wp-head-callback' => '_custom_background_cb',
	    'admin-head-callback' => '',
	    'admin-preview-callback' => ''
	    )
	);

	// rss thingy
	add_theme_support('automatic-feed-links');

	// registering wp3+ menus
	register_nav_menus(
		array(
			'main-nav' => __( 'The Main Menu', THEME ),   // main nav in header
		)
	);
} /* end DS theme support */

/*********************
MENUS & NAVIGATION
*********************/
function ds_main_nav_fallback() {
    
    wp_page_menu( array(
        'show_home' => true,
        'menu_class' => 'cssmenu'      // adding custom nav class
    ));
}

/*********************
RELATED POSTS FUNCTION
*********************/
function DS_related_posts() {
	echo '<ul id="DS-related-posts">';
	global $post;
	$tags = wp_get_post_tags($post->ID);
	if($tags) {
		foreach($tags as $tag) { $tag_arr .= $tag->slug . ','; }
        $args = array(
        	'tag' => $tag_arr,
        	'numberposts' => 5, /* you can change this to show more */
        	'post__not_in' => array($post->ID)
     	);
        $related_posts = get_posts($args);
        if($related_posts) {
        	foreach ($related_posts as $post) : setup_postdata($post); ?>
	           	<li class="related_post"><a class="entry-unrelated" href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
	        <?php endforeach; }
	    else { ?>
            <?php echo '<li class="no_related_post">No Related Posts Yet!</li>'; ?>
		<?php }
	}
	wp_reset_query();
    
	echo '</ul>';
} 

/*********************
PAGE NAVI
*********************/
function DS_page_navi($before = '', $after = '') {
	global $wpdb, $wp_query;
	$request = $wp_query->request;
	$posts_per_page = intval(get_query_var('posts_per_page'));
	$paged = intval(get_query_var('paged'));
	$numposts = $wp_query->found_posts;
	$max_page = $wp_query->max_num_pages;
	if ( $numposts <= $posts_per_page ) { 
        return; 
    }
	if(empty($paged) || $paged == 0) {
		$paged = 1;
	}
	$pages_to_show = 7;
	$pages_to_show_minus_1 = $pages_to_show-1;
	$half_page_start = floor($pages_to_show_minus_1/2);
	$half_page_end = ceil($pages_to_show_minus_1/2);
	$start_page = $paged - $half_page_start;
	if($start_page <= 0) {
		$start_page = 1;
	}
	$end_page = $paged + $half_page_end;
	if(($end_page - $start_page) != $pages_to_show_minus_1) {
		$end_page = $start_page + $pages_to_show_minus_1;
	}
	if($end_page > $max_page) {
		$start_page = $max_page - $pages_to_show_minus_1;
		$end_page = $max_page;
	}
	if($start_page <= 0) {
		$start_page = 1;
	}
	echo $before.'<nav class="page-navigation"><ol class="DS_page_navi clearfix">'."";
	if ($start_page >= 2 && $pages_to_show < $max_page) {
		$first_page_text = "First";
		echo '<li class="bpn-first-page-link"><a href="'.get_pagenum_link().'" title="'.$first_page_text.'">'.$first_page_text.'</a></li>';
	}
	echo '<li class="bpn-prev-link">';
	previous_posts_link('<<');
	echo '</li>';
	for($i = $start_page; $i  <= $end_page; $i++) {
		if($i == $paged) {
			echo '<li class="bpn-current">'.$i.'</li>';
		} else {
			echo '<li><a href="'.get_pagenum_link($i).'">'.$i.'</a></li>';
		}
	}
	echo '<li class="bpn-next-link">';
	next_posts_link('>>');
	echo '</li>';
	if ($end_page < $max_page) {
		$last_page_text = "Last";
		echo '<li class="bpn-last-page-link"><a href="'.get_pagenum_link($max_page).'" title="'.$last_page_text.'">'.$last_page_text.'</a></li>';
	}
	echo '</ol></nav>'.$after."";	
}

/*********************
RANDOM CLEANUP ITEMS
*********************/
// This adds a button to the Read More link
function DS_excerpt_more($more) {
	global $post;
	// edit here if you like
	return '...'; // <br /><a href="'. get_permalink($post->ID) . '" title="Read More" class="button ds-read-more clearfix">Read More</a>'; //&raquo;
}

/*
 * This is a modified the_author_posts_link() which just returns the link.
 *
 * This is necessary to allow usage of the usual l10n process with printf().
 */
function ds_get_the_author_posts_link() {
        global $authordata;
        if ( !is_object( $authordata ) )
                return false;
        $link = sprintf(
                '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
                get_author_posts_url( $authordata->ID, $authordata->user_nicename ),
                esc_attr( sprintf( __( 'Posts by %s', THEME ), get_the_author() ) ), // No further l10n needed, core will take care of this one
                get_the_author()
        );
        return $link;
}

if ( ! function_exists( 'ds_posted_on' ) ) :
/**
 * Print HTML with meta information for the current post-date/time and author.
 */
function ds_posted_on() {
    // Set up and print post meta information.
    printf( '<span class="entry-date"><i class="genericon genericon-time"></i><a href="%1$s">Posted %2$s ago</a></span>',get_permalink(), 
        human_time_diff( get_the_time('U'), current_time('timestamp') ));
}
endif;

/**
 * Apply theme's stylesheet to the visual editor.
 *
 * @uses add_editor_style() Links a stylesheet to visual editor
 * @uses get_stylesheet_uri() Returns URI of theme stylesheet
 */

function ds_add_editor_styles() {
    add_editor_style( THEME_URI . '/css/style.css' );
}

// Search form with font awesome maginifying glass icon
function DS_wpsearch($form) {
    $form = '<form role="search" method="get" id="searchform" action="' . home_url( '/' ) . '" >
    <div class="input-prepend"><span class="add-on"><a class="fa fa-search"></a></span><input type="text" value="' . get_search_query() . '" name="s" id="searchbox" placeholder="'.esc_attr__('Search...',THEME).'" /> 
    <input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" /> </div>
    </form>';
    return $form;
} 
?>