<?php
/* 
Plugin Name: Carousel Widget 
Description: Carousel posts widget.
Version: 1.0 
Author: Stefan Naumovski 
*/    

add_action( 'widgets_init', 'carousel_posts_widget' );

function carousel_posts_widget() {register_widget( 'Carousel_widget' );}

class Carousel_widget extends WP_Widget {
	
	/* Register widget with WordPress. */
	
	function __construct() {
		parent::__construct(
			'Carousel_widget_tn', // Widget ID
			'Carousel posts', // Name
			array( 'description' => 'Carousel posts- display your posts in carousel style by selecting tags.', ) // Args
			);}
	
		/* Front-end display of widget. */
		
		public function widget( $args, $instance ) {
			$title = apply_filters( 'widget_title', $instance['title'] );
			$number = $instance['number'];
			$tags = $instance['tags'];
			
		echo $args ['before_widget'];
			if (! empty( $title ))
				echo '<h3><div class="widget-title">'. $title . '</div>';?>

              <div class="carousel-navigation">
                  <a href="#" class="carousel-prev" ></a>
                  <a href="#" class="carousel-next"></a>
              </div><!--carousel-navigation-->
              
              <?php echo '</h3>';?>
              
<div class="carousel">
	<ul class="slides">
		<?php $tn_posts = new WP_Query(array( 'tag' => $tags, 'posts_per_page' => $number)); while($tn_posts->have_posts()) : $tn_posts->the_post();?>
		<li>
			<div class="carousel-image">
				<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { ?>
				<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
				<?php the_post_thumbnail('carousel-thumb'); if ( 'video' == get_post_format() ): echo '<span class="play-icon"></span>'; endif;?>
				</a>
				<?php } ?>
   			</div><!---carousel-image--> 
            <div class="carousel-text">
                <div class="carousel-category"><?php $category = get_the_category(); if($category[0]){echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';} ?>
                </div><!--carousel-category-->
                <div class="carousel-title"><a href="<?php the_permalink() ?>">
                <?php $tit = the_title('','',FALSE); echo substr($tit, 0, 40); if (strlen($tit) > 40) echo " ..."; ?>
                </a>
                </div><!--carousel-title--> 
            </div><!--carousel-text--> 
		</li>
		<?php endwhile; ?>
	</ul>
</div><!--carousel-->

<?php

	/* After widget. */
		
		echo $args['after_widget'];
		}

	/* Widget settings. */
	
	
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		/* Strip tags. */
		
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['number'] = strip_tags( $new_instance['number'] );
		$instance['tags'] = $new_instance['tags'];


		return $instance;
	}


	function form( $instance ) {

		/* Default widget settings. */
		
		$defaults = array( 'title' => 'Carousel Widget', 'number' => 20);
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>

		<!-- Widget Title-->
		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
			<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" />
		</p>
		
		<!-- Maximum number of posts -->
		<p>
		  <label for="<?php echo $this->get_field_id( 'number' ); ?>">Maximum number of posts to show:</label>
		  <input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo $instance['number']; ?>" size="3" />
		</p>
		
		<!-- Pick a tag to display -->
		<p>
		  <label for="<?php echo $this->get_field_id('tags'); ?>">IMPORTANT!!!! if you dont select a tag nothing will show on the slider. Select tag to display in slider:</label>
		  <select id="<?php echo $this->get_field_id('tags'); ?>" name="<?php echo $this->get_field_name('tags'); ?>" style="width:100%;">
			<option value='all' <?php if ('all' == (isset($instance['tags']))) echo 'selected="selected"'; ?>>No Tag Selected</option>
			<?php $tags = get_tags('hide_empty=0'); ?>
			<?php foreach($tags as $tag) { ?>
			<option value='<?php echo $tag->slug; ?>' <?php if(isset($instance['tags'])){ if ($tag->slug == $instance['tags']) echo 'selected="selected"';} ?>><?php echo $tag->name; ?></option>
			<?php } ?>
		  </select>
		</p>
<?php }}?>