<?php

add_action( 'widgets_init', 'latest_tweet_widget' );
function latest_tweet_widget() {
	register_widget( 'Latest_Tweets' );
}
class Latest_Tweets extends WP_Widget {

	function Latest_Tweets() {
		$widget_ops = array( 'classname' => 'twitter-widget'  );
		$control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'latest-tweets-widget' );
		$this->WP_Widget('latest-tweets',__('( LioIT ) Latest Twitts', 'lioit'),$widget_ops);
	}
	
	function widget( $args, $instance ) {
		extract( $args );

		$title = apply_filters('widget_title', $instance['title'] );
		$no_of_tweets = $instance['no_of_tweets'];
		$accounts = $instance['accounts'];
		$avatar = $instance['avatar'];

		echo $before_widget;
		
		

		if ( $title )
			echo $before_title; ?>
			<a href="http://twitter.com/<?php echo $accounts ?>"><?php echo $title ; ?></a>
		<?php echo $after_title; ?>

		<div id="twitter-widget">
			<div class="tweet"></div>
		</div>
			<script type='text/javascript'>
				jQuery(document).ready(function(){
					jQuery(".tweet").tweet({
						username: "<?php echo $accounts ?>",
						<?php if($avatar) echo 'avatar_size:32,' ?>
						count: <?php echo $no_of_tweets ?>,
						loading_text: '<span></span>',
					});
				});
			</script>

	<?php 
		echo $after_widget;
	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['no_of_tweets'] = strip_tags( $new_instance['no_of_tweets'] );
		$instance['accounts'] = strip_tags( $new_instance['accounts'] );
		$instance['avatar'] = strip_tags( $new_instance['avatar'] );
		return $instance;
	}

	function form( $instance ) {
		$defaults = array( 'title' =>__('@Follow Me' , 'tie') , 'no_of_tweets' => '5', 'avatar' => '1', 'accounts' => 'Lioit' );
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>

		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title : ' , 'lioit'); ?></label>
			<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" class="widefat" type="text" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'no_of_tweets' ); ?>"><?php _e('No of Tweets to show : ' , 'lioit'); ?></label>
			<input id="<?php echo $this->get_field_id( 'no_of_tweets' ); ?>" name="<?php echo $this->get_field_name( 'no_of_tweets' ); ?>" value="<?php echo $instance['no_of_tweets']; ?>" type="text" size="3" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'accounts' ); ?>"><?php _e('Twitter username : ' , 'lioit'); ?></label>
			<input id="<?php echo $this->get_field_id( 'accounts' ); ?>" name="<?php echo $this->get_field_name( 'accounts' ); ?>" value="<?php echo $instance['accounts']; ?>" class="widefat" type="text" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'avatar' ); ?>"><?php _e('Display the avatar : ' , 'lioit'); ?></label>
			<input id="<?php echo $this->get_field_id( 'avatar' ); ?>" name="<?php echo $this->get_field_name( 'avatar' ); ?>" value="true" <?php if( $instance['avatar'] ) echo 'checked="checked"'; ?> type="checkbox" />
		</p>

	<?php
	}
}
?>