<?php
/*
/*-----------------------------------------------*/
/* KENTOOZ FRAMEWORK FOR FREE THEME
/* Website: kentooz.com
/* The Author: Gian Mokhammad Ramadhan 
/* Social network :twitter.com/g14nnakal facebook.com/gianmr
/* Version :1.0
/*-----------------------------------------------*/

// Do not load directly...
if ( ! defined( 'ABSPATH' ) ) { die( 'Direct access forbidden.' ); }

/*******************************************
# Comment navigation on hook system ~ post
*******************************************/
if ( ! function_exists( 'ktz_comment_nav' ) ):
function ktz_comment_nav() {
	global $wp_query; ?>
	<ul class="pager">
		<?php previous_comments_link( __( '<span class="meta-nav">&larr;</span> Older Comments', ktz_theme_textdomain ) ); ?>
		<?php next_comments_link( __( 'Newer Comments <span class="meta-nav">&rarr;</span>', ktz_theme_textdomain ) ); ?>
	</ul>
	<?php
}
endif; 

/*******************************************
# Default navigation on hook system ~ post
*******************************************/
if ( ! function_exists( 'ktz_default_nav' ) ) {
function ktz_default_nav() {
	global $wp_query;
	if ( is_single() ) :
	echo '<div class="btn-group">';
	echo previous_post_link( '<div class="btn btn-small">%link</div>', __( 'Prev', 'Previous post link', ktz_theme_textdomain ) );
	echo next_post_link( '<div class="btn btn-small">%link</div>', __( 'Next', 'Next post link', ktz_theme_textdomain ) );
	echo '</div>';
	elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) :
	echo '<ul class="pager">';
		if ( get_next_posts_link() ) :
		echo '<li>';
		echo next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', ktz_theme_textdomain ) );
		echo '</li>';
		endif;
		if ( get_previous_posts_link() ) :
		echo '<li>';
		echo previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', ktz_theme_textdomain ) );
		echo '</li>';
		endif;
	echo '</ul>';
	endif; 
	}
}


/*******************************************
# Number navigation on hook system ~ post
*******************************************/
function round_num($num, $to_nearest) {
   return floor($num/$to_nearest)*$to_nearest;
}
if ( ! function_exists( 'ktz_pagenavi' ) ):
function ktz_pagenavi($before = '', $after = '') {
    global $wpdb, $wp_query;
    $pagenavi_options = array();
    $pagenavi_options['pages_text'] = __('Page %CURRENT_PAGE% of %TOTAL_PAGES%:',ktz_theme_textdomain);
    $pagenavi_options['current_text'] = __('%PAGE_NUMBER%',ktz_theme_textdomain);
    $pagenavi_options['page_text'] = __('%PAGE_NUMBER%',ktz_theme_textdomain);
    $pagenavi_options['first_text'] = __('First Page',ktz_theme_textdomain);
    $pagenavi_options['last_text'] = __('Last Page',ktz_theme_textdomain);
    $pagenavi_options['dotright_text'] = '';
    $pagenavi_options['dotleft_text'] = '';
    $pagenavi_options['num_pages'] = 5;
    $pagenavi_options['always_show'] = 0;
    $pagenavi_options['num_larger_page_numbers'] = 0;
    $pagenavi_options['larger_page_numbers_multiple'] = 5;
 
    if (!is_single()) {
        $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(empty($paged) || $paged == 0) { $paged = 1; }
 
        $pages_to_show = intval($pagenavi_options['num_pages']);
        $larger_page_to_show = intval($pagenavi_options['num_larger_page_numbers']);
        $larger_page_multiple = intval($pagenavi_options['larger_page_numbers_multiple']);
        $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; }
 
        $larger_per_page = $larger_page_to_show*$larger_page_multiple;
        $larger_start_page_start = (round_num($start_page, 10) + $larger_page_multiple) - $larger_per_page;
        $larger_start_page_end = round_num($start_page, 10) + $larger_page_multiple;
        $larger_end_page_start = round_num($end_page, 10) + $larger_page_multiple;
        $larger_end_page_end = round_num($end_page, 10) + ($larger_per_page);
 
        if($larger_start_page_end - $larger_page_multiple == $start_page) {
            $larger_start_page_start = $larger_start_page_start - $larger_page_multiple;
            $larger_start_page_end = $larger_start_page_end - $larger_page_multiple;
        }
        if($larger_start_page_start <= 0) { $larger_start_page_start = $larger_page_multiple; }
        if($larger_start_page_end > $max_page) { $larger_start_page_end = $max_page; }
        if($larger_end_page_end > $max_page) { $larger_end_page_end = $max_page; }
        if($max_page > 1 || intval($pagenavi_options['always_show']) == 1) {
			echo $before.'<div class="pagination warning"><ul>'."\n";
 
            if ($start_page >= 2 && $pages_to_show < $max_page) {
                $first_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options['first_text']);
                echo '<li class="disable"><a href="'.esc_url(get_pagenum_link()).'" class="first" title="'.$first_page_text.'">1</a></li>';
                if(!empty($pagenavi_options['dotleft_text'])) {
                    echo '<li class="disable">'.$pagenavi_options['dotleft_text'].'</li>';
                }
            }
 
            if($larger_page_to_show > 0 && $larger_start_page_start > 0 && $larger_start_page_end <= $max_page) {
                for($i = $larger_start_page_start; $i < $larger_start_page_end; $i+=$larger_page_multiple) {
                    $page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
                    echo '<li><a href="'.esc_url(get_pagenum_link($i)).'" title="'.$page_text.'">'.$page_text.'</a></li>';
                }
            }
 
            for($i = $start_page; $i  <= $end_page; $i++) {
                if($i == $paged) {
                    $current_page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['current_text']);
					$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
                    echo '<li class="active"><a href="'.esc_url(get_pagenum_link($i)).'" title="'.$page_text.'">'.$current_page_text.'</a></li>';
                } else {
                    $page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
                    echo '<li><a href="'.esc_url(get_pagenum_link($i)).'" title="'.$page_text.'">'.$page_text.'</a></li>';
                }
            }
 
            if ($end_page < $max_page) {
                if(!empty($pagenavi_options['dotright_text'])) {
                    echo '<li class="disable">'.$pagenavi_options['dotright_text'].'</li>';
                }
                $last_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options['last_text']);
                echo '<li class="disable"><a href="'.esc_url(get_pagenum_link($max_page)).'" title="'.$last_page_text.'">'.$max_page.'</a></li>';
            }
 
            if($larger_page_to_show > 0 && $larger_end_page_start < $max_page) {
                for($i = $larger_end_page_start; $i <= $larger_end_page_end; $i+=$larger_page_multiple) {
                    $page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
                    echo '<li><a href="'.esc_url(get_pagenum_link($i)).'" title="'.$page_text.'">'.$page_text.'</a></li>';
                }
            }
            echo '</ul></div>'.$after."\n";
        }
    }
}
endif; 

/*******************************************
# Add navigation on hook system ~ post
*******************************************/
if ( !function_exists('ktz_navigation') ) {
function ktz_navigation() { 
if (get_theme_option('ktz_nav_select') == 'number') { 
		ktz_pagenavi(); 
	} 
elseif (get_theme_option('ktz_nav_select') == 'default') { 
		ktz_default_nav();
	}
  }
}

/*******************************************
# Add related post on hook system ~ post
*******************************************/
if ( !function_exists('ktz_relpost') ) {
function ktz_relpost() {
	if ( get_theme_option('ktz_active_related') != '' ) :
	global $post;
	$orig_post = $post;
	if (get_theme_option('ktz_taxonomy_relpost') == 'tags')
	{
		$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(
				'tag__in' => $tag_ids,
				'post__not_in' => array($post->ID),
				'posts_per_page'=> get_theme_option('ktz_numpost_relpost'),
				'ignore_sticky_posts'=>1
			);
		}
	} 
	else
	{
		$categories = get_the_category($post->ID);
		if ($categories) {
			$category_ids = array();
			foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
			$args=array(
			'category__in' => $category_ids,
			'post__not_in' => array($post->ID),
			'posts_per_page'=> get_theme_option('ktz_numpost_relpost'),
			'ignore_sticky_posts'=>1
			);
		} 
	} 
	if (!isset($args)) $args = '';
	$ktz_query = new WP_Query($args);
	if( $ktz_query->have_posts() ) { 
		echo '<h3 class="related-title clearfix"><span>' . __( 'Related Posts',ktz_theme_textdomain) . '</span></h3>';
		echo '<div class="list_carousel">';
		echo '<ul id="newscarausel">';
		while ( $ktz_query->have_posts() ) : $ktz_query->the_post();
		global $post;
		$permalink = get_permalink();
		$title = get_the_title();
		$postid = get_the_ID();
		echo '<li>';
        echo '<a href="' . $permalink . '" title="' . $title . '">';
		echo ktz_featured_img( 70, 70, 'img-carousel pull-left clearfix' ); // width, height, class
		echo '</a>';
		echo ktz_posted_on();
		echo '<h3 class="entry-title"><a href="' . get_permalink() . '" title="Go to ' . get_the_title() . '" target="_blank" rel="bookmark">' . get_the_title() . '</a></h3>';
		echo '</li>';
		endwhile;
        echo '</ul>';
        echo '<div class="button-carousel">';
		echo '<a id="prev-1" class="prev pull-left clearfix" href="#"><span></span></a>';
		echo '<a id="next-1" class="next pull-right clearfix" href="#"><span></span></a>';
		echo '</div></div>';
	} // if have posts
	$post = $orig_post; 
	wp_reset_query();
	endif;
	}
}

/*******************************************
# Add share article in single post ~ Post
*******************************************/
if ( !function_exists('ktz_share_post') ) :
function ktz_share_post() { 
	if ( get_theme_option('ktz_active_shared') != '' ) :
	global $wp_query, $post;
	$blogname = urlencode( get_bloginfo( 'name' ) . ' ' . get_bloginfo( 'description' ) );
	$post = $wp_query->post;
	$excerpt = urlencode(strip_tags(strip_shortcodes( $post->post_excerpt )));
	if ($excerpt == '') {$excerpt = urlencode(substr(strip_tags(strip_shortcodes( $post->post_content )), 0, 250 ));}
	$excerpt = str_replace( '+','%20',$excerpt );
	$excerpt = str_replace( '%0D%0A','',$excerpt );
	
	$permalink = get_permalink( $post->ID );
	
	$title = str_replace( '+', '%20', urlencode( $post->post_title ));
	echo '<div class="sharedpost pull-right"><div class="sharenav pull-right">';
	echo ktz_default_nav();
	echo '</div>';
	echo '<ul class="icon24pxshare">';
	echo '<li><a href="http://twitter.com/home?status=' . $title . '%20-%20' . $permalink . '" target="_blank" class="twitter" data-original-title="Share This On twitter"></a></li>';
	echo '<li><a href="http://www.facebook.com/share.php?u=' . $permalink . '&amp;t=' . $title . '" target="_blank" class="facebook" data-original-title="Share This On facebook"></a></li>';
	echo '<li><a href="http://www.stumbleupon.com/submit?url=' . $permalink . '&amp;title=' . $title . '" target="_blank" class="stumbleupon" data-original-title="Share This On stumbleupon"></a></li>';
	echo '<li><a href="http://reddit.com/submit?url=' . $permalink . '&amp;title=' . $title . '" target="_blank" class="reddit" data-original-title="Share This On reddit"></a></li>';
	echo '<li><a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=' . $permalink . '&amp;title=' . $title . '&amp;source=' . $blogname . '&amp;summary=' . $excerpt . '" target="_blank" class="linkedin" data-original-title="Share This On linkedin"></a></li>';
	echo '<li><a href="http://digg.com/submit?phase=2&amp;url=' . $permalink . '&amp;title=' . $title . '&amp;bodytext=' . $excerpt . '" target="_blank" class="digg" data-original-title="Share This On digg"></a></li>';
	echo '<li><a href="http://delicious.com/post?url=' . $permalink . '&amp;title=' . $title . '&amp;notes=' . $excerpt . '" target="_blank" class="delicious" data-original-title="Share This On delicious"></a></li>';				
	echo '</ul></div>';
	endif;
	} 
endif;

/*******************************************
# All banner function ######################
*******************************************/
if ( !function_exists('ktz_headbanner') ) {
function ktz_headbanner() { 
	if ( get_theme_option('ktz_ban72890_top') != '' ) :
	echo get_theme_option('ktz_ban72890_top');
	endif;
  }
}

if ( !function_exists('ktz_rightfeat2banner') ) {
function ktz_rightfeat2banner() { 
	if ( get_theme_option('ktz_ban30080_top') != '' ) :
	echo get_theme_option('ktz_ban30080_top');
	endif;
  }
}

if ( !function_exists('ktz_centerbanner') ) {
function ktz_centerbanner() { 
	if ( get_theme_option('ktz_ban46860_ctr') != '' ) :
	echo '<div class="span6">';
	echo get_theme_option('ktz_ban46860_ctr');
	echo '</div>';
	endif;
  }
}

if ( !function_exists('ktz_centerbanner2') ) {
function ktz_centerbanner2() { 
	if ( get_theme_option('ktz_ban46860_ctr2') != '' ) :
	echo '<div class="span6">';
	echo get_theme_option('ktz_ban46860_ctr2');
	echo '</div>';
	endif;
  }
}

if ( !function_exists('ktz_archivebanner') ) {
function ktz_archivebanner() { 
	if ( get_theme_option('ktz_ban46860_archive') != '' ) :
	echo '<div class="archivebanner">';
	echo get_theme_option('ktz_ban46860_archive');
	echo '</div>';
	endif;
  }
}

if ( !function_exists('ktz_ban72890_singlefoot') ) {
function ktz_ban72890_singlefoot() { 
	if ( get_theme_option('ktz_ban72890_sbot') != '' ) :
	echo '<div class="bannersinglefot">';
		echo get_theme_option('ktz_ban72890_sbot');
	echo '</div>';
	endif;
  }
}
if ( !function_exists('ktz_ban300250_singlefoot') ) {
function ktz_ban300250_singlefoot() { 
	if ( get_theme_option('ktz_ban300250_foot') != '' ) :
	echo '<div class="span4">';
	echo get_theme_option('ktz_ban300250_foot');
	echo '</div>';
	endif;
  }
}

if ( !function_exists('ktz_ban200200_leftmag') ) {
function ktz_ban200200_leftmag() { 
	if ( get_theme_option('ktz_ban200200_leftmag1') != '' ) :
	echo '<div class="modulebanner-1 clearfix">';
	echo get_theme_option('ktz_ban200200_leftmag1');
	echo '</div>';
	endif;
  }
}

/*******************************************
# Function call standart WP COMMENT ########
*******************************************/
if ( !function_exists('ktz_comments_template') ) {
function ktz_comments_template() { 
	if ( get_theme_option('ktz_def_com_activated') != '' ) :
		comments_template( '', true );
	endif;
	} 	
}

/*******************************************
# Function Comment Facebook ########
*******************************************/
if ( !function_exists('ktz_comments_facebook') ) {
function ktz_comments_facebook() { 
	if ( get_theme_option('ktz_facebook_com_activated') != '' ) : 
    echo '<div id="fb-root"></div>';  
    echo '<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>';  
    echo '<fb:comments href="' . get_permalink() . '" width="880"></fb:comments>';  
	endif;
	} 	
}

/*******************************************
# Function add error page ##################
*******************************************/
function ktz_error_page() { ?>
	<img src="<?php echo get_theme_option('ktz_404'); ?>" alt="<?php bloginfo('name'); ?>" title="<?php bloginfo('name'); ?>" />
	<hr class="single" />
	<h3><?php _e( 'The page you are looking for doesn\'t seem to exist.', ktz_theme_textdomain ); ?></h3>
	<hr class="double" />
	<?php 
}

/*******************************************
# Add post not found on hook system ~ post
*******************************************/
if ( ! function_exists( 'ktz_post_notfound' ) ) :
function ktz_post_notfound() { ?>
	<article id="post-0" class="post no-results not-found">
		<header class="entry-header">
			<h3><?php _e( 'Nothing Found', ktz_theme_textdomain ); ?></h3>
		</header>
			<div class="entry-content">
			<p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', ktz_theme_textdomain ); ?></p>
			<?php get_search_form(); ?>
			</div>
	</article>
	<?php } 	
endif;

/*******************************************
# Comment list on hook system ~ single
*******************************************/
if ( ! function_exists( 'ktz_comments' ) ) :
function ktz_comments($comment, $args, $depth) {
		$GLOBALS['comment'] = $comment; ?>
		<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
		<div class="commentwrapper clearfix <?php $author_id = get_the_author_meta('ID'); if($author_id == $comment->user_id) $author_flag = 'true';?>" id="comment-<?php comment_ID(); ?>">
            <div class="author-card">
				<?php echo get_avatar( $comment, $size='64' ); ?>
				<?php if (isset($author_flag) && ($author_flag == 'true')) { ?><span class="author_comment"><?php _e( 'Author',ktz_theme_textdomain ); ?></span><?php }?>
            </div>
            <div class="comment_data">
                <p class="comment_meta"><span class="comment_author_link"><?php comment_author_link(); ?></span><span class="comment-date"><?php printf( __( '%1$s at %2$s',ktz_theme_textdomain ), get_comment_date(get_option('date_format')),  get_comment_time() );?></span><?php if (comments_open() && ($depth < $args['max_depth'])) { ?><span class="comment-reply"><?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth'], 'reply_text' => __( 'Reply',ktz_theme_textdomain )))); ?></span><?php } edit_comment_link( __( 'Edit',ktz_theme_textdomain ), '<span class="edit_comment">', '</span>' ); ?></p>
                <?php if ($comment->comment_approved == '0') : ?><p><em><?php _e( 'Your comment is awaiting moderation.',ktz_theme_textdomain); ?></em></p><?php endif;
                comment_text(); ?>
            </div><!-- .comment-data -->
		</div><!-- .commentwrapper -->
<?php }
endif;