<?
	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();
?>
<?
	$query = "
		SELECT 
			t1.* 
		FROM 
			advert AS t1 
		WHERE 
			(SELECT status FROM advert_page_link WHERE page_id=(SELECT id FROM page WHERE name='".$_SESSION['page']."') AND advert_id=t1.id)=1 AND 
			t1.position='".$_GET['position']."' AND 
			t1.status='enabled' 
		ORDER BY 
			t1.sequence ASC";
	//fb($query);
	$mysqldb1->query($query);
	while ($advert = $mysqldb1->fetchArray()) {
		?>
        <div class="column_module">
		<?
		$target = "_blank";
		if (strstr($advert['url'], $domain)) { $target = "_self"; }
		?>
        <? if ($advert['show_name']) { ?>
		<? if ($advert['url'] != '') { ?>
        <? 
			if ($webceo_id != '') {
				if (!strstr($advert['url'], 'mailto')) { 
					$advert['url'] = 'http://track.websiteceo.com/r/'.$webceo_id.'/'.str_replace('http://', '', $advert['url']);
				}
			}
		?>
        <div class="column_module_heading"><a href="<? print $advert['url']; ?>" target="<? print $target; ?>"><? print $advert['name']; ?></a></div>
        <? } else { ?>
        <div class="column_module_heading"><? print $advert['name']; ?></div>
        <? } ?>
		<? } ?>
        <div class="column_module_body">
		<?
		$query = "
		SELECT 
			t1.* 
		FROM 
			file AS t1
		WHERE 
			t1.status='enabled' AND 
			t1.record_id=".$advert['id']." AND 
			t1.source='advert' AND
			t1.type='image' 
		ORDER BY 
			t1.sequence ASC";
		//fb($query);
		$mysqldb2->query($query);
		while ($image = $mysqldb2->fetchArray()) {
			?>
            <? if ($advert['url'] != '') { ?>
            <div class="image"><a href="<? print $advert['url']; ?>" target="<? print $target; ?>"><img src="/<? print $smallDIR.$image['name']; ?>" alt="<? print $image['name']; ?>" title="<? print $image['description']; ?>" border="0"/></a></div>
            <? } else { ?>
            <div class="image"><img src="/<? print $smallDIR.$image['name']; ?>" alt="<? print $image['name']; ?>" title="<? print $image['description']; ?>"/></div>
            <? } ?>
        	<?
       	}
    	?>
       	<? if ($advert['content'] != '') { ?><? print str_replace('../..', 'http://'.$domain, html_entity_decode($advert['content'])); ?><? } ?>
        </div>
        </div>
	<?
	}
?>
<?
	$mysqldb1->close();
	$mysqldb2->close();
?>