<?php
/**
 * @license For the full license information, please view the Licensing folder
 * that was distributed with this source code.
 *
 * @package Woohoo News Theme
 */

// Prevent direct script access.
if ( ! defined( 'ABSPATH' ) ) {
	die( 'No direct script access allowed' );
}

$postID = false;
if( empty( $postID ) ){
	$postID = get_the_ID();
}

if( is_category() || is_single() )
{
	$cat_id = '';
	if( is_category() ) $cat_id = get_query_var( 'cat' ) ;

	if( is_single() )
	{
		$categories = get_the_category( get_the_ID() );
		if( !empty( $categories[0]->term_id ) ) $cat_id = $categories[0]->term_id;
	}
	$bdaia_get_cat_meta = get_option( "bd_cat_$cat_id" );
}

$bdaia_get_post_meta          = get_post_meta( get_the_ID(), 'meta_post_options_bd', true );
$post_template_style = "";
if( isset( $bdaia_get_post_meta['post_template_bd'] ) )
	$post_template_style = $bdaia_get_post_meta['post_template_bd'];

elseif( isset( $bdaia_get_cat_meta['bdaia_cat_post_template'] ) )
	$post_template_style = $bdaia_get_cat_meta['bdaia_cat_post_template'];

if( $post_template_style == '' ) $post_template_style = woohoo_get_option( 'bdaia_post_template' );

if( $post_template_style == 'postStyle3' ) {
	$bd_rel_col_class = "grid-5col";
	$numpost = 5;
}
else {
	$bd_rel_col_class = "grid-3col";
	$numpost    = woohoo_get_option('article_related_numb') ? woohoo_get_option('article_related_numb') : 3;
}

$related_no_load_more = woohoo_get_option( 'bdaia_related_no_load_more' ); ?>

<div class="bdaia-ralated-content bdaia-posts-grid light <?php echo $bd_rel_col_class; ?>" id="content-more-author">
    <ul class="bdaia-posts-grid-list">
        <?php echo woohoo_related_author( 0, get_the_author_meta( 'ID' ), $postID, $numpost ); ?>
    </ul>
    <div class="cfix"></div>

	<?php if( ! $related_no_load_more ) { ?>
	    <div class="bdayh-posts-load-wait">
	        <div class="sk-circle"><div class="sk-circle1 sk-child"></div><div class="sk-circle2 sk-child"></div><div class="sk-circle3 sk-child"></div><div class="sk-circle4 sk-child"></div><div class="sk-circle5 sk-child"></div><div class="sk-circle6 sk-child"></div><div class="sk-circle7 sk-child"></div><div class="sk-circle8 sk-child"></div><div class="sk-circle9 sk-child"></div><div class="sk-circle10 sk-child"></div><div class="sk-circle11 sk-child"></div><div class="sk-circle12 sk-child"></div></div>
	    </div>

	    <div class="bdayh-load-more-btn">
	        <div class="bdaia-grid-loadmore-btn bd-more-btn">
	            <?php
	            woohoo_lang_e( 'Load More By' );

	            $user_id = get_query_var( 'author' );
	            $author_name = get_the_author_meta( 'display_name', $user_id );
	            if( $author_name !== null )
	                echo '&nbsp;' . $author_name;
	            ?>
		        <span class="bdaia-io bdaia-io-angle-down"></span>
	        </div>
	    </div>
	<?php } ?>
</div>

<?php if( ! $related_no_load_more ) { ?>
	<script type="text/javascript">
	    jQuery(document).ready(function($) {
	        jQuery('#content-more-author .bdayh-load-more-btn .bdaia-grid-loadmore-btn').click(function(){
	            woohoo_more_author();
	        });
	    });
	    var _bdAuthorPages = 1;
	    function woohoo_more_author() {
	        _bdAuthorPages+=1;

		    var bd_content = jQuery("#content-more-author ul.bdaia-posts-grid-list");

	        jQuery("#content-more-author .bdayh-posts-load-wait").css("display","block");
	        jQuery("#content-more-author .bdayh-load-more-btn").css("display","none");
	        jQuery.ajax({
	            url : "<?php echo admin_url('admin-ajax.php'); ?>",
	            type : "POST",
	            data : "action=woohoo_related_author_fun&page_no="+_bdAuthorPages+"&user_id=<?php echo get_the_author_meta( 'ID' );?>&post_id=<?php echo $postID ?>&numpost=<?php echo $numpost ?>",
	            success: function(data) {
	                jQuery("#content-more-author .bdayh-posts-load-wait").css("display","none");
	                if (data.trim()!="") {
	                    var content = jQuery(data);
		                bd_content.append(content);
	                    jQuery("#content-more-author .bdayh-load-more-btn").css("display","block");
		                var bd_re   = bd_content.find( '.post-image' );
		                bd_re.addClass( 'bdaia-img-show' );
		                i_refresh.refresh();
	                }
	            }
	        }, 'html');
	        return false;
	    }
	</script>
<?php } ?>