<?php

// no direct access
defined('_JEXEC') or die('Restricted access');

$mainframe = JFactory::getApplication();
$lang = JFactory::getLanguage();
$lang->load("plg_system_k2multiimages", JPATH_ADMINISTRATOR);
$plugin = JPluginHelper::getPlugin('system', 'k2multiimages');
$plgParams = class_exists('JParameter') ? new JParameter($plugin->params) : new JRegistry($plugin->params);
$img_limit = (int)$plgParams->get("limit", "");	
$timestamp = '?t=' . strtotime("now"); // stamp for prevent cache problems

?>
<!--- added K2MultiImages --->
<!--- item image --->	
<!-- Tab image -->
<meta Http-Equiv="Cache-Control" Content="no-cache">
<meta Http-Equiv="Pragma" Content="no-cache">
<meta Http-Equiv="Expires" Content="0">
<meta Http-Equiv="Pragma-directive: no-cache">
<meta Http-Equiv="Cache-directive: no-cache">

<div class="K2MultiimagesContainer">
	<style>
	li.ui-state-default { list-style: none; display: inline-block; padding-left: 7px; cursor: pointer; }
	li.ui-state-highlight { list-style: none; height: 95px; width: 75px; border: 2px dashed #ccc; display: inline-block; margin-left: 7px; }
	table.multiimages td.key { padding-right: 15px; vertical-align: top; font-size: 13px !important; }
	table.multiimages td.key.preview { vertical-align: middle; }
	span.sep { display: inline-block; font-size: 14px; margin: 0 25px; }
	</style>
	<script type="text/javascript">	
		jQuery.noConflict();
		(function(jQuery) {
			//standard form replacement
			jQuery("#k2TabImage, #k2Tab2").html("");
			jQuery(".K2MultiimagesContainer").appendTo("#k2TabImage, #k2Tab2");
				//fix plugins tab
				jQuery("#k2TabPlugins legend, #k2Tab7 legend").each(function() {
					if(jQuery(this).text() == "k2multiimages") {
						jQuery(this).remove();
					}
				});
				//hide plugins tab if empty
				//backend and frontend
				if(jQuery("#k2TabPlugins").find("legend").length == 0 && jQuery("#k2Tab7").find("legend").length == 0) {
					jQuery("li#tabPlugins").remove();
				}
	
			//add new image button click
			jQuery("body").on("click", "a.AddNewImage", function() {
				var image = parseInt(jQuery('div.itemImages table.admintable:last-child').find('input.fileUpload').attr("name").slice(5));
				var newImage = image + 1;
				jQuery("div.itemImages").append("<p>&nbsp;</p><p><?php echo JText::_('PLG_K2_MULTIIMAGES_IMAGE'); ?> "+newImage+"</p><hr />");
				jQuery('div.itemImages table.admintable:eq(0)').clone().appendTo("div.itemImages");			
				jQuery('div.itemImages table.admintable:last-child').removeClass("image1").addClass("image"+newImage);
				jQuery('div.itemImages table.admintable:last-child input.fileUpload').attr("name", "image"+newImage);
				jQuery('div.itemImages table.admintable:last-child input.fileUpload').val("");
				jQuery('div.itemImages table.admintable:last-child input[name=existingImage1]').val("");
				jQuery('div.itemImages table.admintable:last-child input[name=existingImage1]').attr("id", "existingImageValue"+newImage);
				jQuery('div.itemImages table.admintable:last-child input[name=existingImage1]').attr("name", "existingImage"+newImage);
				jQuery('div.itemImages table.admintable:last-child input.k2ImageBrowseServer').attr("rel", "existingImageValue"+newImage);
				jQuery('div.itemImages table.admintable:last-child input[name=image1_caption]').attr("name", "image"+newImage+"_caption").val("");
				jQuery('div.itemImages table.admintable:last-child input[name=image1_credits]').attr("name", "image"+newImage+"_credits").val("");
				
				jQuery('div.itemImages table.admintable:last-child tr.imagePreview').remove();	
				
				<?php if($mainframe->isSite() && $img_limit != ""): ?>
				var img_limit = <?php echo $img_limit; ?>;
				if(newImage == img_limit) {
					jQuery(".K2MultiimagesContainer a.AddNewImage").after("<?php echo JText::_("PLG_K2_MULTIIMAGES_LIMIT_TEXT"); ?>").remove();
				}
				<?php endif; ?>
				return false;
			});
					
			jQuery("body").on("click", ".k2ImageBrowseServer", function(event) {
				event.preventDefault();
				SqueezeBox.initialize();
				var number = jQuery(this).attr("rel");
				var link = K2BasePath+"index.php?option=com_k2&view=media&type=image&tmpl=component&fieldID="+number;			
				SqueezeBox.fromElement(this, {
					handler: "iframe",
					url: link,
					size: {x: 800, y: 434}
				});
			});
			
			//images ordering
			setTimeout(function() {
				jQuery("#sortableImages").sortable({
					placeholder: "ui-state-highlight",
					update: function() {
						var value = '';
						jQuery("#sortableImages li").each(function() {
							value += jQuery(this).attr("rel") + "|";
						});
						jQuery("input[name=imagesOrdering]").val(value);
					}
				});
				jQuery("#sortableImages").disableSelection();
			}, 500);
			
			<?php if(!$mainframe->isSite()) : ?>
			//multiple images select
			jQuery("input.fileUploadMultiple").on("change", function() {
				var last_image = parseInt(jQuery('div.itemImages table.admintable:last-child').find('input.fileUpload').attr("name").slice(5));
				var newImage = last_image + 1;
				if(jQuery('div.itemImages table.admintable:last-child').find('tr.imagePreview').length == 0 && jQuery('div.itemImages table.admintable:last-child').find('input.fileUpload').val() == "") {
					newImage = last_image;
				}
				jQuery("input[name=image_multiple_start]").val(newImage);
			});
			<?php endif; ?>
		}(jQuery));
	</script>
	
	<?php 
	if(JFile::exists(JPATH_SITE.'/media/k2/items/src/'.md5("Image".$item->id)."_2.jpg")) : ?>
	<div class="imagesOrdering">
		<p style="margin-top: 20px;"><?php echo JText::_('PLG_K2_MULTIIMAGES_ORDERING_TEXT'); ?></p>
		<ul id="sortableImages" style="padding-top: 0px; margin: 10px 0 10px 0;">
		<?php 	
		$filecount = 1;
		while(
		JFile::exists(JPATH_SITE.'/media/k2/items/src/'.md5("Image".$item->id)."_".$filecount.".jpg") || 
		(JFile::exists(JPATH_SITE.'/media/k2/items/src/'.md5("Image".$item->id).".jpg") && $filecount == 1)
		) {
			if($filecount == 1) {
				$image_thumb = JURI::root().'media/k2/items/cache/'.md5("Image".$item->id)."_S.jpg";
			}
			else {
				$image_thumb = JURI::root().'media/k2/items/cache/'.md5("Image".$item->id)."_".$filecount."_S.jpg";
			}
		?>		
			<li class="ui-state-default" rel="<?php echo $filecount; ?>">
				<img alt="<?php echo $item->title; ?>" src="<?php echo $image_thumb . $timestamp; ?>" class="k2AdminImage" style="height: 75px; margin-top: 0px;" />	
			</li>
			<?php 
			$filecount++;
		}
		?>
		</ul>	
		<input type="hidden" name="imagesOrdering" value="" />
	</div>
	<?php endif; ?>
	
	<!-- added for prevent images error on K2 2.7+ -->
	<input style="display: none" type="file" name="image" value="" />
	
	<div class="itemImages">		
		<?php 			
		$filecount = 1;
		while(
		JFile::exists(JPATH_SITE.'/media/k2/items/src/'.md5("Image".$item->id)."_".$filecount.".jpg") || 
		(JFile::exists(JPATH_SITE.'/media/k2/items/src/'.md5("Image".$item->id).".jpg") && $filecount == 1)
		) {
			if($filecount > 1) echo "<p>&nbsp;</p>";
		?>
		<p><?php echo JText::_('PLG_K2_MULTIIMAGES_IMAGE')." ".$filecount; ?></p>
		<hr />		
		<table class="admintable image1 multiimages">
			<tr>
				<td align="right" class="key">
					<?php echo JText::_('K2_ITEM_IMAGE'); ?>
				</td>
				<td>
					<input type="file" name="image<?php echo $filecount; ?>" class="fileUpload" accept="image/gif, image/jpeg, image/png" />
					<i>(<?php echo JText::_('K2_MAX_UPLOAD_SIZE'); ?>: <?php echo ini_get('upload_max_filesize'); ?>)</i>
					<span class="sep">OR</span> 
					<input type="text" name="existingImage<?php echo $filecount; ?>" class="text_area" id="existingImageValue<?php echo $filecount; ?>" readonly />
					<input rel="existingImageValue<?php echo $filecount; ?>" type="button" value="<?php echo JText::_('K2_BROWSE_SERVER'); ?>" class="k2ImageBrowseServer" />
					<br />
					<br />
				</td>
			</tr>
			<tr>
				<?php
				$caption = explode("|", $item->image_caption);
				$credits = explode("|", $item->image_credits);
				?>
				<td align="right" class="key">
					<?php echo JText::_('K2_ITEM_IMAGE_CAPTION'); ?>
				</td>
				<td>
					<input type="text" name="image<?php echo $filecount; ?>_caption" size="30" class="text_area" value="<?php echo @$caption[$filecount-1]; ?>" />
				</td>
			</tr>
			<tr>
				<td align="right" class="key">
					<?php echo JText::_('K2_ITEM_IMAGE_CREDITS'); ?>
				</td>
				<td>
					<input type="text" name="image<?php echo $filecount; ?>_credits" size="30" class="text_area" value="<?php echo @$credits[$filecount-1]; ?>" />
				</td>
			</tr>
			
			<?php 
				if($filecount == 1) {
					$image_full = JURI::root().'media/k2/items/src/'.md5("Image".$item->id).".jpg";
					$image_thumb = JURI::root().'media/k2/items/cache/'.md5("Image".$item->id)."_S.jpg";;
				}
				else {
					$image_full = JURI::root().'media/k2/items/src/'.md5("Image".$item->id)."_".$filecount.".jpg";
					$image_thumb = JURI::root().'media/k2/items/cache/'.md5("Image".$item->id)."_".$filecount."_S.jpg";
				}
			?>
			<tr class="imagePreview">
				<td align="right" class="key preview">
					<?php echo JText::_('K2_ITEM_IMAGE_PREVIEW'); ?>
				</td>
				<td>
					<a class="modal" rel="{handler: 'image'}" href="<?php echo $image_full . $timestamp; ?>" title="<?php echo JText::_('K2_CLICK_ON_IMAGE_TO_PREVIEW_IN_ORIGINAL_SIZE'); ?>"> <img alt="<?php echo $item->title; ?>" src="<?php echo $image_thumb . $timestamp; ?>" class="k2AdminImage"/> </a>
					
					<input type="checkbox" name="del_image<?php echo $filecount; ?>" id="del_image<?php echo $filecount; ?>" />
					<label for="del_image<?php echo $filecount; ?>"><?php echo JText::_('K2_CHECK_THIS_BOX_TO_DELETE_CURRENT_IMAGE_OR_JUST_UPLOAD_A_NEW_IMAGE_TO_REPLACE_THE_EXISTING_ONE'); ?></label>
				</td>
			</tr>
		</table>
		
		<?php 
		$filecount++;
		}
		?>	
		
		<?php if(!JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'src'.DS.md5("Image".$item->id).".jpg")) : ?>
		<p><?php echo JText::_('PLG_K2_MULTIIMAGES_IMAGE')." 1"; ?></p>
		<hr />
		<table class="admintable image1 multiimages">
			<tr>
				<td align="right" class="key">
					<?php echo JText::_('K2_ITEM_IMAGE'); ?>
				</td>
				<td>
					<input type="file" name="image1" class="fileUpload" accept="image/gif, image/jpeg, image/png" />
					<i>(<?php echo JText::_('K2_MAX_UPLOAD_SIZE'); ?>: <?php echo ini_get('upload_max_filesize'); ?>)</i>
					<span class="sep">OR</span>
					<input type="text" name="existingImage1" class="text_area" id="existingImageValue1" readonly />
					<input rel="existingImageValue1" type="button" value="<?php echo JText::_('K2_BROWSE_SERVER'); ?>" class="k2ImageBrowseServer" />
					<br />
					<br />
				</td>
			</tr>
			<tr>
				<td align="right" class="key">
					<?php echo JText::_('K2_ITEM_IMAGE_CAPTION'); ?>
				</td>
				<td>
					<input type="text" name="image1_caption" size="30" class="text_area" value="" />
				</td>
			</tr>
			<tr>
				<td align="right" class="key">
					<?php echo JText::_('K2_ITEM_IMAGE_CREDITS'); ?>
				</td>
				<td>
					<input type="text" name="image1_credits" size="30" class="text_area" value="" />
				</td>
			</tr>	
		</table>
	<?php endif; ?>
	</div><!-- //item images -->
	
	<p>&nbsp;</p>
	<?php if($img_limit != "" && $filecount > $img_limit && $mainframe->isSite()) : ?>
		<?php echo JText::_("PLG_K2_MULTIIMAGES_LIMIT_TEXT"); ?>
	<?php else: ?>
		<a href="#" class="AddNewImage" style="font-size: 14px;"><?php echo JText::_('PLG_K2_MULTIIMAGES_IMAGE_NEW'); ?></a>
		<div class="multipleUpload" style="font-size: 12px;">
			<?php echo JText::_('PLG_K2_MULTIIMAGES_IMAGE_NEW_MULTIPLE'); ?> &nbsp;
			<input multiple="multiple" type="file" name="image_multiple[]" class="fileUploadMultiple" accept="image/gif, image/jpeg, image/png" />
			<input type="hidden" name="image_multiple_start" value="1" />
		</div>
	<?php endif; ?>
</div>
<!--- //// item image --->
<!--- /added K2MultiImages --->