function do_album_navigator($parent, $page, $skip, $propclass, $extraclause = '')
    {
        global $wpdb;
        static $level;
        static $ca;
        if (!$level) {
            $level = '1';
            if (isset($_REQUEST['wppa-album'])) {
                $ca = $_REQUEST['wppa-album'];
            } elseif (isset($_REQUEST['album'])) {
                $ca = $_REQUEST['album'];
            } else {
                $ca = '0';
            }
            $ca = wppa_force_numeric_else($ca, '0');
            if ($ca && !wppa_album_exists($ca)) {
                //				wppa_log('dbg', 'Non-existent album '.$ca.' in url. Referrer= '.$_ENV["HTTP_REFERER"].', Request uri= '.$_ENV["REQUEST_URI"]);
                $ca = '0';
            }
        } else {
            $level++;
        }
        $slide = wppa_opt('album_navigator_widget_linktype') == 'slide' ? '&wppa-slide=1' : '';
        $w = $this->get_widget_id();
        $p = $parent;
        $result = '';
        $albums = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_ALBUMS . "` WHERE `a_parent` = %s " . $extraclause . wppa_get_album_order(max('0', $parent)), $parent), ARRAY_A);
        if (!empty($albums)) {
            wppa_cache_album('add', $albums);
            $result .= '<ul>';
            foreach ($albums as $album) {
                $a = $album['id'];
                $treecount = wppa_treecount_a($a);
                if ($treecount['albums'] || $treecount['photos'] > wppa_opt('min_thumbs') || $skip == 'no') {
                    $result .= '
						<li class="anw-' . $w . '-' . $p . $propclass . '" style="list-style:none; display:' . ($level == '1' ? '' : 'none') . ';">';
                    if (wppa_has_children($a)) {
                        $result .= '
							<div style="cursor:default;width:12px;float:left;text-align:center;font-weight:bold;" class="anw-' . $w . '-' . $a . '-" onclick="jQuery(\'.anw-' . $w . '-' . $a . '\').css(\'display\',\'\'); jQuery(\'.anw-' . $w . '-' . $a . '-\').css(\'display\',\'none\');" >' . ($a == $ca ? '&raquo;' : '+') . '</div>
							<div style="cursor:default;width:12px;float:left;text-align:center;font-weight:bold;display:none;" class="anw-' . $w . '-' . $a . '" onclick="jQuery(\'.anw-' . $w . '-' . $a . '-\').css(\'display\',\'\'); jQuery(\'.anw-' . $w . '-' . $a . '\').css(\'display\',\'none\'); jQuery(\'.p-' . $w . '-' . $a . '\').css(\'display\',\'none\');" >' . ($a == $ca ? '&raquo;' : '-') . '</div>';
                    } else {
                        $result .= '
							<div style="width:12px;float:left;" >&nbsp;' . ($a == $ca ? '&raquo;' : '') . '</div>';
                    }
                    $result .= '
							<a href="' . wppa_encrypt_url(wppa_get_permalink($page) . '&amp;wppa-album=' . $a . '&amp;wppa-cover=0&amp;wppa-occur=1' . $slide) . '">' . wppa_get_album_name($a) . '</a>
						</li>';
                    $newpropclass = $propclass . ' p-' . $w . '-' . $p;
                    $result .= '<li class="anw-' . $w . '-' . $p . $propclass . '" style="list-style:none;" >' . $this->do_album_navigator($a, $page, $skip, $newpropclass, $extraclause) . '</li>';
                }
            }
            $result .= '</ul>';
            if ($level == '1' && $ca) {
                // && $parent != '-1' ) {
                $result .= '<script type="text/javascript" >';
                while ($ca != '0' && $ca != '-1') {
                    $result .= '
								jQuery(\'.anw-' . $w . '-' . $ca . '\').css(\'display\',\'\'); jQuery(\'.anw-' . $w . '-' . $ca . '-\').css(\'display\',\'none\');';
                    $ca = wppa_get_parentalbumid($ca);
                }
                $result .= '</script>';
            }
        }
        $level--;
        return str_replace('<ul></ul>', '', $result);
    }
 /** @see WP_Widget::widget */
 function widget($args, $instance)
 {
     global $wpdb;
     require_once dirname(__FILE__) . '/wppa-links.php';
     require_once dirname(__FILE__) . '/wppa-styles.php';
     require_once dirname(__FILE__) . '/wppa-functions.php';
     require_once dirname(__FILE__) . '/wppa-thumbnails.php';
     require_once dirname(__FILE__) . '/wppa-boxes-html.php';
     require_once dirname(__FILE__) . '/wppa-slideshow.php';
     wppa_initialize_runtime();
     wppa('in_widget', 'topten');
     wppa_bump_mocc();
     extract($args);
     $instance = wp_parse_args((array) $instance, array('title' => '', 'sortby' => 'mean_rating', 'title' => '', 'album' => '', 'display' => 'thumbs', 'meanrat' => 'yes', 'ratcount' => 'yes', 'viewcount' => 'yes', 'includesubs' => 'yes', 'medalsonly' => 'no', 'showowner' => 'no', 'showalbum' => 'no'));
     $widget_title = apply_filters('widget_title', $instance['title']);
     $page = in_array(wppa_opt('topten_widget_linktype'), wppa('links_no_page')) ? '' : wppa_get_the_landing_page('topten_widget_linkpage', __('Top Ten Photos', 'wp-photo-album-plus'));
     $albumlinkpage = wppa_get_the_landing_page('topten_widget_album_linkpage', __('Top Ten Photo album', 'wp-photo-album-plus'));
     $max = wppa_opt('topten_count');
     $album = $instance['album'];
     switch ($instance['sortby']) {
         case 'mean_rating':
             $sortby = '`mean_rating` DESC, `rating_count` DESC, `views` DESC';
             break;
         case 'rating_count':
             $sortby = '`rating_count` DESC, `mean_rating` DESC, `views` DESC';
             break;
         case 'views':
             $sortby = '`views` DESC, `mean_rating` DESC, `rating_count` DESC';
             break;
     }
     $display = $instance['display'];
     $meanrat = $instance['meanrat'] == 'yes';
     $ratcount = $instance['ratcount'] == 'yes';
     $viewcount = $instance['viewcount'] == 'yes';
     $includesubs = $instance['includesubs'] == 'yes';
     $albenum = '';
     $medalsonly = $instance['medalsonly'] == 'yes';
     $showowner = $instance['showowner'] == 'yes';
     $showalbum = $instance['showalbum'] == 'yes';
     wppa('medals_only', $medalsonly);
     $likes = wppa_opt('rating_display_type') == 'likes';
     // When likes only, mean rating has no meaning, chan to (rating)(like)count
     if ($likes && $instance['sortby'] == 'mean_rating') {
         $instance['sortby'] = 'rating_count';
     }
     // Album specified?
     if ($album) {
         // All albums ?
         if ($album == '-2') {
             $album = '0';
         }
         // Albums of owner is current logged in user or public?
         if ($album == '-3') {
             $temp = $wpdb->get_results("SELECT `id` FROM `" . WPPA_ALBUMS . "` WHERE `owner` = '--- public ---' OR `owner` = '" . wppa_get_user() . "' ORDER BY `id`", ARRAY_A);
             $album = '';
             if ($temp) {
                 foreach ($temp as $t) {
                     $album .= '.' . $t['id'];
                 }
                 $album = ltrim($album, '.');
             }
         }
         // Including subalbums?
         if ($includesubs) {
             $albenum = wppa_alb_to_enum_children($album);
             $albenum = wppa_expand_enum($albenum);
             $album = str_replace('.', ',', $albenum);
         }
         // Doit
         if ($medalsonly) {
             $thumbs = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE `album` IN (" . $album . ") " . "AND `status` IN ( 'gold', 'silver', 'bronze' ) " . "ORDER BY " . $sortby . " " . "LIMIT " . $max, ARRAY_A);
         } else {
             $thumbs = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE `album` IN (" . $album . ") " . "ORDER BY " . $sortby . " " . "LIMIT " . $max, ARRAY_A);
         }
     } else {
         if ($medalsonly) {
             $thumbs = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE `status` IN ( 'gold', 'silver', 'bronze' ) " . "ORDER BY " . $sortby . " " . "LIMIT " . $max, ARRAY_A);
         } else {
             $thumbs = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` " . "ORDER BY " . $sortby . " " . "LIMIT " . $max, ARRAY_A);
         }
     }
     $widget_content = "\n" . '<!-- WPPA+ TopTen Widget start -->';
     $maxw = wppa_opt('topten_size');
     $maxh = $maxw;
     $lineheight = wppa_opt('fontsize_widget_thumb') * 1.5;
     $maxh += $lineheight;
     if ($meanrat) {
         $maxh += $lineheight;
     }
     if ($ratcount) {
         $maxh += $lineheight;
     }
     if ($viewcount) {
         $maxh += $lineheight;
     }
     if ($showowner) {
         $maxh += $lineheight;
     }
     if ($showalbum) {
         $maxh += $lineheight;
     }
     if ($thumbs) {
         foreach ($thumbs as $image) {
             $thumb = $image;
             // Make the HTML for current picture
             if ($display == 'thumbs') {
                 $widget_content .= "\n" . '<div class="wppa-widget" style="width:' . $maxw . 'px; height:' . $maxh . 'px; margin:4px; display:inline; text-align:center; float:left;">';
             } else {
                 $widget_content .= "\n" . '<div class="wppa-widget" >';
             }
             if ($image) {
                 $no_album = !$album;
                 if ($no_album) {
                     $tit = __('View the top rated photos', 'wp-photo-album-plus');
                 } else {
                     $tit = esc_attr(__(stripslashes($image['description'])));
                 }
                 $compressed_albumenum = wppa_compress_enum($albenum);
                 $link = wppa_get_imglnk_a('topten', $image['id'], '', $tit, '', $no_album, $compressed_albumenum);
                 $file = wppa_get_thumb_path($image['id']);
                 $imgstyle_a = wppa_get_imgstyle_a($image['id'], $file, $maxw, 'center', 'ttthumb');
                 $imgurl = wppa_get_thumb_url($image['id'], '', $imgstyle_a['width'], $imgstyle_a['height']);
                 $imgevents = wppa_get_imgevents('thumb', $image['id'], true);
                 $title = $link ? esc_attr(stripslashes($link['title'])) : '';
                 $widget_content .= wppa_get_the_widget_thumb('topten', $image, $album, $display, $link, $title, $imgurl, $imgstyle_a, $imgevents);
                 $widget_content .= "\n\t" . '<div style="font-size:' . wppa_opt('fontsize_widget_thumb') . 'px; line-height:' . $lineheight . 'px;">';
                 // Display (owner) ?
                 if ($showowner) {
                     $widget_content .= '<div>(' . $image['owner'] . ')</div>';
                 }
                 // Display (album) ?
                 if ($showalbum) {
                     $href = wppa_convert_to_pretty(wppa_encrypt_url(wppa_get_album_url($image['album'], $albumlinkpage, 'content', '1')));
                     $widget_content .= '<div>(<a href="' . $href . '" >' . wppa_get_album_name($image['album']) . '</a>)</div>';
                 }
                 // Display the rating
                 if ($likes) {
                     $lt = wppa_get_like_title_a($image['id']);
                 }
                 switch ($instance['sortby']) {
                     case 'mean_rating':
                         if ($meanrat == 'yes') {
                             $widget_content .= '<div>' . wppa_get_rating_by_id($image['id']) . '</div>';
                         }
                         if ($ratcount == 'yes') {
                             $n = wppa_get_rating_count_by_id($image['id']);
                             $widget_content .= '<div>' . sprintf(_n('%d vote', '%d votes', $n, 'wp-photo-album-plus'), $n) . '</div>';
                         }
                         if ($viewcount == 'yes') {
                             $n = $image['views'];
                             $widget_content .= '<div>' . sprintf(_n('%d view', '%d views', $n, 'wp-photo-album-plus'), $n) . '</div>';
                         }
                         break;
                     case 'rating_count':
                         if ($ratcount == 'yes') {
                             $n = wppa_get_rating_count_by_id($image['id']);
                             $widget_content .= '<div>' . ($likes ? $lt['display'] : sprintf(_n('%d vote', '%d votes', $n, 'wp-photo-album-plus'), $n)) . '</div>';
                         }
                         if ($meanrat == 'yes') {
                             $widget_content .= '<div>' . wppa_get_rating_by_id($image['id']) . '</div>';
                         }
                         if ($viewcount == 'yes') {
                             $n = $image['views'];
                             $widget_content .= '<div>' . sprintf(_n('%d view', '%d views', $n, 'wp-photo-album-plus'), $n) . '</div>';
                         }
                         break;
                     case 'views':
                         if ($viewcount == 'yes') {
                             $n = $image['views'];
                             $widget_content .= '<div>' . sprintf(_n('%d view', '%d views', $n, 'wp-photo-album-plus'), $n) . '</div>';
                         }
                         if ($meanrat == 'yes') {
                             $widget_content .= '<div>' . wppa_get_rating_by_id($image['id']) . '</div>';
                         }
                         if ($ratcount == 'yes') {
                             $n = wppa_get_rating_count_by_id($image['id']);
                             $widget_content .= '<div>' . ($likes ? $lt['display'] : sprintf(_n('%d vote', '%d votes', $n, 'wp-photo-album-plus'), $n)) . '</div>';
                         }
                         break;
                 }
                 $widget_content .= '</div>';
             } else {
                 // No image
                 $widget_content .= __('Photo not found', 'wp-photo-album-plus');
             }
             $widget_content .= "\n" . '</div>';
         }
     } else {
         $widget_content .= __('There are no rated photos (yet)', 'wp-photo-album-plus');
     }
     $widget_content .= '<div style="clear:both"></div>';
     $widget_content .= "\n" . '<!-- WPPA+ TopTen Widget end -->';
     echo "\n" . $before_widget;
     if (!empty($widget_title)) {
         echo $before_title . $widget_title . $after_title;
     }
     echo $widget_content . $after_widget;
     //wppa( 'in_widget', false );
     wppa_reset_occurrance();
 }
 /** @see WP_Widget::widget */
 function widget($args, $instance)
 {
     global $wpdb;
     wppa('in_widget', 'potd');
     wppa_bump_mocc();
     require_once dirname(__FILE__) . '/wppa-links.php';
     require_once dirname(__FILE__) . '/wppa-styles.php';
     require_once dirname(__FILE__) . '/wppa-functions.php';
     require_once dirname(__FILE__) . '/wppa-thumbnails.php';
     require_once dirname(__FILE__) . '/wppa-boxes-html.php';
     require_once dirname(__FILE__) . '/wppa-slideshow.php';
     wppa_initialize_runtime();
     extract($args);
     $widget_title = apply_filters('widget_title', $instance['title']);
     // get the photo  ($image)
     $image = wppa_get_potd();
     // Make the HTML for current picture
     $widget_content = "\n" . '<!-- WPPA+ Photo of the day Widget start -->';
     $ali = wppa_opt('potd_align');
     if ($ali != 'none') {
         $align = 'text-align:' . $ali . ';';
     } else {
         $align = '';
     }
     $widget_content .= "\n" . '<div class="wppa-widget-photo" style="' . $align . ' padding-top:2px;position:relative;" >';
     if ($image) {
         $id = $image['id'];
         $w = wppa_opt('potd_widget_width');
         $ratio = wppa_get_photoy($id) / wppa_get_photox($id);
         $h = round($w * $ratio);
         $usethumb = wppa_use_thumb_file($id, wppa_opt('potd_widget_width'), '0');
         $imgurl = wppa_fix_poster_ext($usethumb ? wppa_get_thumb_url($id, '', $w, $h) : wppa_get_photo_url($id, '', $w, $h), $id);
         $name = wppa_get_photo_name($id);
         $page = in_array(wppa_opt('potd_linktype'), wppa('links_no_page')) && !wppa_switch('potd_counter') ? '' : wppa_get_the_landing_page('potd_linkpage', __('Photo of the day', 'wp-photo-album-plus'));
         $link = wppa_get_imglnk_a('potdwidget', $id);
         $is_video = wppa_is_video($id);
         $has_audio = wppa_has_audio($id);
         if ($link['is_lightbox']) {
             $lightbox = ($is_video ? ' data-videohtml="' . esc_attr(wppa_get_video_body($id)) . '"' . ' data-videonatwidth="' . wppa_get_videox($id) . '"' . ' data-videonatheight="' . wppa_get_videoy($id) . '"' : '') . ($has_audio ? ' data-audiohtml="' . esc_attr(wppa_get_audio_body($id)) . '"' : '') . ' ' . wppa('rel') . '="' . wppa_opt('lightbox_name') . '"' . ' data-alt="' . esc_attr(wppa_get_imgalt($id, true)) . '"';
         } else {
             $lightbox = '';
         }
         if ($link) {
             if ($link['is_lightbox']) {
                 $cursor = ' cursor:url(' . wppa_get_imgdir() . wppa_opt('magnifier') . '),pointer;';
                 $title = wppa_zoom_in($id);
                 $ltitle = wppa_get_lbtitle('potd', $id);
             } else {
                 $cursor = ' cursor:pointer;';
                 $title = $link['title'];
                 $ltitle = $title;
             }
         } else {
             $cursor = ' cursor:default;';
             $title = esc_attr(stripslashes(__($image['name'], 'wp-photo-album-plus')));
         }
         // The medal if on top
         $widget_content .= wppa_get_medal_html_a(array('id' => $id, 'size' => 'M', 'where' => 'top'));
         // The link, if any
         if ($link) {
             $widget_content .= "\n\t" . '<a href = "' . $link['url'] . '" target="' . $link['target'] . '" ' . $lightbox . ' ' . wppa('lbtitle') . '="' . $ltitle . '">';
         }
         // The image
         if (wppa_is_video($id)) {
             $widget_content .= "\n\t\t" . wppa_get_video_html(array('id' => $id, 'width' => wppa_opt('potd_widget_width'), 'title' => $title, 'controls' => wppa_opt('potd_linktype') == 'none', 'cursor' => $cursor));
         } else {
             $widget_content .= '<img' . ' src="' . $imgurl . '"' . ' style="width: ' . wppa_opt('potd_widget_width') . 'px;' . $cursor . '"' . ' ' . wppa_get_imgalt($id) . ($title ? 'title="' . $title . '"' : '') . ' />';
         }
         // Close the link
         if ($link) {
             $widget_content .= '</a>';
         }
         // The medal if at the bottom
         $widget_content .= wppa_get_medal_html_a(array('id' => $id, 'size' => 'M', 'where' => 'bot'));
         // The counter
         if (wppa_switch('potd_counter')) {
             // If we want this
             $alb = wppa_get_photo_item($id, 'album');
             $c = $wpdb->get_var("SELECT COUNT(*) FROM `" . WPPA_PHOTOS . "` WHERE `album` = " . $alb) - 1;
             if ($c > 0) {
                 if (wppa_opt('potd_counter_link') == 'thumbs') {
                     $lnk = wppa_get_album_url($alb, $page, 'thumbs', '1');
                 } elseif (wppa_opt('potd_counter_link') == 'slide') {
                     $lnk = wppa_get_slideshow_url($alb, $page, $id, '1');
                 } elseif (wppa_opt('potd_counter_link') == 'single') {
                     $lnk = wppa_encrypt_url(get_permalink($page) . '?occur=1&photo=' . $id);
                     //	wppa_get_image_page_url_by_id( $id, true, false, $page );
                 } else {
                     wppa_log('Err', 'Unimplemented counter link type in wppa-potd-widget: ' . wppa_opt('potd_counter_link'));
                 }
                 $widget_content .= '<a href="' . $lnk . '" >' . '<div style="font-size:12px;position:absolute;right:4px;bottom:4px;" >+' . $c . '</div>' . '</a>';
             }
         }
         // Audio
         if (wppa_has_audio($id)) {
             $widget_content .= wppa_get_audio_html(array('id' => $id, 'width' => wppa_opt('potd_widget_width'), 'controls' => true));
         }
     } else {
         // No image
         $widget_content .= __('Photo not found', 'wp-photo-album-plus');
     }
     $widget_content .= "\n" . '</div>';
     // Add subtitle, if any
     if ($image) {
         switch (wppa_opt('potd_subtitle')) {
             case 'none':
                 break;
             case 'name':
                 $widget_content .= '<div class="wppa-widget-text wppa-potd-text" style="' . $align . '">' . wppa_get_photo_name($id) . '</div>';
                 break;
             case 'desc':
                 $widget_content .= "\n" . '<div class="wppa-widget-text wppa-potd-text" style="' . $align . '">' . wppa_get_photo_desc($id) . '</div>';
                 break;
             case 'owner':
                 $owner = $image['owner'];
                 $user = get_user_by('login', $owner);
                 $owner = $user->display_name;
                 $widget_content .= "\n" . '<div class="wppa-widget-text wppa-potd-text" style="' . $align . '">' . __('By:', 'wp-photo-album-plus') . ' ' . $owner . '</div>';
                 break;
             default:
                 wppa_log('Err', 'Unimplemented potd_subtitle found in wppa-potd-widget: ' . wppa_opt('potd_subtitle'));
         }
     }
     $widget_content .= '<div style="clear:both;" ></div>';
     $widget_content .= "\n" . '<!-- WPPA+ Photo of the day Widget end -->';
     echo "\n" . $before_widget;
     if (!empty($widget_title)) {
         echo $before_title . $widget_title . $after_title;
     }
     echo $widget_content . $after_widget;
     wppa('in_widget', false);
 }
function wppa_get_thumb_masonry($id)
{
    global $wpdb;
    // Init
    if (!$id) {
        wppa_dbg_msg('Please check file wppa-theme.php or any other php file that calls wppa_thumb_masonry(). Argument 1: photo id is missing!', 'red', 'force');
        die('Please check your configuration');
    }
    $result = '';
    // Encrypted photo id
    $xid = wppa_encrypt_photo($id);
    $cont_width = wppa_get_container_width();
    $count_cols = ceil($cont_width / wppa_opt('thumbsize'));
    // Get the photo info
    $thumb = wppa_cache_thumb($id);
    // Get the album info
    $album = wppa_cache_album($thumb['album']);
    // Get photo info
    $is_video = wppa_is_video($id);
    $has_audio = wppa_has_audio($id);
    $imgsrc = wppa_fix_poster_ext(wppa_get_thumb_path($id), $id);
    if (!wppa_is_video($id) && !is_file($imgsrc)) {
        $result .= '<div' . ' class=""' . ' style="' . 'font-size:10px;' . 'color:red;' . 'width:' . wppa_opt('thumbsize') . 'px;' . 'position:static;' . 'float:left;' . '"' . ' >' . sprintf(__('Missing thumbnail image #%s', 'wp-photo-album-plus'), $id) . '</div>';
        return $result;
    }
    $alt = $album['alt_thumbsize'] == 'yes' ? '_alt' : '';
    $imgattr_a = wppa_get_imgstyle_a($id, $imgsrc, wppa_opt('thumbsize' . $alt), 'optional', 'thumb');
    // Verical style ?
    if (wppa_opt('thumbtype') == 'masonry-v') {
        $imgwidth = wppa_opt('thumbsize');
        $imgheight = $imgwidth * wppa_get_thumbratioyx($id);
        $imgstyle = 'width:100%; height:auto; margin:0; position:relative; box-sizing:border-box;';
        $frame_h = '';
    } else {
        $imgheight = wppa_opt('thumbsize');
        $imgwidth = $imgheight * wppa_get_thumbratioxy($id);
        $imgstyle = 'height:100%;' . 'width:auto;' . 'margin:0;' . 'position:relative;' . 'box-sizing:border-box;' . '';
        $frame_h = 'height:100%; ';
    }
    // Mouseover effect?
    if (wppa_switch('use_thumb_opacity')) {
        $opac = wppa_opt('thumb_opacity');
        $imgstyle .= ' opacity:' . $opac / 100 . '; filter:alpha( opacity=' . $opac . ' );';
    }
    // Padding
    if (wppa_is_int(wppa_opt('tn_margin') / 2)) {
        $imgstyle .= ' padding:' . wppa_opt('tn_margin') / 2 . 'px;';
    } else {
        $p1 = floor(wppa_opt('tn_margin') / 2);
        $p2 = ceil(wppa_opt('tn_margin') / 2);
        $imgstyle .= ' padding:' . $p1 . 'px ' . $p2 . 'px ' . $p2 . 'px ' . $p1 . 'px;';
    }
    // Cursor
    $cursor = $imgattr_a['cursor'];
    // Popup ?
    if (wppa_switch('use_thumb_popup')) {
        // Landscape?
        if ($imgwidth > $imgheight) {
            $popwidth = wppa_opt('popupsize');
            $popheight = round($popwidth * $imgheight / $imgwidth);
        } else {
            $popheight = wppa_opt('popupsize');
            $popwidth = round($popheight * $imgwidth / $imgheight);
        }
    } else {
        $popwidth = $imgwidth;
        $popheight = $imgheight;
    }
    $imgurl = wppa_fix_poster_ext(wppa_get_thumb_url($id, '', $popwidth, $popheight), $id);
    $events = wppa_get_imgevents('thumb', $id);
    $imgalt = wppa_get_imgalt($id);
    // returns something like ' alt="Any text" '
    $title = esc_attr(wppa_get_masonry_title($id));
    // esc_attr( wppa_get_photo_name( $id ) );
    // Feed ?
    if (is_feed()) {
        $imgattr_a = wppa_get_imgstyle_a($id, $imgsrc, '100', '4', 'thumb');
        $style = $imgattr_a['style'];
        $result .= '<a href="' . get_permalink() . '">' . '<img' . ' src="' . $imgurl . '"' . ' ' . $imgalt . ($title ? ' title="' . $title . '"' : '') . ' style="' . $style . '"' . ' />' . '</a>';
        return;
    }
    // Get the image link
    if (wppa('is_topten')) {
        $no_album = !wppa('start_album');
        if ($no_album) {
            $tit = __('View the top rated photos', 'wp-photo-album-plus');
        } else {
            $tit = esc_attr(__(stripslashes($thumb['description'])));
        }
        $link = wppa_get_imglnk_a('thumb', $id, '', $tit, '', $no_album);
    } else {
        $link = wppa_get_imglnk_a('thumb', $id);
    }
    // voor parent uplr
    // Open the thumbframe
    // Add class wppa-mas-h-{mocc} for ie if horizontal
    $is_ie_or_chrome = strpos($_SERVER["HTTP_USER_AGENT"], 'Trident') || strpos($_SERVER["HTTP_USER_AGENT"], 'Chrome');
    $result .= '
				<div' . ' id="thumbnail_frame_masonry_' . $xid . '_' . wppa('mocc') . '"' . ($is_ie_or_chrome && wppa_opt('thumbtype') == 'masonry-h' ? ' class="wppa-mas-h-' . wppa('mocc') . '"' : '') . ' style="' . $frame_h . 'position:static;' . 'float:left;' . 'font-size:12px;' . 'line-height:8px;' . 'overflow:hidden;' . 'box-sizing:content-box;' . '" >';
    // The medals
    $result .= wppa_get_medal_html_a(array('id' => $id, 'size' => 'M', 'where' => 'top'));
    // See if ajax possible
    if ($link) {
        if ($link['is_url']) {
            // is url
            if (wppa_switch('allow_ajax') && wppa_opt('thumb_linktype') == 'photo' && wppa_opt('thumb_linkpage') == '0' && !wppa_switch('thumb_blank') && !(wppa_switch('thumb_overrule') && $thumb['linkurl']) && !wppa('is_topten') && !wppa('is_lasten') && !wppa('is_comten') && !wppa('is_featen') && !wppa('is_tag') && !wppa('is_upldr') && !wppa('src') && !wppa('supersearch') && (wppa_is_int(wppa('start_album')) || wppa('start_album') == '')) {
                // Ajax	possible
                // The a img ajax
                $p = wppa('calendar') ? '' : '&amp;wppa-photo=' . $xid;
                $onclick = 'wppaDoAjaxRender( ' . wppa('mocc') . ', \'' . wppa_encrypt_url(wppa_get_slideshow_url_ajax(wppa('start_album'), '0')) . '&amp;wppa-photo=' . $xid . '\', \'' . wppa_convert_to_pretty(wppa_encrypt_url(wppa_get_slideshow_url(wppa('start_album'), '0') . $p)) . '\' )';
                // old			$onclick = "wppaDoAjaxRender( ".wppa( 'mocc' ).", '".wppa_get_slideshow_url_ajax( wppa( 'start_album' ), '0' ).'&amp;wppa-photo='.$id."', '".wppa_convert_to_pretty( wppa_get_slideshow_url( wppa( 'start_album' ), '0' )."&amp;wppa-photo=".$id )."' )";
                $result .= '<a style="position:static;" class="thumb-img" id="x-' . $id . '-' . wppa('mocc') . '">';
                if ($is_video) {
                    //					$result .= '<video preload="metadata" onclick="'.$onclick.'" id="i-'.$id.'-'.wppa( 'mocc' ).'" '.$imgalt.' title="'.$title.'" style="'.$imgstyle.' cursor:pointer;" '.$events.' >'.wppa_get_video_body( $id ).'</video>';
                    $result .= wppa_get_video_html(array('id' => $id, 'controls' => wppa_switch('thumb_video'), 'margin_top' => '0', 'margin_bottom' => '0', 'tagid' => 'i-' . $xid . '-' . wppa('mocc'), 'cursor' => 'cursor:pointer;', 'events' => $events, 'title' => $title, 'preload' => 'metadata', 'onclick' => $onclick, 'lb' => false, 'class' => '', 'style' => $imgstyle, 'use_thumb' => true));
                } else {
                    $result .= '<img' . ' onclick="' . $onclick . '"' . ' id="i-' . $xid . '-' . wppa('mocc') . '"' . ' src="' . $imgurl . '"' . ' ' . $imgalt . ($title ? ' title="' . $title . '"' : '') . ' style="' . $imgstyle . ' cursor:pointer;"' . ' ' . $events . ' />';
                }
                $result .= '</a>';
            } else {
                // non ajax
                // The a img non ajax
                $result .= '<a style="position:static;" href="' . $link['url'] . '" target="' . $link['target'] . '" class="thumb-img" id="x-' . $xid . '-' . wppa('mocc') . '">';
                if ($is_video) {
                    //					$result .= '<video preload="metadata" id="i-'.$id.'-'.wppa( 'mocc' ).'" '.$imgalt.' title="'.$title.'" width="'.$imgwidth.'" height="'.$imgheight.'" style="'.$imgstyle.' cursor:pointer;" '.$events.' >'.wppa_get_video_body( $id ).'</video>';
                    $result .= wppa_get_video_html(array('id' => $id, 'controls' => wppa_switch('thumb_video'), 'margin_top' => '0', 'margin_bottom' => '0', 'tagid' => 'i-' . $xid . '-' . wppa('mocc'), 'cursor' => 'cursor:pointer;', 'events' => $events, 'title' => $title, 'preload' => 'metadata', 'onclick' => '', 'lb' => false, 'class' => '', 'style' => $imgstyle, 'use_thumb' => true));
                } else {
                    $result .= '<img' . ' id="i-' . $xid . '-' . wppa('mocc') . '"' . ' src="' . $imgurl . '"' . ' ' . $imgalt . ($title ? ' title="' . $title . '"' : '') . ' style="' . $imgstyle . 'cursor:pointer;"' . ' ' . $events . ' />';
                }
                $result .= '</a>';
            }
        } elseif ($link['is_lightbox']) {
            // The a img
            $title = wppa_get_lbtitle('thumb', $id);
            $result .= '<a href="' . $link['url'] . '"' . ' target="' . $link['target'] . '"' . ($is_video ? ' data-videohtml="' . esc_attr(wppa_get_video_body($id)) . '"' . ' data-videonatwidth="' . wppa_get_videox($id) . '"' . ' data-videonatheight="' . wppa_get_videoy($id) . '"' : '') . ($has_audio ? ' data-audiohtml="' . esc_attr(wppa_get_audio_body($id)) . '"' : '') . ' ' . wppa('rel') . '="' . wppa_opt('lightbox_name') . '[occ' . wppa('mocc') . ']"' . ($title ? ' ' . wppa('lbtitle') . '="' . $title . '"' : '') . ' class="thumb-img"' . ' id="x-' . $xid . '-' . wppa('mocc') . '"' . ' data-alt="' . esc_attr(wppa_get_imgalt($id, true)) . '"' . ' >';
            // The image
            $title = wppa_zoom_in($id);
            // Video?
            if ($is_video) {
                $result .= wppa_get_video_html(array('id' => $id, 'controls' => wppa_switch('thumb_video'), 'margin_top' => '0', 'margin_bottom' => '0', 'tagid' => 'i-' . $xid . '-' . wppa('mocc'), 'cursor' => $cursor, 'events' => $events, 'title' => $title, 'preload' => 'metadata', 'onclick' => '', 'lb' => false, 'class' => '', 'style' => $imgstyle, 'use_thumb' => true));
            } else {
                $result .= '<img' . ' id="i-' . $xid . '-' . wppa('mocc') . '"' . ' src="' . $imgurl . '"' . ' ' . $imgalt . ($title ? ' title="' . $title . '"' : '') . ' style="' . $imgstyle . $cursor . '"' . ' ' . $events . ' />';
            }
            $result .= '</a>';
        } else {
            // The div img
            $result .= '<div onclick="' . $link['url'] . '" class="thumb-img" id="x-' . $id . '-' . wppa('mocc') . '" style="height:100%;" >';
            // Video?
            if ($is_video) {
                $result .= wppa_get_video_html(array('id' => $id, 'controls' => wppa_switch('thumb_video'), 'margin_top' => '0', 'margin_bottom' => '0', 'tagid' => 'i-' . $xid . '-' . wppa('mocc'), 'cursor' => 'cursor:pointer;', 'events' => $events, 'title' => $title, 'preload' => 'metadata', 'onclick' => '', 'lb' => false, 'class' => '', 'style' => $imgstyle, 'use_thumb' => true));
            } else {
                $result .= '<img' . ' id="i-' . $xid . '-' . wppa('mocc') . '"' . ' src="' . $imgurl . '"' . ' ' . $imgalt . ($title ? ' title="' . $title . '"' : '') . ' style="' . $imgstyle . 'cursor:pointer;"' . ' ' . $events . ' />';
            }
            $result .= '</div>';
            $result .= '<script type="text/javascript">';
            $result .= '/* <![CDATA[ */';
            $result .= 'wppaPopupOnclick[' . $id . '] = "' . $link['url'] . '";';
            $result .= '/* ]]> */';
            $result .= '</script>';
        }
    } else {
        // no link
        if (wppa_switch('use_thumb_popup')) {
            $result .= '<div id="x-' . $id . '-' . wppa('mocc') . '" style="height:100%" >';
            if ($is_video) {
                $result .= wppa_get_video_html(array('id' => $id, 'controls' => false, 'margin_top' => '0', 'margin_bottom' => '0', 'tagid' => 'i-' . $xid . '-' . wppa('mocc'), 'cursor' => '', 'events' => $events, 'title' => $title, 'preload' => 'metadata', 'onclick' => '', 'lb' => false, 'class' => '', 'style' => $imgstyle, 'use_thumb' => true));
            } else {
                $result .= '<img' . ' id="i-' . $xid . '-' . wppa('mocc') . '"' . ' src="' . $imgurl . '"' . ' ' . $imgalt . ($title ? ' title="' . $title . '"' : '') . ' width="' . $imgwidth . '"' . ' height="' . $imgheight . '"' . ' style="' . $imgstyle . '"' . ' ' . $events . ' />';
            }
            $result .= '</div>';
        } else {
            if ($is_video) {
                //				$result .= '<video preload="metadata" '.$imgalt.' title="'.$title.'" width="'.$imgwidth.'" height="'.$imgheight.'" style="'.$imgstyle.'" '.$events.' >'.wppa_get_video_body( $id ).'</video>';
                $result .= wppa_get_video_html(array('id' => $id, 'controls' => false, 'margin_top' => '0', 'margin_bottom' => '0', 'tagid' => 'i-' . $xid . '-' . wppa('mocc'), 'cursor' => '', 'events' => $events, 'title' => $title, 'preload' => 'metadata', 'onclick' => '', 'lb' => false, 'class' => '', 'style' => $imgstyle, 'use_thumb' => true));
            } else {
                $result .= '<img' . ' id="i-' . $xid . '-' . wppa('mocc') . '"' . ' src="' . $imgurl . '"' . ' ' . $imgalt . ($title ? ' title="' . $title . '"' : '') . ' width="' . $imgwidth . '"' . ' height="' . $imgheight . '"' . ' style="' . $imgstyle . '" ' . $events . ' />';
            }
        }
    }
    // The audio when no popup
    if (wppa_switch('thumb_audio') && wppa_has_audio($id)) {
        $result .= '<div style="position:relative;z-index:11;">';
        //	$is_safari 	= strpos( $_SERVER["HTTP_USER_AGENT"], 'Safari' );
        //	$cont_h 	= $is_safari ? 16 : 28;
        //	$audiotop 	= $imgattr_a['height'] + $imgattr_a['margin-top'] - $cont_h;
        //			if ( ! is_file( $imgsrc ) ) { // Audio without image
        //				$audiotop 	= wppa_get_audio_control_height();
        //				$imgwidth 	= wppa_opt( 'tf_width' );
        //				$imgheight 	= wppa_get_audio_control_height();
        //			}
        $result .= wppa_get_audio_html(array('id' => $id, 'tagid' => 'a-' . $xid . '-' . wppa('mocc'), 'style' => 'width:100%;position:absolute;bottom:0;margin:0;padding:' . wppa_opt('tn_margin') / 2 . 'px;left:0;border:none;z-index:10;'));
        $result .= '</div>';
    }
    // The medals
    $result .= wppa_get_medal_html_a(array('id' => $id, 'size' => 'M', 'where' => 'bot'));
    // Close the thumbframe
    $result .= '</div>';
    return $result;
}
function wppa_bcitem($value = '', $href = '', $title = '', $class = '', $ajax = '', $hash = '#wppa-container-')
{
    static $sep;
    // Encrypt
    if ($href) {
        $href = wppa_encrypt_url($href);
    }
    if ($ajax) {
        $ajax = wppa_encrypt_url($ajax);
    }
    // Convert url to pretty
    if ($href) {
        $href = wppa_convert_to_pretty($href) . $hash . wppa('mocc');
    }
    // Has content?
    if (!$value) {
        return;
    }
    // No content
    // If in ajax modal dialog, distinguish from original bc for update during slideshow
    $bc_pname = 'bc-pname-';
    if (wppa('ajax') && wppa_switch('ajax_render_modal')) {
        $bc_pname = 'bc-pname-modal-';
    }
    if ($href) {
        wppa_out('<a' . ($ajax && wppa_switch('allow_ajax') ? ' onclick="wppaDoAjaxRender(' . wppa('mocc') . ', \'' . $ajax . '\', \'' . $href . '\' );"' : ' href="' . $href . '"') . ' class="wppa-nav-text ' . $class . '"' . ' style="cursor:pointer;' . __wcs('wppa-nav-text') . '"' . ' title="' . esc_attr($title) . '" >' . $value . '</a>');
    } else {
        // No link, its the last item
        wppa_out('<span' . ' id="' . $bc_pname . wppa('mocc') . '"' . ' class="wppa-nav-text ' . $class . '"' . ' style="' . ($title ? 'cursor:pointer;' : '') . __wcs('wppa-nav-text') . '"' . ' title="' . esc_attr($title) . '"' . ' >' . $value . '</span>');
        return;
    }
    // Add seperator
    if (!$sep) {
        // Compute the seperator
        $temp = wppa_opt('bc_separator');
        switch ($temp) {
            case 'url':
                $size = wppa_opt('fontsize_nav');
                if ($size == '') {
                    $size = '12';
                }
                $style = 'height:' . $size . 'px;';
                $sep = ' ' . '<img' . ' src="' . wppa_opt('bc_url') . '"' . ' class="no-shadow"' . ' style="' . $style . '"' . ' />' . ' ';
                break;
            case 'txt':
                $sep = ' ' . html_entity_decode(stripslashes(wppa_opt('bc_txt')), ENT_QUOTES) . ' ';
                break;
            default:
                $sep = ' &' . $temp . '; ';
        }
    }
    wppa_out('<span' . ' class="wppa-nav-text ' . $class . '"' . ' style="' . __wcs('wppa-nav-text') . '"' . ' >' . $sep . '</span>');
}
    /** @see WP_Widget::widget */
    function widget($args, $instance)
    {
        global $wpdb;
        require_once dirname(__FILE__) . '/wppa-links.php';
        require_once dirname(__FILE__) . '/wppa-styles.php';
        require_once dirname(__FILE__) . '/wppa-functions.php';
        require_once dirname(__FILE__) . '/wppa-thumbnails.php';
        require_once dirname(__FILE__) . '/wppa-boxes-html.php';
        require_once dirname(__FILE__) . '/wppa-slideshow.php';
        wppa_initialize_runtime();
        wppa('in_widget', 'upldr');
        wppa_bump_mocc();
        extract($args);
        $instance = wp_parse_args((array) $instance, array('title' => '', 'sortby' => 'name', 'ignore' => 'admin', 'parent' => ''));
        $widget_title = apply_filters('widget_title', $instance['title']);
        $page = in_array('album', wppa('links_no_page')) ? '' : wppa_get_the_landing_page('upldr_widget_linkpage', __('User uploaded photos', 'wp-photo-album-plus'));
        $ignorelist = explode(',', $instance['ignore']);
        $upldrcache = wppa_get_upldr_cache();
        $needupdate = false;
        $users = wppa_get_users();
        $workarr = array();
        $showownercount = $instance['showownercount'] == 'on';
        $showphotocount = $instance['showphotocount'] == 'on';
        $total_ownercount = 0;
        $total_photocount = 0;
        $selalbs = str_replace('.', ',', wppa_expand_enum(wppa_alb_to_enum_children(wppa_expand_enum($instance['parent']))));
        // Make the data we need
        if ($users) {
            foreach ($users as $user) {
                if (!in_array($user['user_login'], $ignorelist)) {
                    $me = wppa_get_user();
                    if ($user['user_login'] != $me && isset($upldrcache[$this->get_widget_id()][$user['user_login']]['c'])) {
                        $photo_count = $upldrcache[$this->get_widget_id()][$user['user_login']]['c'];
                    } else {
                        if ($instance['parent']) {
                            $query = $wpdb->prepare("SELECT COUNT(*) FROM `" . WPPA_PHOTOS . "` WHERE `owner` = %s AND `album` IN (" . $selalbs . ") AND ( ( `status` <> 'pending' AND `status` <> 'scheduled' ) OR `owner` = %s )", $user['user_login'], $me);
                            //);
                        } else {
                            $query = $wpdb->prepare("SELECT COUNT(*) FROM `" . WPPA_PHOTOS . "` WHERE `owner` = %s AND ( ( `status` <> 'pending' AND `status` <> 'scheduled' ) OR `owner` = %s )", $user['user_login'], $me);
                            //);
                        }
                        $photo_count = $wpdb->get_var($query);
                        if ($user['user_login'] != $me) {
                            $upldrcache[$this->get_widget_id()][$user['user_login']]['c'] = $photo_count;
                            $needupdate = true;
                        }
                    }
                    if ($photo_count) {
                        if ($user['user_login'] != $me && isset($upldrcache[$this->get_widget_id()][$user['user_login']]['d'])) {
                            $last_dtm = $upldrcache[$this->get_widget_id()][$user['user_login']]['d'];
                        } else {
                            if ($instance['parent']) {
                                $last_dtm = $wpdb->get_var($wpdb->prepare("SELECT `timestamp` FROM `" . WPPA_PHOTOS . "` WHERE `owner` = %s AND `album` IN (" . $selalbs . ") AND ( ( `status` <> 'pending' AND `status` <> 'scheduled' ) OR `owner` = %s ) ORDER BY `timestamp` DESC LIMIT 1", $user['user_login'], $me));
                            } else {
                                $last_dtm = $wpdb->get_var($wpdb->prepare("SELECT `timestamp` FROM `" . WPPA_PHOTOS . "` WHERE `owner` = %s AND ( ( `status` <> 'pending' AND `status` <> 'scheduled' ) OR `owner` = %s ) ORDER BY `timestamp` DESC LIMIT 1", $user['user_login'], $me));
                            }
                        }
                        if ($user['user_login'] != $me) {
                            $upldrcache[$this->get_widget_id()][$user['user_login']]['d'] = $last_dtm;
                            $needupdate = true;
                        }
                        $workarr[] = array('login' => $user['user_login'], 'name' => $user['display_name'], 'count' => $photo_count, 'date' => $last_dtm);
                        $total_photocount += $photo_count;
                        $total_ownercount++;
                    }
                }
            }
        } else {
            $widget_content = __('There are too many registered users in the system for this widget', 'wp-photo-album-plus');
            echo "\n" . $before_widget;
            if (!empty($widget_title)) {
                echo $before_title . $widget_title . $after_title;
            }
            echo $widget_content . $after_widget;
            return;
        }
        if ($needupdate) {
            update_option('wppa_upldr_cache', $upldrcache);
        }
        // Bring me to top
        $myline = false;
        if (is_user_logged_in()) {
            $me = wppa_get_user();
            foreach (array_keys($workarr) as $key) {
                $user = $workarr[$key];
                if ($user['login'] == $me) {
                    $myline = $workarr[$key];
                    unset($workarr[$key]);
                }
            }
        }
        // Sort workarray
        $ord = $instance['sortby'] == 'name' ? SORT_ASC : SORT_DESC;
        $workarr = wppa_array_sort($workarr, $instance['sortby'], $ord);
        // Create widget content
        $widget_content = "\n" . '<!-- WPPA+ Upldr Widget start -->';
        $widget_content .= '<div class="wppa-upldr" >';
        if ($showownercount) {
            $widget_content .= sprintf(_n('Number of contributors: %d', 'Number of contributors: %d', $total_ownercount, 'wp-photo-album-plus'), $total_ownercount) . '<br />';
        }
        if ($showphotocount) {
            $widget_content .= sprintf(_n('Number of photos: %d', 'Number of photos: %d', $total_photocount, 'wp-photo-album-plus'), $total_photocount) . '<br /><br />';
        }
        $widget_content .= '<table><tbody>';
        $albs = $instance['parent'] ? wppa_expand_enum(wppa_alb_to_enum_children(wppa_expand_enum($instance['parent']))) : '';
        $a = $albs ? wppa_trim_wppa_('&amp;wppa-album=' . $albs) : '';
        if ($myline) {
            $user = $myline;
            $widget_content .= '<tr class="wppa-user" >
									<td style="padding: 0 3px;" ><a href="' . wppa_encrypt_url(wppa_get_upldr_link($user['login']) . $a) . '" title="' . __('Photos uploaded by', 'wp-photo-album-plus') . ' ' . $user['name'] . '" ><b>' . $user['name'] . '</b></a></td>
									<td style="padding: 0 3px;" ><b>' . $user['count'] . '</b></td>
									<td style="padding: 0 3px;" ><b>' . wppa_get_time_since($user['date']) . '</b></td>
								</tr>';
        }
        foreach ($workarr as $user) {
            $widget_content .= '<tr class="wppa-user" >
									<td style="padding: 0 3px;" ><a href="' . wppa_encrypt_url(wppa_get_upldr_link($user['login']) . $a) . '" title="' . __('Photos uploaded by', 'wp-photo-album-plus') . ' ' . $user['name'] . '" >' . $user['name'] . '</a></td>
									<td style="padding: 0 3px;" >' . $user['count'] . '</td>
									<td style="padding: 0 3px;" >' . wppa_get_time_since($user['date']) . '</td>
								</tr>';
        }
        $widget_content .= '</tbody></table></div>';
        $widget_content .= '<div style="clear:both"></div>';
        $widget_content .= "\n" . '<!-- WPPA+ Upldr Widget end -->';
        // Output
        echo "\n" . $before_widget;
        if (!empty($widget_title)) {
            echo $before_title . $widget_title . $after_title;
        }
        echo $widget_content . $after_widget;
        wppa('in_widget', false);
    }
function wppa_get_ss_to_tn_link($page = '0', $id = '0')
{
    global $thumbs;
    global $thumbs_ids;
    // Search ?
    if (wppa('src') && wppa('mocc') == '1' && !wppa('is_related')) {
        $thumbhref = wppa_get_permalink($page) . 'wppa-cover=0&amp;wppa-occur=' . wppa('occur') . '&amp;wppa-searchstring=' . stripslashes(wppa('searchstring'));
    } elseif (wppa('is_upldr')) {
        if (wppa('start_album')) {
            $thumbhref = wppa_get_permalink($page) . 'wppa-cover=0&amp;wppa-occur=' . wppa('occur') . '&amp;wppa-upldr=' . wppa('is_upldr') . '&amp;wppa-album=' . wppa('start_album');
        } else {
            $thumbhref = wppa_get_permalink($page) . 'wppa-cover=0&amp;wppa-occur=' . wppa('occur') . '&amp;wppa-upldr=' . wppa('is_upldr');
        }
    } elseif (wppa('is_topten')) {
        $thumbhref = wppa_get_permalink($page) . 'wppa-cover=0&amp;wppa-occur=' . wppa('occur') . '&amp;wppa-topten=' . wppa('topten_count') . '&amp;wppa-album=' . wppa('start_album');
    } elseif (wppa('is_lasten')) {
        $thumbhref = wppa_get_permalink($page) . 'wppa-cover=0&amp;wppa-occur=' . wppa('occur') . '&amp;wppa-lasten=' . wppa('lasten_count') . '&amp;wppa-album=' . wppa('start_album');
    } elseif (wppa('is_comten')) {
        $thumbhref = wppa_get_permalink($page) . 'wppa-cover=0&amp;wppa-occur=' . wppa('occur') . '&amp;wppa-comten=' . wppa('comten_count') . '&amp;wppa-album=' . wppa('start_album');
    } elseif (wppa('is_featen')) {
        $thumbhref = wppa_get_permalink($page) . 'wppa-cover=0&amp;wppa-occur=' . wppa('occur') . '&amp;wppa-featen=' . wppa('featen_count') . '&amp;wppa-album=' . wppa('start_album');
    } elseif (wppa('is_tag')) {
        $thumbhref = wppa_get_permalink($page) . 'wppa-cover=0&amp;wppa-occur=' . wppa('occur') . '&amp;wppa-tag=' . wppa('is_tag') . '&amp;wppa-album=' . wppa('start_album');
    } elseif (wppa('is_cat')) {
        $thumbhref = wppa_get_permalink($page) . 'wppa-cover=0&amp;wppa-occur=' . wppa('occur') . '&amp;wppa-cat=' . wppa('is_cat') . '&amp;wppa-album=' . wppa('start_album');
    } else {
        $thumbhref = wppa_get_permalink($page) . 'wppa-cover=0&amp;wppa-occur=' . wppa('occur') . '&amp;wppa-album=' . wppa('start_album');
    }
    // $id is the id. See to what page we have to go
    $page = '1';
    $p = wppa_opt('thumb_page_size');
    if ($p) {
        $i = '0';
        foreach ($thumbs_ids as $ti) {
            // $thumbs_ids is setup in function wppa_prepare_slideshow_pagination()
            if ($id == $ti) {
                $page = floor($i / $p) + '1';
            }
            $i++;
        }
        if ($page > '1') {
            $thumbhref .= '&amp;wppa-page=' . $page;
        }
    }
    // Make sure the clicked photos thumb is highligted
    $thumbhref .= '&amp;wppa-hilite=' . $id;
    $thumbhref = wppa_convert_to_pretty(wppa_trim_wppa_(wppa_encrypt_url($thumbhref)));
    return $thumbhref;
}
function wppa_get_calendar_html()
{
    global $wpdb;
    // Init
    $result = '';
    $secinday = 24 * 60 * 60;
    $calendar_type = wppa('calendar');
    $autoall = wppa('calendarall');
    $albums = wppa('start_album') ? wppa_expand_enum(wppa_alb_to_enum_children(wppa('start_album'))) : '';
    $alb_clause = $albums ? ' AND `album` IN ( ' . str_replace('.', ',', $albums) . ' ) ' : '';
    $alb_arg = wppa('start_album') ? 'wppa-album=' . wppa_alb_to_enum_children(wppa('start_album')) . '&' : '';
    $reverse = wppa('reverse') ? ' DESC ' : '';
    $from = 0;
    $to = 0;
    // Get todays daynumber and range
    $today = floor(time() / $secinday);
    switch ($calendar_type) {
        case 'exifdtm':
            $photos = $wpdb->get_results("SELECT `id`, `exifdtm` " . "FROM `" . WPPA_PHOTOS . "` " . "WHERE `exifdtm` <> '' " . "AND `status` <> 'pending' " . "AND `status` <> 'scheduled' " . $alb_clause . "ORDER BY `exifdtm`" . $reverse, ARRAY_A);
            $dates = array();
            foreach ($photos as $photo) {
                $date = substr($photo['exifdtm'], 0, 10);
                if (wppa_is_exif_date($date)) {
                    if (isset($dates[$date])) {
                        $dates[$date]++;
                    } else {
                        $dates[$date] = '1';
                    }
                }
            }
            $from = 0;
            $to = count($dates);
            break;
        case 'timestamp':
        case 'modified':
            $photos = $wpdb->get_results("SELECT `id`, `" . $calendar_type . "` " . "FROM `" . WPPA_PHOTOS . "` " . "WHERE `" . $calendar_type . "` > 0 " . "AND `status` <> 'pending' " . "AND `status` <> 'scheduled' " . $alb_clause . "ORDER BY `" . $calendar_type . "`" . $reverse, ARRAY_A);
            $dates = array();
            foreach ($photos as $photo) {
                $date = floor($photo[$calendar_type] / $secinday);
                if (isset($dates[$date])) {
                    $dates[$date]++;
                } else {
                    $dates[$date] = '1';
                }
            }
            $from = 0;
            $to = count($dates);
            break;
        default:
            wppa_log('err', 'Unexpected calender type: ' . $calendar_type . ' found in wppa_get_calendar_html()', true);
    }
    // Display minicovers
    $result .= '<div' . ' style="' . 'width:' . 33 * ($to - $from) . 'px;' . 'position:relative;' . '"' . ' >';
    $result .= '<script type="text/javascript" >' . 'wppaWaitForCounter = 0;' . '</script>';
    switch ($calendar_type) {
        case 'exifdtm':
            $keys = array_keys($dates);
            for ($day = $from; $day < $to; $day++) {
                $date = date_create_from_format('Y:m:d', $keys[$day]);
                if (is_object($date)) {
                    $ajaxurl = wppa_encrypt_url(wppa_get_ajaxlink('', '1') . 'wppa-calendar=exifdtm&' . 'wppa-caldate=' . $keys[$day] . '&' . $alb_arg . 'wppa-occur=1');
                    if ($autoall) {
                        $onclick = '';
                    } else {
                        $onclick = 'jQuery( \'.wppa-minicover-' . wppa('mocc') . '\' ).removeClass( \'wppa-minicover-current\' );' . 'jQuery( this ).addClass( \'wppa-minicover-current\' );' . 'wppaDoAjaxRender( ' . (wppa('mocc') + '1') . ', \'' . $ajaxurl . '\', \'\' );';
                    }
                    $result .= '<a' . ($autoall ? ' href="#wppa-' . $day . '"' : '') . ' class="wppa-minicover-' . wppa('mocc') . '"' . ' onclick="' . $onclick . '"' . ' >' . '<div' . ' id="wppa-minicover-' . $day . '"' . ' class="wppa-minicover"' . ' style="' . 'border:1px solid gray;' . 'margin-right:1px;' . 'float:left;' . 'text-align:center;' . 'width:30px;"' . ' >' . $date->format('M') . '<br />' . $date->format('d') . '<br />' . $date->format('D') . '<br />' . $date->format('Y') . '<br />' . '(' . $dates[$keys[$day]] . ')' . '</div>' . '</a>';
                    if ($autoall) {
                        $addlabel = '<a id=\\"wppa-' . $day . '\\" ></a>';
                        $result .= '<script type="text/javascript" >' . 'wppaDoAjaxRender( ' . (wppa('mocc') + '1') . ', \'' . str_replace('&amp;', '&', $ajaxurl) . '\', \'\', \'' . $addlabel . '\', ' . ($day + '1') . ' );' . '</script>';
                    }
                }
            }
            break;
        case 'timestamp':
        case 'modified':
            $keys = array_keys($dates);
            for ($day = $from; $day < $to; $day++) {
                $date = $keys[$day];
                $ajaxurl = wppa_encrypt_url(wppa_get_ajaxlink('', '1') . 'wppa-calendar=' . $calendar_type . '&' . 'wppa-caldate=' . $keys[$day] . '&' . $alb_arg . 'wppa-occur=1');
                if ($autoall) {
                    $onclick = '';
                } else {
                    $onclick = 'jQuery( \'.wppa-minicover-' . wppa('mocc') . '\' ).removeClass( \'wppa-minicover-current\' );' . 'jQuery( this ).addClass( \'wppa-minicover-current\' );' . 'wppaDoAjaxRender( ' . (wppa('mocc') + '1') . ', \'' . $ajaxurl . '\', \'\' );';
                }
                $result .= '<a' . ' class="wppa-minicover-' . wppa('mocc') . '"' . ' onclick="' . $onclick . '"' . ' >' . '<div' . ' id="wppa-minicover-' . $day . '"' . ' class="wppa-minicover"' . ' style="' . 'border:1px solid gray;' . 'margin-right:1px;' . 'float:left;' . 'text-align:center;' . 'width:30px;"' . ' >' . date('M', $date * $secinday) . '<br />' . date('d', $date * $secinday) . '<br />' . date('D', $date * $secinday) . '<br />' . date('Y', $date * $secinday) . '<br />' . '(' . $dates[$keys[$day]] . ')' . '</div>' . '</a>';
                if ($autoall) {
                    $addlabel = '<a id=\\"wppa-' . $day . '\\" ></a>';
                    $result .= '<script type="text/javascript" >' . 'wppaDoAjaxRender( ' . (wppa('mocc') + '1') . ', \'' . str_replace('&amp;', '&', $ajaxurl) . '\', \'\', \'' . $addlabel . '\', ' . ($day + '1') . ' );' . '</script>';
                }
            }
            break;
    }
    $result .= '<script type="text/javascript" >' . 'jQuery(document).ready(function(){ wppaWaitForCounter = 1; });' . '</script>';
    $result .= '</div>';
    return $result;
}
function wppa_subalbumlinks_html($id, $top = true)
{
    global $wpdb;
    // Do they need us? Anything to display?
    if (wppa_opt('cover_sublinks_display') == 'none') {
        return;
    }
    // Display type
    $display_type = wppa_opt('cover_sublinks_display');
    // Link type
    $link_type = wppa_opt('cover_sublinks');
    // Init
    $is_list = $display_type == 'list' || $display_type == 'recursivelist';
    $is_recursive = $display_type == 'recursivelist';
    $first = true;
    // Get the children
    $subs = $wpdb->get_results("SELECT * FROM `" . WPPA_ALBUMS . "` WHERE `a_parent` = " . $id . " " . wppa_get_album_order($id), ARRAY_A);
    // Only if there are sub-albums
    if (!empty($subs)) {
        wppa_out('<div class="wppa-cover-sublist-container" >');
        // Start list if required
        if ($is_list) {
            wppa_out('<ul' . ' class="wppa-cover-sublink-list"' . ' style="' . 'clear:both;' . 'margin:0;' . 'list-style-type:disc;' . 'list-style-position:inside;' . 'padding:0 0 0 24px;' . '"' . ' >');
        } else {
            wppa_out('<div style="clear:both;" ></div>');
        }
        // Process the sub-albums
        foreach ($subs as $album) {
            // What is the albums title linktype
            $linktype = $album['cover_linktype'];
            if (!$linktype) {
                $linktype = 'content';
            }
            // Default
            // What is the albums title linkpage
            $linkpage = $album['cover_linkpage'];
            if ($linkpage == '-1') {
                $linktype = 'none';
            }
            // for backward compatibility
            // Find the content 'View' link
            $albumid = $album['id'];
            $photocount = wppa_get_photo_count($albumid, 'use_treecounts');
            // Thumbnails and covers, show sub-album covers
            // in case slideshow is requested on an empty album
            if (wppa_opt('cover_sublinks') == 'content' || !$photocount) {
                if (wppa_switch('allow_ajax') && !$linkpage) {
                    $href_content = '';
                    $onclick_content = "wppaDoAjaxRender( " . wppa('mocc') . ", '" . wppa_encrypt_url(wppa_get_album_url_ajax($albumid, $linkpage)) . "', '" . wppa_convert_to_pretty(wppa_encrypt_url($href_content)) . "' )";
                } else {
                    $href_content = wppa_convert_to_pretty(wppa_encrypt_url(wppa_get_album_url($albumid, $linkpage)));
                    $onclick_content = '';
                }
            } else {
                if (wppa_switch('allow_ajax') && !$linkpage) {
                    $href_content = '';
                    $onclick_content = "wppaDoAjaxRender( " . wppa('mocc') . ", '" . wppa_encrypt_url(wppa_get_slideshow_url_ajax($albumid, $linkpage)) . "', '" . wppa_convert_to_pretty($href_content) . "' )";
                } else {
                    $href_content = wppa_convert_to_pretty(wppa_encrypt_url(wppa_get_slideshow_url($albumid, $linkpage)));
                    $onclick_content = '';
                }
            }
            // Do the output
            $title = esc_attr(__('View the album', 'wp-photo-album-plus') . ': ' . wppa_get_album_name($album['id']));
            switch ($display_type) {
                case 'list':
                case 'recursivelist':
                    if ($link_type == 'none') {
                        wppa_out('<li style="margin:0;cursor:pointer;" >' . wppa_get_album_name($album['id']) . '</li>');
                    } else {
                        wppa_out('<li style="margin:0;cursor:pointer;" >' . '<a' . ($href_content ? ' href="' . $href_content . '"' : '') . ($onclick_content ? ' onclick="' . $onclick_content . '"' : '') . ' title="' . $title . '"' . ' >' . wppa_get_album_name($album['id']) . '</a>' . '</li>');
                    }
                    break;
                case 'enum':
                    if (!$first) {
                        wppa_out(', ');
                    }
                    if ($link_type == 'none') {
                        wppa_out(wppa_get_album_name($album['id']));
                    } else {
                        wppa_out('<a' . ($href_content ? ' href="' . $href_content . '"' : '') . ($onclick_content ? ' onclick="' . $onclick_content . '"' : '') . ' title="' . $title . '"' . ' >' . wppa_get_album_name($album['id']) . '</a>');
                    }
                    $first = false;
                    break;
                case 'microthumbs':
                    $coverphoto_id = wppa_get_coverphoto_id($album['id']);
                    $src = wppa_fix_poster_ext(wppa_get_thumb_url($coverphoto_id), $coverphoto_id);
                    if ($link_type == 'none') {
                        wppa_out('<img' . ' class="wppa-cover-sublink-img"' . ' src="' . $src . '"' . ' alt="' . wppa_get_album_name($album['id']) . '"' . ' style="' . 'max-width:100px;' . 'max-height:50px;' . 'padding:1px;' . 'margin:1px;' . 'background-color:' . wppa_opt('bgcolor_img') . ';' . 'float:left;' . '"' . ' />');
                    } else {
                        wppa_out('<a' . ($href_content ? ' href="' . $href_content . '"' : '') . ($onclick_content ? ' onclick="' . $onclick_content . '"' : '') . ' title="' . $title . '"' . ' >' . '<img' . ' class="wppa-cover-sublink-img"' . ' src="' . $src . '"' . ' alt="' . wppa_get_album_name($album['id']) . '"' . ' style="' . 'max-width:100px;' . 'max-height:50px;' . 'padding:1px;' . 'margin:1px;' . 'background-color:' . wppa_opt('bgcolor_img') . ';' . 'float:left;' . '"' . ' />' . '</a>');
                    }
                    break;
            }
            // Go deeper for grandchildren
            if ($is_recursive) {
                wppa_subalbumlinks_html($album['id'], false);
            }
        }
        // End list
        if ($is_list) {
            wppa_out('</ul>');
        }
        wppa_out('</div>');
    }
}