function wppa_get_photo_url($id, $system = 'flat', $x = '0', $y = '0')
{
    global $blog_id;
    global $wppa_supported_stereo_types;
    // Does photo exist?
    $thumb = wppa_cache_thumb($id);
    if (!$thumb) {
        return '';
    }
    // Set owner if required
    wppa_set_owner_to_name($id);
    // Must re-get cached thumb
    $thumb = wppa_cache_thumb($id);
    if (is_feed() && wppa_switch('feed_use_thumb')) {
        return wppa_get_thumb_url($id, $system);
    }
    // If in the cloud...
    $for_sm = wppa('for_sm');
    // Social media do not accept cloud images
    $is_old = wppa_too_old_for_cloud($id);
    if (wppa_cdn('front') && !wppa_is_multi($id) && !$is_old && !wppa_is_stereo($id) && !$for_sm) {
        switch (wppa_cdn('front')) {
            case 'cloudinary':
                $x = round($x);
                $y = round($y);
                $prefix = is_multisite() && !WPPA_MULTISITE_GLOBAL ? $blog_id . '-' : '';
                $t = wppa_switch('enlarge') ? 'fit' : 'limit';
                $q = wppa_opt('jpeg_quality');
                $sizespec = $x && $y ? 'w_' . $x . ',h_' . $y . ',c_' . $t . ',q_' . $q . '/' : '';
                $s = is_ssl() ? 's' : '';
                $url = 'http' . $s . '://res.cloudinary.com/' . get_option('wppa_cdn_cloud_name') . '/image/upload/' . $sizespec . $prefix . $thumb['id'] . '.' . $thumb['ext'];
                return $url;
                break;
        }
    }
    // Stereo?
    if (wppa_is_stereo($id)) {
        // Get type from cookie
        $st = isset($_COOKIE["stereotype"]) ? $_COOKIE["stereotype"] : 'color';
        if (!in_array($st, $wppa_supported_stereo_types)) {
            $st = '_flat';
        }
        // Get glass from cookie
        $sg = 'rc';
        if (isset($_COOKIE["stereoglass"]) && $_COOKIE["stereoglass"] == 'greenmagenta') {
            $sg = 'gm';
        }
        // Create the file if not present
        if (!is_file(wppa_get_stereo_path($id, $st, $sg))) {
            wppa_create_stereo_image($id, $st, $sg);
        }
        // Build the url
        if ($st == '_flat') {
            $url = WPPA_UPLOAD_URL . '/stereo/' . $id . '-' . $st . '.jpg' . '?ver=' . get_option('wppa_photo_version', '1');
        } else {
            $url = WPPA_UPLOAD_URL . '/stereo/' . $id . '-' . $st . '-' . $sg . '.jpg' . '?ver=' . get_option('wppa_photo_version', '1');
        }
        // Done
        return $url;
    }
    if (get_option('wppa_file_system') == 'flat') {
        $system = 'flat';
    }
    // Have been converted, ignore argument
    if (get_option('wppa_file_system') == 'tree') {
        $system = 'tree';
    }
    // Have been converted, ignore argument
    if (!is_numeric($id) || $id < '1') {
        wppa_dbg_msg('Invalid arg wppa_get_photo_url(' . $id . ')', 'red');
    }
    if ($system == 'tree') {
        return WPPA_UPLOAD_URL . '/' . wppa_expand_id($thumb['id']) . '.' . $thumb['ext'] . '?ver=' . get_option('wppa_photo_version', '1');
    } else {
        return WPPA_UPLOAD_URL . '/' . $thumb['id'] . '.' . $thumb['ext'] . '?ver=' . get_option('wppa_photo_version', '1');
    }
}
function wppa_get_slide_info($index, $id, $callbackid = '')
{
    global $wpdb;
    static $user;
    // Make sure $thumb contains our image data
    $thumb = wppa_cache_thumb($id);
    if (!$user) {
        $user = wppa_get_user();
    }
    $photo = wppa_get_get('photo');
    $ratingphoto = wppa_get_get('rating-id');
    if (!$callbackid) {
        $callbackid = $id;
    }
    // Process a comment if given for this photo
    $comment_request = wppa_get_post('commentbtn') && $id == $photo;
    $comment_allowed = !wppa_switch('comment_login') || is_user_logged_in();
    if (wppa_switch('show_comments') && $comment_request && $comment_allowed) {
        wppa_do_comment($id);
    }
    // Find rating
    if (wppa_switch('rating_on') && !wppa('is_slideonly') && !wppa('is_filmonly')) {
        // Find my ( avg ) rating
        $rats = $wpdb->get_results($wpdb->prepare("SELECT `value` FROM `" . WPPA_RATING . "` " . "WHERE `photo` = %s AND `user` = %s AND `status` = 'publish'", $id, $user), ARRAY_A);
        wppa_dbg_q('Q33v');
        if ($rats) {
            $n = 0;
            $accu = 0;
            foreach ($rats as $rat) {
                $accu += $rat['value'];
                $n++;
            }
            $myrat = $accu / $n;
            $i = wppa_opt('rating_prec');
            $j = $i + '1';
            $myrat = sprintf('%' . $j . '.' . $i . 'f', $myrat);
        } else {
            $myrat = '0';
        }
        // Find the avg rating
        $avgrat = wppa_get_rating_by_id($id, 'nolabel');
        if (!$avgrat) {
            $avgrat = '0';
        }
        $avgrat .= '|' . wppa_get_rating_count_by_id($id);
        // Find the dislike count
        $discount = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM `" . WPPA_RATING . "` WHERE `photo` = %s AND `value` = -1 AND `status` = %s", $id, 'publish'));
    } else {
        // Rating off
        $myrat = '0';
        $avgrat = '0';
        $discount = '0';
    }
    // Find comments
    $comment = wppa_switch('show_comments') && !wppa('is_filmonly') && !wppa('is_slideonly') ? wppa_comment_html($id, $comment_allowed) : '';
    // Get the callback url.
    if (wppa_switch('rating_on')) {
        $url = wppa_get_slide_callback_url($callbackid);
        $url = str_replace('&amp;', '&', $url);
        // js use
    } else {
        $url = '';
    }
    // Find link url, link title and link target
    if (wppa_in_widget() == 'ss') {
        $link = wppa_get_imglnk_a('sswidget', $id);
    } else {
        $link = wppa_get_imglnk_a('slideshow', $id);
    }
    $linkurl = $link['url'];
    $linktitle = $link['title'];
    $linktarget = $link['target'];
    // Find full image style and size
    if (wppa('is_filmonly')) {
        $style_a['style'] = '';
        $style_a['width'] = '';
        $style_a['height'] = '';
    } else {
        $style_a = wppa_get_fullimgstyle_a($id);
    }
    // Find image url
    if (wppa_switch('fotomoto_on') && !wppa_is_stereo($id)) {
        $photourl = wppa_get_hires_url($id);
    } elseif (wppa_use_thumb_file($id, $style_a['width'], $style_a['height']) && !wppa_is_stereo($id)) {
        $photourl = wppa_get_thumb_url($id, '', $style_a['width'], $style_a['height']);
    } else {
        $photourl = wppa_get_photo_url($id, '', $style_a['width'], $style_a['height']);
    }
    if (wppa_has_audio($id)) {
        $photourl = wppa_fix_poster_ext($photourl, $id);
    }
    // Find iptc data
    $iptc = wppa_switch('show_iptc') && !wppa('is_slideonly') && !wppa('is_filmonly') ? wppa_iptc_html($id) : '';
    // Find EXIF data
    $exif = wppa_switch('show_exif') && !wppa('is_slideonly') && !wppa('is_filmonly') ? wppa_exif_html($id) : '';
    // Lightbox subtitle
    $doit = false;
    if (wppa_opt('slideshow_linktype') == 'lightbox' || wppa_opt('slideshow_linktype') == 'lightboxsingle') {
        $doit = true;
    }
    // For fullsize
    if (wppa_switch('filmstrip') && wppa_opt('film_linktype') == 'lightbox') {
        // For filmstrip?
        if (!wppa('is_slideonly')) {
            $doit = true;
        }
        // Film below fullsize
        if (wppa('film_on')) {
            $doit = true;
        }
        // Film explicitly on ( slideonlyf )
    }
    if ($doit) {
        $lbtitle = wppa_get_lbtitle('slide', $id);
    } else {
        $lbtitle = '';
    }
    // Name
    if (wppa('is_filmonly')) {
        $name = '';
        $fullname = '';
    } else {
        $name = esc_js(wppa_get_photo_name($id));
        if (!$name) {
            $name = '&nbsp;';
        }
        $fullname = wppa_get_photo_name($id, wppa_switch('show_full_owner'), true, true, wppa_switch('wppa_show_full_name'));
        if (!$fullname) {
            $fullname = '&nbsp;';
        }
    }
    // Shareurl
    if (wppa('is_filmonly') || wppa('is_slideonly')) {
        $shareurl = '';
    } else {
        $shareurl = wppa_get_image_page_url_by_id($id, false, wppa('start_album'));
        $shareurl = wppa_convert_to_pretty($shareurl);
        $shareurl = str_replace('&amp;', '&', $shareurl);
    }
    // Make photo desc, filtered
    $desc = '';
    if ((!wppa('is_slideonly') || wppa('desc_on')) && !wppa('is_filmonly')) {
        $desc .= wppa_get_photo_desc($id, wppa_switch('allow_foreign_shortcodes'), 'do_geo');
        // Foreign shortcodes is handled here
        // Run wpautop on description?
        if (wppa_switch('run_wpautop_on_desc')) {
            $desc = wpautop($desc);
        }
        // And format
        $desc = wppa_html(esc_js(stripslashes($desc)));
        // Remove extra space created by other filters like wpautop
        if (wppa_switch('allow_foreign_shortcodes') && wppa_switch('clean_pbr')) {
            $desc = str_replace(array("<p>", "</p>", "<br>", "<br/>", "<br />"), " ", $desc);
        }
        if (!$desc) {
            $desc = '&nbsp;';
        }
    }
    // Edit photo link
    $editlink = '';
    $dellink = '';
    if (!wppa('is_filmonly') && !wppa('is_slideonly')) {
        if (!wppa_is_user_blacklisted()) {
            if (wppa_user_is('administrator') || current_user_can('wppa_moderate') || wppa_get_user() == wppa_get_photo_owner($id) && wppa_switch('upload_edit')) {
                $editlink = '
					<div style="float:right; margin-right:6px;" >' . '<a' . ' style="color:green;"' . ' onclick="_wppaStop( ' . wppa('mocc') . ' );wppaEditPhoto( ' . wppa('mocc') . ', ' . $thumb['id'] . ' ); return false;"' . ' >' . __('Edit', 'wp-photo-album-plus') . '</a>' . '</div>';
                $dellink = '
					<div style="float:right; margin-right:6px;" >' . '<a' . ' style="color:red;"' . ' onclick="' . '_wppaStop( ' . wppa('mocc') . ' );' . esc_attr('if ( confirm( "' . __('Are you sure you want to remove this photo?', 'wp-photo-album-plus') . '" ) ) ' . 'wppaAjaxRemovePhoto( ' . wppa('mocc') . ', ' . $thumb['id'] . ', true ); return false;') . '"' . '>' . __('Delete', 'wp-photo-album-plus') . '</a>' . '</div>';
            }
        }
    }
    if ($editlink || $dellink) {
        $desc = $editlink . $dellink . '<div style="clear:both"></div>' . $desc;
    }
    if (in_array($thumb['status'], array('pending', 'scheduled'))) {
        $desc .= wppa_html(esc_js(wppa_moderate_links('slide', $id)));
    }
    // Share HTML
    $sharehtml = wppa('is_filmonly') || wppa('is_slideonly') ? '' : wppa_get_share_html($id);
    // Og Description
    $ogdsc = wppa_switch('facebook_comments') && !wppa_in_widget() ? strip_shortcodes(wppa_strip_tags(wppa_html(wppa_get_photo_desc($id)), 'all')) : '';
    $ogdsc = esc_js($ogdsc);
    // Hires url
    $hiresurl = esc_js(wppa_fix_poster_ext(wppa_get_hires_url($id), $id));
    // Video html
    $videohtml = wppa_get_video_body($id);
    // Audio html
    $audiohtml = wppa_get_audio_body($id);
    // Produce final result
    $result = "'" . wppa('mocc') . "','";
    $result .= $index . "','";
    $result .= $photourl . "','";
    $result .= $style_a['style'] . "','";
    $result .= ($videohtml ? wppa_get_videox($id) : $style_a['width']) . "','";
    $result .= ($videohtml ? wppa_get_videoy($id) : $style_a['height']) . "','";
    $result .= $fullname . "','";
    $result .= $name . "','";
    $result .= $desc . "','";
    $result .= $id . "','";
    $result .= $avgrat . "','";
    $result .= $discount . "','";
    $result .= $myrat . "','";
    $result .= $url . "','";
    $result .= $linkurl . "','" . $linktitle . "','" . $linktarget . "','";
    $result .= wppa('in_widget_timeout') . "','";
    $result .= $comment . "','";
    $result .= $iptc . "','";
    $result .= $exif . "','";
    $result .= $lbtitle . "','";
    $result .= $shareurl . "','";
    // Used for history.pushstate()
    $result .= $sharehtml . "','";
    // The content of the SM ( share ) box
    $result .= $ogdsc . "','";
    $result .= $hiresurl . "','";
    $result .= $videohtml . "','";
    $result .= $audiohtml . "'";
    // This is an ingenious line of code that is going to prevent us from very much trouble.
    // Created by OpaJaap on Jan 15 2012, 14:36 local time. Thanx.
    // Make sure there are no linebreaks in the result that would screw up Javascript.
    return str_replace(array("\r\n", "\n", "\r"), " ", $result);
    //	return $result;
}
function wppa_get_photox($id, $force = false)
{
    $result = wppa_get_thumbphotoxy($id, 'photox', $force);
    if (wppa_is_stereo($id)) {
        return floor($result / 2);
    } else {
        return $result;
    }
}
function wppa_create_stereo_image($id, $type, $glass)
{
    static $f299;
    static $f587;
    static $f114;
    // Init
    if ($glass == 'rc') {
        $glass = 'redcyan';
    }
    if (!is_array($f299)) {
        $i = 0;
        while ($i < 256) {
            $f299[$i] = floor(0.299 * $i + 0.5);
            $f587[$i] = floor(0.587 * $i + 0.5);
            $f114[$i] = floor(0.114 * $i + 0.5);
            $i++;
        }
    }
    // Feature enabled?
    if (!wppa_switch('enable_stereo')) {
        return;
    }
    // Init.
    $is_stereo = wppa_is_stereo($id);
    if (!$is_stereo) {
        return;
    }
    $stereodir = WPPA_UPLOAD_PATH . '/stereo';
    if (!is_dir($stereodir)) {
        wppa_mkdir($stereodir);
    }
    $fromfile = wppa_get_photo_path($id);
    $tofile = wppa_get_stereo_path($id, $type, $glass);
    $sizes = getimagesize($fromfile);
    $width = $sizes['0'] / 2;
    $height = $sizes['1'];
    $fromimage = wppa_imagecreatefromjpeg($fromfile);
    $toimage = imagecreatetruecolor($width, $height);
    if ($is_stereo == 1) {
        $offset1 = 0;
        $offset2 = $width;
    } else {
        $offset1 = $width;
        $offset2 = 0;
    }
    // Do the dirty work
    switch ($type) {
        case 'color':
            for ($y = 0; $y < $height; $y++) {
                for ($x = 0; $x < $width; $x++) {
                    $rgb1 = imagecolorat($fromimage, $x + $offset1, $y);
                    $r1 = $rgb1 >> 16 & 0xff;
                    $g1 = $rgb1 >> 8 & 0xff;
                    $b1 = $rgb1 & 0xff;
                    $rgb2 = imagecolorat($fromimage, $x + $offset2, $y);
                    $r2 = $rgb2 >> 16 & 0xff;
                    $g2 = $rgb2 >> 8 & 0xff;
                    $b2 = $rgb2 & 0xff;
                    // Red - Cyan glass
                    if ($glass == 'redcyan') {
                        $newpix = $r2 << 16 | $g1 << 8 | $b1;
                    } else {
                        $newpix = $r1 << 16 | $g2 << 8 | $b1;
                    }
                    imagesetpixel($toimage, $x, $y, $newpix);
                }
            }
            imagejpeg($toimage, $tofile, wppa_opt('jpeg_quality'));
            break;
        case 'gray':
            for ($y = 0; $y < $height; $y++) {
                for ($x = 0; $x < $width; $x++) {
                    $rgb1 = imagecolorat($fromimage, $x + $offset1, $y);
                    $r1 = $rgb1 >> 16 & 0xff;
                    $g1 = $rgb1 >> 8 & 0xff;
                    $b1 = $rgb1 & 0xff;
                    $rgb2 = imagecolorat($fromimage, $x + $offset2, $y);
                    $r2 = $rgb2 >> 16 & 0xff;
                    $g2 = $rgb2 >> 8 & 0xff;
                    $b2 = $rgb2 & 0xff;
                    // Red - Cyan glass
                    if ($glass == 'redcyan') {
                        $r = $f299[$r2] + $f587[$g2] + $f114[$b2];
                        $g = $f299[$r1] + $f587[$g1] + $f114[$b1];
                        $b = $f299[$r1] + $f587[$g1] + $f114[$b1];
                        $newpix = $r << 16 | $g << 8 | $b;
                    } else {
                        $r = $f299[$r1] + $f587[$g1] + $f114[$b1];
                        $g = $f299[$r2] + $f587[$g2] + $f114[$b2];
                        $b = $f299[$r1] + $f587[$g1] + $f114[$b1];
                        $newpix = $r << 16 | $g << 8 | $b;
                    }
                    imagesetpixel($toimage, $x, $y, $newpix);
                }
            }
            imagejpeg($toimage, $tofile, wppa_opt('jpeg_quality'));
            break;
        case 'true':
            for ($y = 0; $y < $height; $y++) {
                for ($x = 0; $x < $width; $x++) {
                    $rgb1 = imagecolorat($fromimage, $x + $offset1, $y);
                    $r1 = $rgb1 >> 16 & 0xff;
                    $g1 = $rgb1 >> 8 & 0xff;
                    $b1 = $rgb1 & 0xff;
                    $rgb2 = imagecolorat($fromimage, $x + $offset2, $y);
                    $r2 = $rgb2 >> 16 & 0xff;
                    $g2 = $rgb2 >> 8 & 0xff;
                    $b2 = $rgb2 & 0xff;
                    // Red - Cyan glass
                    if ($glass == 'redcyan') {
                        $r = $f299[$r1] + $f587[$g1] + $f114[$b1];
                        $g = 0;
                        $b = $f299[$r2] + $f587[$g2] + $f114[$b2];
                        $newpix = $r << 16 | $g << 8 | $b;
                    } else {
                        $r = $f299[$r2] + $f587[$g2] + $f114[$b2];
                        $g = $f299[$r1] + $f587[$g1] + $f114[$b1];
                        $b = 0;
                        $newpix = $r << 16 | $g << 8 | $b;
                    }
                    imagesetpixel($toimage, $x, $y, $newpix);
                }
            }
            imagejpeg($toimage, $tofile, wppa_opt('jpeg_quality'));
            break;
        case 'halfcolor':
            for ($y = 0; $y < $height; $y++) {
                for ($x = 0; $x < $width; $x++) {
                    $rgb1 = imagecolorat($fromimage, $x + $offset1, $y);
                    $r1 = $rgb1 >> 16 & 0xff;
                    $g1 = $rgb1 >> 8 & 0xff;
                    $b1 = $rgb1 & 0xff;
                    $rgb2 = imagecolorat($fromimage, $x + $offset2, $y);
                    $r2 = $rgb2 >> 16 & 0xff;
                    $g2 = $rgb2 >> 8 & 0xff;
                    $b2 = $rgb2 & 0xff;
                    // Red - Cyan glass
                    if ($glass == 'redcyan') {
                        $r = $f299[$r1] + $f587[$g1] + $f114[$b1];
                        //	$g = $g2;
                        //	$b = $b2;
                        $newpix = $r << 16 | $g2 << 8 | $b2;
                    } else {
                        $r = $f299[$r2] + $f587[$g2] + $f114[$b2];
                        //						$g = $g1;
                        //						$b = $b2;
                        $r = ceil(min($r, 255));
                        $newpix = $r << 16 | $g1 << 8 | $b2;
                    }
                    imagesetpixel($toimage, $x, $y, $newpix);
                }
            }
            imagejpeg($toimage, $tofile, wppa_opt('jpeg_quality'));
            break;
        case 'optimized':
            for ($y = 0; $y < $height; $y++) {
                for ($x = 0; $x < $width; $x++) {
                    $rgb1 = imagecolorat($fromimage, $x + $offset1, $y);
                    $r1 = $rgb1 >> 16 & 0xff;
                    $g1 = $rgb1 >> 8 & 0xff;
                    $b1 = $rgb1 & 0xff;
                    $rgb2 = imagecolorat($fromimage, $x + $offset2, $y);
                    $r2 = $rgb2 >> 16 & 0xff;
                    $g2 = $rgb2 >> 8 & 0xff;
                    $b2 = $rgb2 & 0xff;
                    // Red - Cyan glass
                    if ($glass == 'redcyan') {
                        $r = 0.7 * $g1 + 0.3 * $b1;
                        $g = $g2;
                        $b = $b2;
                        $r = ceil(min($r, 255));
                        $newpix = $r << 16 | $g << 8 | $b;
                    } else {
                        $r = 0.7 * $g2 + 0.3 * $b2;
                        $g = $g1;
                        $b = $b2;
                        $r = ceil(min($r, 255));
                        $newpix = $r << 16 | $g << 8 | $b;
                    }
                    imagesetpixel($toimage, $x, $y, $newpix);
                }
            }
            imagejpeg($toimage, $tofile, wppa_opt('jpeg_quality'));
            break;
        case '_flat':
            imagecopy($toimage, $fromimage, 0, 0, 0, 0, $width, $height);
            imagejpeg($toimage, $tofile, wppa_opt('jpeg_quality'));
            break;
        default:
            break;
    }
    // accessable
    wppa_chmod($tofile);
    // Optimized
    wppa_optimize_image_file($tofile);
    // Bump version
    wppa_bump_photo_rev();
}
function wppa_get_imglnk_a($wich, $id, $lnk = '', $tit = '', $onc = '', $noalb = false, $album = '')
{
    global $wpdb;
    // make sure the photo data ia available
    $thumb = wppa_cache_thumb($id);
    if (!$thumb) {
        return false;
    }
    // Is it a video?
    $is_video = wppa_is_video($id, true);
    // Photo Specific Overrule?
    if ($wich == 'sphoto' && wppa_switch('sphoto_overrule') || $wich == 'mphoto' && wppa_switch('mphoto_overrule') || $wich == 'xphoto' && wppa_switch('xphoto_overrule') || $wich == 'thumb' && wppa_switch('thumb_overrule') || $wich == 'topten' && wppa_switch('topten_overrule') || $wich == 'featen' && wppa_switch('featen_overrule') || $wich == 'lasten' && wppa_switch('lasten_overrule') || $wich == 'sswidget' && wppa_switch('sswidget_overrule') || $wich == 'potdwidget' && wppa_switch('potdwidget_overrule') || $wich == 'coverimg' && wppa_switch('coverimg_overrule') || $wich == 'comten' && wppa_switch('comment_overrule') || $wich == 'slideshow' && wppa_switch('slideshow_overrule') || $wich == 'tnwidget' && wppa_switch('thumbnail_widget_overrule')) {
        // Look for a photo specific link
        if ($thumb) {
            // If it is there...
            if ($thumb['linkurl']) {
                // Use it. It superceeds other settings
                $result['url'] = esc_attr($thumb['linkurl']);
                $result['title'] = esc_attr(__(stripslashes($thumb['linktitle'])));
                $result['is_url'] = true;
                $result['is_lightbox'] = false;
                $result['onclick'] = '';
                $result['target'] = $thumb['linktarget'];
                return $result;
            }
        }
    }
    $result['target'] = '_self';
    $result['title'] = '';
    $result['onclick'] = '';
    switch ($wich) {
        case 'sphoto':
            $type = wppa_opt('sphoto_linktype');
            $page = wppa_opt('sphoto_linkpage');
            if ($page == '0') {
                $page = '-1';
            }
            if (wppa_switch('sphoto_blank')) {
                $result['target'] = '_blank';
            }
            break;
        case 'mphoto':
            $type = wppa_opt('mphoto_linktype');
            $page = wppa_opt('mphoto_linkpage');
            if ($page == '0') {
                $page = '-1';
            }
            if (wppa_switch('mphoto_blank')) {
                $result['target'] = '_blank';
            }
            break;
        case 'xphoto':
            $type = wppa_opt('xphoto_linktype');
            $page = wppa_opt('xphoto_linkpage');
            if ($page == '0') {
                $page = '-1';
            }
            if (wppa_switch('xphoto_blank')) {
                $result['target'] = '_blank';
            }
            break;
        case 'thumb':
            $type = wppa_opt('thumb_linktype');
            $page = wppa_opt('thumb_linkpage');
            if (wppa_switch('thumb_blank')) {
                $result['target'] = '_blank';
            }
            break;
        case 'topten':
            $type = wppa_opt('topten_widget_linktype');
            $page = wppa_opt('topten_widget_linkpage');
            if ($page == '0') {
                $page = '-1';
            }
            if (wppa_switch('topten_blank')) {
                $result['target'] = '_blank';
            }
            break;
        case 'featen':
            $type = wppa_opt('featen_widget_linktype');
            $page = wppa_opt('featen_widget_linkpage');
            if ($page == '0') {
                $page = '-1';
            }
            if (wppa_switch('featen_blank')) {
                $result['target'] = '_blank';
            }
            break;
        case 'lasten':
            $type = wppa_opt('lasten_widget_linktype');
            $page = wppa_opt('lasten_widget_linkpage');
            if ($page == '0') {
                $page = '-1';
            }
            if (wppa_switch('lasten_blank')) {
                $result['target'] = '_blank';
            }
            break;
        case 'comten':
            $type = wppa_opt('comment_widget_linktype');
            $page = wppa_opt('comment_widget_linkpage');
            if ($page == '0') {
                $page = '-1';
            }
            if (wppa_switch('comment_blank')) {
                $result['target'] = '_blank';
            }
            break;
        case 'sswidget':
            $type = wppa_opt('slideonly_widget_linktype');
            $page = wppa_opt('slideonly_widget_linkpage');
            if ($page == '0') {
                $page = '-1';
            }
            if (wppa_switch('sswidget_blank')) {
                $result['target'] = '_blank';
            }
            $result['url'] = '';
            if ($type == 'lightbox' || $type == 'lightboxsingle' || $type == 'file') {
                $result['title'] = wppa_zoom_in($id);
                $result['target'] = '';
                return $result;
            }
            break;
        case 'potdwidget':
            $type = wppa_opt('potd_linktype');
            $page = wppa_opt('potd_linkpage');
            if ($page == '0') {
                $page = '-1';
            }
            if (wppa_switch('potd_blank')) {
                $result['target'] = '_blank';
            }
            break;
        case 'coverimg':
            $type = wppa_opt('coverimg_linktype');
            $page = wppa_opt('coverimg_linkpage');
            if ($page == '0') {
                $page = '-1';
            }
            if (wppa_switch('coverimg_blank')) {
                $result['target'] = '_blank';
            }
            if ($type == 'slideshowstartatimage') {
                $result['url'] = wppa_encrypt_url(wppa_get_slideshow_url($album, $page, $id));
                $result['is_url'] = true;
                $result['is_lightbox'] = false;
                return $result;
            }
            break;
        case 'tnwidget':
            $type = wppa_opt('thumbnail_widget_linktype');
            $page = wppa_opt('thumbnail_widget_linkpage');
            if ($page == '0') {
                $page = '-1';
            }
            if (wppa_switch('thumbnail_widget_blank')) {
                $result['target'] = '_blank';
            }
            break;
        case 'slideshow':
            $type = wppa_opt('slideshow_linktype');
            //'';
            $page = wppa_opt('slideshow_linkpage');
            $result['url'] = '';
            if ($type == 'lightbox' || $type == 'lightboxsingle' || $type == 'file') {
                $result['title'] = wppa_zoom_in($id);
                $result['target'] = '';
                return $result;
            }
            if ($type == 'thumbs') {
                $result['url'] = wppa_encrypt_url(wppa_get_ss_to_tn_link($page, $id));
                $result['title'] = __('View thumbnails', 'wp-photo-album-plus');
                $result['is_url'] = true;
                $result['is_lightbox'] = false;
                if (wppa_switch('slideshow_blank')) {
                    $result['target'] = '_blank';
                }
                return $result;
            }
            if ($type == 'slide') {
                // Extension for Johnnymosaic
                $t = wppa('occur');
                if ($page != '0') {
                    wppa('occur', '1');
                }
                $result['url'] = wppa_encrypt_url(wppa_get_slideshow_url(wppa('start_album'), $page, $id));
                wppa('occur', $t);
                $result['title'] = __('View fullsize slideshow', 'wp-photo-album-plus');
                $result['is_url'] = true;
                $result['is_lightbox'] = false;
                if (wppa_switch('slideshow_blank')) {
                    $result['target'] = '_blank';
                }
                return $result;
            }
            if ($type == 'none') {
                return;
            }
            // Continue for 'single'
            break;
        case 'albwidget':
            $type = wppa_opt('album_widget_linktype');
            $page = wppa_opt('album_widget_linkpage');
            if ($page == '0') {
                $page = '-1';
            }
            if (wppa_switch('album_widget_blank')) {
                $result['target'] = '_blank';
            }
            break;
        default:
            return false;
            break;
    }
    if (!$album) {
        $album = wppa('start_album');
    }
    if ($album == '' && !wppa('is_upldr')) {
        /**/
        $album = wppa_get_album_id_by_photo_id($id);
    }
    if (is_numeric($album)) {
        $album_name = wppa_get_album_name($album);
    } else {
        $album_name = '';
    }
    if (!$album) {
        $album = '0';
    }
    if ($wich == 'comten') {
        $album = '0';
    }
    //	if ( wppa( 'is_tag' ) ) $album = '0'; // Tags can now also be on an album selecion ( made by album="#cat,cat|#tags,tag" )
    if (wppa('supersearch')) {
        $album = '0';
    }
    if (wppa('calendar')) {
        $album = wppa('start_album') ? wppa('start_album') : '0';
    }
    //	if ( wppa( 'is_upldr' ) ) $album = '0';	// probeersel upldr parent
    // owner/public?
    if ($album == '-3') {
        $temp = $wpdb->get_results("SELECT `id` FROM `" . WPPA_ALBUMS . "` WHERE `owner` = '" . wppa_get_user() . "' OR `owner` = '--- public ---' ORDER BY `id`", ARRAY_A);
        $album = '';
        if ($temp) {
            foreach ($temp as $t) {
                $album .= '.' . $t['id'];
            }
            $album = ltrim($album, '.');
        }
        $album = wppa_compress_enum($album);
    }
    if ($id) {
        $photo_name = wppa_get_photo_name($id);
    } else {
        $photo_name = '';
    }
    $photo_name_js = esc_js($photo_name);
    $photo_name = esc_attr($photo_name);
    if ($id) {
        $photo_desc = esc_attr(wppa_get_photo_desc($id));
    } else {
        $photo_desc = '';
    }
    $title = __($photo_name, 'wp-photo-album-plus');
    $result['onclick'] = '';
    // Init
    switch ($type) {
        case 'none':
            // No link at all
            return false;
            break;
        case 'file':
            // The plain file
            if ($is_video) {
                $siz = array(wppa_get_videox($id), wppa_get_videoy($id));
                $result['url'] = wppa_get_photo_url($id, '', $siz['0'], $siz['1']);
                reset($is_video);
                $result['url'] = str_replace('xxx', current($is_video), $result['url']);
            } else {
                $siz = array(wppa_get_photox($id), wppa_get_photoy($id));
                $result['url'] = wppa_get_photo_url($id, '', $siz['0'], $siz['1']);
            }
            $result['title'] = $title;
            $result['is_url'] = true;
            $result['is_lightbox'] = false;
            $result['url'] = wppa_fix_poster_ext($result['url'], $id);
            return $result;
            break;
        case 'lightbox':
        case 'lightboxsingle':
            if ($is_video) {
                $siz = array(wppa_get_videox($id), wppa_get_videoy($id));
                $result['url'] = wppa_get_photo_url($id, '', $siz['0'], $siz['1']);
                //$result['url'] = str_replace( 'xxx', $is_video['0'], $result['url'] );
            } else {
                if (wppa_switch('lb_hres') && !wppa_is_stereo($id)) {
                    $result['url'] = wppa_get_hires_url($id);
                } else {
                    $siz = array(wppa_get_photox($id), wppa_get_photoy($id));
                    $result['url'] = wppa_get_photo_url($id, '', $siz['0'], $siz['1']);
                }
            }
            $result['title'] = $title;
            $result['is_url'] = false;
            $result['is_lightbox'] = true;
            wppa_add_wppa_on_page();
            // Make sure wppa js files are loaded
            $result['url'] = wppa_fix_poster_ext($result['url'], $id);
            return $result;
        case 'widget':
            // Defined at widget activation
            $result['url'] = wppa('in_widget_linkurl');
            $result['title'] = esc_attr(wppa('in_widget_linktitle'));
            $result['is_url'] = true;
            $result['is_lightbox'] = false;
            return $result;
            break;
        case 'album':
            // The albums thumbnails
        // The albums thumbnails
        case 'content':
            // For album widget
            switch ($page) {
                case '-1':
                    return false;
                    break;
                case '0':
                    if ($noalb) {
                        $result['url'] = wppa_encrypt_url(wppa_get_permalink() . 'wppa-album=0&amp;wppa-cover=0');
                        $result['title'] = '';
                        // $album_name;
                        $result['is_url'] = true;
                        $result['is_lightbox'] = false;
                    } else {
                        $result['url'] = wppa_encrypt_url(wppa_get_permalink() . 'wppa-album=' . $album . '&amp;wppa-cover=0');
                        $result['title'] = $album_name;
                        $result['is_url'] = true;
                        $result['is_lightbox'] = false;
                    }
                    break;
                default:
                    if ($noalb) {
                        $result['url'] = wppa_encrypt_url(wppa_get_permalink($page) . 'wppa-album=0&amp;wppa-cover=0');
                        $result['title'] = '';
                        //$album_name;//'a++';
                        $result['is_url'] = true;
                        $result['is_lightbox'] = false;
                    } else {
                        $result['url'] = wppa_encrypt_url(wppa_get_permalink($page) . 'wppa-album=' . $album . '&amp;wppa-cover=0');
                        $result['title'] = $album_name;
                        //'a++';
                        $result['is_url'] = true;
                        $result['is_lightbox'] = false;
                    }
                    break;
            }
            break;
        case 'thumbalbum':
            $album = $thumb['album'];
            $album_name = wppa_get_album_name($album);
            switch ($page) {
                case '-1':
                    return false;
                    break;
                case '0':
                    $result['url'] = wppa_encrypt_url(wppa_get_permalink() . 'wppa-album=' . $album . '&amp;wppa-cover=0');
                    $result['title'] = $album_name;
                    $result['is_url'] = true;
                    $result['is_lightbox'] = false;
                    break;
                default:
                    $result['url'] = wppa_encrypt_url(wppa_get_permalink($page) . 'wppa-album=' . $album . '&amp;wppa-cover=0');
                    $result['title'] = $album_name;
                    //'a++';
                    $result['is_url'] = true;
                    $result['is_lightbox'] = false;
                    break;
            }
            break;
        case 'photo':
            // This means: The fullsize photo in a slideshow
        // This means: The fullsize photo in a slideshow
        case 'slphoto':
            // This means: The single photo in the style of a slideshow
            if ($type == 'slphoto') {
                $si = '&amp;wppa-single=1';
            } else {
                $si = '';
            }
            switch ($page) {
                case '-1':
                    return false;
                    break;
                case '0':
                    if ($noalb) {
                        $result['url'] = wppa_encrypt_url(wppa_get_permalink() . 'wppa-album=0&amp;wppa-photo=' . $id . $si);
                        $result['title'] = $title;
                        $result['is_url'] = true;
                        $result['is_lightbox'] = false;
                    } else {
                        $result['url'] = wppa_encrypt_url(wppa_get_permalink() . 'wppa-album=' . $album . '&amp;wppa-photo=' . $id . $si);
                        $result['title'] = $title;
                        $result['is_url'] = true;
                        $result['is_lightbox'] = false;
                    }
                    break;
                default:
                    if ($noalb) {
                        $result['url'] = wppa_encrypt_url(wppa_get_permalink($page) . 'wppa-album=0&amp;wppa-photo=' . $id . $si);
                        $result['title'] = $title;
                        $result['is_url'] = true;
                        $result['is_lightbox'] = false;
                    } else {
                        $result['url'] = wppa_encrypt_url(wppa_get_permalink($page) . 'wppa-album=' . $album . '&amp;wppa-photo=' . $id . $si);
                        $result['title'] = $title;
                        $result['is_url'] = true;
                        $result['is_lightbox'] = false;
                    }
                    break;
            }
            break;
        case 'single':
            switch ($page) {
                case '-1':
                    return false;
                    break;
                case '0':
                    $result['url'] = wppa_encrypt_url(wppa_get_permalink() . 'wppa-photo=' . $id);
                    $result['title'] = $title;
                    $result['is_url'] = true;
                    $result['is_lightbox'] = false;
                    break;
                default:
                    $result['url'] = wppa_encrypt_url(wppa_get_permalink($page) . 'wppa-photo=' . $id);
                    $result['title'] = $title;
                    $result['is_url'] = true;
                    $result['is_lightbox'] = false;
                    break;
            }
            break;
        case 'same':
            $result['url'] = $lnk;
            $result['title'] = $tit;
            $result['is_url'] = true;
            $result['is_lightbox'] = false;
            $result['onclick'] = $onc;
            return $result;
            break;
        case 'fullpopup':
            if ($is_video) {
                // A video can not be printed or downloaded
                $result['url'] = esc_attr('alert( "' . esc_js(__('A video can not be printed or downloaded', 'wp-photo-album-plus')) . '" )');
            } else {
                $wid = wppa_get_photox($id);
                $hig = wppa_get_photoy($id);
                /*
                $imgsize = getimagesize( wppa_get_photo_path( $id ) );
                if ( $imgsize ) {
                	$wid = $imgsize['0'];
                	$hig = $imgsize['1'];
                }
                else {
                	$wid = '0';
                	$hig = '0';
                }
                */
                $url = wppa_fix_poster_ext(wppa_get_photo_url($id, '', $wid, $hig), $id);
                $result['url'] = esc_attr('wppaFullPopUp( ' . wppa('mocc') . ', ' . $id . ', "' . $url . '", ' . $wid . ', ' . $hig . ' )');
            }
            $result['title'] = $title;
            $result['is_url'] = false;
            $result['is_lightbox'] = false;
            return $result;
            break;
        case 'custom':
            if ($wich == 'potdwidget') {
                $result['url'] = wppa_opt('widget_linkurl');
                $result['title'] = wppa_opt('widget_linktitle');
                $result['is_url'] = true;
                $result['is_lightbox'] = false;
                return $result;
            }
            break;
        case 'slide':
            // for album widget
            $result['url'] = wppa_encrypt_url(wppa_get_permalink(wppa_opt('album_widget_linkpage')) . 'wppa-album=' . $album . '&amp;slide');
            $result['title'] = '';
            $result['is_url'] = true;
            $result['is_lightbox'] = false;
            break;
        case 'slidealbum':
            // for lasten widget, slide of the photos in te thumbs album
            $album = $thumb['album'];
            $album_name = wppa_get_album_name($album);
            switch ($page) {
                case '-1':
                    return false;
                    break;
                case '0':
                    $result['url'] = wppa_encrypt_url(wppa_get_permalink() . 'wppa-album=' . $album . '&amp;wppa-photo=' . $thumb['id']);
                    $result['title'] = $album_name;
                    $result['is_url'] = true;
                    $result['is_lightbox'] = false;
                    break;
                default:
                    $result['url'] = wppa_encrypt_url(wppa_get_permalink($page) . 'wppa-album=' . $album . '&amp;wppa-photo=' . $thumb['id']);
                    $result['title'] = $album_name;
                    //'a++';
                    $result['is_url'] = true;
                    $result['is_lightbox'] = false;
                    break;
            }
            break;
        case 'autopage':
            if (!wppa_switch('auto_page')) {
                wppa_dbg_msg('Auto page has been switched off, but there are still links to it (' . $wich . ')', 'red', 'force');
                $result['url'] = '';
            } else {
                $result['url'] = wppa_get_permalink(wppa_get_the_auto_page($id));
            }
            $result['title'] = '';
            $result['is_url'] = true;
            $result['is_lightbox'] = false;
            break;
        case 'plainpage':
            $result['url'] = get_permalink($page);
            $result['title'] = $wpdb->get_var($wpdb->prepare("SELECT `post_title` FROM `" . $wpdb->prefix . "posts` WHERE `ID` = %s", $page));
            $result['is_url'] = true;
            $result['is_lightbox'] = false;
            return $result;
            break;
        default:
            wppa_dbg_msg('Error, wrong type: ' . $type . ' in wppa_get_imglink_a', 'red');
            return false;
            break;
    }
    if ($type != 'thumbalbum' && $type != 'slidealbum') {
        if (wppa('calendar')) {
            $result['url'] .= '&amp;wppa-calendar=' . wppa('calendar') . '&amp;wppa-caldate=' . wppa('caldate');
        }
        if (wppa('supersearch')) {
            $result['url'] .= '&amp;wppa-supersearch=' . urlencode(wppa('supersearch'));
        }
        if (wppa('src') && !wppa('is_related') && !wppa_in_widget()) {
            $result['url'] .= '&amp;wppa-searchstring=' . urlencode(wppa('searchstring'));
        }
        if ($wich == 'topten') {
            $result['url'] .= '&amp;wppa-topten=' . wppa_opt('topten_count');
        } elseif (wppa('is_topten')) {
            $result['url'] .= '&amp;wppa-topten=' . wppa('topten_count');
        }
        if ($wich == 'lasten') {
            $result['url'] .= '&amp;wppa-lasten=' . wppa_opt('lasten_count');
        } elseif (wppa('is_lasten')) {
            $result['url'] .= '&amp;wppa-lasten=' . wppa('lasten_count');
        }
        if ($wich == 'comten') {
            $result['url'] .= '&amp;wppa-comten=' . wppa_opt('comten_count');
        } elseif (wppa('is_comten')) {
            $result['url'] .= '&amp;wppa-comten=' . wppa('comten_count');
        }
        if ($wich == 'featen') {
            $result['url'] .= '&amp;wppa-featen=' . wppa_opt('featen_count');
        } elseif (wppa('is_featen')) {
            $result['url'] .= '&amp;wppa-featen=' . wppa('featen_count');
        }
        if (wppa('is_related')) {
            $result['url'] .= '&amp;wppa-rel=' . wppa('is_related') . '&amp;wppa-relcount=' . wppa('related_count');
        } elseif (wppa('is_tag')) {
            $result['url'] .= '&amp;wppa-tag=' . wppa('is_tag');
        }
        if (wppa('is_upldr')) {
            $result['url'] .= '&amp;wppa-upldr=' . wppa('is_upldr');
        }
        if (wppa('is_inverse')) {
            $result['url'] .= '&amp;wppa-inv=1';
        }
        if (wppa('medals_only')) {
            $result['url'] .= '&amp;wppa-medals-only=1';
        }
    }
    if ($page != '0') {
        // on a different page
        $occur = '1';
        $w = '';
    } else {
        // on the same page, post or widget
        $occur = wppa_in_widget() ? wppa('widget_occur') : wppa('occur');
        $w = wppa_in_widget() ? 'w' : '';
    }
    $result['url'] .= '&amp;wppa-' . $w . 'occur=' . $occur;
    $result['url'] = wppa_encrypt_url($result['url']);
    $result['url'] = wppa_convert_to_pretty($result['url']);
    if ($result['title'] == '') {
        $result['title'] = $tit;
    }
    // If still nothing, try arg
    return $result;
}