<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
//wp_enqueue_style('awl-ld-lightbox-css', PFG_PLUGIN_URL .'lightbox/ld-lightbox/css/lightbox.css');
//wp_enqueue_script('awl-ld-lightbox-js', PFG_PLUGIN_URL .'lightbox/ld-lightbox/js/lightbox.js', array('jquery'), '' , true);
$allimages = array(  'p' => $pf_gallery_id, 'post_type' => 'awl_filter_gallery', 'orderby' => 'ASC', 'post_status' => 'publish');
$loop = new WP_Query( $allimages );
while ( $loop->have_posts() ) : $loop->the_post();
	$post_id = get_the_ID();
	//require filters and sorting control
	require('filtring/filters-sorting-control.php'); ?>
	<div class="filter_gallery_<?php echo $pf_gallery_id; ?> filtr-container filters-div" style="opacity:0;" id="filter_gallery_viewer">
		<?php
		if(isset($pf_gallery_settings['image-ids']) && count($pf_gallery_settings['image-ids']) > 0) {
			
			$count = 0;
			$no = 1;
			
			//******************** Load Image With Limit [Shortcode] *******************//
			if($thumbnail_limit) {
				for($i = 0; $i < $thumbnail_limit; $i++) {
					$attachment_id = $pf_gallery_settings['image-ids'][$i];
					
					// Load Gallery Data & Content
					include('gallery-content.php');
					
					$no++;
					$count++;
				}
			} else if($pf_gallery_load_more == "no") {
				$count = 0;
				foreach($pf_gallery_settings['image-ids'] as $attachment_id) {
					
					// Load Gallery Data & Content
					include('gallery-content.php');
					
					$no++;
					$count++;
				}// end of attachment foreach
			} 
			//******************** LOAD MORE IS ON *******************//
			else if($pf_gallery_load_more == "yes") {
				
				// run loop according to remaining images
				$remain_images = $pf_total_images - $pf_limit;
				if($remain_images < 0){
					$pf_limit = $pf_limit + $remain_images;
				}
				
				for($i = 0; $i < $pf_limit; $i++) {
					$attachment_id = $pf_gallery_settings['image-ids'][$i];
					
					// Load Gallery Data & Content
					include('gallery-content.php');
					
					$no++;
					$count++;
				}
			}
		} else {
			_e('Sorry! No image gallery found ', PFG_TXTDM);
			echo ":[PFG id=$post_id]";
		} // end of if esle of images avaialble check into imager
		?>
	</div>
	<div style="clear:both;"></div>
	<?php
	if($pf_gallery_load_more == "yes") { ?>
		<!-- progress button load more -->
		<div class="pw-loader-wrapper" style="margin-top:25px; color:#000;">
			<section class="pf_loader-<?php echo $pf_gallery_id; ?> page page--mover" style="display:none;">
				<div class="la-square-loader"><div></div></div>
			</section>
			<a id="progress-button" class="lg_load_more pf_load_more-<?php echo $pf_gallery_id; ?> progress-button" style="display:none;" href="javascript:void(0)"><span><?php echo $load_text; ?></span></a>
			<p class="pf_no_more-<?php echo $pf_gallery_id; ?>" style="display:none"><?php echo $no_more_text; ?></p>
		</div>
		<!-- progress-button load more -->
		<?php
		if(isset( $_POST['pf_security'] )) {
			$pf_security = $_POST['pf_security'];
			if(wp_verify_nonce( $pf_security, 'pf_load_more_nonce' )) {
				$pf_limit_start = $_POST['pf_limit_start'];
				$pf_limit_end = $_POST['pf_limit_end'];
				$targetFilter = $_POST['targetFilter'];
				$numItems = $_POST['numItems'];
				
				// remove filtered images of ALL if they load into any filter
				$filter_image_lens = $_POST['filter_image_lens'];
				$filter_image_lens2 = explode(",", $filter_image_lens);
				$img_ids_diff = array_diff($pf_gallery_settings['image-ids'], $filter_image_lens2); 
				$img_ids_diff2 = array_values($img_ids_diff);
				$no = 0;
				
				if($targetFilter != 'all') {
					$targetFilterD = explode(",", $targetFilter);
					foreach($targetFilterD as $key => $targetFilter_r) {
						
						foreach($filter_image[$targetFilter_r] as $key => $filter_image_r) {
						
							$attached[] = $filter_image_r;
						
						}
					}
					// unset already load images
					$img_ids_diff_filter = array_diff($attached, $filter_image_lens2);
					$img_ids_diff_val = array_values($img_ids_diff_filter);
				}
				for($i = $pf_limit_start; $i < $pf_limit_end; $i++) {
					if($targetFilter != 'all') {
						$attachment_id = $img_ids_diff_val[$no];
					} else {
						$attachment_id = $img_ids_diff2[$no];
					}
					
					if($pf_gallery_load_more == "yes") { $load_class = 'pf_result-'.$pf_gallery_id.''; } else { $load_class = ''; }
					
					// Load Gallery Data & Content
					include('gallery-content.php');
					
					$numItems++;
					$count++;
					$no++;
				}
			}
		}
	}
endwhile;
wp_reset_query();
?>