<?php $recent_posts_count = intval(of_get_option('ttrust_recent_posts_count')); ?>
<?php $recent_posts_title = of_get_option('ttrust_recent_posts_title'); ?>

<?php if($recent_posts_count > 0) : ?>

<div id="homePosts" class="full homeSection clearfix">			
	<?php if($recent_posts_title):?>
	<div class="sectionHead">		
		<h3><span><?php echo $recent_posts_title; ?></span></h3>	
		<p><span><?php echo of_get_option('ttrust_recent_posts_description'); ?></span></p>	
	</div>
	<?php endif; ?>		
	<?php
	$args = array(
		'ignore_sticky_posts' => 1,					
    	'posts_per_page' => $recent_posts_count,
    	'post_type' => array(				
		'post'					
		)
	);	
	?>
	<?php $recentPosts = new WP_Query( $args ); ?>
	<div class="posts clearfix">	
		<div class="post-76 post type-post status-publish format-standard hentry category-news tag-lorem">	
				<?php
			global $options;
			$twitteritems= of_get_option('woohg_twitter_item');
			if($twitteritems <> null){
			require_once('twitteroauth.php');
			$twitteruservalue = of_get_option('woohg_twitter_username');
			$twitterconsumerkeyvalue = of_get_option('woohg_twitter_consumer_key');
			$twitterconsumersecretvalue = of_get_option('woohg_twitter_consumer_secret_key');
			$twitteraccesstokenvalue = of_get_option('woohg_twitter_access_token');
			$twitteraccesstokensecretvalue = of_get_option('woohg_twitter_access_secret_token');
			

			$twitteruser = $twitteruservalue == "" ? "madCreativedc" : $twitteruservalue;
			$notweets = 4; //how many tweets you want to retrieve
			$consumerkey = $twitterconsumerkeyvalue == "" ? "IyJ7namXUfS4CJKE3Y7RQ" : $twitterconsumerkeyvalue;
			$consumersecret = $twitterconsumersecretvalue == "" ? "6CRS3dlYCRFLwoIAap3AXm5YUfkOeHHUb8o0h4Ly8" : $twitterconsumersecretvalue;
			$accesstoken = $twitteraccesstokenvalue == "" ? "237212595-oHvZaTsZGKzZAGV2ikYj1HqhD1ODXYF5UsevmN6r" : $twitteraccesstokenvalue;
			$accesstokensecret = $twitteraccesstokensecretvalue == "" ? "1ICxNe10hvfhzgnLrjOWRDlnYvS7KRQCt3Oh2MF88uBFs" : $twitteraccesstokensecretvalue;

			function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
				$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
				return $connection;
			}

			$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);

			$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
			// var_dump($tweets);
			$i = 0;
			$twitterBlock = array();
			while($i < $twitteritems) {
				$rand = rand(1, 15);
				if (! in_array($rand, $twitterBlock)) {
					$twitterBlock[] = $rand;
					$i++;
				}
			}
			// $twitterBlock = array("2","4","9","11");
			// $instagramBlock = array("1","3","5","7","13","15");
			$twitterIndex = $instagramIndex = 0;
			$blockCtr = 1;
				while ($blockCtr <= 15) : ?>
					<?php if (in_array($blockCtr, $twitterBlock)): ?>
						<?php 
							if (count($tweets) == 4) :
								$tweet = $tweets[$twitterIndex];
								$blockContent = '<div class="date"><span class="day">' .  date("d ", strtotime($tweet->created_at)).'</span><span class="month">'.date(" M ", strtotime($tweet->created_at)).'</span></div>'."<div class='inside'><h2>" .$tweet->user->name ."</h2><p>".$tweet->text."</p><p class='user-screen-name' >@".$tweet->user->screen_name."</p></div>";
								
							endif;
						?>
						<?php $twitterIndex++; ?>
					<?php
						else:
							$blockContent = "";
						endif; 
						echo $blockContent;
					?>
				<?php 
					$blockCtr++;
				endwhile;

			?>

			<a href="https://twitter.com/<?php echo $twitteruservalue == "" ? "madCreativedc" : $twitteruservalue; ?>" target="_blank">Follow US in Twitter</a>	
			<?php } ?>
		</div>
	</div>

	<div class="posts clearfix">
	<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>	

		<?php get_template_part( 'part-post'); ?>
	<?php endwhile; ?>	
	</div>
</div>
<?php endif; ?>