/**
 * Puts up random image thumbs from the gallery
 *
 * @param int $number how many images
 * @param string $class optional class
 * @param string $option what you want selected: all for all images, album for selected ones from an album
 * @param mixed $rootAlbum optional album object/folder from which to get the image.
 * @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size.
 * @param integer $height the height/cropheight of the thumb if crop=true else not used
 * @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
 * @param bool $fullimagelink 'false' (default) for the image page link , 'true' for the unprotected full image link (to use Colorbox for example)
 */
function printRandomImages($number = 5, $class = null, $option = 'all', $rootAlbum = '', $width = NULL, $height = NULL, $crop = NULL, $fullimagelink = false)
{
    if (is_null($crop) && is_null($width) && is_null($height)) {
        $crop = 2;
    } else {
        if (is_null($width)) {
            $width = 85;
        }
        if (is_null($height)) {
            $height = 85;
        }
        if (is_null($crop)) {
            $crop = 1;
        } else {
            $crop = (int) $crop && true;
        }
    }
    if (!empty($class)) {
        $class = ' class="' . $class . '"';
    }
    echo "<ul" . $class . ">";
    for ($i = 1; $i <= $number; $i++) {
        switch ($option) {
            case "all":
                $randomImage = getRandomImages();
                break;
            case "album":
                $randomImage = getRandomImagesAlbum($rootAlbum);
                break;
        }
        if (is_object($randomImage) && $randomImage->exists) {
            echo "<li>\n";
            if ($fullimagelink) {
                $randomImageURL = $randomImage->getFullimageURL();
            } else {
                $randomImageURL = $randomImage->getLink();
            }
            echo '<a href="' . html_encode($randomImageURL) . '" title="' . sprintf(gettext('View image: %s'), html_encode($randomImage->getTitle())) . '">';
            switch ($crop) {
                case 0:
                    $sizes = getSizeCustomImage($width, NULL, NULL, NULL, NULL, NULL, NULL, $randomImage);
                    $html = '<img src="' . html_encode(pathurlencode($randomImage->getCustomImage($width, NULL, NULL, NULL, NULL, NULL, NULL, TRUE))) . '" width="' . $sizes[0] . '" height="' . $sizes[1] . '" alt="' . html_encode($randomImage->getTitle()) . '" />' . "\n";
                    break;
                case 1:
                    $sizes = getSizeCustomImage(NULL, $width, $height, $width, $height, NULL, NULL, $randomImage);
                    $html = '<img src="' . html_encode(pathurlencode($randomImage->getCustomImage(NULL, $width, $height, $width, $height, NULL, NULL, TRUE))) . '" width="' . $sizes[0] . '" height="' . $sizes[1] . '" alt="' . html_encode($randomImage->getTitle()) . '" />' . "\n";
                    break;
                case 2:
                    $sizes = getSizeDefaultThumb($randomImage);
                    //$html = '<img src="' . html_encode(pathurlencode($randomImage->getThumb())) . '" width="' . $sizes[0] . '" height="' . $sizes[1] . '" alt="' . html_encode($randomImage->getTitle()) . '" />' . "\n";
                    $html = $randomImage->filename . " (" . $randomImage->album->name . ")";
                    break;
            }
            echo zp_apply_filter('custom_image_html', $html, false);
            echo "</a>";
            echo "</li>\n";
        } else {
            break;
        }
    }
    echo "</ul>";
}
/**
 * Prints image statistic according to $option as an unordered HTML list
 * A css id is attached by default named accordingly'$option'
 *
 * @param string $number the number of albums to get
 * @param string $option "popular" for the most popular images,
 * 		"popular" for the most popular albums,
 * 		"latest" for the latest uploaded by id (Discovery)
 * 		"latest-date" for the latest by date
 * 		"latest-mtime" for the latest by mtime
 *   	"latest-publishdate" for the latest by publishdate
 *    "mostrated" for the most voted,
 * 		"toprated" for the best voted
 * 		"latestupdated" for the latest updated
 * 		"random" for random order (yes, strictly no statistical order...)
 * @param string $albumfolder foldername of an specific album
 * @param bool $showtitle if the image title should be shown
 * @param bool $showdate if the image date should be shown
 * @param bool $showdesc if the image description should be shown
 * @param integer $desclength the length of the description to be shown
 * @param string $showstatistic "hitcounter" for showing the hitcounter (views),
 * 		"rating" for rating,
 * 		"rating+hitcounter" for both.
 * @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size. (Default 85px)
 * @param integer $height the height/cropheight of the thumb if crop=true else not used.  (Default 85px)
 * @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
 * @param bool $collection only if $albumfolder is set: true if you want to get statistics from this album and all of its subalbums
 * @param bool $fullimagelink 'false' (default) for the image page link , 'true' for the unprotected full image link (to use Colorbox for example)
 * @param integer $threshold the minimum number of ratings (for rating options) or hits (for popular option) an image must have to be included in the list. (Default 0)
 * @return string
 */
function printImageStatistic($number, $option, $albumfolder = '', $showtitle = false, $showdate = false, $showdesc = false, $desclength = 40, $showstatistic = '', $width = NULL, $height = NULL, $crop = NULL, $collection = false, $fullimagelink = false, $threshold = 0)
{
    $images = getImageStatistic($number, $option, $albumfolder, $collection, $threshold);
    if (is_null($crop) && is_null($width) && is_null($height)) {
        $crop = 2;
    } else {
        if (is_null($width)) {
            $width = 85;
        }
        if (is_null($height)) {
            $height = 85;
        }
        if (is_null($crop)) {
            $crop = 1;
        } else {
            $crop = (int) $crop && true;
        }
    }
    echo "\n<div id=\"{$option}\">\n";
    echo "<ul>";
    foreach ($images as $image) {
        if ($fullimagelink) {
            $imagelink = $image->getFullImageURL();
        } else {
            $imagelink = $image->getLink();
        }
        echo '<li><a href="' . html_encode($imagelink) . '" title="' . html_encode($image->getTitle()) . "\">\n";
        switch ($crop) {
            case 0:
                $sizes = getSizeCustomImage($width, NULL, NULL, NULL, NULL, NULL, NULL, $image);
                echo '<img src="' . html_encode(pathurlencode($image->getCustomImage($width, NULL, NULL, NULL, NULL, NULL, NULL, TRUE))) . '" width="' . $sizes[0] . '" height="' . $sizes[1] . '" alt="' . html_encode($image->getTitle()) . "\" /></a>\n";
                break;
            case 1:
                $sizes = getSizeCustomImage(NULL, $width, $height, $width, $height, NULL, NULL, $image);
                echo '<img src="' . html_encode(pathurlencode($image->getCustomImage(NULL, $width, $height, $width, $height, NULL, NULL, TRUE))) . '" width="' . $sizes[0] . '" height="' . $sizes[1] . '" alt="' . html_encode($image->getTitle()) . "\" width=\"" . $width . "\" height=\"" . $height . "\" /></a>\n";
                break;
            case 2:
                $sizes = getSizeDefaultThumb($image);
                echo '<img src="' . html_encode(pathurlencode($image->getThumb())) . '" width="' . $sizes[0] . '" height="' . $sizes[1] . '" alt="' . html_encode($image->getTitle()) . "\" /></a>\n<br />";
                break;
        }
        if ($showtitle) {
            echo '<h3><a href="' . html_encode(pathurlencode($image->getLink())) . '" title="' . html_encode($image->getTitle()) . "\">\n";
            echo $image->getTitle() . "</a></h3>\n";
        }
        if ($showdate) {
            echo "<p>" . zpFormattedDate(DATE_FORMAT, strtotime($image->getDateTime())) . "</p>";
        }
        if ($showstatistic === "rating" or $showstatistic === "rating+hitcounter") {
            $votes = $image->get("total_votes");
            $value = $image->get("total_value");
            if ($votes != 0) {
                $rating = round($value / $votes, 1);
            }
            echo "<p>" . sprintf(gettext('Rating: %1$u (Votes: %2$u)'), $rating, $votes) . "</p>";
        }
        if ($showstatistic === "hitcounter" or $showstatistic === "rating+hitcounter") {
            $hitcounter = $image->getHitcounter();
            if (empty($hitcounter)) {
                $hitcounter = "0";
            }
            echo "<p>" . sprintf(gettext("Views: %u"), $hitcounter) . "</p>";
        }
        if ($showdesc) {
            echo shortenContent($image->getDesc(), $desclength, ' (...)');
        }
        echo "</li>";
    }
    echo "</ul></div>\n";
}