<?php
require_once INTENSE_PLUGIN_FOLDER . '/inc/Intense-photosource.php';

class Intense_Widget_Gallery extends WP_Widget {

	function __construct() {
		$widget_options = array( 'classname' => 'gallery',
			'description' => 'Your most recent photos gallery' );
		$control_options = array( 'id_base' => 'gallery-widget' );

		parent::__construct('gallery-widget', '(Intense) Gallery', $widget_options, $control_options);		
	}

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

		$number_of_photos = $instance['number_of_photos'];
		$photo_size = $instance['photo_size'];
		$user = $instance['user'];
		$setid = $instance['set'];
		$groupid = $instance['group'];
		$source = $instance['source'];
		$include = $instance['include'];
		$tag = $instance['tag'];
		$album = $instance['album'];
		$facebookpage = $instance['facebookpage'];
		$marginpercent = $instance['marginpercent'];
		$columns = $instance['columns'];

		$page = 0;

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

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

		/*
			'include' => wordpresssettings			
			'user' => flickrsettings googleplussettings 500pxsettings smugmugsettings instagramsettings deviantartsettings
			'groupid' => flickrsettings facebooksettings
			'facebookpage' => facebooksettings
			'tag' => instagramsettings
			'setid' => flickrsettings zenfoliosettings
			'album' => smugmugsettings googleplussettings facebooksettings			
		 */

		echo intense_run_shortcode( 'intense_gallery', array(
			'size' => $photo_size,
			'source' => $source,
			'user' => $user,
			'groupid' => $groupid,
			'setid' => $setid,
			'include' => $include,
			'tag' => $tag,
			'album' => $album,
			'facebookpage' => $facebookpage,
			'pagesize' => $number_of_photos,
			'page' => $page,
			'no_link' => true,
			'marginpercent' => $marginpercent,
			'columns' => $columns
		));

		echo $after_widget;
	}

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

		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['number_of_photos'] = $new_instance['number_of_photos'];
		$instance['photo_size'] = $new_instance['photo_size'];
		$instance['source'] = $new_instance['source'];
		$instance['user'] = $new_instance['user'];
		$instance['set'] = $new_instance['set'];
		$instance['group'] = $new_instance['group'];
		$instance['facebookpage'] = $new_instance['facebookpage'];
		$instance['album'] = $new_instance['album'];
		$instance['tag'] = $new_instance['tag'];
		$instance['include'] = $new_instance['include'];
		$instance['marginpercent'] = $new_instance['marginpercent'];
		$instance['columns'] = $new_instance['columns'];

		return $instance;
	}

	function form( $instance ) {
		$defaults = array( 
			'title' => 'Photos',
			'number_of_photos' => 6,
			'photo_size' => 'square75',
			'source' => 'flickr',
			'user' => '',
			'set' => '',
			'group' => '',
			'facebookpage' => '',
			'album' => '',
			'tag' => '',
			'include' => '',
			'marginpercent' => '',
			'columns' => '0'
		);

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

		$sources = array(
			'wordpress' => __( 'WordPress', 'intense' ),
			'500px' => __( '500px', 'intense' ),
			'deviantart' => __( 'DeviantART', 'intense' ),
			'facebook' => __( 'Facebook', 'intense' ),
			'flickr' => __( 'Flickr', 'intense' ),
			'google+' => __( 'Google+', 'intense' ),
			'instagram' => __( 'Instagram', 'intense' ),
			'smugmug' => __( 'SmugMug', 'intense' ),
			'zenfolio' => __( 'Zenfolio', 'intense' ),
		);

		$columns = array(
			'0' => __( 'Auto', 'intense' ),
			'1' => '1',
			'2' => '2',
			'3' => '3',
			'4' => '4',
			'5' => '5',
			'6' => '6',
			'7' => '7',
			'8' => '8',
			'9' => '9',
			'10' => '10'
		);

		$sizes = Intense_Photo_Source::get_wordpress_photo_sizes();

		$size_names = array_keys( $sizes );

		$sizes = array_merge( 			
			array_combine( $size_names, $size_names ) 
		);

		?>

		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'intense' ); ?></label>
			<input class="widefat" 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( 'number_of_photos' ); ?>"><?php _e( 'Number of photos to show:', 'intense' ); ?></label>
			<input class="widefat" style="width: 40px;" id="<?php echo $this->get_field_id( 'number_of_photos' ); ?>" name="<?php echo $this->get_field_name( 'number_of_photos' ); ?>" value="<?php echo $instance['number_of_photos']; ?>" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'marginpercent' ); ?>"><?php _e( 'Image Margin:', 'intense' ); ?></label>
			<input class="widefat" style="width: 40px;" id="<?php echo $this->get_field_id( 'marginpercent' ); ?>" name="<?php echo $this->get_field_name( 'marginpercent' ); ?>" value="<?php echo $instance['marginpercent']; ?>" /><br>
			(<?php _e( 'Percent of margin space to show around the photos. Typically a 1% or less margin looks good.', 'intense' ); ?>)
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'columns' ); ?>"><?php _e( 'Columns:', 'intense' ); ?></label><br />
			<select name="<?php echo $this->get_field_name( 'columns' ); ?>" id="<?php echo $this->get_field_id( 'columns' ); ?>" value>
				
				<?php
				foreach ($columns as $key => $value) {
					echo '<option ' . ($instance['columns'] == $key ? 'selected="selected"' : '') . ' value="' . esc_attr( $key ) . '">' . esc_html( $value ) . '</option>';
				}
				?>

			</select>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'photo_size' ); ?>"><?php _e( 'Photo Size:', 'intense' ); ?></label><br />
			<select name="<?php echo $this->get_field_name( 'photo_size' ); ?>" id="<?php echo $this->get_field_id( 'photo_size' ); ?>" value>
				
				<?php
				foreach ($sizes as $key => $value) {
					echo '<option ' . ($instance['photo_size'] == $key ? 'selected="selected"' : '') . ' value="' . esc_attr( $key ) . '">' . esc_html( $value ) . '</option>';
				}
				?>

			</select>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'source' ); ?>"><?php _e( 'Source:', 'intense' ); ?></label><br />
			<select class="gallery-widget-source" name="<?php echo $this->get_field_name( 'source' ); ?>" id="<?php echo $this->get_field_id( 'source' ); ?>" value>

				<?php
				foreach ($sources as $key => $value) {
					echo '<option ' . ($instance['source'] == $key ? 'selected="selected"' : '') . ' value="' . esc_attr( $key ) . '">' . esc_html( $value ) . '</option>';
				}
				?>
				
			</select>
			<p><?php _e( 'The sources can be configured in the Intense options.', 'intense' ); ?></p>
		</p>
		<p class="flickrsettings googleplussettings 500pxsettings smugmugsettings instagramsettings deviantartsettings">
			<label for="<?php echo $this->get_field_id( 'user' ); ?>"><?php _e( 'User:', 'intense' ); ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'user' ); ?>" name="<?php echo $this->get_field_name( 'user' ); ?>" value="<?php echo $instance['user']; ?>" />
		</p>
		<p class="flickrsettings zenfoliosettings">
			<label for="<?php echo $this->get_field_id( 'set' ); ?>"><?php _e( 'Set:', 'intense' ); ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'set' ); ?>" name="<?php echo $this->get_field_name( 'set' ); ?>" value="<?php echo $instance['set']; ?>" />
		</p>
		<p class="smugmugsettings googleplussettings facebooksettings">
			<label for="<?php echo $this->get_field_id( 'album' ); ?>"><?php _e( 'Album:', 'intense' ); ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'album' ); ?>" name="<?php echo $this->get_field_name( 'album' ); ?>" value="<?php echo $instance['album']; ?>" />
		</p>
		<p class="flickrsettings facebooksettings">
			<label for="<?php echo $this->get_field_id( 'group' ); ?>"><?php _e( 'Group:', 'intense' ); ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'group' ); ?>" name="<?php echo $this->get_field_name( 'group' ); ?>" value="<?php echo $instance['group']; ?>" />
		</p>
		<p class="facebooksettings">
			<label for="<?php echo $this->get_field_id( 'facebookpage' ); ?>"><?php _e( 'Facebook Page:', 'intense' ); ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'facebookpage' ); ?>" name="<?php echo $this->get_field_name( 'facebookpage' ); ?>" value="<?php echo $instance['facebookpage']; ?>" />
		</p>
		<p class="instagramsettings">
			<label for="<?php echo $this->get_field_id( 'tag' ); ?>"><?php _e( 'Tag:', 'intense' ); ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'tag' ); ?>" name="<?php echo $this->get_field_name( 'tag' ); ?>" value="<?php echo $instance['tag']; ?>" />
		</p>
		<p class="wpsettings">
			<label for="<?php echo $this->get_field_id( 'include' ); ?>"><?php _e( 'Included IDs:', 'intense' ); ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'include' ); ?>" name="<?php echo $this->get_field_name( 'include' ); ?>" value="<?php echo $instance['include']; ?>" />
			<?php _e( 'enter comma separated WordPress attachment IDs', 'intense' ); ?>
		</p>
		<script>
			jQuery(function($) {
				$(".flickrsettings").hide();
		        $(".facebooksettings").hide();
		        $(".instagramsettings").hide();
		        $(".smugmugsettings").hide();
		        $(".zenfoliosettings").hide();
		        $(".deviantartsettings").hide();
		        $(".500pxsettings").hide();
		        $(".googleplussettings").hide();
		        $(".wpsettings").hide();

		        $(".gallery-widget-source").change(function() {
		            var source = $(this).val();
		            var $widgetForm = $(this).parents("form");

		            
		            $(".wpsettings", $widgetForm).hide();
		            $(".flickrsettings", $widgetForm).hide();
		            $(".facebooksettings", $widgetForm).hide();
		            $(".instagramsettings", $widgetForm).hide();
		            $(".smugmugsettings", $widgetForm).hide();
		            $(".zenfoliosettings", $widgetForm).hide();
		            $(".deviantartsettings", $widgetForm).hide();
		            $(".500pxsettings", $widgetForm).hide();
		            $(".googleplussettings", $widgetForm).hide();                

		            switch(source) {
		                case "wordpress":
		                    $(".wpsettings", $widgetForm).show();
		                    break;
		                case "flickr":
		                    $(".flickrsettings", $widgetForm).show();
		                    break;
		                case "facebook":
		                    $(".facebooksettings", $widgetForm).show();
		                    break;
		                case "instagram":
		                    $(".instagramsettings", $widgetForm).show();
		                    break;
		                case "smugmug":
		                    $(".smugmugsettings", $widgetForm).show();
		                    break;
		                case "zenfolio":
		                    $(".zenfoliosettings", $widgetForm).show();
		                    break;
		                case "deviantart":
		                    $(".deviantartsettings", $widgetForm).show();
		                    break;
		                case "500px":
		                    $(".500pxsettings", $widgetForm).show()
		                    break;
		                case "google+":
		                    $(".googleplussettings", $widgetForm).show();
		                    break;
		            }
		        });

				$(".gallery-widget-source").change();
			});
		</script>
	<?php
	}
}
?>
