<?php
/**
 * Single Product Thumbnails
 *
 * @author 		WooThemes
 * @package 	WooCommerce/Templates
 * @version     3.3.2
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

global $post, $product, $woocommerce;

$attachment_ids = woo_shrk_get_gallery_image_ids( $product );
if ( $attachment_ids ) {
	$loop 		= 0;
	$columns 	= apply_filters( 'woocommerce_product_thumbnails_columns', 3 );
		if ( has_post_thumbnail() ) {

			$classes = array( 'zoom', 'first', 'active' );
			
			$image_link = wp_get_attachment_url( get_post_thumbnail_id() );

			if ( $image_link ) {

				$image       = wp_get_attachment_image( get_post_thumbnail_id(), apply_filters( 'woocommerce_gallery_thumbnail_size', 'woocommerce_thumbnail' ) );
				$image_class = esc_attr( implode( ' ', $classes ) );
				$image_title = esc_attr( get_post_field( 'post_excerpt', get_post_thumbnail_id() ) );

				echo apply_filters( 
					'woocommerce_single_product_image_thumbnail_html', 
					sprintf( 
						'<a href="%s" class="%s" title="%s" data-sw-index="%d">%s</a>', 
						esc_url( $image_link ), 
						esc_attr( $image_class ), 
						esc_attr( $image_title ), 
						esc_attr( $loop ), 
						$image 
					), 
					get_post_thumbnail_id(), 
					$post->ID, 
					$image_class 
				);
				$loop++;
			}

		} else {

			echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', sprintf( '<a href="#" data-sw-index="%d"><img src="%s" alt="%s" /></a>', esc_attr( $loop ), wc_placeholder_img_src(), esc_html__( 'Placeholder', 'ux-shop' ) ), get_post_thumbnail_id(), $post->ID );
			$loop++;	
		}

		foreach ( $attachment_ids as $attachment_id ) {

			$classes = array( 'zoom' );

			if ( $loop == 0 )
				$classes[] = 'active';

			if ( $loop == 0 || $loop % $columns == 0 )
				$classes[] = 'first';

			if ( ( $loop + 1 ) % $columns == 0 )
				$classes[] = 'last';

			$image_link = wp_get_attachment_url( $attachment_id );

			if ( ! $image_link )
				continue;

			$image_title 	= esc_attr( get_post_field( 'post_excerpt', $attachment_id ) );

			$image       = wp_get_attachment_image( $attachment_id, apply_filters( 'woocommerce_gallery_thumbnail_size', 'woocommerce_thumbnail' ), 0, $attr = array(
				'title'	=> esc_attr( $image_title ),
				'alt'	=> esc_attr( $image_title )
				) );

			$image_class = esc_attr( implode( ' ', $classes ) );

			echo apply_filters( 
				'woocommerce_single_product_image_thumbnail_html', 
				sprintf( 
					'<a href="%s" class="%s" data-sw-index="%d">%s</a>', 
					esc_url( $image_link ), 
					esc_attr( $image_class ), 
					esc_attr( $loop ), 
					$image 
				), 
				$attachment_id, 
				$post->ID, 
				$image_class 
			);
			$loop++;
		}
}
