<?php
include_once("logged_in.php");
$lastID = $_GET['lastID'];
$last_sortID = $_GET['last_sortID'];
if ($last_sortID=="") {
	$last_sortID = 0;
}
$show_ID = $_COOKIE['show_ID'];
$loc_ID = $_COOKIE['loc_ID'];
$screen_size = $_COOKIE['screen_size'];
$gamehost = $_SERVER['HTTP_HOST'];	
$last_ID = $_COOKIE['last_ID'];
if ($last_sortID==0) {
	$query = "select adID, sortID, imgurl, adtext, adtype, refresh from adspace 
				where show_ID = ".$show_ID." and loc_ID = ".$loc_ID." and '".$game_date."' between start_date and end_date and sortID > ".$last_sortID." limit 1";
}
else {
	$query = "select adID, sortID, imgurl, adtext, adtype, refresh from adspace 
				where show_ID = ".$show_ID." and loc_ID = ".$loc_ID." and '".$game_date."' between start_date and end_date and adID > ".$last_ID." limit 1";	
}
if ($gamehost=="localhost") { $result = mysqli_query($my_conn_local, $query); } else { $result = mysqli_query($my_conn, $query); }
$num = mysqli_num_rows($result);
$row = mysqli_fetch_assoc($result);
if ($num > 0) {
	$adID = $row['adID'];
	$sortID = $row['sortID'];
	$imgurl = $row['imgurl'];
	$imgurl = str_replace("game/", "", $imgurl);
	$adtext = $row['adtext'];
	$adtype = $row['adtype'];
	$refresh = $row['refresh'];
	if ($adtype=="image") {
		if ($gamehost=="localhost") {
			$url =  $_SERVER['HTTP_HOST'];
		}
		else {
			$url =  $_SERVER['HTTP_HOST']."/game";
		}
		
		$adspace = "<img src='http://".$url."/".$imgurl."' width='500' height='500'>";	
	}
	elseif ($adtype="text") {
	    $adspace = $adtext;
	}
	//set cookie for adID w/o loc, show
	$expire = time() + 60*10;	
	if ($gamehost=="www.triviocity.com") {
		$chost = "triviocity.com";
	}
	setcookie("last_ID", $adID, $expire, "/", $chost);	
}
elseif ($num==0) {
	header('Location: adspace.php');	
}

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="<?php echo $refresh; ?>; url=adspace.php?lastID=<?php echo $adID; ?>&last_sortID=<?php echo $sortID; ?>">
<title>Adspace</title>
<style type="text/css">
body {
	margin: 0px;	
	font-family: Arial, Helvetica, sans-serif;
}
.container {
	width: 500px;
	height: 500px;
}
.adtext {
	padding: 10px;
	color: #FFF;
	font-size: 48px;
	font-weight: bold;
}

.adtext_small {
	padding: 10px;
	color: #FFF;
	font-size: 38px;
	font-weight: bold;
}
</style>
</head>

<body>
<div class="container">
<?php 
if ($adtype=="text") {
?>
<table width="100%" cellpadding=0 cellspacing=0 border=0>
  <tr>
    <td width="500" valign="middle" align="center" height="500" class="adtext"><?php echo $adspace; ?></td>
  </tr>
</table>
<?php
}
else {
	echo $adspace;
}
?>
</div>
</body>
</html>