The code in your post 147 puts the appropriate filename into $menubg.

The code in post 149 would replace that, building the filename a bit at a time.
First the login-or-not part, then a part denoting the page, category, ez-page, etc. and the file extension (.gif) is built and added to the first part.
It needs another tweak to replace the default logo filename
PHP Code:
<?php if ($_SESSION['customer_id']) { 
                        
                        
$menubg 'topmenubg_';
                        
              } else {
                if (
STORE_STATUS == '0') {
                    
                    
$menubg 'topmenubg_login_';
                    
        } } 
?>

<?php //Smart Backgrounds - for foreground logo - 20081212
$smart_image '';
$unique_ezpages true;//change to false to not have diff bg for each ezpage
if ($current_page_base == 'index' or $current_page_base == 'product_info') { //change filename to $menubg and top cat id only if cat image exists
  
$smart_image = (file_exists(DIR_WS_TEMPLATE_IMAGES $menubg str_replace(strstr($_GET[cPath],'_'),'',$_GET[cPath]) . '.gif'))?$menubg str_replace(strstr($_GET[cPath],'_'),'',$_GET[cPath] . '.gif'):'.gif';
} elseif (
$current_page_base == 'page' and $unique_ezpages == true) { //change filename to $menubg_page and ez-page id only if ez-page image exists
  
$smart_image = (file_exists(DIR_WS_TEMPLATE_IMAGES $menubg 'page' $_GET[id] . '.gif'))?$menubg 'page' $_GET[id] . '.gif':'.gif';
} else { 
//change filename to $menubg and pagename only if  page image exists
  
$smart_image = (file_exists(DIR_WS_TEMPLATE_IMAGES $menubg $current_page_base '.gif'))?$menubg $current_page_base '.gif':'.gif'//default/home page image will be as defined ($menubg.gif)
}// /Smart Backgrounds

$menubg .= $smart_image;?>
You would then do whatever you want with the resulting filename.

The image files would be named according to a system:

- for the default cases:
topmenubg_.gif
topmenubg_login_.gif

- for category or product pages:
topmenubg_3_25.gif
topmenubg_login_3_25.gif

- for ez-pages:
topmenubg_page23.gif
topmenubg_login_page23.gif

- for other pages:
topmenubg_pagename.gif
topmenubg_login_pagename.gif