<?php if (!empty($this->files)): ?>
<?php if(empty($_skiptitle)): ?>
<h1><?php __e('Downloads') ?></h1>
<?php endif; ?>
<?php foreach ($this->files as $file): /* @var $file SoftsaleFile */?>
<div class="softsale-file">
    <h2 class="softsale-file-title"><?php p($file->title) ?></h2>
    <div class="softsale-file-desc"><?php echo $file->desc ?></div>
    <div class="softsale-file-versions">
        <select size="1" class="softsale-file-version-select">
            <option value=""><?php __e('choose a version')?></option>
            <?php echo Am_Html::renderOptions(renderUploadsOptions($file->getUploads(true))); ?>
        </select>
    </div>
    <?php foreach ($file->getUploads(true) as $upload): ?>
    <div class="softsale-file-version-detail" style="display:none;" data-version="<?php p($upload->version)?>">
    <h3><?php echo renderUploadTitle($upload); ?></h3>
    <a href="<?php p(renderUploadLink($file, $upload, $this))?>"><?php __e('download')?></a>
    <?php if ($upload->details): ?><div class="softsale-file-version-detail-details"><?php echo $upload->details ?></div><?php endif ?>
    </div>
    <?php endforeach ?>
</div>
<?php endforeach; ?>
<?php endif ?>
<script type="text/javascript">
jQuery(function($){
    jQuery("select.softsale-file-version-select").change(function(){
        var version = jQuery(this).val();
        var details = jQuery(this).closest(".softsale-file").find(".softsale-file-version-detail");
        details.hide().filter("[data-version='"+version+"']").show();
    });
});
</script>
<?php
function renderUploadTitle(SoftsaleFileUpload $upload)
{
    return $upload->version . ' (' . amDate($upload->date) . ')' . ($upload->desc ? " - {$upload->desc}" : '');
}
/**
 * @param SoftsaleFileUpload[] $uploads
 */
function renderUploadsOptions(array $uploads)
{
    $ret = array();
    foreach ($uploads as $upload)
    {
        $ret[$upload->version] = renderUploadTitle($upload);
    }
    return $ret;
}
function renderUploadLink(SoftsaleFile $file, SoftsaleFileUpload $upload, Am_View $view)
{
    return $view->url('softsale/download', array('file_id'=>$file->pk(), 'file_upload_id' => $upload->pk()));
}
