<?
	require_once('control/include/user.php');
	require_once('control/include/mysql.php');
	require_once('control/include/functions.php');
	require_once('control/include/fb.php');
	
	ob_start();
	
	$columns = 5;
	
	// SETUP MYSQL HANDLERS
	$mysqldb1 = new mysql($host, $user, $pswd, $db); $mysqldb1->connect();
	
	$query = "
	SELECT 
		t1.*, 
		(SELECT name FROM file WHERE status='enabled' AND record_id=t1.id AND source='listing' AND type='image' ORDER BY sequence ASC LIMIT 1) AS image 
	FROM 
		listing AS t1 
	WHERE 
		t1.status='enabled' AND 
		(SELECT COUNT(id) FROM file WHERE status='enabled' AND record_id=t1.id AND source='listing' AND type='image') > 0 
	ORDER BY 
		t1.sequence ASC";
	if ($_GET['link_name'] != '') {
		$query = "
		SELECT 
			t1.* 
		FROM 
			file AS t1
		WHERE 
			t1.status='enabled' AND 
			t1.record_id=(SELECT id FROM listing WHERE link_name='".$_GET['link_name']."' LIMIT 1) AND 
			t1.source='listing' AND
			t1.type='image' 
		ORDER BY 
			t1.sequence ASC";
	}
	fb($query);
	$counter = 0;
	$image_counter = 0;
	$mysqldb1->select();
	$mysqldb1->query($query);
	$albums = $mysqldb1->affectedRows();
	
	if ($_GET['link_name'] != '') {
	?>
		<!--<p><a href="javascript:fetchAllPhotos();"><strong>GO BACK</strong></a></p>-->
		<table width="100%" border="0" cellpadding="0" cellspacing="5">
		<tr>
	<?
	} else {
	?>
    	<!--<p>Click on a thumbnail to open an album.</p>-->
		<table width="100%" border="0" cellpadding="0" cellspacing="5">
		<tr>
	<?
	} 
	
	while ($listing = $mysqldb1->fetchArray()) {
		
		$counter++;
        if ($counter % 2 != 0) { $bgcolor = ''; } else { $bgcolor = ''; }
		
		// ALBUM
		if ($_GET['link_name'] != '') {
		
		$title = '';
		if ($listing['description'] != '') {
			$title = $listing['description'];
		}
		
		list($image_width, $image_height, $image_type, $image_attr) = getimagesize($tinyDIR.$listing['name']);
		?>
        <td align="center" valign="middle" class="photo">
        	
            
            <p style="text-align:center; width:<? print $tiny_image_size ?>px;"><? if ($title != '') { ?><? print $title; ?><? } ?></p>
        	<p><a href="/<? print $largeDIR.$listing['name']; ?>" class="popup" rel="shadowbox[<? print $_GET['link_name']; ?>]" title="<? print $title; ?>"><img src="/<? print $tinyDIR.$listing['name']; ?>" alt="<? print $listing['name']; ?>" title="<? print $title; ?>" border="0"/></a></p>
            
        </td>
        <? if ($counter % $columns == 0) { ?>
        </tr></tr>
		<? } ?>
		<? 
		// listing
		} else {
		
		list($image_width, $image_height, $image_type, $image_attr) = getimagesize($tinyDIR.$listing['image']);
		
		$title = $listing['name'];
		if ($listing['description'] != '') {
			$title = $listing['name'].' - '.$listing['description'];
		}
		?>
        <td align="center" valign="middle" class="photo">
            
            <p style="text-align:center; width:<? print $tiny_image_size ?>px;"><? if ($listing['name'] != '') { ?><strong><? print $listing['name']; ?></strong><? } ?></p>
        	<p><a href="javascript:fetchAlbum(<? print $listing['id']; ?>);" title="<? print $title; ?>"><img src="/<? print $tinyDIR.$listing['image']; ?>" alt="<? print $listing['image']; ?>" title="<? print $title; ?>" border="0"/></a></p>
        	<? if ($listing['description'] != '') { ?><p style="text-align:left; width:<? print $tiny_image_size ?>px;"><? print $listing['description']; ?></p><? } ?>
            
        </td>
        <? if ($counter % $columns == 0) { ?>
        </tr></tr>
		<? } ?>
		<?
		}
	} 
?>
		</tr>
		</table>
<?
	$mysqldb1->close();
?>