<?php
   add_action('widgets_init', 'tigu_carousel_home_load_widgets');
   function tigu_carousel_home_load_widgets()
   {
      register_widget('Carousel_Home');
   }
      class Carousel_Home extends WP_Widget {
   function Carousel_Home()
   {
      $widget_ops = array(
     'classname' => 'tigu_carousel_home',
     'description' => 'Display a scroller widget on home page with most popular posts.');
      $control_ops = array(
     'id_base' => 'tigu_carousel_home');
      $this->WP_Widget('tigu_carousel_home', 'GAMELEON: Big Scroller', $widget_ops, $control_ops);
   }
   function widget($args, $instance)
   {
      extract($args);

/* User selected settings. */
      $title = $instance['title'];
      $post_type = $instance['post_type'];
      $categories = $instance['categories'];
      $exclude_cat = !empty($instance['exclude_cat']) ? $instance['exclude_cat'] : NULL;
      $posts = $instance['posts'];
      $autoplay = isset($instance['autoplay']) ? 'true' : 'false';
      echo $before_widget;
   ?>

<!-- START WIDGET -->

      <div class="scroll-grid full">

      <h3><a href="<?php echo get_category_link($categories); ?>"><?php echo $title; ?></a>
          <a class="btn_next" id="next_<?php echo $this->number; ?>" href="#"><span>next</span></a>
          <a class="btn_prev" id="prev_<?php echo $this->number; ?>" href="#"><span>prev</span></a>
      </h3>
<?php
    $posts_per_page = $posts;
    $popular_posts = new WP_Query();
    $popular_posts->query('showposts='.$posts_per_page.'&orderby=comment_count&cat='.$exclude_cat.'');
     
?>


<?php if($autoplay == 'true'): ?>
      <div class="Carousel" style="height:258px" id="Carousel_<?php echo $this->number; ?>">
      <ul>
<?php
      $post_types = get_post_types();
      unset($post_types['page'], $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;
   }
      if($categories != 'all') {
      $categories_array = array($categories);
   }
?>
<?php while ($popular_posts->have_posts()) : $popular_posts->the_post(); ?>
<?php if(has_post_format('video') || has_post_format('audio') || has_post_format('gallery')) {
    $icon = '<span class="' . get_post_format($post->ID) . '-icon"></span>';
  } else {
      $icon = '';
  }
?>
<?php if(has_post_thumbnail() || is_myarcade_game() ): ?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'bigscroller-thumb'); ?>
<?php if ( empty($image[0]) ) $image[0] = myarcade_featured_image (); ?>



<li>
<div class="block grid-caroimage">

<a class=<?php if( get_option('tigu_template_type', 'WordPres Default') == 'WordPres Default') : ?>"hover_play_small-wp"<?php else: ?>"hover_play_small"<?php endif; ?> href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>
<img src="<?php echo $image[0]; ?>" width="183" height="120" alt="<?php the_title(); ?>" />
</a><?php echo $icon; ?>

<h2><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>

<?php if(get_option('tigu_shorten_titles') == 'true'): ?>
<?php $shorttitle = substr(the_title('','',FALSE),0,26); ?><?php echo $shorttitle; if (strlen($shorttitle) >25){ echo '&hellip;'; } ?>
<?php else: ?>
<?php the_title(); ?>
<?php endif; ?>
</a></h2>

<div class="textscroller">
<span class="block-meta"><?php the_time('F j, Y') ?>, 
<?php if( get_option('tigu_comments_type', 'WordPress') == 'WordPress') : ?>
<?php comments_popup_link(); ?>
<?php else: ?>
<fb:comments-count href="<?php echo get_permalink($post->ID); ?>"></fb:comments-count> <?php _e('COMMENTS', 'Gameleon'); ?>
<?php endif; ?>
</span>
<?php echo get_excerpt(90); ?>
      </div>
      </div>
      </li>

<?php else: ?>
     <li>
<div class="block grid-caroimage">

     <?php echo $icon; ?><a class=<?php if( get_option('tigu_template_type', 'WordPres Default') == 'WordPres Default') : ?>"hover_play_small-wp"<?php else: ?>"hover_play_small"<?php endif; ?> href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>
<img src="<?php echo get_template_directory_uri(); ?>/images/thumbnail.png" width="183" height="120" alt="<?php the_title(); ?>" />
</a>

<h2><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>
<?php if(get_option('tigu_shorten_titles') == 'true'): ?>
<?php $shorttitle = substr(the_title('','',FALSE),0,26); ?><?php echo $shorttitle; if (strlen($shorttitle) >25){ echo '&hellip;'; } ?>
<?php else: ?>
<?php the_title(); ?>
<?php endif; ?></a></h2>
<div class="textscroller">
<span class="block-meta"><?php the_time('F j, Y') ?>, 

<?php if( get_option('tigu_comments_type', 'WordPress') == 'WordPress') : ?>
<?php comments_popup_link(); ?>
<?php else: ?>
<fb:comments-count href="<?php echo get_permalink($post->ID); ?>"></fb:comments-count> <?php _e('COMMENTS', 'Gameleon'); ?>
<?php endif; ?>
</span>
<?php echo get_excerpt(90); ?>
      </div>
      </div>
      </li>
<?php endif; ?> 
<?php endwhile; ?>
      </ul>
      </div>
</div>
<!-- ////////////////////////////////////////// -->

<?php else: ?>
      <div class="Carouselstatic" style="height:255px" id="Carouselstatic_<?php echo $this->number; ?>">

      <ul>
<?php
      $post_types = get_post_types();
      unset($post_types['page'], $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;
   }
      if($categories != 'all') {
      $categories_array = array($categories);
   }
?>
<?php while ($popular_posts->have_posts()) : $popular_posts->the_post(); ?>
<?php if(has_post_thumbnail() || is_myarcade_game() ): ?>
<?php if(has_post_format('video') || has_post_format('audio') || has_post_format('gallery')) {
      $icon = '<span class="' . get_post_format($post->ID) . '-icon"></span>';
   } else {
      $icon = '';
   }
?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'bigscroller-thumb'); ?>
<?php if ( empty($image[0]) ) $image[0] = myarcade_featured_image (); ?>        
<li>
<div class="block grid-caroimage">
<a class=<?php if( get_option('tigu_template_type', 'WordPres Default') == 'WordPres Default') : ?>"hover_play_small-wp"<?php else: ?>"hover_play_small"<?php endif; ?> href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>

<img src="<?php echo $image[0]; ?>" width="183" height="120" alt="<?php the_title(); ?>" />
</a><?php echo $icon; ?>

<h2><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>
<?php if(get_option('tigu_shorten_titles') == 'true'): ?>
<?php $shorttitle = substr(the_title('','',FALSE),0,26); ?><?php echo $shorttitle; if (strlen($shorttitle) >25){ echo '&hellip;'; } ?>
<?php else: ?>
<?php the_title(); ?>
<?php endif; ?>
</a></h2>

<div class="textscroller">
<span class="block-meta"><?php the_time('F j, Y') ?>, 

<?php if( get_option('tigu_comments_type', 'WordPress') == 'WordPress') : ?>
<?php comments_popup_link(); ?>
<?php else: ?>
<fb:comments-count href="<?php echo get_permalink($post->ID); ?>"></fb:comments-count> <?php _e('COMMENTS', 'Gameleon'); ?>
<?php endif; ?>

</span>
<?php echo get_excerpt(90); ?>
      </div>
      </div>
      </li>

<?php else: ?>
     <li>
<div class="block grid-caroimage">
 
     <?php echo $icon; ?><a class=<?php if( get_option('tigu_template_type', 'WordPres Default') == 'WordPres Default') : ?>"hover_play_small-wp"<?php else: ?>"hover_play_small"<?php endif; ?> href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>

<img src="<?php echo get_template_directory_uri(); ?>/images/thumbnail.png" width="183" height="120" alt="<?php the_title(); ?>" />

</a>
 
<h2><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>
<?php if(get_option('tigu_shorten_titles') == 'true'): ?>
<?php $shorttitle = substr(the_title('','',FALSE),0,26); ?><?php echo $shorttitle; if (strlen($shorttitle) >25){ echo '&hellip;'; } ?>
<?php else: ?>
<?php the_title(); ?>
<?php endif; ?>
</a></h2>
<div class="textscroller">
<span class="block-meta"><?php the_time('F j, Y') ?>, 

<?php if( get_option('tigu_comments_type', 'WordPress') == 'WordPress') : ?>
<?php comments_popup_link(); ?>
<?php else: ?>
<fb:comments-count href="<?php echo get_permalink($post->ID); ?>"></fb:comments-count> <?php _e('COMMENTS', 'Gameleon'); ?>
<?php endif; ?>

</span>
<?php echo get_excerpt(90); ?>
      </div>
      </div>
      </li>
<?php endif; ?> 
<?php endwhile; ?>
      </ul>
      </div>
      </div>
<!-- END WIDGET -->
<?php endif; ?> 
<?php
      echo $after_widget;
   }
   function update($new_instance, $old_instance)
   {
      $instance = $old_instance;
      $instance['title'] = $new_instance['title'];
      $instance['post_type'] = $new_instance['post_type'];
      $instance['categories'] = $new_instance['categories'];
      $instance['exclude_cat'] =  $new_instance['exclude_cat'];
      $instance['posts'] = $new_instance['posts'];
      $instance['autoplay'] = $new_instance['autoplay'];
      return $instance;
      }
   function form($instance)
   {
      $defaults = array(
            'title' => 'Most Popular Posts',
            'post_type' => 'all',
            'autoplay' => 'on',
            'categories' => 'all',
            'exclude_cat' => '',
            'posts' => 6);
      $instance = wp_parse_args((array) $instance, $defaults); ?>

<!-- START ADMIN WIDGETS AREA -->

      <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('exclude_cat'); ?>">Exclude categories by ID:</label>
         <input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('exclude_cat'); ?>" name="<?php echo $this->get_field_name('exclude_cat'); ?>" value="<?php echo $instance['exclude_cat']; ?>" />
     <br />
      <small>You may exclude some categories by typing their IDs, with minus sign in front of them, separated by comma and no spaces. Example: -3,-4,-21 for multiple IDs and just -3 without comma, for one ID only. Just leave it empty if you do not need to exclude any categories.</small>
      </p>
      <p>
         <label for="<?php echo $this->get_field_id('posts'); ?>">Number of games to scroll:</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']; ?>" />
      <br />
      <small>The widget will allways show 3 games, so you should set number of games to scroll greater than 3</small>
      </p>

      <p>
         <input class="checkbox" type="checkbox" <?php checked($instance['autoplay'], 'on'); ?> id="<?php echo $this->get_field_id('autoplay'); ?>" name="<?php echo $this->get_field_name('autoplay'); ?>" />
         <label for="<?php echo $this->get_field_id('autoplay'); ?>">Autoplay:</label>
      </p>

<!-- END ADMIN WIDGETS AREA -->

<?php }
}
?>