<?php
// ==================================================
// Get Bread Crumb
// ==================================================
function serpentsoft_single_breadcrumb() {
    if(serpentsoft_get_data('serpentsoft_switch_breadcrumb')):
        global $post;
        $get_meta = get_post_custom($post->ID);
        
        if(isset($get_meta['serpentsoft_post_breadcrumb'][0]) && $get_meta['serpentsoft_post_breadcrumb'][0] == 'hide'): echo '';
        else:
            serpentsoft_breadcrumbs();
        endif;
    else:
        echo '';
    endif;
}

// ==================================================
// Get Post Meta Info
// ==================================================
function serpentsoft_single_meta_info() {
   if(serpentsoft_get_data('serpentsoft_switch_box_meta_info')):
       global $post;
       $get_meta = get_post_custom($post->ID);
       
       $arr_postMeta = serpentsoft_get_data('serpentsoft_article_box_meta_info_elements');
       
       if(isset($get_meta['serpentsoft_post_meta_info'][0]) && $get_meta['serpentsoft_post_meta_info'][0] == 'hide'): echo '<hr />'; else: ?>
            <footer class="post-info">
                <div class="post-meta">
                    <ul>
                        <?php if( $arr_postMeta['author'] ): ?>
                        <li><a href="<?php echo get_author_posts_url( get_the_author_meta('ID') ); ?>" title="<?php echo esc_attr( sprintf ( __( 'View %s\'s posts' , 'serpentsoft' ), get_the_author() ) ); ?>"><i class="bo-icon-pencil"></i><?php the_author(); ?></a></li>
                        <?php endif; ?>

                        <?php if( $arr_postMeta['date'] ): ?>
                        <li title="<?php echo esc_attr( sprintf( __( 'Published on %s at %s' , 'serpentsoft' ), get_the_time(get_option('date_format')), get_the_time(get_option('time_format')) ) ); ?>" itemprop="datePublished"><i class="bo-icon-time"></i><?php esc_html( the_time( get_option('date_format') ) ); ?>, <?php esc_html( the_time( get_option('time_format') ) ); ?></li>
                        <?php endif; ?>
                        
                        <?php if( $arr_postMeta['comments'] ): ?>
                            <?php if (comments_open()) : ?>
                        
                                <li><a href="<?php comments_link(); ?>" itemprop="interactionCount" title="<?php echo esc_attr( sprintf( __('%s Comments', 'serpentsoft'), get_comments_number() ) ); ?>"><i class="bo-icon-comment"></i><?php comments_number(); ?></a></li>
                            <?php endif; ?>
                        <?php endif; ?>

                        <?php if( $arr_postMeta['views'] ): ?>
                        <li itemprop="interactionCount" title="<?php echo esc_attr( sprintf( __('Viewed: %s times', 'serpentsoft'), serpentsoft_get_post_views($post->ID) ) ); ?>"><i class="bo-icon-eye-open"></i><?php echo serpentsoft_get_post_views($post->ID); ?></li>
                        <?php endif; ?>
                        
                        <?php if( $arr_postMeta['tags'] ): ?>
                            <li><?php the_tags('<i class="bo-icon-tags"></i>', ', ', ''); ?></li>
                        <?php endif;  ?>
                        
                    </ul>
                </div>
            </footer>
    <?php endif; else: echo "<hr />"; endif;
}

// ==================================================
// Get Social Share Icons
// ==================================================
function serpentsoft_post_social_share_buttons($is_archive = FALSE) {
    if(!$is_archive):
    if( serpentsoft_get_data('serpentsoft_switch_article_share_box') ):
        global $post;
        $get_meta = get_post_custom($post->ID);
        if (isset($get_meta['serpentsoft_post_share_box'][0]) && $get_meta['serpentsoft_post_share_box'][0] == 'hide'): echo ''; else: ?>
            <footer class="row-fluid social-share-footer">
                <span class="share-icon icon-caret-right"></span>
                <div class="share-icons">
                    <?php $txtShareboxScript = serpentsoft_get_data('serpentsoft_text_social_share_script');
                        if( serpentsoft_get_data('serpentsoft_switch_social_share_script') && !empty($txtShareboxScript) ){ echo $txtShareboxScript; } else { serpentsoft_share_buttons_post(); } ?>
                </div>
            </footer>
<?php endif; else: echo ''; endif; else:
        if( serpentsoft_get_data('serpentsoft_switch_article_share_box') ):
            global $post;
            $get_meta = get_post_custom($post->ID);
            if (isset($get_meta['serpentsoft_post_share_box'][0]) && $get_meta['serpentsoft_post_share_box'][0] == 'hide'): echo ''; else: ?>
                <footer class="row-fluid social-share-footer">
                    <span class="share-icon icon-caret-right"></span>
                    <div class="share-icons"><?php serpentsoft_share_buttons_post(); ?></div>
                </footer>
    <?php endif; else: echo ''; endif;
    endif;
}

// ==================================================
// Get Post Tags
// ==================================================
function serpentsoft_single_tags_box() {
    $output = '';
    if( serpentsoft_get_data('serpentsoft_switch_box_tag') ):
        global $post;
        $get_meta = get_post_custom($post->ID);
        
        if(isset($get_meta['serpentsoft_post_tags'][0]) && $get_meta['serpentsoft_post_tags'][0] == 'hide'): echo ''; else: ?>
            <aside class="row-fluid">
                <h3 class="hidden">Article Tags</h3>

                <div class="article-tags clearfix">
                    <?php //the_tags('<ul class="tags-cloud span12"><li>', '</li><li>', '</li></ul>'); ?>
                    <?php $posttags = get_the_tags();
                        if ($posttags):
                            $output .= '<ul class="tags-cloud span12">';
                        
                            foreach($posttags as $tag):
                                $output .= sprintf('<li><a href="%1$s" class="btn-theme">%2$s<span class="badge">%3$s</span></a></li>', get_tag_link($tag->term_id), $tag->name, $tag->count );
                            endforeach;
                            
                            $output .= '</ul>';
                            
                            echo $output;
                        endif; ?>
                </div>
            </aside>
        <?php endif; // End-of get_meta post tags
    else: echo ''; endif; // End-of switch tags
}

// ==================================================
// Get Author Bio
// ==================================================
function serpentsoft_single_author_bio() {
    if( serpentsoft_get_data('serpentsoft_switch_box_author') ):
        global $post;
        $get_meta = get_post_custom($post->ID);
        
        if(isset($get_meta['serpentsoft_post_author_box'][0]) && $get_meta['serpentsoft_post_author_box'][0] == 'hide'): echo ''; else: ?>

        <?php $srpnt_auth_ID = get_the_author_meta('ID'); ?>

            <aside class="author-bio w-user">
                <div class="cat-widget-title">
                    <h3><a href="<?php echo get_author_posts_url($srpnt_auth_ID); ?>" title="<?php esc_attr_e( sprintf (  __('View %s\'s posts' , 'serpentsoft'), get_the_author_meta('display_name') ) ); ?>"><?php the_author_meta('display_name'); ?></a></h3>
                </div>

                <div class="cat-widget-content">
                    <div class="row-fluid">

                        <div class="media" itemprop="author" itemscope itemtype="http://schema.org/Person">
                            
                            <a href="<?php echo get_author_posts_url($srpnt_auth_ID); ?>" class="media-object span2" 
                               title="<?php esc_attr_e( sprintf ( __('View %s\'s posts' , 'serpentsoft' ), get_the_author_meta('display_name') ) ); ?>">
                                <div class="thumb-effect">

                                    <div class="mask"></div>
                                    <div itemprop="image"><?php echo get_avatar($srpnt_auth_ID); ?></div>
                                </div>
                            </a>

                            <div class="media-body span10">

                                <a itemprop="name" class="hidden" href="<?php echo get_author_posts_url($srpnt_auth_ID); ?>" title="<?php the_author_meta('display_name'); ?>">
                                    
                                    <h4 class="media-heading hidden"><?php the_author_meta('display_name'); ?></h4>
                                </a>

                                <div class="media">
                                    <div itemprop="description">
                                        <?php $auth_desc = get_the_author_meta('description', $srpnt_auth_ID); if( !empty( $auth_desc ) ){ ?>
                                        <p><?php echo get_the_author_meta('description', $srpnt_auth_ID); ?></p>
                                        <?php } else { ?>
                                        <p><?php the_author_meta('display_name'); ?></p>
                                        <?php } ?>
                                    </div>

                                    <?php serpentsoft_author_social_icons_list($srpnt_auth_ID); ?>
                                </div>

                            </div>
                        </div>

                    </div>
                </div>
            </aside>
        
    <?php endif; // End-of get_meta author box
    else: echo ''; endif; // End-of switch author box
}

// ==================================================
// Get Author Social Icons List
// ==================================================
function serpentsoft_author_social_icons_list($userId = false) { ?>
    <ul class="author-personal-info social-networks clearfix">
        <?php if (get_the_author_meta('facebook', $userId)): ?>
            <li><a href="<?php echo get_the_author_meta('facebook', $userId); ?>" class="fb-metro-24px-but" rel="tooltip" data-toggle="tooltip" data-placement="top"
                   data-original-title="<?php echo esc_attr( sprintf( __('%s on facebook', 'serpentsoft'), get_the_author_meta('display_name') ) ); ?>" title=""></a></li>
        <?php endif; ?>

        <?php if (get_the_author_meta('twitter', $userId)): ?>
            <li><a href="<?php echo get_the_author_meta('twitter', $userId); ?>" class="twitter-metro-24px-but" rel="tooltip" data-toggle="tooltip" data-placement="top"
                   data-original-title="<?php echo esc_attr( sprintf( __('%s on twitter', 'serpentsoft'), get_the_author_meta('display_name') ) ); ?>" title=""></a></li>
        <?php endif; ?>

        <?php if (get_the_author_meta('gplus', $userId)): ?>
            <li><a href="<?php echo get_the_author_meta('gplus', $userId); ?>?rel=author" class="googleplus-metro-24px-but" rel="tooltip" data-toggle="tooltip" data-placement="top"
                   data-original-title="<?php echo esc_attr( sprintf( __('%s on google+', 'serpentsoft'), get_the_author_meta('display_name') ) ); ?>" title=""></a></li>
        <?php endif; ?>

        <?php if (get_the_author_meta('pinterest', $userId)): ?>
            <li><a href="<?php echo get_the_author_meta('pinterest', $userId); ?>" class="pinterest-metro-24px-but" rel="tooltip" data-toggle="tooltip" data-placement="top"
                   data-original-title="<?php echo esc_attr( sprintf( __('%s on pinterest', 'serpentsoft'), get_the_author_meta('display_name') ) ); ?>" title=""></a></li>
        <?php endif; ?>

        <?php if (get_the_author_meta('youtube', $userId)): ?>
            <li><a href="<?php echo get_the_author_meta('youtube', $userId); ?>" class="youtube-metro-24px-but" rel="tooltip" data-toggle="tooltip" data-placement="top"
                   data-original-title="<?php echo esc_attr( sprintf( __('%s on youtube', 'serpentsoft'), get_the_author_meta('display_name') ) ); ?>" title=""></a></li>
        <?php endif; ?>

        <?php if (get_the_author_meta('linkedin', $userId)): ?>
            <li><a href="<?php echo get_the_author_meta('linkedin', $userId); ?>" class="linkedin-metro-24px-but" rel="tooltip" data-toggle="tooltip" data-placement="top"
                   data-original-title="<?php echo esc_attr( sprintf( __('%s on linkedin', 'serpentsoft'), get_the_author_meta('display_name') ) ); ?>" title=""></a></li>
        <?php endif; ?>

        <?php if ( serpentsoft_get_data('serpentsoft_switch_author_rss') ): ?>
            <li><a href="<?php echo get_author_feed_link($userId, ''); ?>" class="rss-metro-24px-but" rel="tooltip" data-toggle="tooltip" data-placement="top"
                   data-original-title="<?php echo esc_attr( sprintf( __('%s\'s RSS posts feeds', 'serpentsoft'), get_the_author_meta('display_name') ) ); ?>" title=""></a></li>
        <?php endif; ?>

        <?php //echo sprintf( esc_attr__('%s Comments', 'serpentsoft'), get_comments_number() ); ?>
        
        <?php if (get_the_author_meta('user_email', $userId)): ?>
            <li><a href="mailto:<?php echo get_the_author_meta('user_email', $userId); ?>" class="email-metro-24px-but" rel="tooltip" data-toggle="tooltip" data-placement="top"
                   data-original-title="<?php echo esc_attr( sprintf( __('send an email to %s', 'serpentsoft'), get_the_author_meta('display_name') ) ); ?>" title=""></a></li>
        <?php endif; ?>

        <?php if (get_the_author_meta('user_url', $userId)): ?>
            <li><a href="<?php echo get_the_author_meta('user_url', $userId); ?>" class="website-metro-24px-but" rel="tooltip" data-toggle="tooltip" data-placement="top"
                   data-original-title="<?php echo esc_attr( sprintf( __('visit %s\'s website', 'serpentsoft'), get_the_author_meta('display_name') ) ); ?>" title=""></a></li>
            <?php endif; ?>
    </ul>
<?php }


// ==================================================
// Get Author Social Icons List
// ==================================================
function serpentsoft_login_user_social_icons_list($userId = false) { ?>
    <ul class="author-personal-info social-networks clearfix">
        <?php if (get_the_author_meta('facebook', $userId)): ?>
            <li><a href="<?php echo get_the_author_meta('facebook', $userId); ?>" class="fb-metro-24px-but" rel="tooltip" data-toggle="tooltip" data-placement="top"
                   data-original-title="<?php echo esc_attr( sprintf( __('%s on facebook', 'serpentsoft'), get_the_author_meta('display_name') ) ); ?>" title=""></a></li>
        <?php endif; ?>

        <?php if (get_the_author_meta('twitter', $userId)): ?>
            <li><a href="<?php echo get_the_author_meta('twitter', $userId); ?>" class="twitter-metro-24px-but" rel="tooltip" data-toggle="tooltip" data-placement="top"
                   data-original-title="<?php echo esc_attr( sprintf( __('%s on twitter', 'serpentsoft'), get_the_author_meta('display_name') ) ); ?>" title=""></a></li>
        <?php endif; ?>

        <?php if (get_the_author_meta('gplus', $userId)): ?>
            <li><a href="<?php echo get_the_author_meta('gplus', $userId); ?>" class="googleplus-metro-24px-but" rel="tooltip" data-toggle="tooltip" data-placement="top"
                   data-original-title="<?php echo esc_attr( sprintf( __('%s on google+', 'serpentsoft'), get_the_author_meta('display_name') ) ); ?>" title=""></a></li>
        <?php endif; ?>

        <?php if (get_the_author_meta('pinterest', $userId)): ?>
            <li><a href="<?php echo get_the_author_meta('pinterest', $userId); ?>" class="pinterest-metro-24px-but" rel="tooltip" data-toggle="tooltip" data-placement="top"
                   data-original-title="<?php echo esc_attr( sprintf( __('%s on pinterest', 'serpentsoft'), get_the_author_meta('display_name') ) ); ?>" title=""></a></li>
        <?php endif; ?>

        <?php if (get_the_author_meta('youtube', $userId)): ?>
            <li><a href="<?php echo get_the_author_meta('youtube', $userId); ?>" class="youtube-metro-24px-but" rel="tooltip" data-toggle="tooltip" data-placement="top"
                   data-original-title="<?php echo esc_attr( sprintf( __('%s on youtube', 'serpentsoft'), get_the_author_meta('display_name') ) ); ?>" title=""></a></li>
        <?php endif; ?>

        <?php if (get_the_author_meta('linkedin', $userId)): ?>
            <li><a href="<?php echo get_the_author_meta('linkedin', $userId); ?>" class="linkedin-metro-24px-but" rel="tooltip" data-toggle="tooltip" data-placement="top"
                   data-original-title="<?php echo esc_attr( sprintf( __('%s on linkedin', 'serpentsoft'), get_the_author_meta('display_name') ) ); ?>" title=""></a></li>
        <?php endif; ?>

        <?php if ( serpentsoft_get_data('serpentsoft_switch_author_rss') ): ?>
            <li><a href="<?php echo get_author_feed_link($userId, ''); ?>" class="rss-metro-24px-but" rel="tooltip" data-toggle="tooltip" data-placement="top"
                   data-original-title="<?php echo esc_attr( sprintf( __('%s\'s RSS posts feeds', 'serpentsoft'), get_the_author_meta('display_name') ) ); ?>" title=""></a></li>
        <?php endif; ?>

        <?php //echo sprintf( esc_attr__('%s Comments', 'serpentsoft'), get_comments_number() ); ?>
        
        <?php if (get_the_author_meta('user_email', $userId)): ?>
            <li><a href="mailto:<?php echo get_the_author_meta('user_email', $userId); ?>" class="email-metro-24px-but" rel="tooltip" data-toggle="tooltip" data-placement="top"
                   data-original-title="<?php echo esc_attr( sprintf( __('send an email to %s', 'serpentsoft'), get_the_author_meta('display_name') ) ); ?>" title=""></a></li>
        <?php endif; ?>

        <?php if (get_the_author_meta('user_url', $userId)): ?>
            <li><a href="<?php echo get_the_author_meta('user_url', $userId); ?>" class="website-metro-24px-but" rel="tooltip" data-toggle="tooltip" data-placement="top"
                   data-original-title="<?php echo esc_attr( sprintf( __('visit %s\'s website', 'serpentsoft'), get_the_author_meta('display_name') ) ); ?>" title=""></a></li>
            <?php endif; ?>
    </ul>
<?php }


// ==================================================
// Get Related Posts
// ==================================================
function serpentsoft_single_related_posts() {
    $posts_number = serpentsoft_get_data('serpentsoft_related_articles_no_of_posts');
    
    if( !is_numeric($posts_number) || $posts_number < 0 ) { $posts_number = 2; }
    
    if( serpentsoft_get_data('serpentsoft_switch_related_articles') ):
        global $post;
        $get_meta = get_post_custom($post->ID);
        
        $current_post_id = $post->ID;
        
        if(isset($get_meta['serpentsoft_related_posts'][0]) && $get_meta['serpentsoft_related_posts'][0] == 'hide'): echo ''; else:
        
        $tags = wp_get_post_tags($post->ID);
        if ($tags) {
            $tag_ids = array();
            foreach ($tags as $individual_tag)
                $tag_ids[] = $individual_tag->term_id;

            $args = array(
                'orderby' => 'rand',
                'tag__in' => $tag_ids,
                'post__not_in' => array($post->ID),
                'showposts' => $posts_number, // Number of related posts that will be shown.  
                'ignore_sticky_posts' => 1
            );

            $query = new wp_query($args);
            if ($query->have_posts()):
                $first_slide = 1; ?>

                <aside class="cat-related-news w-related">
                    <div class="cat-widget">
                        <div class="cat-widget-title">
                            <h3><?php _e(serpentsoft_get_data('serpentsoft_related_articles_title'), 'serpentsoft'); ?></h3>
                        </div>

                        <div id="cat_related_slider_<?php echo $current_post_id; ?>" class="cat-widget-content">
                            <div class="row-fluid modern-items-list">
                                <div class="flexslider">
                                    <ul class="slides">
                                        <?php while ($query->have_posts()) : $query->the_post(); ?>

                                            <?php if ($first_slide == 1): ?><li><ul class="items left clearfix"><li class="row-fluid"><?php endif; ?>

                                            <div class="span6">
                                                <article class="fold-item span12">
                                                    <div class="clearfix">
        <?php serpentsoft_grab_post_banner_figure($post->ID, array('class' => 'post-img')); ?>

                                                        <div class="description visible-part">
                                                            <h5 class="title"><?php the_title(); ?></h5>

                                                            <div class="info">
                                                                <span><i class="bo-icon-time bo-icon-white"></i><?php the_time( get_option('date_format') ); ?>, <?php the_time( get_option('time_format') ); ?></span>
                                                                <span><i class="bo-icon-comment bo-icon-white"></i><?php comments_number('0', '1', '%'); ?></span>
                                                                <span><i class="bo-icon-eye-open bo-icon-white"></i><?php echo serpentsoft_get_post_views($post->ID); ?></span>
                                                            </div>

                                                            <p class="text"><?php serpentsoft_cats_GetTheExcerpt(); ?>...</p>
                                                        </div>

                                                        <a href="<?php the_permalink(); ?>" class="more" title="<?php the_title(); ?>"></a>
                                                    </div>
                                                </article>
                                            </div>

                                            <?php if ($first_slide / 2 == 1): $first_slide = 0; ?>            
                                                        </li></ul></li>
                                            <?php endif;
                                                $first_slide++;
                                        endwhile; ?>
                                    </ul>
                                </div>
                            </div>
                        </div>

                    </div> <!-- cat-widget -->
                    
                    <script type="text/javascript">
                        //<![CDATA[
                        jQuery(document).ready(function() {
                            "use strict";
                            var divID = '#cat_related_slider_<?php echo $current_post_id; ?>';
                            var is_exist = jQuery(divID).length > 0 ? jQuery(divID).length : 0;

                           if (is_exist > 0) {
                                jQuery(divID).flexslider({
                                    slideshow: false,
                                    animation: "slide",              //String: Select your animation type, "fade" or "slide"
                                    easing: "swing",                 //{NEW} String: Determines the easing method used in $jqSlider transitions. $jqSlider easing plugin is supported!
                                    direction: "horizontal"            // String: Select the sliding direction, "horizontal" or "vertical"
                                });
                            }
                        });
                        //]]>
                    </script>
                    
                </aside>

                <?php
            endif; // if have posts
        } // tags
        
        endif; // get meta
    else: echo ''; endif; // if related articles ON
}

// ==================================================
// Get Post Navigation
// ==================================================
function serpentsoft_single_navigation() {
    if( serpentsoft_get_data('serpentsoft_switch_box_prev_next') ):
        global $post;
        $get_meta = get_post_custom($post->ID);
        
        if(isset($get_meta['serpentsoft_posts_navigation'][0]) && $get_meta['serpentsoft_posts_navigation'][0] == 'hide'): echo ''; else: ?>

            <nav class="row-fluid post-navigation">
                <h3 class="hidden">Posts Next/Prev</h3>

                <div class="row-fluid">

                    <?php $prev_post = get_previous_post(); if (!empty($prev_post)):
                    $prev_post_title = '';
                    if(isset($prev_post->post_title) && !empty($prev_post->post_title)) { $prev_post_title = htmlspecialchars($prev_post->post_title); } else { $prev_post_title = 'Previous Post'; } ?>
                        <div class="span6 prev-post">
                            <a href="<?php echo get_permalink($prev_post->ID); ?>" class="btn-theme" title="<?php esc_attr_e( sprintf( __('Previous post: %s', 'serpentsoft'), $prev_post_title ) ); ?>" rel="prev">
                            <div class="text-content">
                                <i class="icon-arrow-left"></i>
                                <span><?php echo $prev_post_title; ?></span>
                            </div>
                            </a>
                        </div>
                    <?php endif; ?>

                    <?php $next_post = get_next_post(); if (!empty($next_post)): ?>
                    <?php $next_post_title = '';
                    if(isset($next_post->post_title) && !empty($next_post->post_title)) { $next_post_title = htmlspecialchars($next_post->post_title); } else { $next_post_title = 'Next Post'; } ?>
                        <div class="span6 pull-right next-post">
                            <a href="<?php echo get_permalink($next_post->ID); ?>" class="pull-right btn-theme" title="<?php esc_attr_e( sprintf ( __('Next post: %s', 'serpentsoft'), $next_post_title ) ); ?>" rel="next">
                                <div class="text-content">
                                    <span><?php echo $next_post_title; ?>
                                    </span>
                                    <i class="icon-arrow-right"></i>
                                </div>
                            </a>
                        </div>
                    <?php endif; ?>
                </div>
            </nav>

        <?php endif; // End-of get_meta navigation
    else: echo ''; endif;
}

// ==================================================
// Get Random Post
// ==================================================
add_action('init', 'serpentsoft_random_add_rewrite');
function serpentsoft_random_add_rewrite() {
       global $wp;
       $wp->add_query_var('random');
       add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}

add_action('template_redirect','serpentsoft_random_template');
function serpentsoft_random_template() {
    if (get_query_var('random') == 1) {
        $posts = get_posts('post_type=post&orderby=rand&numberposts=1');
        foreach($posts as $post) {
            $link = get_permalink($post);
        }
        wp_redirect($link, 307);
        exit;
    }
} ?>