<?php include($setting['sitepath'].'/includes/pagination.class.php'); ?>
<div id="left">
<h1><div align="center">Favorite Games</div></h1><div id="category_wrap">
<?php
if(isset($_SESSION["user"])) { 
	$user=$_SESSION["user"]; 
	$userid = $_SESSION["userid"];
	if (isset($_POST['favourite'])) {
		yasDB_delete("DELETE FROM favourite WHERE userid='$userid' AND gameid = '{$_POST['gamedelete']}'") or die("Error deleting game from favorites");
	} 
	$result = yasDB_select("SELECT gameid FROM favourite WHERE userid='{$_SESSION['userid']}'",false);
	$numrows = $result->num_rows;
	$result->close();
	$pageurl = new pagination($numrows, $setting['seo'], 'favourites', 1, 3);
	$query = yasDB_select("SELECT * FROM favourite WHERE userid= '$userid' LIMIT " . $pageurl->start . ", " . $pageurl->limit);
	while ($row = $query->fetch_array(MYSQLI_ASSOC)) {
		$games = yasDB_select("SELECT * FROM games WHERE id = '{$row['gameid']}'");
		$games = $games->fetch_array(MYSQLI_ASSOC);
		$gameurl = prepGame($games['file']);
		$games['description'] = stripslashes($games['description']);
		if(strlen($games['description']) > 75){
			$games['description'] = substr($games['description'], 0, 75)."...";
		}
		else {
			$games['description'] = $games['description'];
		} 
		$pp = isset($_GET['page']) ? $_GET['page'] : '1';
		if (file_exists($games['thumbnail'])) {
			$thumbnail = $games['thumbnail'];
			$thumbnail = urldecode($thumbnail);
		} else {
			$thumbnail = $setting['siteurl'].'templates/'.$setting['theme'].'/skins/'.$setting['skin'].'/images/nopic.jpg';
		}
		$pic_settings = array('w'=>80,'h'=>80);
		if ($setting['seo'] == 'yes') {
           echo '<div class="category_box"><div class="gamepic">
                 <a href="' . $setting['siteurl'] . 'game/' . $games['id'] . '/' . $gameurl . '.html' . '">
                 <img align="absmiddle" src="' . $setting['siteurl'] . '' . $games['thumbnail'] . '" alt="' . $games['title'] . 'width="80" height="80"" /></div><div class="game_text"><strong>' . 
				 $games['title'] . '</strong></a><div>' . $games['description'] . '</div></div>
                 <div class="game_stats"><strong>Plays:</strong>' . $games['plays'] . '&nbsp;&nbsp;<form method="post" action="">
				 <input type="hidden" name="userdelete" value="' . $userid . '"  />
				 <input type="hidden" name="gamedelete" value="' . $games['id'] .'" />
				 <input type="submit" name="favourite" value="Remove" onclick="deleteFavorite(\''.$games['id'].'\', \''.addslashes($games['title']).'\', \''.$pp.'\');return false"/>
				 </form></div><br style="clear: both"/></div>';  
        }
		else {
            echo '<div class="category_box"><div class="gamepic">
                  <a href="' . $setting['siteurl'] . 'index.php?act=game&id=' . $games['id'] . '">
                  <img align="absmiddle" src="' . $setting['siteurl'] . '' . $games['thumbnail'] . '"  alt="' . $games['title'] . '" width="80" height="80"" /></div><div class="game_text"><strong>' . 
				  $games['title'] . '</strong></a><div>' . $games['description'] . '</div></div>
                  <div class="game_stats"><strong>Plays:</strong>' . $games['plays'] . '&nbsp;&nbsp;<form method="post" action="">
				  <input type="hidden" name="userdelete" value="' . $userid . '"  />
				  <input type="hidden" name="gamedelete" value="' . $games['id'] .'" />
				  <input type="submit" name="favourite" value="Remove"  onclick="deleteFavorite(\''.$games['id'].'\', \''.addslashes($games['title']).'\', \''.$pp.'\');return false"/>
				  </form></div><br style="clear: both"/></div>';  
    	}
	
	}
	if ($query->num_rows == 0) {
		echo '<center>There are no games in your Favorites.</center>';
	}
	if ($numrows > 0) {
		?>
		<br/><div class="catpages">
		<?php
		echo $pageurl->showPagination();
		?></div><?php
	}
}
?>
<div class="clear"></div>
</div>