<?php

// Set up the content width value based on the theme's design and stylesheet.	
if ( ! isset( $content_width ) ) $content_width = 700;


/**
 * grassy setup.
 *
 * Sets up theme defaults and registers the various WordPress features that
 * grassy supports.
 *
 * @uses load_theme_textdomain() For translation/localization support.
 * @uses add_theme_support() To add support for post thumbnails, automatic feed links,
 * 	custom background, and post formats.
 * @uses register_nav_menu() To add support for navigation menus.
 * @uses set_post_thumbnail_size() To set a custom post thumbnail size.
 *
 * @since grassy 1.2
 */
 
function grassy_setup() {
	/*
	 * Makes grassy available for translation.
	 *
	 * Translations can be added to the /languages/ directory.
	 * If you're building a theme based on grassy, use a find and replace
	 * to change 'grassy' to the name of your theme in all the template files.
	 */

	// Adds RSS feed links to <head> for posts and comments.
	add_theme_support( 'automatic-feed-links' );

	// This theme supports a variety of post formats.
	add_theme_support( 'post-formats', array( 'aside', 'image', 'link', 'quote', 'status' ) );

	/*
	 * This theme supports custom background color and image,
	 * and here we also set up the default background color.
	 */
	add_theme_support( 'custom-background', array(
		'default-color' => '84C680',
	) );
	$args = array(
    'header-text'           => false,
    'default-text-color'        => '000',
    'width'             => 360,
    'height'            => 150,
    'random-default'        => false,
	'uploads'       => true,

);
    add_theme_support( 'custom-header', $args );
	// This theme uses a custom image size for featured images, displayed on "standard" posts.
	add_theme_support( 'post-thumbnails' );
	set_post_thumbnail_size( 200, 200 ); // Unlimited height, soft crop
}
add_action( 'after_setup_theme', 'grassy_setup' );
 
 
/* This code for theme options */

add_filter( 'ot_show_pages', '__return_false' );
add_filter( 'ot_show_new_layout', '__return_false' );
add_filter( 'ot_theme_mode', '__return_true' );
load_template( get_template_directory() . '/option-tree/ot-loader.php' );
load_template( get_template_directory() . '/includes/theme-options.php' );

	
	
		//Register Navigations
add_action( 'init', 'grassy_my_custom_menus' );
function grassy_my_custom_menus() {
    register_nav_menus(
        array(
			'primary-menu' => __( 'Grassy Primary Menu', 'grassy' ),
        )
    );
}
		function grassy_widget_areas() {	
		
		/*sidebar*/
		register_sidebar(array(
    'name' =>__('Right Hand Sidebar'),
    'id' => 'right-sidebar',
    'description'=>__('right in the sidebar'),
    'before_widget' => '<div id="%1$s" class="widget %2$s">',
    'after_widget' => '</div>',
    'before_title' => '<h1>',
    'after_title' => '</h1>',
));
		
		
		
		/*Footer 1 Widget Area*/
		
	register_sidebar( array(
			'name' => __( 'Footer 1', 'grassy' ),
			'id' => 'widget1',
			'before_widget' => '<div class="f-widget1">',
	        'after_widget' => '</div>',
	        'before_title' => '<h2>',
	        'after_title' => '</h2>',
	    ) );	
		
		
			/*Footer 2 Widget Area*/
			
	register_sidebar( array(
			'name' => __( 'Footer 2', 'grassy' ),
			'id' => 'widget2',
			'before_widget' => '<div class="f-widget2">',
	        'after_widget' => '</div>',
	        'before_title' => '<h2>',
	        'after_title' => '</h2>',
	    ) );	
		
			/*Footer 3 Widget Area*/
			register_sidebar( array(
			'name' => __( 'Footer 3', 'grassy' ),
			'id' => 'widget3',
			'before_widget' => '<div class="f-widget3">',
	        'after_widget' => '</div>',
	        'before_title' => '<h2>',
	        'after_title' => '</h2>',
	    ) );
		
		
		}
		add_action('widgets_init', 'grassy_widget_areas');
		// function to display number of posts.
function grassy_getPostViews($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 View";
    }
    return $count.' Views';
}

// function to count views.
function grassy_setPostViews($postID) {
    $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);
    }
}


// Add it to a column in WP-Admin
add_filter('manage_posts_columns', 'posts_column_views');
add_action('manage_posts_custom_column', 'posts_custom_column_views',5,2);
function posts_column_views($defaults){
	$defaults['post_views'] = __('Views', 'grassy');
    return $defaults;
}
function posts_custom_column_views($column_name, $id){
	if($column_name === 'post_views'){
        echo getPostViews(get_the_ID());
    }
}

    /* This code for Featured Image Support */


add_image_size( 'post-image', 120, 120, true );
add_image_size( 'grassy-related-image', 75, 75, true );
	
	function grassy_numeric_posts_nav() {

	if( is_singular() )
		return;

	global $wp_query;

	/** Stop execution if there's only 1 page */
	if( $wp_query->max_num_pages <= 1 )
		return;

	$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
	$max   = intval( $wp_query->max_num_pages );

	/**	Add current page to the array */
	if ( $paged >= 1 )
		$links[] = $paged;

	/**	Add the pages around the current page to the array */
	if ( $paged >= 3 ) {
		$links[] = $paged - 1;
		$links[] = $paged - 2;
	}

	if ( ( $paged + 2 ) <= $max ) {
		$links[] = $paged + 2;
		$links[] = $paged + 1;
	}

	echo '<div class="navigation"><ul>' . "\n";

	/**	Previous Post Link */
	if ( get_previous_posts_link() )
		printf( '<li>%s</li>' . "\n", get_previous_posts_link() );

	/**	Link to first page, plus ellipses if necessary */
	if ( ! in_array( 1, $links ) ) {
		$class = 1 == $paged ? ' class="active"' : '';

		printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );

		if ( ! in_array( 2, $links ) )
			echo '<li>...</li>';
	}

	/**	Link to current page, plus 2 pages in either direction if necessary */
	sort( $links );
	foreach ( (array) $links as $link ) {
		$class = $paged == $link ? ' class="active"' : '';
		printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
	}

	/**	Link to last page, plus ellipses if necessary */
	if ( ! in_array( $max, $links ) ) {
		if ( ! in_array( $max - 1, $links ) )
			echo '<li>...</li>' . "\n";

		$class = $paged == $max ? ' class="active"' : '';
		printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
	}

	/**	Next Post Link */
	if ( get_next_posts_link() )
		printf( '<li>%s</li>' . "\n", get_next_posts_link() );

	echo '</ul></div>' . "\n";

}
if ( ! function_exists( 'grassy_comment' ) ) :
/**
 * Template for comments and pingbacks.
 *
 * Used as a callback by wp_list_comments() for displaying the comments.
 *
 * @since Grassy 1.0
 */
function grassy_comment( $comment, $args, $depth ) {
    $GLOBALS['comment'] = $comment;
    switch ( $comment->comment_type ) :
        case 'pingback' :
        case 'trackback' :
    ?>
    <li class="post pingback">
        <p><?php _e( 'Pingback:', 'Grassy' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( '(Edit)', 'Grassy' ), ' ' ); ?></p>
    <?php
            break;
        default :
    ?>
    <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
        <article id="comment-<?php comment_ID(); ?>" class="comment">
            <footer>
                <div class="comment-author vcard">
                    <?php echo get_avatar( $comment, 40 ); ?>
                    <?php printf( __( '%s <span class="says">says:</span>', 'Grassy' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
                </div><!-- .comment-author .vcard -->
                <?php if ( $comment->comment_approved == '0' ) : ?>
                    <em><?php _e( 'Your comment is awaiting moderation.', 'Grassy' ); ?></em>
                    <br />
                <?php endif; ?>
 
                <div class="comment-meta commentmetadata">
                    <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>"><time pubdate datetime="<?php comment_time( 'c' ); ?>">
                    <?php
                        /* translators: 1: date, 2: time */
                        printf( __( '%1$s at %2$s', 'Grassy' ), get_comment_date(), get_comment_time() ); ?>
                    </time></a>
                    <?php edit_comment_link( __( '(Edit)', 'Grassy' ), ' ' );
                    ?>
                </div><!-- .comment-meta .commentmetadata -->
            </footer>
 
            <div class="comment-content"><?php comment_text(); ?></div>
 
            <div class="reply">
                <?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
            </div><!-- .reply -->
        </article><!-- #comment-## -->
 
    <?php
            break;
    endswitch;
}
endif; // ends check for grassy_comment()



if ( is_singular() ) wp_enqueue_script( "comment-reply" );




/*  Excerpt length
/* ------------------------------------ */
if ( ! function_exists( 'grassy_excerpt_length' ) ) {

	function grassy_excerpt_length( $length ) {
		return ot_get_option('excerpt-length',$length);
	}
	
}
add_filter( 'excerpt_length', 'grassy_excerpt_length', 999 );

/*  Custom favicon
/* ------------------------------------ */
if ( ! function_exists( 'grassy_favicon' ) ) {

	function grassy_favicon() {
		if ( ot_get_option('favicon') ) {
			echo '<link rel="shortcut icon" href="'.ot_get_option('favicon').'" />'."\n";
		}
	}
	
}
add_filter( 'wp_head', 'grassy_favicon' );

function grassy_enqueue_script() {
	wp_enqueue_style( 'grassy-style',get_stylesheet_directory_uri().'/style.css');	
}
add_action( 'wp_enqueue_scripts', 'grassy_enqueue_script' );

function grassy_wp_title( $title, $sep ) {
	global $paged, $page;

	if ( is_feed() )
		return $title;

	// Add the site name.
	$title .= get_bloginfo( 'name' );

	// Add the site description for the home/front page.
	$site_description = get_bloginfo( 'description', 'display' );
	if ( $site_description && ( is_home() || is_front_page() ) )
		$title = "$title $sep $site_description";

	// Add a page number if necessary.
	if ( $paged >= 2 || $page >= 2 )
		$title = "$title $sep " . sprintf( __( 'Page %s', 'grassy' ), max( $paged, $page ) );

	return $title;
}
add_filter( 'wp_title', 'grassy_wp_title', 10, 2 );
		?>