<?php
/**
 * This file will eventually place the codes for theme widgets that is
 * compatible with WordPress 2.8.
 */
 
class Arras_Tabbed_Sidebar extends WP_Widget {
	var $display_thumbs, $query_source;
	var $commentcount, $postcount;

	// Constructor
	function Arras_Tabbed_Sidebar() {
		$widget_args = array(
			'classname'		=> 'arras_tabbed_sidebar',
			'description'	=> __('Sidebar containing tabs that displays posts, comments and tags.', 'arras'),
		);
		$this->WP_Widget('arras_tabbed_sidebar', __('Tabbed Sidebar', 'arras'), $widget_args);
		
		add_action('arras_tabbed_sidebar_tab-featured', array(&$this, 'featured_tab'));
		add_action('arras_tabbed_sidebar_tab-latest', array(&$this, 'latest_tab'));
		add_action('arras_tabbed_sidebar_tab-comments', array(&$this, 'comments_tab'));
		add_action('arras_tabbed_sidebar_tab-tags', array(&$this, 'tags_tab'));
		add_action('arras_tabbed_sidebar_tab-popular', array(&$this, 'popular_tab'));
	}
	
	function get_tabs() {
		$_default_tabs = array(
			'featured'		=> __('Featured', 'arras'), 
			'latest'		=> __('Latest', 'arras'),
			'comments'		=> __('Comments', 'arras'), 
			'tags'			=> __('Tags', 'arras')
		);
		
		if ( function_exists('akpc_most_popular') ) {
			$_default_tabs['popular'] = __('Popular', 'arras');
		}
		
		return apply_filters('arras_tabbed_sidebar_tabs', $_default_tabs);
	}
	
	function widget($args, $instance) {
		global $wpdb;		
		extract($args, EXTR_SKIP);
		
		$this->query_source = $instance['query'];
		$this->postcount = $instance['postcount'];
		$this->commentcount = $instance['commentcount'];
		$this->display_thumbs = isset($instance['display_thumbs']);
		
		if (!$instance['order']) $instance['order'] = $this->get_tabs();
		
		if ($instance['display_home'] && !is_home()) {
			return false;
		}
		?>
		<li class="multi-sidebar-container">
		<div class="multi-sidebar clearfix">
		<ul class="tabs clearfix">
		<?php $this->render_sidebar_tabs($instance['order']) ?>
		</ul>
		<?php
		foreach ($instance['order'] as $tab) {
			echo '<div id="s-' . $tab . '" class="widgetcontainer clearfix">';
			do_action('arras_tabbed_sidebar_tab-' . $tab);
			echo '</div><!-- #s-' . $tab . ' -->';
		}
		?>
		</div>
		</li>
		<?php
		
	}
	
	function featured_tab() {
		
		switch ($this->query_source) {
			case 'slideshow':
				$q = arras_parse_query( 
					arras_get_option('slideshow_cat'),
					$this->postcount, 
					0, 
					arras_get_option('slideshow_posttype'), 
					arras_get_option('slideshow_tax')
				);
				break;
			case 'featured2':
				$q = arras_parse_query( 
					arras_get_option('featured2_cat'),
					$this->postcount, 
					0, 
					arras_get_option('featured2_posttype'), 
					arras_get_option('featured2_tax')
				);
				break;
			default:
				$q = arras_parse_query( 
					arras_get_option('featured1_cat'),
					$this->postcount, 
					0, 
					arras_get_option('featured1_posttype'), 
					arras_get_option('featured1_tax')
				);
		}
		
		$f = new WP_Query($q);
		if (!$f->have_posts()) {
			echo '<span class="textCenter sub">' . __('No posts at the moment. Check back again later!', 'arras') . '</span>';
		} else {
			echo '<ul>';
			while ($f->have_posts()) {
				$f->the_post();
				?>
				<li class="clearfix">
				<?php if ($this->display_thumbs) : ?>
				<span class="thumb"><?php echo arras_get_thumbnail('sidebar-thumb') ?></span>
				<?php endif ?>
				<a href="<?php the_permalink() ?>"><?php the_title() ?></a><br />
				<span class="sub"><?php the_time( __('d F Y g:i A', 'arras') ); ?> | 
				<?php comments_number( __('No Comments', 'arras'), __('1 Comment', 'arras'), __('% Comments', 'arras') ); ?></span>
				</li>
				<?php
			}
			echo '</ul>';
		}
	}
	
	function latest_tab() {
		$f = new WP_Query('showposts=' . $this->postcount);
		if (!$f->have_posts()) {
			echo '<span class="textCenter sub">' . __('No posts at the moment. Check back again later!', 'arras') . '</span>';
		} else {
			echo '<ul>';
			while ($f->have_posts()) {
				$f->the_post();
				?>
				<li class="clearfix">
				<?php if ($this->display_thumbs) : ?>
				<span class="thumb"><?php echo arras_get_thumbnail('sidebar-thumb',get_the_ID()) ?></span>
				<?php endif ?>
				<a href="<?php the_permalink() ?>"><?php the_title() ?></a><br />
				<span class="sub"><?php the_time( __('d F Y g:i A', 'arras') ); ?> | 
				<?php comments_number( __('No Comments', 'arras'), __('1 Comment', 'arras'), __('% Comments', 'arras') ); ?></span>
				</li>
				<?php
			}
			echo '</ul>';
		}
	}
	
	function comments_tab() {
		$comments = get_comments( array('status' => 'approve', 'number' => $this->commentcount) );	
		if ($comments) {
			echo '<ul id="recentcomments">';
			foreach ($comments as $comment) {
				echo '<li class="recentcomments clearfix">';
				if ($this->display_thumbs) echo get_avatar($comment->user_id, 36);
				echo '<span class="author">' . $comment->comment_author . '</span><br />';
				echo '<a href="' . get_permalink($comment->comment_post_ID) . '">' . get_the_title($comment->comment_post_ID) . '</a>';
				echo '</li>';
			}
			echo '</ul>';
		}
	}
	
	function tags_tab() {
		echo '<div class="tags">';
		if (function_exists('wp_cumulus_insert')) {
			$args = array(
				'width'		=> 280,
				'height'	=> 280
			);
			wp_cumulus_insert($args);
		} else {
			wp_tag_cloud('smallest=9&largest=16');
		}
		echo '</div>';
	}
	
	function popular_tab() {
		if ( function_exists('akpc_most_popular') ) {
			echo '<ul>';
			akpc_most_popular(10, '<li>', '</li>');
			echo '</ul>';
		}
	}
	
	function update($new_instance, $old_instance) {
		$instance = $old_instance;
		$instance['order'] = $new_instance['order'];
		$instance['display_home'] = $new_instance['display_home'];
		$instance['display_thumbs'] = $new_instance['display_thumbs'];
		$instance['query'] = $new_instance['query'];
		$instance['postcount'] = $new_instance['postcount'];
		$instance['commentcount'] = $new_instance['commentcount'];
		
		return $instance;
	}
	
	function form($instance) {
		$instance = wp_parse_args( (array)$instance, array( 
			'order' => array('featured', 'latest', 'comments', 'tags'), 
			'display_home' => true, 
			'display_thumbs' => true, 
			'query' => 'slideshow',
			'postcount' => 8,
			'commentcount' => 8
		) );
		$order = $instance['order'];

		?>
		<p>
		<label for="<?php echo $this->get_field_id('featured_cat') ?>"><?php _e('Retrieve Posts from:', 'arras') ?></label><br />
		<select style="width: 200px;" name="<?php echo $this->get_field_name('query') ?>">
			<option<?php if ($instance['query'] == 'slideshow') echo ' selected="selected" ' ?> value="slideshow"><?php _e('Slideshow', 'arras') ?></option>
			<option<?php if ($instance['query'] == 'featured1') echo ' selected="selected" ' ?> value="featured1"><?php _e('Featured Posts #1', 'arras') ?></option>
			<option<?php if ($instance['query'] == 'featured2') echo ' selected="selected" ' ?> value="featured2"><?php _e('Featured Posts #2', 'arras') ?></option>
		</select>
		</p>

		<p>
		<label for="<?php echo $this->get_field_id('order') ?>"><?php _e('Tabbed Sidebar Order:', 'arras') ?></label><br />
		<select style="width: 200px" name="<?php echo $this->get_field_name('order') ?>[0]"><?php $this->get_tabbed_opts( $order[0], 'featured'); ?></select><br />
		<select style="width: 200px" name="<?php echo $this->get_field_name('order') ?>[1]"><?php $this->get_tabbed_opts( $order[1], 'latest'); ?></select><br />
		<select style="width: 200px" name="<?php echo $this->get_field_name('order') ?>[2]"><?php $this->get_tabbed_opts( $order[2], 'comments'); ?></select><br />
		<select style="width: 200px" name="<?php echo $this->get_field_name('order') ?>[3]"><?php $this->get_tabbed_opts( $order[3], 'tags'); ?></select>
		</p>
		<p style="font-size:11px"><?php _e('The popular posts option is only enabled when the plugin <a href="http://wordpress.org/extend/plugins/popularity-contest/">Popularity Contest</a> is enabled.', 'arras') ?></p>
		
		<p><label for="<?php echo $this