<?php
/**
 * Single Product Thumbnails
 *
 * @author 		WooThemes
 * @package 	WooCommerce/Templates
 * @version     2.0.3
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $post, $product, $woocommerce;

$attachment_ids = $product->get_gallery_attachment_ids();

$videos = get_post_meta($post->ID, 'video');

if ( $attachment_ids ) {
	?>

	<div class="flexslider more-views">
		<?php

		$loop = 0;
		$columns = apply_filters( 'woocommerce_product_thumbnails_columns', 3 );

		$thumbImageWidth = etheme_get_option('single_product_thumb_width', 63);
		$thumbImageHeight = etheme_get_option('single_product_thumb_height', 90);
		$thumbImageCrop = false;

		?>

		<ul class="slides">

			<?php
			foreach ( $attachment_ids as $key => $attachment_id) {

				$classes = array( 'zoom' );

				if ( $loop == 0 || $loop % $columns == 0 ) {
					$classes[] = 'first';
				}

				if ( ( $loop + 1 ) % $columns == 0 ) {
					$classes[] = 'last';
				}

				$image_link = etheme_get_image($attachment_id, $thumbImageWidth, $thumbImageHeight, $thumbImageCrop);
				if ( ! $image_link ) {
					continue;
				}

				$image = '<img src="'.$image_link.'" />';
				$image_class = esc_attr( implode( ' ', $classes ) );
				$image_title = esc_attr( get_the_title( $attachment_id ) );
				$big_image = wp_get_attachment_url( $attachment_id );
				$data_rel = "useZoom: 'zoom1', smallImage: '". $big_image ."'";

				echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', sprintf( '<li><a href="%s" class="cloud-zoom-gallery" title="%s"  rel="prettyPhoto[product-gallery]" data-rel="%s" alt="" width="62" height="62">%s</a></li>', $big_image, $image_title, $data_rel, $image ), $attachment_id, $post->ID, $image_class ) . '</li>';

				$loop++;
			}

			if ( !empty($videos) ) {

				foreach ( $videos as $video ) { ?>

					<li><?php do_shortcode('[wft_youtube_video id="' . $video . '" thumb="true"]'); ?></li>

				<?php } ?>

				<script type="text/javascript">

					jQuery(document).ready(function() {
						jQuery("a.video").click(function() {

							jQuery.fancybox({
								'padding'		: 0,
								'autoScale'		: false,
								'transitionIn'	: 'none',
								'transitionOut'	: 'none',
								'title'			: this.title,
								'width'		: 680,
								'height'		: 495,
								'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
								'type'			: 'swf',
								'swf'			: {
									'wmode'		: 'transparent',
									'allowfullscreen'	: 'true'
								}
							});

							return false;
						});
					});

				</script>

				<?php
			}

			?>
		</ul>

	</div>
	<?php
}