<?php
add_action('widgets_init', 'pyre_homepage_1col_load_widgets');

function pyre_homepage_1col_load_widgets()
{
	register_widget('Pyre_Homepage_1col_Widget');
}

class Pyre_Homepage_1col_Widget extends WP_Widget {
	
	function Pyre_Homepage_1col_Widget()
	{
		$widget_ops = array('classname' => 'pyre_homepage_1col', 'description' => 'Homepage 1-column recent posts widget.');

		$control_ops = array('id_base' => 'pyre_homepage_1col-widget');

		$this->WP_Widget('pyre_homepage_1col-widget', 'Broadway: Home 1-column', $widget_ops, $control_ops);
	}
	
	function widget($args, $instance)
	{
		global $post;
		
		extract($args);
		
		$title = $instance['title'];
		$categories = $instance['categories'];
		
		echo $before_widget;
		?>
		
		<div class="block full clearfix">
			
			<h3><a href="<?php echo get_category_link($categories); ?>"><?php echo $title; ?></a><a href="<?php echo get_category_link($categories); ?>"><span></span></a></h3>
			
			<?php
			$recent_posts = new WP_Query(array(
				'showposts' => 4,
				'cat' => $categories,
			));
			$count = 1;
			?>

			<div class="block-content clearfix">
				<?php while($recent_posts->have_posts()): $recent_posts->the_post(); ?>
				<?php if($count == 1): ?>
				<div class="big-block clearfix">
					<?php if(has_post_thumbnail()): ?>
					<div class="post-thumb-container clearfix">
						<?php
						if(has_post_format('video') || has_post_format('audio') || has_post_format('gallery')) {
							$icon = '<span class="post-format-big ' . get_post_format($post->ID) . '"></span>';
						} else {
							$icon = '';
						}
						echo $icon;
						?>
						<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="post-thumbnail">
							<?php the_post_thumbnail('one-column-big', array('alt' => get_the_title(), 'title' => get_the_title())); ?>
						</a>
						<?php if(get_post_meta($post->ID, 'pyre_overall_score', true)): ?>
						<div class="post-score"><img src="<?php bloginfo('template_directory'); ?>/images/stars/<?php echo get_post_meta($post->ID, 'pyre_overall_score', true); ?>.png" alt="" /></div>
						<?php endif; ?>
					</div>
					<?php endif; ?>
					<h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
					<p class="post-meta"><?php the_time('F jS, Y'); ?>, by <?php the_author_posts_link(); ?></p>
					<p class="excerpt"><?php echo string_limit_words(get_the_excerpt(), 14); ?></p>
				</div>
				<?php else: ?>
				<div class="normal-block clearfix">
					<?php if(has_post_thumbnail()): ?>
					<div class="post-thumb-container">
						<?php
						if(has_post_format('video') || has_post_format('audio') || has_post_format('gallery')) {
							$icon = '<span class="post-format ' . get_post_format($post->ID) . '"></span>';
						} else {
							$icon = '';
						}
						echo $icon;
						?>
						<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="post-thumbnail">
							<?php the_post_thumbnail('one-column-small', array('alt' => get_the_title(), 'title' => get_the_title())); ?>
						</a>
					</div>
					<?php endif; ?>
					<div class="post-desc">
						<h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
						<p class="post-meta"><?php the_time('F jS, Y'); ?>, by <?php the_author_posts_link(); ?></p>
						<?php if(get_post_meta($post->ID, 'pyre_overall_score', true)): ?>
						<div class="normal-post-score"><img src="<?php bloginfo('template_directory'); ?>/images/stars/<?php echo get_post_meta($post->ID, 'pyre_overall_score', true); ?>.png" alt="" /></div>
						<?php endif; ?>
					</div>
				</div>
				<?php endif; ?>
				<?php $count++; endwhile; ?>
			</div>
		
		</div>
		
		<?php
		echo $after_widget;
	}
	
	function update($new_instance, $old_instance)
	{
		$instance = $old_instance;
		
		$instance['title'] = $new_instance['title'];
		$instance['categories'] = $new_instance['categories'];
		
		return $instance;
	}

	function form($instance)
	{
		$defaults = array('title' => 'Recent Posts', 'categories' => 'all');
		$instance = wp_parse_args((array) $instance, $defaults); ?>
		<p>
			<label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
			<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" />
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id('categories'); ?>">Filter by Category:</label> 
			<select id="<?php echo $this->get_field_id('categories'); ?>" name="<?php echo $this->get_field_name('categories'); ?>" class="widefat categories" style="width:100%;">
				<option value='all' <?php if ('all' == $instance['categories']) echo 'selected="selected"'; ?>>all categories</option>
				<?php $categories = get_categories('hide_empty=0&depth=1&type=post'); ?>
				<?php foreach($categories as $category) { ?>
				<option value='<?php echo $category->term_id; ?>' <?php if ($category->term_id == $instance['categories']) echo 'selected="selected"'; ?>><?php echo $category->cat_name; ?></option>
				<?php } ?>
			</select>
		</p>
	<?php }
}
?>