<?php

/* see https://developers.facebook.com/docs/plugins/page-plugin */

class Intense_Widget_Facebook_Page extends WP_Widget {

	function __construct() {
		$widget_options = array(
			'classname'   => 'facebook_page',
			'description' => __( 'Shows Facebook page information', 'intense' )
		);
		$control_options = array( 'id_base' => 'facebook-page-widget' );

		parent::__construct( 'facebook-page-widget', '(Intense) Facebook Page', $widget_options, $control_options );
	}

	function widget( $args, $instance ) {
		extract( $args );

		$page_url  = isset( $instance['page_url'] ) ? $instance['page_url'] : '';
		$widget_width = isset( $instance['width'] ) ? $instance['width'] : 268;
		$friends = isset( $instance['friends'] ) ? 'true' : 'false';
		$posts = isset( $instance['posts'] ) ? 'true' : 'false';
		$cover_photo = isset( $instance['cover_photo'] ) ? 'false' : 'true';
		$small_header = isset( $instance['small_header'] ) ? 'true' : 'false';
		$height = '65';

		$height = ( 'true' == $friends ) ? '240' : $height;
		$height = ( 'true' == $posts ) ? '515' : $height;
		$height = ( 'true' == $posts && 'true' == $friends && ( 'true' == $cover_photo || 'false' == $cover_photo ) ) ? '540' : $height;		
		$height = ( 'true' == $cover_photo ) ? $height + 30 : $height;

		echo $before_widget;

		$title = apply_filters( 'widget_title', isset( $instance['title'] ) ? $instance['title'] : '' );		

		if ( $title ) {
			echo $before_title . $title . $after_title;
		}
		?>

		<?php if ( $page_url ) : ?>
			<script>
			 	window.fbAsyncInit = function() {
				    FB.init({
				      //appId      : 'your-app-id',			      
				      xfbml      : true,
				      version    : 'v2.4'
				    });
				};

				(function(d, s, id){
			     var js, fjs = d.getElementsByTagName(s)[0];
			     if (d.getElementById(id)) {return;}
			     js = d.createElement(s); js.id = id;
			     js.src = "//connect.facebook.net/<?php echo get_locale(); ?>/sdk.js";
			     fjs.parentNode.insertBefore(js, fjs);
			    }(document, 'script', 'facebook-jssdk'));				

				jQuery(window).resize(function() {
					var $container_width = jQuery( '.<?php echo $args['widget_id']; ?>' ).width();

					if ( $container_width != jQuery('.<?php echo $args['widget_id']; ?> .fb-page' ).data( 'width' ) ) {
						jQuery('.<?php echo $args['widget_id']; ?> .fb-page' ).attr( 'data-width', $container_width );
						FB.XFBML.parse();
					}
				});
			</script>

			<div class="fb-page-container <?php echo $args['widget_id']; ?>" id="fb-root-<?php echo $args['widget_id']; ?>">
				<div class="fb-page" 
					data-href="<?php echo $page_url; ?>" 
					data-width="<?php echo $widget_width; ?>" 
					data-height="<?php echo $height; ?>" 
					data-hide-cover="<?php echo $cover_photo; ?>" 
					data-show-facepile="<?php echo $friends; ?>" 
					data-show-posts="<?php echo $posts; ?>"					
					data-small-header="<?php echo $small_header; ?>" 
					data-adapt-container-width="true"				
				>
				</div>				
			</div>
		<?php endif;

		echo $after_widget;

	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['page_url'] = $new_instance['page_url'];
		$instance['width'] = $new_instance['width'];
		$instance['small_header'] = $new_instance['small_header'];
		$instance['cover_photo'] = $new_instance['cover_photo'];
		$instance['friends'] = $new_instance['friends'];
		$instance['posts'] = $new_instance['posts'];
		
		return $instance;
	}

	function form( $instance ) {
		$defaults = array(
			'title' => __( 'Find us on Facebook', 'intense' ),			
			'page_url' => '',
			'width' => '292',
			'friends' => 'on',
			'posts' => false,
			'cover_photo' => false,
			'small_header' => false
		);

		$instance = wp_parse_args( ( array ) $instance, $defaults ); ?>		

		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'intense' ); ?></label>
			<input class="widefat" type="text" 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( 'page_url' ); ?>"><?php _e( 'Page URL:', 'intense' ); ?></label>
			<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'page_url' ); ?>" name="<?php echo $this->get_field_name( 'page_url' ); ?>" value="<?php echo $instance['page_url']; ?>" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'width' ); ?>"><?php _e( 'Width (Min. 180 to Max. 500):', 'intense' ); ?></label>
			<input class="widefat" type="text" style="width: 80px;" id="<?php echo $this->get_field_id( 'width' ); ?>" name="<?php echo $this->get_field_name( 'width' ); ?>" value="<?php echo $instance['width']; ?>" />
		</p>
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['small_header'], 'on' ); ?> id="<?php echo $this->get_field_id( 'small_header' ); ?>" name="<?php echo $this->get_field_name( 'small_header' ); ?>" />
			<label for="<?php echo $this->get_field_id( 'small_header' ); ?>"><?php _e( 'Use Small Header', 'intense' ); ?></label>
		</p>
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['cover_photo'], 'on' ); ?> id="<?php echo $this->get_field_id( 'cover_photo' ); ?>" name="<?php echo $this->get_field_name( 'cover_photo' ); ?>" />
			<label for="<?php echo $this->get_field_id( 'cover_photo' ); ?>"><?php _e( 'Hide Cover Photo', 'intense' ); ?></label>
		</p>
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['friends'], 'on' ); ?> id="<?php echo $this->get_field_id( 'friends' ); ?>" name="<?php echo $this->get_field_name( 'friends' ); ?>" />
			<label for="<?php echo $this->get_field_id( 'friends' ); ?>"><?php _e( 'Show Friend\'s Faces', 'intense' ); ?></label>
		</p>
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['posts'], 'on' ); ?> id="<?php echo $this->get_field_id( 'posts' ); ?>" name="<?php echo $this->get_field_name( 'posts' ); ?>" />
			<label for="<?php echo $this->get_field_id( 'posts' ); ?>"><?php _e( 'Show Page Posts', 'intense' ); ?></label>
		</p>
	<?php
	}
}
