<?php
	$page = $_GET['page'];
	
	// Initialize arrays
	$pages = array('home', 'tutorials', 'games', 'tools', 'music', 'aboutme', 'links');
	$pageTitles = array('Home', 'Tutorials', 'Games', 'Tools', 'Music', 'About Me', 'Links');
	$numPages = count($pages);
	
	// When first accessing page this happens
	if(!isset($page) || $page == "")
		$page = "home";
	
	// Get page title from page id
	for($i=0; $i<$numPages; $i++)
	{
		if($page == $pages[$i])
		{
			$curPageTitle = $pageTitles[$i];
			break;
		}
	}
	
	// ???
	if(!isset($curPageTitle))
		$curPageTitle = "Unknown";
	
	$remoteIp = $HTTP_SERVER_VARS["REMOTE_ADDR"];
	$userAgent = $HTTP_SERVER_VARS["HTTP_USER_AGENT"];
	
	// Load current state of counter out of file
	$countFile = "textfiles/counter.txt";
	$fp = fopen($countFile, "r");
	$tempArray = fscanf($fp, "%d\n");
	$oldViews = (int)$tempArray[0];
	fclose($fp);
	
	// Get last ip
	$lipFile = "textfiles/lastIp.txt";
	$fp = fopen($lipFile, "r");
	$tempArray = fscanf($fp, "%s\n");
	$lastIp = $tempArray[0];
	fclose($fp);
	
	// You're not the last visitor -> increase count
	if($lastIp != $remoteIp)
	{
		// Save yourself as last visitor
		$fp = fopen($lipFile, "w");
		fwrite($fp, $remoteIp);
		fclose($fp);
		
		// Increase counter
		$oldViews += 1;
		$fp = fopen($countFile, "w");
		fwrite($fp, $oldViews);
		fclose($fp);		
	}
	
	$pageViews = $oldViews;
	
?>
<html>

<head>
	<title>Andr&eacute; Schnabel's Homepage</title>
	
	<!-- Meta-Daten -->
	<meta name="author" content="Andr&eacute; Schnabel">
	<meta name="keywords" content="Andr&eacute; Schnabel, Homepage, Freie Software, Linux Spiele, GPL, Python, SDL, Pygame">
	<meta name="description" content="Andr&eacute; Schnabel's Personal Home Page">
	
	<!-- Stylesheets -->
	<style type="text/css">
	<!--
		table { font-family: Verdana;
				font-size: 9pt;
				border-collapse: collapse; }
		a:link { text-decoration: none; }
		a:visited { text-decoration: none; }
		a:active { text-decoration: none; }
		a:hover { font-weight: bold; }
	-->
	</style>
	
</head>

<body background="images/pattern.png" bgcolor="#ffffff" text="#000000" link="#0290aa" alink="#0290aa" vlink="#007088">

	<table border="0" width="100%" height="100%">
	<tr><td valign="center">

	<div align="center">
	
		<table border="0" cellspacing="0" cellpadding="0" width="80%" height="50%"><tr><td>
	
		<table border="1" bgcolor="#000000" bordercolor="#000000" width="100%" height="100%">
		
			<tr bgcolor="#666666">
			<td><div align="center"><img src="images/logo.png" border=0></div></td>			
			</tr>
		
			<tr><td><div align="center">
				<table width="700" border="0" style="color:#ffffff;">
					<tr>					
					<?php						
						
						for($i=0; $i<$numPages; $i++)
						{
							$link = "<a href=\"index.php?page=".$pages[$i]."\">";
							echo "<td valign=\"center\">".$link;
							echo "<img src=\"images/".$pages[$i].".png\" border=0></a></td>\n";
							echo "<td valign=\"center\">".$link."<font color=\"#ffffff\">";
							
							if($pages[$i] == $page)
								echo "<u>";
							
							echo $pageTitles[$i]; 
							
							if($pages[$i] == $page)
								echo "</u>";
							
							echo "</font></a></td>";
						}
					
					?>
					
					
					<td valign="center">
						<a href="http://electronicviolence.blogspot.com/" target="_blank">
							<img src="images/blog.png" border=0></a></td>
							<td valign="center"><a href="http://electronicviolence.blogspot.com/" target="_blank">
							<font color="#ffffff">
							Blog
							</font>
						</a>
					</td>

					
					</tr>
				</table>
			</div></td></tr>
			<tr><td bgcolor="#ffffff">
				<table border="0" cellpadding="5" width="100%"><tr><td>
			<?php				
				echo "<h2>".$curPageTitle."</h2>";
				
				$isValidPage = false;
				
				for($i=0; $i<$numPages; $i++)
					if($page == $pages[$i])
						$isValidPage = true;
				
				if($isValidPage)
					require("includes/".$page.".inc.php");
				else
					echo "<font color=\"#ff0000\">Unknown page...</font>";
			?> </td></tr></table>
			</td></tr>
		
		</table>
		
		</td><td background="images/shadowRight.png" width="10" valign="top"><img src="images/shadowRU.png" border=0></td></tr>
		<tr height="10"><td background="images/shadowDown.png"><img src="images/shadowLD.png" border=0></td><td background="images/shadowRD.png"></td></tr>
		</table>
		
		<p>
			<font color="#000000">
			<table border="0"><tr><td>
			<font size="-2">
			<?php
				echo "<b>IP-Address:</b> ".$remoteIp."</font></td></tr>";
				echo "<tr><td><font size=\"-2\"><b>Browser:</b> ".$userAgent."</font></td></tr>";
				echo "<tr><td><font size=\"-2\"><b>Page views:</b> ".$pageViews;/*."</font></td></tr>";*/
				//echo "<tr><td><font size=\"-2\"><b>Random quote:</b> ".$curQuote;
			?>
			</font></td></tr></table>
			<p>
			<b>2007 by Andr&eacute; Schnabel<br>
				Iconset by <a href="http://tango.freedesktop.org/Tango_Desktop_Project" target="_blank">
				Tango Desktop Project</a></b>
			</font>
		</p>
	
	</div>
	
	</td></tr></table>

</body>

</html>
