function sp_gallery($parameters, $id, $return_parameters = false)
{
    global $smcFunc, $context, $modSettings, $scripturl;
    global $sourcedir, $txt, $settings, $boardurl, $galurl;
    static $mod, $GD_Installed;
    $block_parameters = array('limit' => 'int', 'type' => 'select', 'direction' => 'select');
    if ($return_parameters) {
        return $block_parameters;
    }
    $limit = empty($parameters['limit']) ? 1 : (int) $parameters['limit'];
    $type = empty($parameters['type']) ? 0 : 1;
    $direction = empty($parameters['direction']) ? 0 : 1;
    if (!isset($mod)) {
        if (file_exists($sourcedir . '/Aeva-Media.php')) {
            $mod = 'aeva_media';
        } elseif (file_exists($sourcedir . '/MGallery.php')) {
            $mod = 'smf_media_gallery';
        } elseif (file_exists($sourcedir . '/Gallery.php') || file_exists($sourcedir . '/Gallery2.php')) {
            $mod = 'smf_gallery';
        } else {
            $mod = '';
        }
    }
    if (empty($mod)) {
        echo '
								', $txt['error_sp_no_gallery_found'];
        return;
    } elseif ($mod == 'aeva_media') {
        require_once $sourcedir . '/Aeva-Subs.php';
        $items = aeva_getMediaItems(0, $limit, $type ? 'RAND()' : 'm.id_media DESC');
    } elseif ($mod == 'smf_media_gallery') {
        require_once $sourcedir . '/Subs-MGallery.php';
        loadMGal_Settings();
        loadLanguage('MGallery', sp_languageSelect('MGallery'));
        $items = getMediaItems(0, $limit, $type ? 'RAND()' : 'm.id_media DESC');
    } elseif ($mod == 'smf_gallery') {
        loadLanguage('Gallery', sp_languageSelect('Gallery'));
        if (!isset($GD_Installed)) {
            $GD_Installed = function_exists('imagecreate');
        }
        if (empty($modSettings['gallery_url'])) {
            $modSettings['gallery_url'] = $boardurl . '/gallery/';
        }
        $request = $smcFunc['db_query']('', '
			SELECT
				p.id_picture, p.commenttotal, p.filesize, p.views, p.thumbfilename,
				p.filename, p.height, p.width, p.title, p.id_member, m.member_name,
				m.real_name, p.date, p.description
			FROM {db_prefix}gallery_pic AS p
				LEFT JOIN {db_prefix}members AS m ON (m.id_member = p.id_member)
			WHERE p.approved = {int:is_approved}
			ORDER BY {raw:type}
			LIMIT {int:limit}', array('is_approved' => 1, 'type' => $type ? 'RAND()' : 'p.id_picture DESC', 'limit' => $limit));
        $items = array();
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            $items[] = array('id' => $row['id_picture'], 'title' => $row['title'], 'views' => $row['views'], 'poster_id' => $row['id_member'], 'poster_name' => $row['real_name'], 'poster_link' => empty($row['id_member']) ? $txt['gallery_guest'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>', 'thumbfilename' => $row['thumbfilename'], 'filename' => $row['filename'], 'src' => $modSettings['gallery_url'] . ($GD_Installed ? $row['thumbfilename'] : $row['filename'] . '" width="120'));
        }
        $smcFunc['db_free_result']($request);
    }
    if (empty($items)) {
        echo '
								', $txt['error_sp_no_pictures_found'];
        return;
    }
    echo '
								<table class="sp_auto_align">', $direction ? '
									<tr>' : '';
    foreach ($items as $item) {
        echo !$direction ? '
									<tr>' : '', '
										<td>
											<div class="sp_image smalltext">';
        if ($mod == 'aeva_media') {
            echo '
												<a href="', $galurl, 'sa=item;id=', $item['id'], '">', $item['title'], '</a><br />
												<a href="', $galurl, 'sa=item;id=', $item['id'], '"><img src="', $galurl, 'sa=media;id=', $item['id'], ';thumb" alt="" /></a><br />
												', $txt['aeva_views'], ': ', $item['views'], '<br />
												', $txt['aeva_posted_by'], ': <a href="', $scripturl, '?action=profile;u=', $item['poster_id'], '">', $item['poster_name'], '</a><br />
												', $txt['aeva_in_album'], ': <a href="', $galurl, 'sa=album;id=', $item['id_album'], '">', $item['album_name'], '</a>', $item['is_new'] ? '<br /><img alt="" src="' . $settings['images_url'] . '/' . $context['user']['language'] . '/new.gif" border="0" />' : '';
        } elseif ($mod == 'smf_media_gallery') {
            echo '
												<a href="', $galurl, 'sa=item;id=', $item['id'], '">', $item['title'], '</a><br />
												<a href="', $galurl, 'sa=item;id=', $item['id'], '"><img src="', $galurl, 'sa=media;id=', $item['id'], ';thumb" alt="" /></a><br />
												', $txt['mgallery_views'], ': ', $item['views'], '<br />
												', $txt['mgallery_posted_by'], ': <a href="', $scripturl, '?action=profile;u=', $item['poster_id'], '">', $item['poster_name'], '</a><br />
												', $txt['mgallery_in_album'], ': <a href="', $galurl, 'sa=album;id=', $item['id_album'], '">', $item['album_name'], '</a>', $item['is_new'] ? '<br /><img alt="" src="' . $settings['images_url'] . '/' . $context['user']['language'] . '/new.gif" border="0" />' : '';
        } elseif ($mod == 'smf_gallery') {
            echo '
												<a href="', $scripturl, '?action=gallery;sa=view;id=', $item['id'], '">', $item['title'], '</a><br />
												<a href="', $scripturl, '?action=gallery;sa=view;id=', $item['id'], '"><img src="', $item['src'], '" alt="" /></a><br />
												', $txt['gallery_text_views'], $item['views'], '<br />
												', $txt['gallery_text_by'], ' ', $item['poster_link'], '<br />';
        }
        echo '
											</div>
										</td>', !$direction ? '
									</tr>' : '';
    }
    echo $direction ? '
									</tr>' : '', '
								</table>';
}
/**
 * Gallery Block, show a gallery box with gallery items
 *
 * @param mixed[] $parameters
 *		'limit' => number of gallery items to show
 *		'type' =>
 *		'direction' => 0 horizontal or 1 vertical display in the block
 * @param int $id - not used in this block
 * @param boolean $return_parameters if true returns the configuration options for the block
 */
function sp_gallery($parameters, $id, $return_parameters = false)
{
    global $scripturl, $txt, $scripturl, $modSettings;
    static $mod;
    $block_parameters = array('limit' => 'int', 'type' => 'select', 'direction' => 'select');
    if ($return_parameters) {
        return $block_parameters;
    }
    $limit = empty($parameters['limit']) ? 1 : (int) $parameters['limit'];
    $type = empty($parameters['type']) ? 0 : 1;
    $direction = empty($parameters['direction']) ? 0 : 1;
    // right now we only know about one gallery, but more may be added, maybe even ones we can
    // tell folks about :P
    if (!isset($mod)) {
        if (file_exists(SOURCEDIR . '/Aeva-Media.php')) {
            $mod = 'aeva_media';
        } else {
            $mod = '';
        }
    }
    if (empty($mod)) {
        echo '
								', $txt['error_sp_no_gallery_found'];
        return;
    } elseif ($mod == 'aeva_media') {
        require_once SUBSDIR . '/Aeva-Subs.php';
        $items = aeva_getMediaItems(0, $limit, $type ? 'RAND()' : 'm.id_media DESC');
    }
    // No items in the gallery?
    if (empty($items)) {
        echo '
								', $txt['error_sp_no_pictures_found'];
        return;
    }
    // We have gallery items to show!
    echo '
								<table class="sp_auto_align">', $direction ? '
									<tr>' : '';
    foreach ($items as $item) {
        echo !$direction ? '
									<tr>' : '', '
										<td>
											<div class="sp_image smalltext">';
        if ($mod == 'aeva_media') {
            echo '
												<a href="', $scripturl, '?action=media;sa=item;in=', $item['id'], '">', $item['title'], '</a><br />' . (!empty($modSettings['fancybox_enabled']) ? '
												<a href="' . $scripturl . '?action=media;sa=media;in=' . $item['id'] . '" rel="gallery" class="fancybox">' : '
												<a href="' . $scripturl . '?action=media;sa=item;in=' . $item['id'] . '">') . '
												<img src="', $scripturl, '?action=media;sa=media;in=', $item['id'], ';thumb" alt="" /></a><br />
												', $txt['aeva_views'], ': ', $item['views'], '<br />
												', $txt['aeva_posted_by'], ': <a href="', $scripturl, '?action=profile;u=', $item['poster_id'], '">', $item['poster_name'], '</a><br />
												', $txt['aeva_in_album'], ': <a href="', $scripturl, '?action=media;sa=album;in=', $item['id_album'], '">', $item['album_name'], '</a>';
        }
        echo '
											</div>
										</td>', !$direction ? '
									</tr>' : '';
    }
    echo $direction ? '
									</tr>' : '', '
								</table>';
}