<?
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();
session_start();

$columns = 3;

// SETUP MYSQL HANDLERS
$mysqldb1 = new mysql($host, $user, $pswd, $db); $mysqldb1->connect(); $mysqldb1->select();
$mysqldb2 = new mysql($host, $user, $pswd, $db); $mysqldb2->connect(); $mysqldb2->select();

$filterSQL = "";
if ($_GET['gallery_category_id'] == '')  {
    $query = "
    SELECT
        t1.*
    FROM
        gallery_category AS t1
    WHERE
        t1.name!='' AND
        (SELECT COUNT(id) FROM gallery WHERE status='enabled' AND gallery_category_id=t1.id) > 0 AND
        t1.status='enabled'
    ORDER BY
        t1.sequence ASC,
        t1.name ASC
    LIMIT 1";
    $mysqldb1->query($query);
    $gallery_category = $mysqldb1->fetchArray();
    $gallery_category_name = $gallery_category['name'];
    $gallery_category_id = $gallery_category['id'];
    $filterSQL = "t1.gallery_category_id=".$gallery_category['id']." AND ";
} else {
    $query = "
    SELECT
        t1.*
    FROM
        gallery_category AS t1
    WHERE
        t1.id=".$_GET['gallery_category_id'];
    $mysqldb1->query($query);
    $gallery_category = $mysqldb1->fetchArray();
    $gallery_category_name = $gallery_category['name'];
    $gallery_category_id = $_GET['gallery_category_id'];
    $filterSQL = "t1.gallery_category_id=".$_GET['gallery_category_id']." AND ";
}
?>
<?
$categories = "";
$query = "
SELECT
    t1.*
FROM
    gallery_category AS t1
WHERE
    t1.name!='' AND
    (SELECT COUNT(id) FROM gallery WHERE status='enabled' AND gallery_category_id=t1.id) > 0 AND
    t1.status='enabled'
ORDER BY
    t1.sequence ASC,
    t1.name ASC";
//fb($query);
$mysqldb2->query($query);
while ($gallery_category = $mysqldb2->fetchArray()) {
    $gallery_categories .= '<a href="javascript:filterGallery('.$gallery_category['id'].');" target="_self">'.$gallery_category['name'].'</a> | ';
}
?>
<p><strong>Choose a category:</strong></p>
<p><? print rtrim($gallery_categories, ' | '); ?></p>
<?
$gallery_header = $gallery_category_name;
if ($_GET['gallery_id'] != '') {
    $query = "
    SELECT
        t1.*
    FROM
        gallery AS t1
    WHERE
        t1.id=".$_GET['gallery_id'];
    $mysqldb1->query($query);
    $gallery = $mysqldb1->fetchArray();
    $gallery_header = $gallery_category_name.' - '.$gallery['name'];
}
?>
<p class="content_header"><? print $gallery_header; ?></p>
<?
$query = "
SELECT 
    t1.*,
    (SELECT name FROM file WHERE status='enabled' AND record_id=t1.id AND source='gallery' AND type='image' ORDER BY sequence ASC LIMIT 1) AS image
FROM 
    gallery AS t1
WHERE
    ".$filterSQL."
    t1.status='enabled' AND
    (SELECT COUNT(id) FROM file WHERE status='enabled' AND record_id=t1.id AND source='gallery' AND type='image') > 0
ORDER BY 
    t1.sequence ASC";
if ($_GET['gallery_id'] != '') {
    $query = "
    SELECT
        t1.*
    FROM
        file AS t1
    WHERE
        t1.status='enabled' AND
        t1.record_id=".$_GET['gallery_id']." AND
        t1.source='gallery' AND
        t1.type='image'
    ORDER BY
        t1.sequence ASC";
}
fb($query);
$counter = 0;
$image_counter = 0;
$mysqldb1->query($query);
$albums = $mysqldb1->affectedRows();
?>
    <?
	if ($_GET['gallery_id'] != '') {
	?>
		<!-- <p><a href="javascript:fetchGallery();"><img src="/website/styles/default/back_large.png" alt="back" width="80" height="30" border="0" /></a></p> -->
        <table width="100%" border="0" cellpadding="0" cellspacing="5">
		<tr>
	<?
	} else {
	?>
		<table width="100%" border="0" cellpadding="0" cellspacing="5">
		<tr>
	<?
	} 
	
	while ($gallery = $mysqldb1->fetchArray()) {
		
		$counter++;
        if ($counter % 2 != 0) { $bgcolor = ''; } else { $bgcolor = ''; }
		
		// ALBUM
		if ($_GET['gallery_id'] != '') {
		
		$title = '';
		if ($gallery['description'] != '') {
			$title = $gallery['description'];
		}
		
		list($image_width, $image_height, $image_type, $image_attr) = getimagesize($smallDIR.$gallery['name']);
		?>
        <td align="center" valign="middle" style="background-color:<? print $bgcolor; ?>;" class="gallery_cell">
        	
            
            <p style="text-align:center; width:<? print $small_image_size ?>px;"><? if ($title != '') { ?><? print $title; ?><? } ?></p>
        	<p><a href="/<? print $largeDIR.$gallery['name']; ?>" class="popup" rel="shadowbox[<? print $_GET['gallery_id']; ?>]" title="<? print $title; ?>"><img src="/<? print $smallDIR.$gallery['name']; ?>" alt="<? print $gallery['name']; ?>" title="<? print $title; ?>" border="1"/></a></p>
            
        </td>
        <? if ($counter % $columns == 0) { ?>
        </tr></tr>
		<? } ?>
		<? 
		// GALLERY
		} else {
		
		list($image_width, $image_height, $image_type, $image_attr) = getimagesize($smallDIR.$gallery['image']);
		
		$title = $gallery['name'];
		if ($gallery['description'] != '') {
			$title = $gallery['name'].' - '.$gallery['description'];
		}
		?>
        <td align="center" valign="middle" style="background-color:<? print $bgcolor; ?>;" class="gallery_cell">
            
            <p style="text-align:center; width:<? print $small_image_size ?>px;"><? if ($gallery['name'] != '') { ?><strong><? print $gallery['name']; ?></strong><? } ?></p>
        	<p><a href="javascript:fetchAlbum(<? print $gallery['id']; ?>, <? print $gallery_category_id; ?>);" title="<? print $title; ?>"><img src="/<? print $smallDIR.$gallery['image']; ?>" alt="<? print $gallery['image']; ?>" title="<? print $title; ?>" border="1"/></a></p>
        	<? if ($gallery['description'] != '') { ?><p style="text-align:left; width:<? print $small_image_size ?>px;"><? print $gallery['description']; ?></p><? } ?>
            
        </td>
        <? if ($counter % $columns == 0) { ?>
        </tr></tr>
		<? } ?>
		<?
		}
	} 
?>
		</tr>
		</table>
<?
	$mysqldb1->close();
    $mysqldb2->close();
?>