<?php
function theme_shortcode_blog($atts, $content = null, $code) {
	global $wp_filter;
	$the_content_filter_backup = $wp_filter['the_content'];
	extract(shortcode_atts(array(
		'count' => 3,
		'cat' => '',
		'author' => '',
		'posts' => '',
		'image' => 'true',
		'imagetype'=>'full',
		'meta' => 'true',
		'desc' => 'true',
		'full' => 'false',
		'nopaging' => 'true',
		'paged' => '',
		'height' => false,
		'default'=>true,
		'desc_length' => 'default',
		'category__and' =>'',
		'category__not_in' => '',
		'read_more' => 'default',
		'read_more_text' => '',
		'offset' => 0,
		'order'=> 'ASC',
		'orderby'=> 'none', //none, id, author, title, date, modified, parent, rand, comment_count, menu_order
	), $atts));
	
	$query = array(
		'posts_per_page' => (int)$count,
		'post_type'=>'post',
	);
	if($paged){
		$query['paged'] = $paged;
	}
	if($cat){
		$query['cat'] = $cat;
	}
	if($category__and){
		$query['category__and'] = explode(',',$category__and);
	}
	if($category__not_in){
		$query['category__not_in'] = explode(',',$category__not_in);
	}
	if($author){
		$query['author'] = $author;
	}
	if($posts){
		$query['post__in'] = explode(',',$posts);
	}
	if((int)$offset != 0){
		$query['offset'] = (int)$offset;
	}
	if($orderby != 'none'){
		$query['order'] = $order;
		$query['orderby'] = $orderby;
	}
	if(!$height){
		$height = null;
	}
	if($read_more == 'default'){
		$read_more =  theme_get_option('blog','read_more');
	}elseif($read_more == 'true'){
		$read_more = true;
	}else{
		$read_more = false;
	}
	if($read_more_text == ''){
		$read_more_text = wpml_t(THEME_NAME, 'Blog Post Read More Button Text',stripslashes(theme_get_option('blog','read_more_text')));
	}
	if ($nopaging == 'false') {
		global $wp_version;
		if((is_front_page() || is_home() ) && version_compare($wp_version, "3.1", '>=')){//fix wordpress 3.1 paged query 
			$paged = (get_query_var('paged')) ?get_query_var('paged') : ((get_query_var('page')) ? get_query_var('page') : 1);
		}else{
			$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
		}
		$query['paged'] = $paged;
	} else {
		$query['showposts'] = $count;
	}
	if($image == 'true'){
		$featured_image_type = $imagetype;
	}else{
		$featured_image_type = 'none';
	}
	if($desc_length != 'default'){
		$excerpt_constructor = new Theme_The_Excerpt_Length_Constructor($desc_length);
		add_filter( 'excerpt_length', array($excerpt_constructor,'get_length'));
	}
	global $post ;
	if(is_page($post->ID))
		$layout = get_post_meta($post->ID, '_page_layout', true);
	else
		$layout = theme_get_option('general','layout');
	if($layout == 'default')
		$layout = theme_get_option('general','layout');
	if($default == 'true'){
		$display_default = true;
	}else{
		$display_default = false;
	}
	$meta_items = theme_get_option('blog','meta_items');
	ob_start();
	$r = new WP_Query($query);
	if ($r->have_posts()):
		while ($r->have_posts()) : 
		$r->the_post();
	$has_image = has_post_thumbnail();
	$has_image = $has_image ? true : $display_default;  
?>
<article id="post-<?php the_ID(); ?>" class="post <?php echo $featured_image_type.'-feature';?>">
<?php if($featured_image_type == "none" || !$has_image):?>
	<div class="no-feature">
			<div class="cont-vert">
				<div class="outer-vert">
					<div class="inner-vert">
						<h2 class="post-title-overimage vert-position">
							<a href="<?php echo get_permalink() ?>" rel="bookmark" title="<?php printf( __("Permanent Link to %s", THEME_FRONT_DOMAIN), get_the_title() ); ?>"><?php the_title(); ?></a>
						</h2>
					</div>
				</div>
			</div>
<?php if(in_array('comment',$meta_items)):?>
			<div class="post-comments-overimage">
				<div class="post-comments-bubble">
					<?php comments_popup_link(__('0',THEME_FRONT_DOMAIN), __('1',THEME_FRONT_DOMAIN), __('%',THEME_FRONT_DOMAIN),'',__('X',THEME_FRONT_DOMAIN));?>
				</div>
			</div>
<?php endif;?>
			<div class="clear"></div>
			</div>
	<?php if($meta == 'true' && ( count($meta_items)>1 || (count($meta_items)==1 && !in_array('comment',$meta_items)))):?>
		<div class="post-bottoms">
			<div class="post-meta">
			<?php echo theme_generator('post_meta'); //edit_post_link( __( 'Edit', THEME_FRONT_DOMAIN ), '<span class="sep">|</span> <span class="edit-link">', '</span>' ); ?>
			</div>
		</div>
	<?php endif;?>
	<div class="post-content">
	<?php 
		if($full == 'true'):
			global $more;
			$more = 0;
			the_content($read_more_text,false);
		else:
			the_excerpt();
			$is_button = theme_get_option('blog','read_more_button');
	?>
		<?php 
			if($read_more):
		?>
		<a class="read_more_link <?php echo $is_button?'button medium white':'';  ?>" href="<?php the_permalink(); ?>">
		<?php 
			echo '<span class="text" style="">';
			echo $read_more_text;
			echo '</span>';
		?>
		</a>
		<?php
			endif;
		?>
		<?php endif; ?>
		<div class="clear"></div>
	</div>
<?php elseif($featured_image_type == "full" && $has_image):?>
	<div class="post-title-slug">
		<?php echo theme_generator('blog_featured_image',$featured_image_type,$layout,$height); ?>
		<div class="post-image-overlay">
			<div class="cont-vert">
				<div class="outer-vert">
					<div class="inner-vert">
						<h2 class="post-title-overimage vert-position">
							<a href="<?php echo get_permalink() ?>" rel="bookmark" title="<?php printf( __("Permanent Link to %s", THEME_FRONT_DOMAIN), get_the_title() ); ?>"><?php the_title(); ?></a>
						</h2>
					</div>
				</div>
			</div>
<?php if(in_array('comment',$meta_items)):?>
			<div class="post-comments-overimage">
				<div class="post-comments-bubble">
					<?php comments_popup_link(__('0',THEME_FRONT_DOMAIN), __('1',THEME_FRONT_DOMAIN), __('%',THEME_FRONT_DOMAIN),'');?>
				</div>
			</div>
<?php endif; ?>
			<div class="clear"></div>
		</div>
	</div>
	<div class="post-content">
	<?php 
		if($full == 'true'):
			global $more;
			$more = 0;
			the_content($read_more_text,false);
		else:
			the_excerpt();
			$is_button = theme_get_option('blog','read_more_button');
	?>
		<?php 
			if($read_more):
		?>
		<a class="read_more_link <?php echo $is_button?'button medium white':'';  ?>" href="<?php the_permalink(); ?>">
		<?php 
			echo '<span class="text" style="">';
			echo wpml_t(THEME_NAME, 'Blog Post Read More Button Text',stripslashes(theme_get_option('blog','read_more_text')));
			echo '</span>';
		?>
		</a>
		<?php endif; ?>
	<?php endif; ?>
		<div class="clear"></div>
	</div>
	<?php if($meta == 'true' && ( count($meta_items)>1 || (count($meta_items)==1 && !in_array('comment',$meta_items)))):?>
		<div class="post-bottoms">
			<div class="post-meta">
			<?php echo theme_generator('post_meta'); //edit_post_link( __( 'Edit', THEME_FRONT_DOMAIN ), '<span class="sep">|</span> <span class="edit-link">', '</span>' ); ?>
			</div>
		</div>
	<?php endif;?>
<?php elseif(($featured_image_type == "left"||$featured_image_type=='right') && $has_image):?>
	<div class="post-title-slug">
		<?php echo theme_generator('blog_featured_image',$featured_image_type,$layout); ?>
	</div>
	<div class="no-feature <?php echo in_array('comment',$meta_items)?'comment':'';?>">
	<div class="title-wrap">
		<h2 class="post-title-overimage vert-position">
			<a href="<?php echo get_permalink() ?>" rel="bookmark" title="<?php printf( __("Permanent Link to %s", THEME_FRONT_DOMAIN), get_the_title() ); ?>"><?php the_title(); ?></a>
		</h2>	
	</div>	
<?php if(in_array('comment',$meta_items)):?>
			<div class="post-comments-overimage">
				<div class="post-comments-bubble">
					<?php comments_popup_link(__('0',THEME_FRONT_DOMAIN), __('1',THEME_FRONT_DOMAIN), __('%',THEME_FRONT_DOMAIN),'',__('X',THEME_FRONT_DOMAIN));?>
				</div>
			</div>
<?php endif;?>
	<div class="clear"></div>
	</div>
<?php 
	if($full == 'true'):
		global $more;
		$more = 0;
		the_content($read_more_text,false);
	else:
		the_excerpt();
		$is_button = theme_get_option('blog','read_more_button');
?>
		<?php 
			if($read_more):
		?>
		<a class="read_more_link <?php echo $is_button?'button medium white':'';  ?>" href="<?php the_permalink(); ?>">
			<?php

				echo '<span class="text" style="">';
				echo wpml_t(THEME_NAME, 'Blog Post Read More Button Text',stripslashes(theme_get_option('blog','read_more_text')));
				echo '</span>';

			?>
		</a>
		<?php endif;?>
	<?php endif;?>
	<div class="clear"></div>
	<?php if($meta=='true' && ( count($meta_items)>1 || (count($meta_items)==1 && !in_array('comment',$meta_items)))):?>
	<div class="post-bottoms">
		<div class="post-meta">
		<?php echo theme_generator('post_meta'); //edit_post_link( __( 'Edit', THEME_FRONT_DOMAIN ), '<span class="sep">|</span> <span class="edit-link">', '</span>' ); ?>
		</div>
	</div>
	<?php endif; ?>
<?php endif;?>
</article>
<?php
		endwhile;
	endif;
	$output = ob_get_contents();
	ob_end_clean();
	if($desc_length != 'default'){
		remove_filter( 'excerpt_length', array($excerpt_constructor,'get_length'));
	}
	if ($nopaging == 'false') {
		ob_start();
		theme_blog_pagenavi('', '', $r, $paged);
		$output .= ob_get_clean();
	}

	wp_reset_postdata();
	$wp_filter['the_content'] = $the_content_filter_backup;
	return $output;
}
add_shortcode('blog','theme_shortcode_blog');

function theme_blog_pagenavi($before = '', $after = '', $blog_query, $paged) {
	global $wpdb, $wp_query;
	
	if (is_single())
		return;
	
	$pagenavi_options = array(
		//'pages_text' => __('Page %CURRENT_PAGE% of %TOTAL_PAGES%',THEME_FRONT_DOMAIN),
		'pages_text' => '',
		'current_text' => '%PAGE_NUMBER%',
		'page_text' => '%PAGE_NUMBER%',
		'first_text' => __('&laquo; First',THEME_FRONT_DOMAIN),
		'last_text' => __('Last &raquo;',THEME_FRONT_DOMAIN),
		'next_text' => __('&raquo;',THEME_FRONT_DOMAIN),
		'prev_text' => __('&laquo;',THEME_FRONT_DOMAIN),
		'dotright_text' => __('...',THEME_FRONT_DOMAIN),
		'dotleft_text' => __('...',THEME_FRONT_DOMAIN),
		'style' => 1,
		'num_pages' => 4,
		'always_show' => 0,
		'num_larger_page_numbers' => 3,
		'larger_page_numbers_multiple' => 10,
		'use_pagenavi_css' => 0,
	);
	
	$request = $blog_query->request;
	$posts_per_page = intval(get_query_var('posts_per_page'));
	global $wp_version;
	if((is_front_page() || is_home() ) && version_compare($wp_version, "3.1", '>=')){//fix wordpress 3.1 paged query 
		$paged = (get_query_var('paged')) ?intval(get_query_var('paged')) : intval(get_query_var('page'));
	}else{
		$paged = intval(get_query_var('paged'));
	}
	
	$numposts = $blog_query->found_posts;
	$max_page = intval($blog_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_pages_array = array();
	if ($larger_page_multiple)
		for($i = $larger_page_multiple; $i <= $max_page; $i += $larger_page_multiple)
			$larger_pages_array[] = $i;
	
	if ($max_page > 1 || intval($pagenavi_options['always_show'])) {
		$pages_text = str_replace("%CURRENT_PAGE%", number_format_i18n($paged), $pagenavi_options['pages_text']);
		$pages_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pages_text);
		echo $before . '<div class="pagenavi">' . "\n";
		switch(intval($pagenavi_options['style'])){
			// Normal
			case 1:
				if (! empty($pages_text)) {
					echo '<span class="pages">' . $pages_text . '</span>';
				}
				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 '<a href="' . esc_url(get_pagenum_link()) . '" class="first" title="' . $first_page_text . '">' . $first_page_text . '</a>';
					if (! empty($pagenavi_options['dotleft_text'])) {
						echo '<span class="extend">' . $pagenavi_options['dotleft_text'] . '</span>';
					}
				}
				$larger_page_start = 0;
				foreach($larger_pages_array as $larger_page) {
					if ($larger_page < $start_page && $larger_page_start < $larger_page_to_show) {
						$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($larger_page), $pagenavi_options['page_text']);
						echo '<a href="' . esc_url(get_pagenum_link($larger_page)) . '" class="page" title="' . $page_text . '">' . $page_text . '</a>';
						$larger_page_start++;
					}
				}
				previous_posts_link($pagenavi_options['prev_text']);
				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']);
						echo '<span class="current">' . $current_page_text . '</span>';
					} else {
						$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
						echo '<a href="' . esc_url(get_pagenum_link($i)) . '" class="page" title="' . $page_text . '">' . $page_text . '</a>';
					}
				}
				next_posts_link($pagenavi_options['next_text'], $max_page);
				$larger_page_end = 0;
				foreach($larger_pages_array as $larger_page) {
					if ($larger_page > $end_page && $larger_page_end < $larger_page_to_show) {
						$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($larger_page), $pagenavi_options['page_text']);
						echo '<a href="' . esc_url(get_pagenum_link($larger_page)) . '" class="page" title="' . $page_text . '">' . $page_text . '</a>';
						$larger_page_end++;
					}
				}
				if ($end_page < $max_page) {
					if (! empty($pagenavi_options['dotright_text'])) {
						echo '<span class="extend">' . $pagenavi_options['dotright_text'] . '</span>';
					}
					$last_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options['last_text']);
					echo '<a href="' . esc_url(get_pagenum_link($max_page)) . '" class="last" title="' . $last_page_text . '">' . $last_page_text . '</a>';
				}
				break;
			// Dropdown
			case 2:
				echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF']) . '" method="get">' . "\n";
				echo '<select size="1" onchange="document.location.href = this.options[this.selectedIndex].value;">' . "\n";
				for($i = 1; $i <= $max_page; $i++) {
					$page_num = $i;
					if ($page_num == 1) {
						$page_num = 0;
					}
					if ($i == $paged) {
						$current_page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['current_text']);
						echo '<option value="' . esc_url(get_pagenum_link($page_num)) . '" selected="selected" class="current">' . $current_page_text . "</option>\n";
					} else {
						$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
						echo '<option value="' . esc_url(get_pagenum_link($page_num)) . '">' . $page_text . "</option>\n";
					}
				}
				echo "</select>\n";
				echo "</form>\n";
				break;
		}
		echo '</div>' . $after . "\n";
	}
}
