function wppa_albums($id = '', $type = '', $size = '', $align = '')
{
    global $wppa_lang;
    global $wppa_locale;
    global $wpdb;
    global $thumbs;
    global $wppa_session;
    wppa_add_wppa_on_page();
    // Diagnostics
    wppa_dbg_msg('Entering wppa_albums');
    wppa_dbg_msg('Lang=' . $wppa_lang . ', Locale=' . $wppa_locale . ', Ajax=' . wppa('ajax'));
    wppa_dbg_msg('Get=' . serialize($_GET));
    wppa_dbg_msg('Post=' . serialize($_POST));
    //	wppa_dbg_msg( '$wppa_session = ' . serialize( $wppa_session ) );
    // List content filters
    // Data struct:	$wp_filter[$tag]->callbacks[$priority][$idx] = array( 'function' => $function_to_add, 'accepted_args' => $accepted_args );
    if (wppa('debug') && wppa('mocc') == '0') {
        global $wp_filter;
        wppa_dbg_msg('Start content filters', 'green');
        foreach (array_keys($wp_filter) as $tag) {
            if ($tag == 'the_content') {
                $filters = $wp_filter[$tag]->callbacks;
                foreach (array_keys($filters) as $pri) {
                    foreach (array_keys($filters[$pri]) as $item) {
                        if (!is_array($filters[$pri][$item]['function'])) {
                            wppa_dbg_msg($tag . ', Pri:' . $pri . ', Func:' . $filters[$pri][$item]['function'] . ', args=' . $filters[$pri][$item]['accepted_args']);
                        }
                    }
                }
            }
        }
        wppa_dbg_msg('End content filters', 'green');
    }
    // Process a user upload request, if any. Do it here: it may affect this occurences display
    wppa_user_upload();
    // Test for scheduled publications
    wppa_publish_scheduled();
    // First calculate the occurance
    if (wppa('ajax')) {
        if (wppa_get_get('moccur')) {
            wppa('mocc', wppa_get_get('moccur'));
            if (!is_numeric(wppa('mocc'))) {
                wp_die('Security check failure 1');
            }
        } else {
            wppa('mocc', '1');
        }
        wppa('fullsize', wppa_get_get('wppa-size', wppa_get_container_width()));
        if (wppa_get_get('occur')) {
            wppa('occur', wppa_get_get('occur'));
            if (!is_numeric(wppa('occur'))) {
                wp_die('Security check failure 2');
            }
        }
        if (wppa_get_get('woccur')) {
            wppa('widget_occur', wppa_get_get('woccur'));
            wppa('in_widget', true);
            if (!is_numeric(wppa('widget_occur'))) {
                wp_die('Security check failure 3');
            }
        }
    } else {
        wppa('mocc', wppa('mocc') + '1');
        if (wppa_in_widget()) {
            wppa('widget_occur', wppa('widget_occur') + '1');
        } else {
            wppa('occur', wppa('occur') + '1');
        }
    }
    // Set wppa( 'src' ) = true and wppa( 'searchstring' ) if this occurrance processes a search request.
    wppa_test_for_search();
    // There are 3 ways to get here:
    // in order of priority:
    // 1. The given query string applies to this invocation ( occurrance )
    //    This invocation requires the ignorance of the filter results and the interpretation of the querystring.
    if ((wppa_get_get('occur') || wppa_get_get('woccur')) && (wppa_in_widget() && wppa('widget_occur') == wppa_get_get('woccur') || !wppa_in_widget() && wppa('occur') == wppa_get_get('occur')) && !wppa('is_autopage')) {
        // Process query string
        wppa_out(wppa_dbg_msg('Querystring applied', 'brown', false, true));
        // Test validity of album arg
        wppa('start_album', wppa_get_get('album'));
        //		if ( wppa( 'start_album' ) == '-9' ) exit;
        //		echo 'sa='.wppa( 'start_album' ).'<br />';
        wppa('is_cover', wppa_get_get('cover'));
        wppa('is_slide', wppa_get_get('slide') || wppa_get_get('album') !== false && wppa_get_get('photo'));
        if (wppa('is_slide')) {
            wppa('start_photo', wppa_get_get('photo'));
            // Start a slideshow here
        } else {
            wppa('single_photo', wppa_get_get('photo'));
            // Photo is the single photoid
        }
        wppa('is_single', wppa_get_get('single'));
        // Is a one image slideshow
        wppa('topten_count', wppa_force_numeric_else(wppa_get_get('topten'), wppa_opt('topten_count')));
        wppa('is_topten', wppa('topten_count') != '0');
        wppa('lasten_count', wppa_force_numeric_else(wppa_get_get('lasten'), wppa_opt('lasten_count')));
        wppa('is_lasten', wppa('lasten_count') != '0');
        wppa('comten_count', wppa_force_numeric_else(wppa_get_get('comten'), wppa_opt('comten_count')));
        wppa('is_comten', wppa('comten_count') != '0');
        wppa('featen_count', wppa_force_numeric_else(wppa_get_get('featen'), wppa_opt('featen_count')));
        wppa('is_featen', wppa('featen_count') != '0');
        wppa('albums_only', wppa_get_get('albums-only'));
        wppa('photos_only', wppa_get_get('photos-only'));
        wppa('medals_only', wppa_get_get('medals-only'));
        wppa('related_count', wppa_force_numeric_else(wppa_get_get('relcount'), wppa_opt('related_count')));
        wppa('is_related', wppa_get_get('rel'));
        if (wppa('is_related') == 'tags') {
            wppa('is_tag', wppa_get_related_data());
            if (wppa('related_count') == '0') {
                wppa('related_count', wppa_opt('related_count'));
            }
        } else {
            wppa('is_tag', trim(strip_tags(wppa_get_get('tag')), ',;'));
        }
        if (wppa('is_related') == 'desc') {
            wppa('src', true);
            if (wppa('related_count') == '0') {
                wppa('related_count', wppa_opt('related_count'));
            }
            wppa('searchstring', str_replace(';', ',', wppa_get_related_data()));
            wppa('photos_only', true);
        }
        if (wppa('is_tag')) {
            wppa_dbg_msg('Is Tag: ' . wppa('is_tag'));
        } else {
            wppa_dbg_msg('Is NOT Tag');
        }
        wppa('page', wppa_get_get('page'));
        if (wppa_get_get('superview')) {
            $wppa_session['superview'] = wppa('is_slide') ? 'slide' : 'thumbs';
            $wppa_session['superalbum'] = wppa('start_album');
            wppa_save_session();
            wppa('photos_only', true);
        }
        wppa('is_upldr', wppa_get_get('upldr'));
        if (wppa('is_upldr')) {
            wppa('photos_only', true);
        }
        wppa('is_owner', wppa_get_get('owner'));
        if (wppa('is_owner')) {
            $albs = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_ALBUMS . "` WHERE `owner` = %s ", wppa('is_owner')), ARRAY_A);
            wppa_cache_album('add', $albs);
            $id = '';
            if ($albs) {
                foreach ($albs as $alb) {
                    $id .= $alb['id'] . '.';
                }
            }
            $id = rtrim($id, '.');
            wppa('start_album', $id);
        }
        wppa('supersearch', strip_tags(wppa_get_get('supersearch')));
        $wppa_session['supersearch'] = wppa('supersearch');
        wppa_save_session();
        if (wppa('supersearch')) {
            $ss_info = explode(',', wppa('supersearch'));
            if ($ss_info['0'] == 'a') {
                wppa('albums_only', true);
            } else {
                wppa('photos_only', true);
            }
        }
        wppa('calendar', strip_tags(wppa_get_get('calendar')));
        wppa('caldate', strip_tags(wppa_get_get('caldate')));
        wppa('is_inverse', wppa_get_get('inv'));
        //		if ( ! isset( $_REQUEST['album'] ) && ! isset( $_REQUEST['wppa-album'] ) ) {
        //			wppa_dbg_msg( 'No album spec' . ( wppa_is_virtual() ? ' on virtual album' : ' on real album (0)' ) , 'red', 'force' );
        //		}
    } elseif ($id != '' || $type != '' || $size != '' || $align != '') {
        // Do NOT Set internal defaults here, they may be set before the call
        // Interprete function args
        if ($type == 'album') {
        } elseif ($type == 'cover') {
            wppa('is_cover', true);
        } elseif ($type == 'slide') {
            wppa('is_slide', true);
        } elseif ($type == 'slideonly') {
            wppa('is_slideonly', true);
        }
        if ($type == 'photo' || $type == 'mphoto' || $type == 'slphoto' || $type == 'xphoto') {
            // Any type of single photo? id given is photo id
            if ($id) {
                wppa('single_photo', $id);
            }
        } else {
            // Not single photo: id given is album id
            if ($id) {
                wppa('start_album', $id);
            }
        }
    } else {
        if (wppa('is_admins_choice')) {
            $args = wppa('admins_choice_users');
            wppa_admins_choice_box($args);
            $out = wppa('out');
            wppa_reset_occurrance();
            return $out;
        }
        if (wppa('bestof')) {
            $args = wppa('bestof_args');
            wppa_bestof_box($args);
            $out = wppa('out');
            wppa_reset_occurrance();
            return $out;
        } elseif (wppa('is_landing') && !wppa('src')) {
            wppa_dbg_msg('Nothing to do...');
            wppa_reset_occurrance();
            return '';
            // Do nothing on a landing page without a querystring while it is also not a search operation
        } elseif (wppa('is_autopage')) {
            $photo = $wpdb->get_var($wpdb->prepare("SELECT `id` FROM `" . WPPA_PHOTOS . "` WHERE `page_id` = %d LIMIT 1", get_the_ID()));
            wppa('single_photo', $photo);
            if (!wppa('single_photo')) {
                wppa_dbg_msg('No photo found for page ' . get_the_ID(), 'red', 'force');
                wppa_reset_occurrance();
                return '';
                // Give up
            }
            $type = wppa_opt('auto_page_type');
            switch ($type) {
                case 'photo':
                    break;
                case 'mphoto':
                    wppa('is_mphoto', true);
                    break;
                case 'xphoto':
                    wppa('is_xphoto', true);
                    break;
                case 'slphoto':
                    wppa('is_slide', true);
                    wppa('start_photo', wppa('single_photo'));
                    wppa('is_single', true);
                    break;
                default:
                    wppa_dbg_msg('Unimplemented type autopage display: ' . $type, 'red', 'force');
            }
        }
    }
    // Convert any keywords and / or names to numbers
    // Search for album keyword
    if (wppa('start_album') && !wppa_is_int(wppa('start_album'))) {
        if (substr(wppa('start_album'), 0, 1) == '#') {
            // Keyword
            $keyword = wppa('start_album');
            if (strpos($keyword, ',')) {
                $keyword = substr($keyword, 0, strpos($keyword, ','));
            }
            switch ($keyword) {
                //	( substr( wppa( 'start_album'], 0, 5 ) ) {
                case '#last':
                    // Last upload
                    $id = wppa_get_youngest_album_id();
                    if (wppa('is_cover')) {
                        // To make sure the ordering sequence is ok.
                        $temp = explode(',', wppa('start_album'));
                        if (isset($temp['1'])) {
                            wppa('last_albums_parent', $temp['1']);
                        } else {
                            wppa('last_albums_parent', '0');
                        }
                        if (isset($temp['2'])) {
                            wppa('last_albums', $temp['2']);
                        } else {
                            wppa('last_albums', false);
                        }
                    } else {
                        // Ordering seq is not important, convert to album enum
                        $temp = explode(',', wppa('start_album'));
                        if (isset($temp['1'])) {
                            $parent = wppa_album_name_to_number($temp['1']);
                        } else {
                            $parent = '0';
                        }
                        if ($parent === false) {
                            return;
                        }
                        if (isset($temp['2'])) {
                            $limit = $temp['2'];
                        } else {
                            $limit = false;
                        }
                        if ($limit) {
                            if ($parent) {
                                if ($limit) {
                                    $q = $wpdb->prepare("SELECT * FROM `" . WPPA_ALBUMS . "` WHERE `a_parent` = %s ORDER BY `timestamp` DESC LIMIT %d", $parent, $limit);
                                } else {
                                    $q = $wpdb->prepare("SELECT * FROM `" . WPPA_ALBUMS . "` WHERE `a_parent` = %s ORDER BY `timestamp` DESC", $parent);
                                }
                            } else {
                                if ($limit) {
                                    $q = $wpdb->prepare("SELECT * FROM `" . WPPA_ALBUMS . "` ORDER BY `timestamp` DESC LIMIT %d", $limit);
                                } else {
                                    $q = "SELECT * FROM `" . WPPA_ALBUMS . "` ORDER BY `timestamp` DESC";
                                }
                            }
                            $albs = $wpdb->get_results($q, ARRAY_A);
                            wppa_cache_album('add', $albs);
                            if (is_array($albs)) {
                                foreach (array_keys($albs) as $key) {
                                    $albs[$key] = $albs[$key]['id'];
                                }
                            }
                            $id = implode('.', $albs);
                        }
                    }
                    break;
                case '#topten':
                    $temp = explode(',', wppa('start_album'));
                    $id = isset($temp[1]) ? $temp[1] : '0';
                    $cnt = wppa_opt('topten_count');
                    if (isset($temp[2])) {
                        if ($temp[2] > 0) {
                            $cnt = $temp[2];
                        }
                    }
                    wppa('topten_count', $cnt);
                    wppa('is_topten', true);
                    if (wppa('is_cover')) {
                        wppa_dbg_msg('A topten album has no cover. ' . wppa('start_album'), 'red', 'force');
                        wppa_reset_occurrance();
                        return;
                        // Give up this occurence
                    }
                    if (isset($temp[3])) {
                        if ($temp[3] == 'medals') {
                            wppa('medals_only', true);
                        }
                    }
                    break;
                case '#lasten':
                    $temp = explode(',', wppa('start_album'));
                    $id = isset($temp[1]) ? $temp[1] : '0';
                    wppa('lasten_count', isset($temp[2]) ? $temp[2] : wppa_opt('lasten_count'));
                    wppa('is_lasten', true);
                    // Limit to owner?
                    if (isset($temp[3])) {
                        wppa('is_upldr', $temp[3]);
                    }
                    if (wppa('is_cover')) {
                        wppa_dbg_msg('A lasten album has no cover. ' . wppa('start_album'), 'red', 'force');
                        wppa_reset_occurrance();
                        return;
                        // Give up this occurence
                    }
                    break;
                case '#comten':
                    $temp = explode(',', wppa('start_album'));
                    $id = isset($temp[1]) ? $temp[1] : '0';
                    wppa('comten_count', isset($temp[2]) ? $temp[2] : wppa_opt('comten_count'));
                    wppa('is_comten', true);
                    if (wppa('is_cover')) {
                        wppa_dbg_msg('A comten album has no cover. ' . wppa('start_album'), 'red', 'force');
                        wppa_reset_occurrance();
                        return;
                        // Give up this occurence
                    }
                    break;
                case '#featen':
                    $temp = explode(',', wppa('start_album'));
                    $id = isset($temp[1]) ? $temp[1] : '0';
                    wppa('featen_count', isset($temp[2]) ? $temp[2] : wppa_opt('featen_count'));
                    wppa('is_featen', true);
                    if (wppa('is_cover')) {
                        wppa_dbg_msg('A featen album has no cover. ' . wppa('start_album'), 'red', 'force');
                        wppa_reset_occurrance();
                        return;
                        // Give up this occurence
                    }
                    break;
                case '#related':
                    $temp = explode(',', wppa('start_album'));
                    $type = isset($temp[1]) ? $temp[1] : 'tags';
                    // tags is default type
                    wppa('related_count', isset($temp[2]) ? $temp[2] : wppa_opt('related_count'));
                    wppa('is_related', $type);
                    $data = wppa_get_related_data();
                    if ($type == 'tags') {
                        wppa('is_tag', $data);
                    }
                    if ($type == 'desc') {
                        wppa('src', true);
                        wppa('searchstring', str_replace(';', ',', $data));
                        wppa('photos_only', true);
                    }
                    wppa('photos_only', true);
                    $id = '0';
                    break;
                case '#tags':
                    // See if they did not use the #cat / #tags combination in wrong sequence order
                    $seppos = strpos(wppa('start_album'), '|');
                    if ($seppos !== false) {
                        wppa_dbg_msg('Syntax error in shortcode attribute album=. Expected: album="#cat,...|#tags,...", seen: album="' . wppa('start_album') . '"', 'red', 'force');
                        wppa_reset_occurrance();
                        return;
                    }
                    wppa('is_tag', wppa_sanitize_tags(substr(wppa('start_album'), 6), true));
                    $id = '0';
                    wppa('photos_only', true);
                    break;
                case '#cat':
                    // See if the #cat,cat|#tags,tag special case has been used
                    $seppos = strpos(wppa('start_album'), '|');
                    if ($seppos !== false) {
                        // Yes, process the second part, the #tags clause
                        if (substr(wppa('start_album'), $seppos, 7) != '|#tags,') {
                            wppa_dbg_msg('Syntax error in shortcode attribute album=. Expected: album="#cat,...|#tags,...", seen: album="' . wppa('start_album') . '"', 'red', 'force');
                            wppa_reset_occurrance();
                            return;
                            // Forget this occurrance
                        }
                        wppa('is_tag', wppa_sanitize_tags(substr(wppa('start_album'), $seppos + 7), true));
                        wppa('photos_only', true);
                        wppa('start_album', substr(wppa('start_album'), 0, $seppos));
                    }
                    $cats = substr(wppa('start_album'), 5);
                    $cats = trim(wppa_sanitize_tags($cats, true), ',;');
                    wppa('is_cat', $cats);
                    if (!$cats) {
                        wppa_dbg_msg('Missing cat #cat album spec: ' . wppa('start_album'), 'red', 'force');
                        wppa_reset_occurrance();
                        return;
                        // Forget this occurrance
                    }
                    // Get all albums and cache its data
                    $albs = $wpdb->get_results("SELECT * FROM `" . WPPA_ALBUMS . "`", ARRAY_A);
                    wppa_cache_album('add', $albs);
                    // $cats is not empty. If it contains a , all cats must be met ( AND case )
                    // It may contain a ; any cat must be met
                    // AND case
                    if (strpos($cats, ',') !== false) {
                        // Do not accept a mix of , and ; Convert ; to ,
                        $cats = str_replace(';', ',', $cats);
                        $cats = explode(',', $cats);
                        $id = '';
                        if ($albs) {
                            foreach ($albs as $alb) {
                                $albcats = explode(',', $alb['cats']);
                                // Assume in
                                $in = true;
                                // Test all required cats against album cats array
                                foreach ($cats as $cat) {
                                    if (!in_array($cat, $albcats, true)) {
                                        $in = false;
                                    }
                                }
                                if ($in) {
                                    $id .= $alb['id'] . '.';
                                }
                            }
                        }
                    } else {
                        $cats = explode(';', $cats);
                        $id = '';
                        if ($albs) {
                            foreach ($albs as $alb) {
                                $albcats = explode(',', $alb['cats']);
                                // Assume out
                                $in = false;
                                // Test all possible cats against album cats array
                                foreach ($cats as $cat) {
                                    if (in_array($cat, $albcats, true)) {
                                        $in = true;
                                    }
                                }
                                if ($in) {
                                    $id .= $alb['id'] . '.';
                                }
                            }
                        }
                    }
                    // Remove possible trailing dot
                    $id = rtrim($id, '.');
                    // Nothing found?
                    if (!$id) {
                        $id = '-9';
                    }
                    // Add children?
                    if (wppa_switch('cats_inherit')) {
                        $id = wppa_alb_to_enum_children($id);
                    }
                    break;
                case '#owner':
                    $temp = explode(',', wppa('start_album'));
                    $owner = isset($temp[1]) ? $temp[1] : '';
                    if ($owner == '#me') {
                        if (is_user_logged_in()) {
                            $owner = wppa_get_user();
                        } else {
                            // User not logged in, ignore shortcode
                            wppa_reset_occurrance();
                            return;
                            // Forget this occurrance
                        }
                    }
                    if (!$owner) {
                        wppa_dbg_msg('Missing owner in #owner album spec: ' . wppa('start_album'), 'red', 'force');
                        wppa_reset_occurrance();
                        return;
                        // Forget this occurrance
                    }
                    $parent = isset($temp[2]) ? wppa_album_name_to_number($temp[2]) : '0';
                    if ($parent === false) {
                        return;
                    }
                    if (!$parent) {
                        $parent = '-1.0';
                    }
                    if ($parent) {
                        // Valid parent spec
                        $parent_arr = explode('.', wppa_expand_enum($parent));
                        $id = wppa_alb_to_enum_children($parent);
                        // Verify all albums are owned by $owner and are directly under a parent album
                        $id = wppa_expand_enum($id);
                        $albs = explode('.', $id);
                        if ($albs) {
                            foreach (array_keys($albs) as $idx) {
                                if (wppa_get_album_item($albs[$idx], 'owner') != $owner || !in_array(wppa_get_album_item($albs[$idx], 'a_parent'), $parent_arr)) {
                                    unset($albs[$idx]);
                                }
                            }
                        }
                        $id = implode('.', $albs);
                        if (!$id) {
                            $id = '-9';
                            // Force nothing found
                            //				wppa_reset_occurrance();
                            //				return;	// No children found
                        }
                    }
                    wppa('is_owner', $owner);
                    break;
                case '#upldr':
                    $temp = explode(',', wppa('start_album'));
                    $owner = isset($temp[1]) ? $temp[1] : '';
                    if ($owner == '#me') {
                        if (is_user_logged_in()) {
                            $owner = wppa_get_user();
                        } else {
                            // User not logged in, ignore shortcode
                            wppa_reset_occurrance();
                            return;
                            // Forget this occurrance
                        }
                    }
                    if (!$owner) {
                        wppa_dbg_msg('Missing owner in #upldr album spec: ' . wppa('start_album'), 'red', 'force');
                        wppa_reset_occurrance();
                        return;
                        // Forget this occurrance
                    }
                    $parent = isset($temp[2]) ? wppa_album_name_to_number($temp[2]) : '0';
                    if ($parent === false) {
                        return;
                    }
                    // parent specified but not a valid value
                    if ($parent) {
                        // Valid parent spec
                        $id = wppa_alb_to_enum_children(wppa_expand_enum($parent));
                        if (!$id) {
                            wppa_reset_occurrance();
                            return;
                            // No children found
                        }
                    } else {
                        // No parent spec
                        $id = '0';
                    }
                    wppa('is_upldr', $owner);
                    wppa('photos_only', true);
                    break;
                case '#all':
                    $id = '-2';
                    break;
                default:
                    wppa_dbg_msg('Unrecognized album keyword found: ' . wppa('start_album'), 'red', 'force');
                    wppa_reset_occurrance();
                    return;
                    // Forget this occurrance
            }
            wppa('start_album', $id);
        }
    }
    // See if the album id is a name and convert it if possible
    wppa('start_album', wppa_album_name_to_number(wppa('start_album'), true));
    if (wppa('start_album') === false) {
        wppa_reset_occurrance();
        return;
    }
    // Also for parents
    wppa('last_albums_parent', wppa_album_name_to_number(wppa('last_albums_parent')));
    if (wppa('last_albums_parent') === false) {
        wppa_reset_occurrance();
        return;
    }
    // Check if album is valid
    if (strpos(wppa('start_album'), '.') !== false) {
        // Album may be enum
        if (!wppa_series_to_array(wppa('start_album'))) {
            // Syntax error
            wppa_reset_occurrance();
            return;
        }
    } elseif (wppa('start_album') && !is_numeric(wppa('start_album'))) {
        wppa_stx_err('Unrecognized Album identification found: ' . wppa('start_album'));
        wppa_reset_occurrance();
        return;
        // Forget this occurrance
    } elseif (wppa('start_album') > '0') {
        // -2 is #all
        if (!wppa_album_exists(wppa('start_album'))) {
            wppa_stx_err('Album does not exist: ' . wppa('start_album'));
            wppa_reset_occurrance();
            return;
            // Forget this occurrance
        }
    }
    // See if the photo id is a keyword and convert it if possible
    if (wppa('single_photo') && !is_numeric(wppa('single_photo'))) {
        if (substr(wppa('single_photo'), 0, 1) == '#') {
            // Keyword
            switch (wppa('single_photo')) {
                case '#potd':
                    // Photo of the day
                    $t = wppa_get_potd();
                    if (is_array($t)) {
                        $id = $t['id'];
                        wppa('start_photo', $id);
                    } else {
                        wppa_dbg_msg('Photo of the day not found', 'red', 'force');
                        wppa_reset_occurrance();
                        return;
                        // Forget this occurrance
                    }
                    break;
                case '#last':
                    // Last upload
                    $id = wppa_get_youngest_photo_id();
                    break;
                default:
                    wppa_dbg_msg('Unrecognized photo keyword found: ' . wppa('single_photo'), 'red', 'force');
                    wppa_reset_occurrance();
                    return;
                    // Forget this occurrance
            }
            wppa('single_photo', $id);
        }
    }
    // See if the photo id is a name and convert it if possible
    if (wppa('single_photo') && !is_numeric(wppa('single_photo'))) {
        if (substr(wppa('single_photo'), 0, 1) == '$') {
            // Name
            $id = wppa_get_photo_id_by_name(substr(wppa('single_photo'), 1));
            if ($id > '0') {
                wppa('single_photo', $id);
            } else {
                wppa_dbg_msg('Photo name not found: ' . wppa('single_photo'), 'red', 'force');
                wppa_reset_occurrance();
                return;
                // Forget this occurrance
            }
        }
    }
    // Size and align
    if (is_numeric($size)) {
        wppa('fullsize', $size);
    } elseif ($size == 'auto') {
        wppa('auto_colwidth', true);
    }
    if ($align == 'left' || $align == 'center' || $align == 'right') {
        wppa('align', $align);
    }
    // Empty related shortcode?
    if (wppa('is_related')) {
        $thumbs = wppa_get_thumbs();
        if (empty($thumbs)) {
            wppa_errorbox(__('No related photos found.', 'wp-photo-album-plus'));
            $result = wppa('out');
            wppa_reset_occurrance();
            // Forget this occurrance
            return $result;
        }
    }
    // Subsearch or rootsearch?
    if (wppa('occur') == '1' && !wppa('in_widget') && ($wppa_session['has_searchbox'] || isset($_REQUEST['wppa-forceroot']))) {
        // Is it a search now?
        if (wppa('src')) {
            // Is the subsearch box checked?
            wppa('is_subsearch', wppa_get_get('subsearch') || wppa_get_post('subsearch'));
            // Is the rootsearch box checked?
            wppa('is_rootsearch', wppa_get_get('rootsearch') || wppa_get_post('rootsearch'));
            // Is it even a forced root search?
            if (isset($_REQUEST['wppa-forceroot'])) {
                $wppa_session['search_root'] = strval(intval($_REQUEST['wppa-forceroot']));
                wppa('is_rootsearch', true);
                wppa('start_album', strval(intval($_REQUEST['wppa-forceroot'])));
                wppa_save_session();
            }
            // No rootsearch, forget previous root
            if (!wppa('is_rootsearch')) {
                $wppa_session['search_root'] = '0';
                wppa_save_session();
            }
            wppa_dbg_msg('Forceroot=' . (isset($_REQUEST['wppa-forceroot']) ? $_REQUEST['wppa-forceroot'] : 'none') . ', is_rootsearch=' . wppa('is_rootsearch') . ', start_album=' . wppa('start_album'), 'red');
        } else {
            // Find new potential searchroot
            if (isset($_REQUEST['wppa-searchroot'])) {
                wppa('start_album', strval(intval($_REQUEST['wppa-searchroot'])));
            }
            // Update session with new searchroot
            $wppa_session['search_root'] = wppa('start_album');
            wppa_save_session();
        }
        // Update searchroot in search boxes
        $rt = $wppa_session['search_root'];
        if (!$rt) {
            $rt = '0';
        }
        // must be non-empty string
        wppa_add('src_script', 'jQuery(document).ready(function(){wppaUpdateSearchRoot( \'' . esc_js(wppa_display_root($rt)) . '\', \'' . $rt . '\' )});');
        // If not search forget previous results
        if (!wppa('src')) {
            $wppa_session['use_searchstring'] = '';
            $wppa_session['display_searchstring'] = '';
            wppa_save_session();
            wppa_add('src_script', "\n" . 'jQuery(document).ready(function(){wppaClearSubsearch()});');
        } else {
            // Enable subbox
            wppa_add('src_script', 'jQuery(document).ready(function(){wppaEnableSubsearch()});');
        }
    }
    // Is it url?
    if (wppa('is_url')) {
        if (wppa_photo_exists(wppa('single_photo'))) {
            wppa_out(wppa_get_hires_url(wppa('single_photo')));
        } else {
            wppa_dbg_msg(sprintf('Photo %s not found', wppa('single_photo')), 'red', 'force');
        }
    } elseif (wppa('is_stereobox')) {
        wppa_stereo_box();
    } elseif (wppa('is_searchbox')) {
        wppa_search_box('', wppa('may_sub'), wppa('may_root'));
    } elseif (wppa('is_superviewbox')) {
        wppa_superview_box(wppa('start_album'));
    } elseif (wppa('is_multitagbox')) {
        wppa_multitag_box(wppa('tagcols'), wppa('taglist'));
    } elseif (wppa('is_tagcloudbox')) {
        wppa_tagcloud_box(wppa('taglist'), wppa_opt('tagcloud_min'), wppa_opt('tagcloud_max'));
    } elseif (wppa('is_upload')) {
        wppa_upload_box();
    } elseif (wppa('is_supersearch')) {
        wppa_supersearch_box();
    } elseif (wppa('is_xphoto') == '1') {
        if (wppa('is_autopage')) {
            wppa_auto_page_links('top');
        }
        wppa_smx_photo('x');
        if (wppa('is_autopage')) {
            wppa_auto_page_links('bottom');
        }
    } elseif (wppa('is_mphoto') == '1') {
        if (wppa('is_autopage')) {
            wppa_auto_page_links('top');
        }
        wppa_smx_photo('m');
        if (wppa('is_autopage')) {
            wppa_auto_page_links('bottom');
        }
    } elseif (wppa_page('oneofone')) {
        if (wppa('is_autopage')) {
            wppa_auto_page_links('top');
        }
        wppa_smx_photo('s');
        if (wppa('is_autopage')) {
            wppa_auto_page_links('bottom');
        }
    } elseif (wppa('is_calendar')) {
        wppa_calendar_box();
    } else {
        if (function_exists('wppa_theme')) {
            if (wppa('is_autopage')) {
                wppa_auto_page_links('top');
            }
            wppa_theme();
            // Call the theme module
            if (wppa('is_autopage')) {
                wppa_auto_page_links('bottom');
            }
        } else {
            wppa_out('<span style="color:red">ERROR: Missing function wppa_theme(), check the installation of WPPA+. Remove customized wppa_theme.php</span>');
        }
        global $wppa_version;
        $expected_version = '6-4-17-002';
        if ($wppa_version != $expected_version) {
            wppa_dbg_msg('WARNING: customized wppa-theme.php is out of rev. Expected version: ' . $expected_version . ' found: ' . $wppa_version, 'red');
        }
    }
    // Done
    $out = str_replace('w#location', wppa('geo'), wppa('out'));
    // Reset
    wppa_reset_occurrance();
    return $out;
}
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;
}