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

	// 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();
	$mysqldb3 = new mysql($host, $user, $pswd, $db); $mysqldb3->connect(); $mysqldb3->select();
	$mysqldb4 = new mysql($host, $user, $pswd, $db); $mysqldb4->connect(); $mysqldb4->select();
?>
<?
switch ($_GET['position']) {
case 'top':
?>
<ul class="sf-menu">
<?
$query = "
SELECT
	DISTINCT t1.page,
	(SELECT COUNT(id) FROM page WHERE page=t1.page AND hidden=0 AND status='enabled') AS subpages
FROM
	page AS t1
WHERE
	t1.hidden=0 AND
	t1.status='enabled'
ORDER BY
	t1.page DESC";
//fb('distinct page: '.$query);
$mysqldb1->query($query);
while ($menu = $mysqldb1->fetchArray()) {
	if ($menu['subpages'] == 1) { // if there is only one page and no subpages
		$query = "SELECT * FROM page AS t1 WHERE t1.page='".mysql_real_escape_string($menu['page'])."' AND t1.hidden=0 AND t1.status='enabled' "; $mysqldb3->query($query);
		//fb('page: '.$query);
		$page = $mysqldb3->fetchArray();
		$href = '/page/'.$page['name']; $target = '_self';
		if ($page['url'] != '') {
			$href = $page['url'];
			if (!strstr($page['url'], $domain)) { $target = "_blank"; }
		}
		switch ($page['name']) {
		case 'listings':
		?>
		<li class="sf-with-ul"><a href="#"><? print $menu['page']; ?></a>
			<ul>
				<?
				$query = "
				SELECT
					t1.*,
					(SELECT COUNT(listing_subcategory_id) FROM listing WHERE listing_category_id=t1.id AND listing_subcategory_id!=0 AND status='enabled') AS listing_subcategories
				FROM
					listing_category AS t1
				WHERE
					(SELECT COUNT(listing_category_id) FROM listing WHERE listing_category_id=t1.id AND status='enabled') > 0 AND
					t1.status='enabled'
				ORDER BY
					t1.sequence ASC, t1.id DESC";

				$mysqldb2->query($query);
				//fb($query);
				while ($listing_category = $mysqldb2->fetchArray()) {
				if ($listing_category['listing_subcategories'] == 0) {
				$href = '/listings/'.urlencode($listing_category['link_name']); $target = '_self';
				?>
				<li><a href="<? print $href; ?>" target="<? print $target; ?>"><? print $listing_category['name']; ?></a></li>
				<?
				} else {
				?>
				<li class="sf-with-ul"><a href="#"><? print $listing_category['name']; ?></a>
					<ul>
						<?
						$query = "
						SELECT
							t1.*,
							(SELECT COUNT(listing_subsubcategory_id) FROM listing WHERE listing_subcategory_id=t1.id AND listing_category_id=t1.listing_category_id AND listing_subsubcategory_id!=0 AND status='enabled') AS listing_subsubcategories
						FROM
							listing_subcategory AS t1
						WHERE
							(SELECT COUNT(listing_subcategory_id) FROM listing WHERE listing_category_id=".$listing_category['id']." AND listing_subcategory_id=t1.id AND status='enabled') > 0 AND
							t1.listing_category_id=".$listing_category['id']." AND
							t1.status='enabled'
						ORDER BY
							t1.sequence ASC, t1.id DESC";

						$mysqldb3->query($query);
						//fb($query);
						while ($listing_subcategory = $mysqldb3->fetchArray()) {
                        if ($listing_subcategory['listing_subsubcategories'] == 0) {
                        $href = '/listings/'.urlencode($listing_category['link_name']).'/'.urlencode($listing_subcategory['link_name']); $target = '_self';
                        ?>
                        <li><a href="<? print $href; ?>" target="<? print $target; ?>"><? print $listing_subcategory['name']; ?></a></li>
                        <?
                        } else {
                        ?>
                        <li class="sf-with-ul"><a href="#"><? print $listing_subcategory['name']; ?></a>
							<ul>
                            <?
							$query = "
							SELECT
								t1.*
							FROM
								listing_subsubcategory AS t1
							WHERE
								(SELECT COUNT(listing_subsubcategory_id) FROM listing WHERE listing_category_id=".$listing_category['id']." AND listing_subcategory_id=".$listing_subcategory['id']." AND listing_subsubcategory_id=t1.id AND status='enabled') > 0 AND
								t1.listing_category_id=".$listing_category['id']." AND
								t1.listing_subcategory_id=".$listing_subcategory['id']." AND
								t1.status='enabled'
							ORDER BY
								t1.sequence ASC, t1.id DESC";

							$mysqldb4->query($query);
							//fb($query);
							while ($listing_subsubcategory = $mysqldb4->fetchArray()) {
							$href = '/listings/'.urlencode($listing_category['link_name']).'/'.urlencode($listing_subcategory['link_name']).'/'.urlencode($listing_subsubcategory['link_name']); $target = '_self';
							?>
							<li><a href="<? print $href; ?>" target="<? print $target; ?>"><? print $listing_subsubcategory['name']; ?></a></li>
							<?
							}
							?>
                            </ul>
                        </li>
						<?
						}
						}
						?>
					</ul>
				</li>
				<?
				}
				}
				?>
			</ul>
		</li>
		<?
		break;
		default:
		?>
	<li><a href="<? print $href; ?>" target="<? print $target; ?>"><? print $menu['page']; ?></a></li>
		<?
		}
	} else {
	?>
    <li class="sf-with-ul"><a href="#"><? print $menu['page']; ?></a>
    	<ul>
        <?
		$query = "
		SELECT
			DISTINCT t1.subpage,
			(SELECT COUNT(id) FROM page WHERE page='".mysql_real_escape_string($menu['page'])."' AND subpage=t1.subpage AND hidden=0 AND status='enabled') AS subsubpages
		FROM
			page AS t1
		WHERE
			t1.page='".mysql_real_escape_string($menu['page'])."' AND
			t1.hidden=0 AND
			t1.status='enabled'
		ORDER BY
			t1.subpage DESC";
		//fb('distinct subpage: '.$query);
		$mysqldb2->query($query);
		while ($submenu = $mysqldb2->fetchArray()) {
			//fb('subsubpages: '.$submenu['subsubpages']);
			if ($submenu['subsubpages'] == 1) { // if there is only one subpage and no subsubpages
				$query = "SELECT * FROM page AS t1 WHERE t1.page='".mysql_real_escape_string($menu['page'])."' AND t1.subpage='".mysql_real_escape_string($submenu['subpage'])."' AND t1.hidden=0 AND t1.status='enabled' "; $mysqldb3->query($query);
				//fb('subpage: '.$query);
				$page = $mysqldb3->fetchArray();
				$href = '/page/'.$page['name']; $target = '_self';
				if ($page['url'] != '') {
					$href = $page['url'];
					if (!strstr($page['url'], $domain)) { $target = "_blank"; }
				}
				switch ($page['name']) {
				default:
				?>
				<li><a href="<? print $href; ?>" target="<? print $target; ?>"><? print $submenu['subpage']; ?></a></li>
				<?
				}
			} else {
			?>
			<li class="sf-with-ul"><a href="#"><? print $submenu['subpage']; ?></a>
				<ul>
				<?
                $query = "
                SELECT
                    t1.*
                FROM
                    page AS t1
                WHERE
                    t1.page='".mysql_real_escape_string($menu['page'])."' AND
					t1.subpage='".mysql_real_escape_string($submenu['subpage'])."' AND
                    t1.hidden=0 AND
                    t1.status='enabled'
                ORDER BY
                    t1.sequence ASC, t1.id DESC";
				//fb('subsubpage: '.$query);
                $mysqldb4->query($query);
                while ($subsubmenu = $mysqldb4->fetchArray()) {
                    $href = '/page/'.$subsubmenu['name']; $target = '_self';
					if ($subsubmenu['url'] != '') {
						$href = $subsubmenu['url'];
						if (!strstr($subsubmenu['url'], $domain)) { $target = "_blank"; }
					}
                    ?>
                    <li><a href="<? print $href; ?>" target="<? print $target; ?>"><? print $subsubmenu['subsubpage']; ?></a></li>
                    <?
                }
                ?>
                </ul>
            </li>
        	<?
        	}
        }
        ?>
        </ul>
    </li>
    <?
	}
}
?>
</ul>
<?
break;
case 'left':
?>
<ul class="sf-menu sf-vertical">
<?
$query = "
SELECT
	DISTINCT t1.page,
	(SELECT COUNT(id) FROM page WHERE page=t1.page AND hidden=0 AND status='enabled') AS subpages
FROM
	page AS t1
WHERE
	t1.hidden=0 AND
	t1.status='enabled'
ORDER BY
	t1.page DESC";
//fb('distinct page: '.$query);
$mysqldb1->query($query);
while ($menu = $mysqldb1->fetchArray()) {
	if ($menu['subpages'] == 1) { // if there is only one page and no subpages
		$query = "SELECT * FROM page AS t1 WHERE t1.page='".mysql_real_escape_string($menu['page'])."' AND t1.hidden=0 AND t1.status='enabled' "; $mysqldb3->query($query);
		//fb('page: '.$query);
		$page = $mysqldb3->fetchArray();
		$href = '/page/'.$page['name']; $target = '_self';
		if ($page['url'] != '') {
			$href = $page['url'];
			if (!strstr($page['url'], $domain)) { $target = "_blank"; }
		}
		switch ($page['name']) {
		case 'listings':
		?>
		<!--<li class="sf-with-ul"><a href="#"><? print $menu['page']; ?></a>
			<ul>-->
				<?
				$query = "
				SELECT
					t1.*,
					(SELECT COUNT(listing_subcategory_id) FROM listing WHERE listing_category_id=t1.id AND listing_subcategory_id!=0 AND status='enabled') AS listing_subcategories
				FROM
					listing_category AS t1
				WHERE
					(SELECT COUNT(listing_category_id) FROM listing WHERE listing_category_id=t1.id AND status='enabled') > 0 AND
					t1.status='enabled'
				ORDER BY
					t1.sequence ASC, t1.id DESC";

				$mysqldb2->query($query);
				//fb($query);
				while ($listing_category = $mysqldb2->fetchArray()) {
				if ($listing_category['listing_subcategories'] == 0) {
				$href = '/listings/'.urlencode($listing_category['link_name']); $target = '_self';
				?>
				<li><a href="<? print $href; ?>" target="<? print $target; ?>"><? print $listing_category['name']; ?></a></li>
				<?
				} else {
				?>
				<li class="sf-with-ul"><a href="#"><? print $listing_category['name']; ?></a>
					<ul>
						<?
						$query = "
						SELECT
							t1.*,
							(SELECT COUNT(listing_subsubcategory_id) FROM listing WHERE listing_subcategory_id=t1.id AND listing_category_id=t1.listing_category_id AND listing_subsubcategory_id!=0 AND status='enabled') AS listing_subsubcategories
						FROM
							listing_subcategory AS t1
						WHERE
							(SELECT COUNT(listing_subcategory_id) FROM listing WHERE listing_category_id=".$listing_category['id']." AND listing_subcategory_id=t1.id AND status='enabled') > 0 AND
							t1.listing_category_id=".$listing_category['id']." AND
							t1.status='enabled'
						ORDER BY
							t1.sequence ASC, t1.id DESC";

						$mysqldb3->query($query);
						//fb($query);
						while ($listing_subcategory = $mysqldb3->fetchArray()) {
                        if ($listing_subcategory['listing_subsubcategories'] == 0) {
                        $href = '/listings/'.urlencode($listing_category['link_name']).'/'.urlencode($listing_subcategory['link_name']); $target = '_self';
                        ?>
                        <li><a href="<? print $href; ?>" target="<? print $target; ?>"><? print $listing_subcategory['name']; ?></a></li>
                        <?
                        } else {
                        ?>
                        <li class="sf-with-ul"><a href="#"><? print $listing_subcategory['name']; ?></a>
							<ul>
                            <?
							$query = "
							SELECT
								t1.*
							FROM
								listing_subsubcategory AS t1
							WHERE
								(SELECT COUNT(listing_subsubcategory_id) FROM listing WHERE listing_category_id=".$listing_category['id']." AND listing_subcategory_id=".$listing_subcategory['id']." AND listing_subsubcategory_id=t1.id AND status='enabled') > 0 AND
								t1.listing_category_id=".$listing_category['id']." AND
								t1.listing_subcategory_id=".$listing_subcategory['id']." AND
								t1.status='enabled'
							ORDER BY
								t1.sequence ASC, t1.id DESC";

							$mysqldb4->query($query);
							//fb($query);
							while ($listing_subsubcategory = $mysqldb4->fetchArray()) {
							$href = '/listings/'.urlencode($listing_category['link_name']).'/'.urlencode($listing_subcategory['link_name']).'/'.urlencode($listing_subsubcategory['link_name']); $target = '_self';
							?>
							<li><a href="<? print $href; ?>" target="<? print $target; ?>"><? print $listing_subsubcategory['name']; ?></a></li>
							<?
							}
							?>
                            </ul>
                        </li>
						<?
						}
						}
						?>
					</ul>
				</li>
				<?
				}
				}
				?>
			<!--</ul>
		</li>-->
		<?
		break;
		default:
		?>
	<li><a href="<? print $href; ?>" target="<? print $target; ?>"><? print $menu['page']; ?></a></li>
		<?
		}
	} else {
	?>
    <li class="sf-with-ul"><a href="#"><? print $menu['page']; ?></a>
    	<ul>
        <?
		$query = "
		SELECT
			DISTINCT t1.subpage,
			(SELECT COUNT(id) FROM page WHERE page='".mysql_real_escape_string($menu['page'])."' AND subpage=t1.subpage AND hidden=0 AND status='enabled') AS subsubpages
		FROM
			page AS t1
		WHERE
			t1.page='".mysql_real_escape_string($menu['page'])."' AND
			t1.hidden=0 AND
			t1.status='enabled'
		ORDER BY
			t1.subpage DESC";
		//fb('distinct subpage: '.$query);
		$mysqldb2->query($query);
		while ($submenu = $mysqldb2->fetchArray()) {
			//fb('subsubpages: '.$submenu['subsubpages']);
			if ($submenu['subsubpages'] == 1) { // if there is only one subpage and no subsubpages
				$query = "SELECT * FROM page AS t1 WHERE t1.page='".mysql_real_escape_string($menu['page'])."' AND t1.subpage='".mysql_real_escape_string($submenu['subpage'])."' AND t1.hidden=0 AND t1.status='enabled' "; $mysqldb3->query($query);
				fb('subpage: '.$query);
				$page = $mysqldb3->fetchArray();
				$href = '/page/'.$page['name']; $target = '_self';
				if ($page['url'] != '') {
					$href = $page['url'];
					if (!strstr($page['url'], $domain)) { $target = "_blank"; }
				}
				switch ($page['name']) {
				default:
				?>
			<li><a href="<? print $href; ?>" target="<? print $target; ?>"><? print $submenu['subpage']; ?></a></li>
				<?
				}
			} else {
			?>
			<li class="sf-with-ul"><a href="#"><? print $submenu['subpage']; ?></a>
				<ul>
				<?
                $query = "
                SELECT
                    t1.*
                FROM
                    page AS t1
                WHERE
                    t1.page='".mysql_real_escape_string($menu['page'])."' AND
					t1.subpage='".mysql_real_escape_string($submenu['subpage'])."' AND
                    t1.hidden=0 AND
                    t1.status='enabled'
                ORDER BY
                    t1.sequence ASC, t1.id DESC";
				//fb('subsubpage: '.$query);
                $mysqldb4->query($query);
                while ($subsubmenu = $mysqldb4->fetchArray()) {
                    $href = '/page/'.$subsubmenu['name']; $target = '_self';
                    if ($subsubmenu['url'] != '') {
						$href = $subsubmenu['url'];
						if (!strstr($subsubmenu['url'], $domain)) { $target = "_blank"; }
					}
                    ?>
                    <li><a href="<? print $href; ?>" target="<? print $target; ?>"><? print $subsubmenu['subsubpage']; ?></a></li>
                    <?
                }
                ?>
                </ul>
            </li>
        	<?
        	}
        }
        ?>
        </ul>
    </li>
    <?
	}
}
?>
</ul>
<?
break;
default:
?>
<?
break;
}
?>
<?
	$mysqldb1->close();
	$mysqldb2->close();
	$mysqldb3->close();
	$mysqldb4->close();
?>