<?php
add_action('widgets_init', 'Reedwan_Magazine_load_widgets');

function Reedwan_Magazine_load_widgets()
{
	register_widget('Reedwan_Magazine_Widget');
}

class Reedwan_Magazine_Widget extends WP_Widget {
	
	function Reedwan_Magazine_Widget()
	{
		$widget_ops = array('classname' => 'reedwan_magazine', 'description' => '1 Column magazine recent posts widget for magazine widget.');

		$control_ops = array('id_base' => 'reedwan_magazine-widget');

		$this->WP_Widget('reedwan_magazine-widget', 'Backstreet : 1 Column Magazine', $widget_ops, $control_ops);
	}
	
	function widget($args, $instance)
	{
		extract($args);
		
		$title = $instance['title'];
		$post_type = 'all';
		$categories = $instance['categories'];
		$posts = $instance['posts'];
		$show_image = isset($instance['show_image']) ? 'true' : 'false';
		echo $before_widget;
		?>
		
		<?php
		$post_types = get_post_types();
		unset($post_types['page'],$post_types['reviews'], $post_types['attachment'], $post_types['revision'], $post_types['nav_menu_item']);
		
		if($post_type == 'all') {
			$post_type_array = $post_types;
		} else {
			$post_type_array = $post_type;
		}
		?>
		
		<div class="widget-magazine">
			<?php
			if($title) {
				echo $before_title.$title.$after_title;
			}
			?>
			<?php
			$recent_posts = new WP_Query(array(
				'showposts' => $posts,
				'post_type' => $post_type_array,
				'cat' => $categories,
			));
			?>
			<?php
			$big_count = round($posts / 5);
			if(!$big_count) { $big_count = 1; }
			?>
			<?php
			$counter = 1; 
			while($recent_posts->have_posts()): $recent_posts->the_post(); 
			if(has_post_format('video')) 
			{
				$format_icon = 'class="video-format-icon"';
				$format_icon_small = ' (Video)';
			}
			else if (has_post_format('audio'))
			{
				$format_icon = 'class="audio-format-icon"';
				$format_icon_small = ' (Audio)';
			}
			else if (has_post_format('gallery'))
			{
				$format_icon = 'class="gallery-format-icon"';
				$format_icon_small = ' (Gallery)';
			}
			else {
				$format_icon = 'class="standard-format-icon"';
				$format_icon_small = '';
			}
			?>
			<?php if($counter <= $big_count): ?>
			<?php if($counter == $big_count) { $last = 'block-big-last'; } else { $last = ''; }?>
			
			<div class="block-big <?php echo $last; ?>">
				<?php if(has_post_thumbnail()): ?>
				<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'big-post-thumb'); ?>
				<div class="magz-image"><a <?php echo $format_icon; ?> href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img class="fadeover" src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" width='300' height='150' /></a>
				</div>
				<?php else: ?>
				<div class="magz-image"><a <?php echo $format_icon; ?> href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img class="fadeover" src="<?php echo get_template_directory_uri(); ?>/timthumb.php?src=<?php echo get_template_directory_uri(); ?>/images/thumbnail.png&w=300&h=150" alt="<?php the_title(); ?>" width='300' height='150' /></a>
				</div>
				<?php endif; ?>
				<div class="big-desc">
				<div class="big-title"><h3><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a></h3></div>
				<div class="magz-meta">Posted On <?php the_time('F j, Y'); ?>  |  <?php comments_popup_link(); ?></div>
				<p><?php echo string_limit_words(get_the_excerpt(), 35); ?><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>' class="readmore"> [...]</a></p>
				
				</div>
			</div>
			
			<?php else: ?>
			<?php if($show_image == 'true'): ?>
			<div class="block-small<?php if($counter%2 != 0) { echo " right"; } else { echo " left"; }?>">
				<?php if(has_post_thumbnail()): ?>
					<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'small-post-thumb'); ?>
					<div class="magz-image"><a <?php echo $format_icon; ?> href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img class="fadeover" src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" width='75' height='60' /></a></div>
					<?php else: ?>
					<div class="magz-image"><a <?php echo $format_icon; ?> href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img class="fadeover" src="<?php echo get_template_directory_uri(); ?>/timthumb.php?src=<?php echo get_template_directory_uri(); ?>/images/thumbnail.png&w=75&h=60" alt="<?php the_title(); ?>"  width='75' height='60' /></a></div>
				<?php endif; ?>
				<div class="description">
					<h3><a class="des-title" href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a></h3>
					<div class="magz-meta"><?php the_time('F j, Y'); ?>  |  <?php comments_popup_link(); ?></div>
				</div>
			</div>
			<?php endif; ?>
			<?php if($show_image == 'false'): ?>
			<div class="block-small-noimage<?php if($counter%2 != 0) { echo " right"; } else { echo " left"; }?>">
				<h3><a class="des-title" href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?><?php echo $format_icon_small; ?></a></h3>
			</div>
			<?php endif; ?>
			<?php endif; ?>
			<?php $counter++; endwhile; ?>
		
		</div>
		<?php
		echo $after_widget;
	}
	
	function update($new_instance, $old_instance)
	{
		$instance = $old_instance;
		
		$instance['title'] = $new_instance['title'];
		$instance['post_type'] = 'all';
		$instance['categories'] = $new_instance['categories'];
		$instance['posts'] = $new_instance['posts'];
		$instance['show_image'] = $new_instance['show_image'];
		return $instance;
	}

	function form($instance)
	{
		$defaults = array('title' => 'Recent Posts', 'post_type' => 'all', 'categories' => 'all', 'posts' => 5, 'show_image'=>null );
		$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>
		<p>
			<input class="checkbox" type="checkbox" <?php checked($instance['show_image'], 'on'); ?> id="<?php echo $this->get_field_id('show_image'); ?>" name="<?php echo $this->get_field_name('show_image'); ?>" /> 
			<label for="<?php echo $this->get_field_id('show_image'); ?>">Show thumbnail image</label>
		</p>
		
	<?php }
}
?>