<?php
// load file
if (isset($_REQUEST['u']))
{
    $file = file::loadById($_REQUEST['u']);
    if (!$file)
    {
        // failed lookup of file
        coreFunctions::redirect(WEB_ROOT . '/account_home.' . SITE_CONFIG_PAGE_EXTENSION);
    }

    // TODO - check current user has permission to access file
    //if ($file->userId != $Auth->id)
    //{
    //    coreFunctions::redirect(WEB_ROOT . '/account_home.' . SITE_CONFIG_PAGE_EXTENSION);
    //}
}
else
{
    coreFunctions::redirect(WEB_ROOT . '/account_home.' . SITE_CONFIG_PAGE_EXTENSION);
}

$html = '';
$themeObj = themeHelper::getLoadedInstance();
$similarImages = $themeObj->getSimilarImages($file);
$totalImages = COUNT($similarImages);
if($totalImages)
{
    // find index of currently selected
    $selectedIndex = 0;
    if($totalImages > 11)
    {
        foreach($similarImages AS $k=>$similarImage)
        {
            if($similarImage->id == $file->id)
            {
                $selectedIndex = $k;
            }
        }
    }
    
    $html .= '<div class="similar-images-list" data-slick=\'{"initialSlide": '.$selectedIndex.'}\'>';
    foreach($similarImages AS $similarImage)
    {
        $imageLink = file::getIconPreviewImageUrl((array) $similarImage, false, 48, false, 280, 280, 'middle');
        $html .= '<div><a href="#" onClick="showImage('.$similarImage->id.'); return false;"><img data-lazy="'.$imageLink.'"/></a></div>';
    }
    $html .= '</div>';
}

// prepare result
$returnJson = array();
$returnJson['success'] = true;
$returnJson['html'] = $html;

echo json_encode($returnJson);