<?php
/**
 * Shortcode Title: Latest posts 2
 * Shortcode: latest_posts_2
 * Usage: [latest_posts_2 description="Short description" url="http://url.."]
 */
add_shortcode('latest_posts_2', 'ts_latest_posts_2_func');

function ts_latest_posts_2_func( $atts, $content = null ) {
    extract(shortcode_atts(array(
		"limit" => 10,
		"category" => ""
		),
	$atts));

	if (!(int)$limit)
	{
		$limit = 10;
	}
	$rand = rand(1,1000);
	global $query_string, $post;
	$args = array(
		'posts_per_page'  => $limit,
		'offset'          => 0,
		'cat'        =>  $category,
		'orderby'         => 'date',
		'order'           => 'DESC',
		'include'         => '',
		'exclude'         => '',
		'meta_key'        => '',
		'meta_value'      => '',
		'post_type'       => 'post',
		'post_mime_type'  => '',
		'post_parent'     => '',
		'paged'				=> 1,
		'post_status'     => 'publish'
	);
	$the_query = new WP_Query( $args );

	$content = '';

	$rand = rand(1,1000);

	if ( $the_query->have_posts() )
	{
		$embadded_video = '';
		$galleries = array();
		$list = '';
		while ( $the_query->have_posts() )
		{
			$the_query->the_post();

			$media = '';
			$embadded_video = '';
			if (get_post_format($post -> ID) == 'video')
			{
				$url = get_post_meta($post -> ID, 'video_url',true);
				if (!empty($url))
				{
					$embadded_video = ts_get_embaded_video($url);
				}
				else if (empty($url))
				{
					$embadded_video = get_post_meta($post -> ID, 'embedded_video',true);
				}
			}
			if (!empty($embadded_video))
			{
				$media = '<div class="post-img post-video">';
				$media .= '<div class="videoWrapper"><img src="'.get_bloginfo('template_directory').'/img/360x181.png" width="360" height="181" alt="">';
				$media .= $embadded_video;
				$media .= '</div>';
				$media .= '</div>';
				$media .= '<div class="clear"></div>';
			}
			else if (get_post_format($post -> ID) == 'gallery')
			{
				$gallery = get_post_meta($post->ID, 'gallery_images',true);

				if (is_array($gallery) && count($gallery) > 0)
				{
					$rand2 = rand(1,1000);
					$media = '<div class="flexslider fs-inner latest-posts-gallery" id="flexslider-latest-posts-gallery-'.$post -> ID.'-'.$rand2.'">';
					$media .= '<ul class="slides-inner">';
					foreach ($gallery as $image)
					{
						$media .= '<li>'.ts_get_resized_image_by_size($image['image'], 'latest-posts', $image['title']).'</li>';
					}
					$media .= '</ul>';
					$media .= '</div>';
					$galleries[] = $post -> ID.'-'.$rand2;
				}
				else
				{
					$media = ts_get_resized_post_thumbnail($post->ID,'latest-posts', get_the_title($post->ID));
				}
			}
			else if (has_post_thumbnail())
			{
				$media = ts_get_resized_post_thumbnail($post->ID,'latest-posts', get_the_title($post->ID));
			}
			$comments = get_comments_number();

			$list .= "
				<li>
					<article class='item post ".(get_post_format() ? 'format-'.get_post_format() : '')."'>";


			if (!empty($media))
			{
				$list .= '<div class="media"><div class="overlay"></div>'.$media.'</div>';
			}
			
//			<div class='post-author'>
//									".__('By','circles')."
//									<span>
//										".get_the_author_link()."
//									</span>
//								</div>
//			
			$list .= "
						<div class='item-body'>
							<div class='item-helper'>
								<div class='post-type'>
									<span></span>
								</div>
							</div>
							<header>
								<a href='".get_permalink()."'><h2>".ts_get_shortened_string_by_letters( get_the_title(), 30)."</h2></a>
							</header>
							<div class='meta'>
								<div class='post-author inline'>
									".__('By','circles')."
									<span>
										".get_the_author_link()."
									</span>
								</div>
								<div class='post-date'>
									<span>".get_the_time('M d,Y')."</span>
								</div>
								<div class='post-category'>
									<span>
										".get_the_category_list( ', ', '', $post -> ID )."
									</span>
								</div>
							</div>
							<div class='excerpt'>
								<p>".ts_get_the_excerpt_theme('short')."</p>
							</div>
						</div>
					</article>
				</li>";

		}
		$content = "
			<section class='widget latest_posts_2'>
				<div class='grid_12'>
					<div class='flexslider latest_posts_2-container clearfix'>
						<ul class='slides'>".$list."</ul>
					</div>
				</div>
			</section>
			<div class='clear'></div>";
	}
	// Restor original Query & Post Data
	wp_reset_query();
	wp_reset_postdata();
	return $content;
}