<?php
add_action('widgets_init', 'pyre_posts_load_widgets');

function pyre_posts_load_widgets()
{
	register_widget('Pyre_Posts_Widget');
}

class Pyre_Posts_Widget extends WP_Widget {
	
	function Pyre_Posts_Widget()
	{
		$widget_ops = array('classname' => 'pyre_posts', 'description' => '');

		$control_ops = array('id_base' => 'pyre_posts-widget');

		$this->WP_Widget('pyre_posts-widget', 'Broadway: Recent Posts', $widget_ops, $control_ops);
	}
	
	function widget($args, $instance)
	{
		global $post;
		
		extract($args);
		
		$title = $instance['title'];
		$categories = $instance['categories'];
		$posts = $instance['posts'];
		
		echo $before_widget;
		?>
		<!-- BEGIN WIDGET -->
		<?php
		if($title) {
			echo $before_title.$title.$after_title;
		}
		?>
		
		<?php
		$recent_posts = new WP_Query(array(
			'showposts' => $posts,
			'cat' => $categories,
		));
		?>
		<div class='block-item-small-container'>
		<?php while($recent_posts->have_posts()): $recent_posts->the_post(); ?>
		<div class="block-item-small 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 endwhile; ?>
		</div>
		<!-- END WIDGET -->
		<?php
		echo $after_widget;
	}
	
	function update($new_instance, $old_instance)
	{
		$instance = $old_instance;
		
		$instance['title'] = $new_instance['title'];
		$instance['categories'] = $new_instance['categories'];
		$instance['posts'] = $new_instance['posts'];
		
		return $instance;
	}

	function form($instance)
	{
		$defaults = array('title' => 'Recent Posts', 'categories' => 'all', 'posts' => 3);
		$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>
		
		<p>
			<label for="<?php echo $this->get_field_id('posts'); ?>">Number of posts:</label>
			<input class="widefat" style="width: 30px;" id="<?php echo $this->get_field_id('posts'); ?>" name="<?php echo $this->get_field_name('posts'); ?>" value="<?php echo $instance['posts']; ?>" />
		</p>
	<?php }
}
?>