<?php

/*	##################################
   	REGISTER SIDEBARS
	################################## */

if (function_exists('register_sidebar')) :

	register_sidebar(array(
	'name' => 'Sidebar',
	'before_widget' => '<div id="%1$s" class="sb-box">',
	'after_widget' => '</div>',
	'before_title' => '<h3 class="sb-title">',
	'after_title' => '</h3>'));
	
	register_sidebar(array(
	'name' => 'Sidebar-Home',
	'before_widget' => '<div id="%1$s" class="sb-box">',
	'after_widget' => '</div>',
	'before_title' => '<h3 class="sb-title">',
	'after_title' => '</h3>'));
	
	register_sidebar(array(
	'name' => 'Sidebar-Category',
	'before_widget' => '<div id="%1$s" class="sb-box">',
	'after_widget' => '</div>',
	'before_title' => '<h3 class="sb-title">',
	'after_title' => '</h3>'));
	
	register_sidebar(array(
	'name' => 'Sidebar-Post',
	'before_widget' => '<div id="%1$s" class="sb-box">',
	'after_widget' => '</div>',
	'before_title' => '<h3 class="sb-title">',
	'after_title' => '</h3>'));
	
	register_sidebar(array(
	'name' => 'Sidebar-Page',
	'before_widget' => '<div id="%1$s" class="sb-box">',
	'after_widget' => '</div>',
	'before_title' => '<h3 class="sb-title">',
	'after_title' => '</h3>'));
        
endif;


/*	##################################
	TWITTER WIDGET
	################################## */

class TS_Twitter extends WP_Widget {
 
	function TS_Twitter() {
	
		global $options;
		
        $widget_ops = array('classname' => 'widget_ts_twitter', 'description' => __('Display latest tweets in', TS_DOMAIN).' '.TS_THEME );
		$this->WP_Widget('ts_twitter', TS_THEME.' '.__('Twitter', TS_DOMAIN), $widget_ops);
    
    }
 
    function widget($args, $instance) {
    
    	global $options;
       
        extract( $args );
        
        $title	= empty($instance['title']) ? __('Latest Tweets', TS_DOMAIN) : $instance['title'];
        $user	= empty($instance['user']) ? ts_get_option('ts_twitter') : $instance['user'];
        $link	= $instance['twitter_link'] ? '1' : '0';
        $label	= empty($instance['twitter_label']) ? __('More on Twitter', TS_DOMAIN) : $instance['twitter_label'];
        if ( !$nr = (int) $instance['twitter_nr'] )
			$nr = 5;
		else if ( $nr < 1 )
			$nr = 1;
		else if ( $nr > 15 )
			$nr = 15;
 
        ?>
			<?php echo $before_widget; ?>
				<?php echo $before_title . $title . $after_title; ?>
				
				<div id="twitter_div">
    				<ul id="twitter_update_list"><li></li></ul>
    			</div>
                  
                <script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
    			<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/<?php echo $user; ?>.json?callback=twitterCallback2&amp;count=<?php echo $nr; ?>"></script>
                  
                <?php if($link) : ?>
                <p align="right"><a href="http://twitter.com/<?php echo $user; ?>"><?php echo $label; ?></a></p>
                <?php endif; ?>
 
			<?php echo $after_widget; ?>
        <?php
    }

    function update($new_instance, $old_instance) {  
    
    	$instance['title'] = strip_tags($new_instance['title']);
    	$instance['user'] = strip_tags($new_instance['user']);
    	$instance['twitter_link'] = $new_instance['twitter_link'] ? 1 : 0;
    	$instance['twitter_label'] = strip_tags($new_instance['twitter_label']);
    	$instance['twitter_nr'] = (int) $new_instance['twitter_nr'];
                  
        return $new_instance;
    }
 
    function form($instance) {
    
    	global $options;
        
		$instance	= wp_parse_args( (array) $instance, array( 'title' => '', 'user' => '', 'twitter_link' => '', 'twitter_label' => '', 'twitter_nr' => '') );
		$title		= empty($instance['title']) ? __('Latest Tweets', TS_DOMAIN) : $instance['title'];
		$user		= $instance['user'];
		$link 		= $instance['twitter_link'] ? 1 : 0;
		$label		= empty($instance['twitter_label']) ? __('More on Twitter', TS_DOMAIN) : $instance['twitter_label'];
		if (!$nr = (int) $instance['twitter_nr']) $nr = 5;
?>

		<p>
			<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title'); ?>:
			<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" />
			</label>
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id('user'); ?>"><?php _e('User'); ?>:
			<input class="widefat" id="<?php echo $this->get_field_id('user'); ?>" name="<?php echo $this->get_field_name('user'); ?>" type="text" value="<?php echo attribute_escape($user); ?>" />
			</label>
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id('twitter_nr'); ?>"><?php _e('Number of tweets to show', TS_DOMAIN); ?>:</label>
			<input id="<?php echo $this->get_field_id('twitter_nr'); ?>" name="<?php echo $this->get_field_name('twitter_nr'); ?>" type="text" value="<?php echo $nr; ?>" size="3" /><br />
			<small><?php _e('(at most 15)'); ?></small>
		</p>
		
		<p>
			<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('twitter_link'); ?>" name="<?php echo $this->get_field_name('twitter_link'); ?>"<?php checked($link); ?> />
			<label for="<?php echo $this->get_field_id('twitter_link'); ?>"><?php _e('Show link to Twitter', TS_DOMAIN); ?></label>		
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id('twitter_label'); ?>"><?php _e('Link label', TS_DOMAIN); ?>:
			<input class="widefat" id="<?php echo $this->get_field_id('twitter_label'); ?>" name="<?php echo $this->get_field_name('twitter_label'); ?>" type="text" value="<?php echo attribute_escape($label); ?>" />
			</label>
		</p>
		
<?php
	}

}
 
register_widget('TS_Twitter');


/*	##################################
	FLICKR WIDGET
	################################## */

class TS_Flickr extends WP_Widget {
 
	function TS_Flickr() {
		global $options;
        $widget_ops = array('classname' => 'widget_ts_flickr', 'description' => __('Display flickr badge in', TS_DOMAIN).' '.TS_THEME );
		$this->WP_Widget('ts_flickr', TS_THEME.' '.__('Flickr', TS_DOMAIN), $widget_ops);
    
    }
 
    function widget($args, $instance) {
    
    	global $options;
        
        extract( $args );
        
        $title	= empty($instance['title']) ? __('<span style="color:#3993ff">flick<span style="color:#ff1c92">r</span></span> fotos', TS_DOMAIN) : apply_filters('widget_title', $instance['title']);
        $user	= empty($instance['user']) ? ts_get_option('ts_flickr') : $instance['user'];
        
        if ( !$nr = (int) $instance['flickr_nr'] )
			$nr = 6;
		else if ( $nr < 1 )
			$nr = 3;
		else if ( $nr > 15 )
			$nr = 15;
 
        ?>
			<?php echo $before_widget; ?>
				<?php echo $before_title . $title . $after_title; ?>
				
    			<div id="flickr_badge_wrapper" class="clearfix"></div>

    			<script type="text/javascript">    			
					jQuery(document).ready(function(){    			
    					jQuery('#flickr_badge_wrapper').jflickrfeed({
							limit: <?php echo $nr; ?>,
							qstrings: {
								id: '<?php echo $user; ?>'
							},
							itemTemplate:
							'<div class="flickr_badge_image">' +
								'<a href="{{link}}" title="{{title}}" rel="prettyPhoto">' +
									'<img src="{{image_s}}" alt="{{title}}" />' +
								'</a>' +
							'</div>'
						}, function(data) {
							jQuery('#flickr_badge_wrapper a').fancybox();
						});
					});    			
    			</script>    			
 
			<?php echo $after_widget; ?>
        <?php
    }

    function update($new_instance, $old_instance) {  
    
    	$instance['title'] = strip_tags($new_instance['title']);
    	$instance['user'] = strip_tags($new_instance['user']);
    	$instance['flickr_nr'] = (int) $new_instance['flickr_nr'];
                  
        return $new_instance;
    }
 
    function form($instance) {
    
    	global $options;
        
		$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'user' => '', 'flickr_nr' => '') );
		$title = strip_tags($instance['title']);
		$user = empty($instance['user']) ? ts_get_option('ts_flickr') : $instance['user'];
		if (!$nr = (int) $instance['flickr_nr']) $nr = 6;
?>

		<p>
			<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title'); ?>:
			<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" />
			</label>
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id('user'); ?>"><?php _e('User'); ?>:
			<input class="widefat" id="<?php echo $this->get_field_id('user'); ?>" name="<?php echo $this->get_field_name('user'); ?>" type="text" value="<?php echo attribute_escape($user); ?>" />
			</label>
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id('flickr_nr'); ?>"><?php _e('Number of fotos to show', TS_DOMAIN); ?>:</label>
			<input id="<?php echo $this->get_field_id('flickr_nr'); ?>" name="<?php echo $this->get_field_name('flickr_nr'); ?>" type="text" value="<?php echo $nr; ?>" size="3" /><br />
			<small><?php _e('(at most 15)'); ?></small>
		</p>
		
<?php
	}

}
 
register_widget('TS_Flickr');


/*	##################################
   	RECENT COMMENTS WIDGET
	################################## */ 
	
class TS_Comments extends WP_Widget {
 
	function TS_Comments() {
		global $options;
        $widget_ops = array('classname' => 'widget_ts_comments', 'description' => __('Display recent comments in', TS_DOMAIN).' '.TS_THEME );
		$this->WP_Widget('ts_comments', TS_THEME.' '.__('Comments', TS_DOMAIN), $widget_ops);
    
    }
 
    function widget($args, $instance) {
    
    	global $options;
       
        extract( $args );
        
        $title	= empty($instance['title']) ? __('Recent Comments', TS_DOMAIN) : apply_filters('widget_title', $instance['title']);
        
        if ( !$nr = (int) $instance['comments_nr'] )
			$nr = 5;
		else if ( $nr < 1 )
			$nr = 1;
		else if ( $nr > 15 )
			$nr = 15;
			
		if ( !$exc = (int) $instance['comments_exc'] ) $exc = 75;		
 
        ?>
			<?php echo $before_widget; ?>
			
				<?php echo $before_title . $title . $after_title; ?>
				
				<?php ts_recent_comments($nr,$exc); ?>
 
			<?php echo $after_widget; ?>
        <?php
    }

    function update($new_instance, $old_instance) {  
    
    	$instance['title'] = strip_tags($new_instance['title']);
    	$instance['comments_nr'] = (int) $new_instance['comments_nr'];
    	$instance['comments_exc'] = (int) $new_instance['comments_exc'];
    	$instance['boxed'] = $new_instance['boxed'] ? 1 : 0;
                  
        return $new_instance;
    }
 
    function form($instance) {
    
    	global $options;
        
		$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'comments_nr' => '') );
		$title = strip_tags($instance['title']);
		if (!$nr = (int) $instance['comments_nr']) $nr = 5;
		if (!$exc = (int) $instance['comments_exc']) $exc = 75;
?>

		<p>
			<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title'); ?>:
			<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" />
			</label>
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id('comments_nr'); ?>"><?php _e('Number of comments', TS_DOMAIN); ?>:</label>
			<input id="<?php echo $this->get_field_id('comments_nr'); ?>" name="<?php echo $this->get_field_name('comments_nr'); ?>" type="text" value="<?php echo $nr; ?>" size="3" /><br />
			<small><?php _e('(at most 15)'); ?></small>
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id('comments_exc'); ?>"><?php _e('Length of comment excerpt', TS_DOMAIN); ?>:</label>
			<input id="<?php echo $this->get_field_id('comments_exc'); ?>" name="<?php echo $this->get_field_name('comments_exc'); ?>" type="text" value="<?php echo $exc; ?>" size="3" />
		</p>
		
<?php
	}

}

function ts_recent_comments($rc_count=5, $rc_length=75, $rc_pre='<ul>', $rc_post='</ul>') {

	global $wpdb, $options;
	
	$sql = "SELECT DISTINCT ID,
			post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type, 
			SUBSTRING(comment_content,1,$rc_length) AS com_excerpt 
			FROM $wpdb->comments 
			LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) 
			WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' 
			ORDER BY comment_date_gmt DESC 
			LIMIT $rc_count";
			
	$comments = $wpdb->get_results($sql);
	
	$output = $rc_pre;
	foreach ($comments as $comment) {
		$com_length = strlen($comment->com_excerpt);
		if ($com_length < $rc_length) : $rc_dots = ""; else : $rc_dots = "&hellip;"; endif;
		$output .= "\n\t<li class=\"recent-comment\">" . $comment->comment_author . " ".__('on', TS_DOMAIN)." <a href=\"" . get_permalink($comment->ID) . "#comment-" . $comment->comment_ID  . "\" title=\"" . $comment->post_title . "\">" . $comment->post_title . "</a>:<br />" . strip_tags($comment->com_excerpt) . $rc_dots . "</li>";
	}
	$output .= $rc_post;
		
	echo $output;

}

register_widget('TS_Comments');


/*	##################################
	UNREGISTER WIDGETS
	################################## */
	
function unregister_default_wp_widgets() {

    // unregister_widget('WP_Widget_Recent_Posts');
}

add_action('widgets_init', 'unregister_default_wp_widgets', 1);


?>