<?php 

add_action('widgets_init','Lioit_social_counter');

function Lioit_social_counter() {
	register_widget('Lioit_social_counter');
	}

class Lioit_social_counter extends WP_Widget {
	function Lioit_social_counter() {
			
		$widget_ops = array('classname' => 'Lioit_social_counter','description' => __('Widget display a count of your rss subscribers, Twitter followers, facebook fans','lioit'));
		
		$this->WP_Widget('Lioit_social_counter',__('( LioIT ) Social Counter','lioit'),$widget_ops);

		}
		
	function widget( $args, $instance ) {
		extract( $args );
		/* User-selected settings. */
	$title = apply_filters('widget_title', $instance['title'] );
	$twitter = $instance['twitter'];
	$facebook = $instance['facebook'];
	$rss_name = $instance['rss_name'];
	$rss_text = $instance['rss_text'];


		

?>

				
	<?php 
	
		function get_facebook_likes($page_name)
		{
			$fb_data = json_decode(file_get_contents('http://graph.facebook.com/'.$page_name.''));
			return($fb_data->likes);
		}
		
		function get_twitter_followers($user_name)
		{
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_URL, "http://api.twitter.com/1/users/show.xml?screen_name=".$user_name);
			$data = curl_exec($ch);
			curl_close($ch);
			$xml = new SimpleXMLElement($data);
			return($xml->followers_count);
		
		}
	?>		

		 					
                        							<div class="widget">
                        							
                        	<div class="socialBoxs">
                            	<div class="socialBox twitterBox">
                                	<div class="socialBoxData">
                                        <a href="http://www.twitter.com/<?php echo $twitter ?>" target="_blank" title="">
                                        <img alt="" src="<?php echo Lioit_IMG; ?>/icons/social/twitter.png">
                                        </a>
                                        <span class="socialBoxCount"><?php echo get_twitter_followers($twitter);?></span>
                                        <a href="http://www.twitter.com/<?php echo $twitter ?>"><?php _e('Followers', 'lioit'); ?></a>
                                    </div><!--End SocialBox Data-->
                                </div><!--End socialBox-->
                                <div class="socialBox facebookBox">
                                	<div class="socialBoxData">
                                        <a href="http://www.facebook.com/<?php echo $facebook ?>" target="_blank" title="">
                                        <img alt="" src="<?php echo Lioit_IMG; ?>/icons/social/facebook.png">
                                        </a>
                                        <span class="socialBoxCount"><?php echo get_facebook_likes($facebook); ?></span>
                                        <a href="http://www.facebook.com/<?php echo $facebook ?>"><?php _e('Fans', 'lioit'); ?></a>
                                    </div><!--End SocialBox Data-->
                                </div><!--End socialBox-->
                                <div class="socialBox rssBox">
                                	<div class="socialBoxData">
                                        <a href="http://feeds.feedburner.com/<?php echo $rss_name ?>" target="_blank" title="">
                                        <img alt="" src="<?php echo Lioit_IMG; ?>/icons/social/rss.png">
                                        </a>
                                        <span class="socialBoxCount"><?php echo $rss_text; ?></span>
                                        <a href="http://feeds.feedburner.com/<?php echo $rss_name ?>"><?php _e('Subscribers', 'lioit'); ?></a>
                                    </div><!--End SocialBox Data-->
                                </div><!--End socialBox-->
                            </div><!--End SocialBoxs-->
                        </div><!--End Widget-->
                       
<?php 
		
	}
	
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		/* Strip tags (if needed) and update the widget settings. */
			$instance['title'] = strip_tags( $new_instance['title'] );
			$instance['twitter'] = $new_instance['twitter'];
			$instance['facebook'] = $new_instance['facebook'];
			$instance['rss_name'] = $new_instance['rss_name'];
			$instance['rss_text'] = $new_instance['rss_text'];


		return $instance;
	}
	
function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array( 
			'title' => '',
			'twitter' => 'Lioit',
			'facebook' => 'Lioit',
			'rss_name' => 'Lioit',
			'rss_text' => '1000+'
 			);
		$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" />
		</p>

		<p>
		<label for="<?php echo $this->get_field_id( 'rss_text' ); ?>"><?php _e('rss number', 'lioit'); ?></label>
		<input id="<?php echo $this->get_field_id( 'rss_text' ); ?>" name="<?php echo $this->get_field_name( 'rss_text' ); ?>" value="<?php echo $instance['rss_text']; ?>" class="widefat" />
		</p>

		<p>
		<label for="<?php echo $this->get_field_id( 'rss_name' ); ?>"><?php _e('rss name', 'lioit'); ?></label>
		<input id="<?php echo $this->get_field_id( 'rss_name' ); ?>" name="<?php echo $this->get_field_name( 'rss_name' ); ?>" value="<?php echo $instance['rss_name']; ?>" class="widefat" />
		</p>

	


		<p>
		<label for="<?php echo $this->get_field_id( 'twitter' ); ?>"><?php _e('Twitter Name', 'lioit'); ?></label>
		<input id="<?php echo $this->get_field_id( 'twitter' ); ?>" name="<?php echo $this->get_field_name( 'twitter' ); ?>" value="<?php echo $instance['twitter']; ?>" class="widefat" />
		</p>

		<p>
		<label for="<?php echo $this->get_field_id( 'facebook' ); ?>"><?php _e('facebook page ID (<a target="_blank" href="http://hellboundbloggers.com/2010/07/10/find-facebook-profile-and-page-id/">more Info</a>)', 'lioit'); ?></label>
		<input id="<?php echo $this->get_field_id( 'facebook' ); ?>" name="<?php echo $this->get_field_name( 'facebook' ); ?>" value="<?php echo $instance['facebook']; ?>" class="widefat" />
		</p>


   <?php 
}
	} //end class