<?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();
function formatFileSize($bytes) 
{
	if (!is_numeric($bytes) || !$bytes) {
		return '';
	}
	if ($bytes >= 1000000000) {
		return round($bytes / 1000000000,2).' '.GetMessage("GB");
	}
	if ($bytes >= 1000000) {
		return round($bytes / 1000000,2).' '.GetMessage("MB");
	}
	return round($bytes / 1000,2). ' '.GetMessage("KB");
}
?>
<div class="catalog-section">
<?if($arParams["DISPLAY_TOP_PAGER"]):?>
	<?=$arResult["NAV_STRING"]?><br />
<?endif;?>
<table class = "catalog_tbl"cellpadding="0" cellspacing="0" border="0">
		<?foreach($arResult["ITEMS"] as $cell=>$arElement):?>
		<?
		$this->AddEditAction($arElement['ID'], $arElement['EDIT_LINK'], CIBlock::GetArrayByID($arParams["IBLOCK_ID"], "ELEMENT_EDIT"));
		$this->AddDeleteAction($arElement['ID'], $arElement['DELETE_LINK'], CIBlock::GetArrayByID($arParams["IBLOCK_ID"], "ELEMENT_DELETE"), array("CONFIRM" => GetMessage('CT_BCS_ELEMENT_DELETE_CONFIRM')));
		?>
		<?if($cell%$arParams["LINE_ELEMENT_COUNT"] == 0):?>
		<tr>
		<?endif;?>

		<td valign="top" width="<?=round(100/$arParams["LINE_ELEMENT_COUNT"])?>%" id="<?=$this->GetEditAreaId($arElement['ID']);?>">

			<table cellpadding="0" cellspacing="0" style="border-bottom: 1px solid #DEDEE2;">
				<tr>
					<?if(is_array($arElement["PREVIEW_PICTURE"])):?>
						<td valign="top" style="padding-right: 10px;">
						<a href="<?=$arElement["DETAIL_PAGE_URL"]?>">
							<?=CFile::ShowImage($arElement["PREVIEW_PICTURE"]["SRC"], 100, 200, "border=0", "", false)?>
						</a><br />
						</td>
					<?elseif(is_array($arElement["DETAIL_PICTURE"])):?>
						<td valign="top" style="padding-right: 10px;">
							<a href="<?=$arElement["DETAIL_PAGE_URL"]?>">
								<?=CFile::ShowImage($arElement["DETAIL_PICTURE"]["SRC"], 100, 200, "border=0", "", false)?>
							</a><br />
						</td>
					<?endif?>
					<td valign="top"><a href="<?=$arElement["DETAIL_PAGE_URL"]?>" class="orange"><?=$arElement["NAME"]?></a><br />
						<?=$arElement["PREVIEW_TEXT"]?>
						<?foreach($arElement["DISPLAY_PROPERTIES"] as $pid=>$arProperty):
							if($pid!="FILE"):?>
								<small>
								<?=$arProperty["NAME"]?>:&nbsp;
								<?if(is_array($arProperty["DISPLAY_VALUE"])):?>
									<?=implode("&nbsp;/&nbsp;", $arProperty["DISPLAY_VALUE"]);?>
								<?else:?>
									<?=$arProperty["DISPLAY_VALUE"];?>
								<?endif?>
								</small><br />
							<?endif;
						endforeach;?>
						<br />
						<br />
						<?if (!empty($arElement["DISPLAY_PROPERTIES"]['FILE']["DISPLAY_VALUE"])):?>
							<div class="files-download">
								<?if(is_array($arElement["DISPLAY_PROPERTIES"]['FILE']["DISPLAY_VALUE"])) {
									$arFiles = $arElement["DISPLAY_PROPERTIES"]['FILE']["DISPLAY_VALUE"];
									$arFilesData = $arElement["DISPLAY_PROPERTIES"]['FILE']['FILE_VALUE'];
								}
								else {
									$arFiles = array('file');
									$arFilesData = array($arElement["DISPLAY_PROPERTIES"]['FILE']['FILE_VALUE']);
								}
								foreach($arFiles as $key=>$value):	
									if(!$arFilesData[$key]['DESCRIPTION'])
										$arFilesData[$key]['DESCRIPTION']=$arFilesData[$key]['ORIGINAL_NAME'];
									$ext=substr($arFilesData[$key]['SRC'], strrpos($arFilesData[$key]['SRC'], '.') + 1);
									if (file_exists($_SERVER['DOCUMENT_ROOT'].'/bitrix/redirect.php'))
										$fileLink = 'href="/bitrix/redirect.php?goto='.$arFilesData[$key]['SRC'].'&event1=file&event3='.$arFilesData[$key]['FILE_NAME'].'&event2=download"';
									else
										$fileLink = 'target="blank" href="'.$arFilesData[$key]['SRC'].'"';
									?>
									<div>
										<a class="<?=$ext?>" <?=$fileLink?> title="<?=GetMessage("DOWNLOAD")?> <?=$arFilesData[$key]['DESCRIPTION']?>">
											<span><?=GetMessage("DOWNLOAD")?> "<?=$arFilesData[$key]['DESCRIPTION']?>"</span>
											<i> 
												(<?=formatFileSize($arFilesData[$key]['FILE_SIZE'])?>, <?=$ext?>)
											</i>
										</a>
									</div>
								<?endforeach;?>
							</div>
						<?endif?>
					</td>
				</tr>
		</table>
			&nbsp;
		</td>

		<?$cell++;
		if($cell%$arParams["LINE_ELEMENT_COUNT"] == 0):?>
			</tr>
		<?endif?>

		<?endforeach; // foreach($arResult["ITEMS"] as $arElement):?>

		<?if($cell%$arParams["LINE_ELEMENT_COUNT"] != 0):?>
			<?while(($cell++)%$arParams["LINE_ELEMENT_COUNT"] != 0):?>
				<td>&nbsp;</td>
			<?endwhile;?>
			</tr>
		<?endif?>

</table>
<?if($arParams["DISPLAY_BOTTOM_PAGER"]):?>
	<br /><?=$arResult["NAV_STRING"]?>
<?endif;?>
</div>
