<?php 

if ( ! function_exists( 'gp_images' ) ) {

	function gp_images( $atts, $content = null ) {	
		
		extract( shortcode_atts( array(
			'widget_title' => '',
			'upload_images' => '',
			'number' => '8',
			'image_width' => '176',
			'image_height' => '116',
			'hard_crop' => true,
			'see_all' => 'disabled',
			'see_all_link' => '',
			'see_all_text' => 'See All Images',
			'classes' => '',
		), $atts ) );	
		
		global $gp;

		// Unique Name	
		STATIC $i = 0;
		$i++;
		$name = 'gp_images_wrapper_' . $i;
		
		 // Get images page images if none uploaded
		$gp_image_ids = '';
		if ( empty ( $upload_images ) && get_post_meta( get_the_ID(), '_wp_page_template', true ) == 'hub-template.php' ) {
			$gp_pages = get_pages( 'child_of=' . get_the_ID() );
			foreach( $gp_pages as $gp_page ) {
				if ( get_post_meta( $gp_page->ID, '_wp_page_template', true ) == 'images-template.php' ) {
					$gp_image_ids = array_filter( explode( ',', redux_post_meta( 'gp', $gp_page->ID, 'images_gallery' ) ) );
					break;
				} else {
					$gp_image_ids = '';
				}
			}
		} elseif ( ! empty ( $upload_images ) ) {
			$gp_image_ids = array_filter( explode( ',', $upload_images ) );
		}

		// Get images page permalink if none specified
		if ( empty ( $see_all_link ) && get_post_meta( get_the_ID(), '_wp_page_template', true ) == 'hub-template.php' ) {
			$gp_pages = get_pages( 'child_of=' . get_the_ID() );
			foreach( $gp_pages as $gp_page ) {
				if ( get_post_meta( $gp_page->ID, '_wp_page_template', true ) == 'images-template.php' ) {		
					$see_all_link = get_permalink( $gp_page->ID );
				}					
			}
		}
		
		ob_start();
					
		?>

		<div id="<?php echo sanitize_html_class( $name ); ?>" class="images-shortcode <?php echo sanitize_html_class( $classes ); ?>">

			<?php if ( $widget_title OR $see_all == 'enabled' ) { ?>
				<div class="element-title">
					<?php if ( $widget_title ) { ?><h3><?php echo esc_attr( $widget_title ); ?></h3><?php } ?>
					<?php if ( $see_all == 'enabled' ) { ?>
						<div class="see-all-link"><a href="<?php echo esc_url( $see_all_link ); ?>"><?php echo esc_attr( $see_all_text ); ?><i></i></a></div>
					<?php } ?>
					<div class="element-title-line"></div>
				</div>
			<?php } ?>
		
			<div class="inner-loop">
			
				<?php if ( $gp_image_ids ) { ?> 
					
					<?php $count = 0; foreach ( array_reverse( $gp_image_ids ) as $gp_image_id ) { 
						
						if ( $count++ < $number ) {
						
							// Get attachment data
							$attachment = get_post( $gp_image_id ); ?>
						
							<div class="image-loop">
							
								<?php $image = aq_resize( wp_get_attachment_url( $gp_image_id ), $image_width, $image_height, $hard_crop, false, true ); ?>
								<?php if ( $gp['retina'] == 'enabled' ) {
									$retina = aq_resize(wp_get_attachment_url( $gp_image_id ),  $image_width * 2, $image_height * 2, $hard_crop, true, true );
								} else {
									$retina = '';
								} ?>
						
								<a href="<?php echo wp_get_attachment_url( $gp_image_id ); ?>" data-rel="prettyPhoto[group]" class="prettyPhoto" title="<?php echo esc_attr( $attachment->post_excerpt ); ?>">
									<img src="<?php echo esc_url( $image[0] ); ?>" data-rel="<?php echo esc_url( $retina ); ?>" width="<?php echo absint( $image[1] ); ?>" height="<?php echo absint( $image[2] ); ?>" alt="<?php if ( get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true) ) { echo get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true); } else { the_title_attribute( array( 'post' => get_the_ID() ) ); } ?>" class="wp-post-image" />
								</a>
						
							</div>
																					
						<?php } ?>
					
					<?php } ?>
						
				<?php } else { ?>
								
					<strong class="no-items-found"><?php _e( 'No items found.', 'gp_lang' ); ?></strong>
				
				<?php } ?>
				
			</div>	
						
		</div><!-- END IMAGES WRAPPER -->
			 				
		<?php

		$output_string = ob_get_contents();
		ob_end_clean(); 
		return $output_string;

	}

}

add_shortcode( 'images', 'gp_images' );
	
?>