<?php

// no direct access
defined('_JEXEC') or die('Restricted access');
K2HelperUtilities::setDefaultImage($item, 'item', $item->params);
$k2_image_hash = md5("Image".$item->id);
?>

<?php if($item->params->get('itemImage') && !empty($item->image)): ?>
<!-- added K2Multiimage -->
<!-- Item Image -->
<style>
	.itemImageBlock { display: none; }
	.itemImageContainer > .itemImageBlock { display: block; }
	.highslide-container > div > .highslide-controls { display: none; }
	.highslide-container > div.highslide-wrapper .highslide-controls { display: block; }
</style>
<div class="itemImageContainer">
	<?php 		
	$filecount = 1;
	$bcount = 0;
	$caption = explode("|", $item->image_caption);
	$credits = explode("|", $item->image_credits);

	while(
	JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.$k2_image_hash."_".$filecount."_XL.jpg") ||
	(JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.$k2_image_hash."_XL.jpg") && $filecount == 1)
	) {
		if($filecount == 1) {
			$path_full = JURI::root() . 'media/k2/items/cache/'.$k2_image_hash."_XL.jpg";
			$path_thumb = JURI::root() . 'media/k2/items/cache/'.$k2_image_hash."_S.jpg";
		}
		else {
			$path_full = JURI::root() . 'media/k2/items/cache/'.$k2_image_hash."_".$filecount."_XL.jpg";
			$path_thumb = JURI::root() . 'media/k2/items/cache/'.$k2_image_hash."_".$filecount."_S.jpg";			
		}
	?> 
	
	<?php if($filecount > 1 && $bcount != 1) {
		echo "<div class=\"itemImageBlock-container\" style=\"text-align: center;\">";
		$bcount = 1;
		}
	?>
	  <div class="itemImageBlock" <?php if($filecount > 1) : ?>style="display: inline-block;"<?php endif; ?>>
		  <span class="itemImage">
			<a class="modal" rel="{handler: 'image'}" href="<?php echo $path_full; ?>" title="<?php echo JText::_('K2_CLICK_TO_PREVIEW_IMAGE'); ?>">
				<?php if($filecount == 1) : ?>
					<img src="<?php echo $path_full; ?>" alt="<?php if(!empty($caption[$filecount - 1])) echo K2HelperUtilities::cleanHtml($caption[$filecount - 1]); else echo K2HelperUtilities::cleanHtml($item->title); ?>" style="width: <?php echo $item->imageWidth; ?>px; height: auto; max-width: 100%;" />
				<?php else : ?>
					<img src="<?php echo $path_thumb; ?>" alt="<?php if(!empty($caption[$filecount - 1])) echo K2HelperUtilities::cleanHtml($caption[$filecount - 1]); else echo K2HelperUtilities::cleanHtml($item->title); ?>" style="width:auto; height:100px;" />
				<?php endif; ?>
			</a>
		  </span>

		  <?php if($item->params->get('itemImageMainCaption') && !empty($caption[$filecount - 1])): ?>
		  <!-- Image caption -->
		  <span class="itemImageCaption" style="display: block; float: none; width: 150px;"><?php echo $caption[$filecount - 1]; ?></span>
		  <?php endif; ?>

		  <?php if($item->params->get('itemImageMainCredits') && !empty($credits[$filecount - 1])): ?>
		  <!-- Image credits -->
		  <span class="itemImageCredits" style="display: block; float: none; width: 150px;"><?php echo $credits[$filecount - 1]; ?></span>
		  <?php endif; ?>

		  <div class="clr"></div>
	  </div>
	  
	  <?php 
		if($filecount > 1 && !JFile::exists(JPATH_SITE.'/media/k2/items/cache/'.$k2_image_hash."_".($filecount+1)."_XL.jpg")) {
			echo "<div class=\"clr\"></div></div>";
		}	  
		$filecount++;
	  }
	  ?>
	  <div class="clr"></div>
</div>
<!-- added K2Multiimage -->
<?php endif; ?>