<?php
session_start();

ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);


# $_SESSION['theme'] = isset($_SESSION['theme']) ? $_SESSION['theme'] : "none";
# $_SESSION['source'] = isset($_SESSION['source']) ? $_SESSION['source'] : "python";

$theme = 'none';
$source = 'text';

$themes = array();
// Scan css dir for theme css files
$css_dir = 'css/';
if (is_dir($css_dir)) {
	if ($dh = opendir($css_dir)) {
		while (($file = readdir($dh)) !== false) {
			if(substr($file, -4) != '.css') continue;
			$themes[] = $file;
		}
		closedir($dh);
	}
}

$sources = array();
// Scan css dir for theme css files
$source_dir = 'code/';
if (is_dir($source_dir)) {
	if ($dh = opendir($source_dir)) {
		while (($file = readdir($dh)) !== false) {
			if(substr($file, -5) != '.html') continue;
			$sources[] = $file;
		}
		closedir($dh);
	}
}

$fonts = array('Andale Mono', 'Anonymous', 'BitStream Vera Sans Mono', 'Courier', 'Courier New', 'Dejavu Sans Mono', 'Lucida Sans Typewriter', 'Luxi Mono', 'Monaco', 'MPW', 'ProFont, ProFontX', 'Zapfino');


echo('<?xml version="1.0" encoding="utf-8"?>');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>TextMate Theme Sourcerer</title>
	<link rel="stylesheet" href="master.css.php<?php if(isset($_REQUEST['no-wrapping'])) echo "?no-wrapping"; ?>" type="text/css" title="master" charset="utf-8" media="screen" />
	<style type="text/css">
	/* <![CDATA[ */
		<?php
			foreach($themes as $value) {
				echo('@import url("css/'.$value.'");'."\n");
			}
		?>
		/* ]]> */
	</style>
	<script type="text/javascript" src="scripts/prototype.js"></script>
	<script type="text/javascript" src="scripts/source.js"></script>
</head>

<body>
<div id="content">
	<p class="small" style="font-size: 80%; margin-left: -2em;"><a href="../">Back to Project TextMate</a></p>
	<h1>TextMate Theme Sourcerer</h1>
	<h2>The Source</h2>
	<pre class="textmate-source none" id="code-foo" style="font-family: monospace;"><div id="gutter"></div><?php
		readfile('code/'.$source.'.html');
	?></pre>
	<h2>Options</h2>
	<div class="options">
		<div id="options-right">
			<h3>Bring your own Code</h3>
			<img src="img/generic.png" width="128" height="128" alt="Generic" style="float: left;"/>
			<p>
				Paste code from within TextMate<br />
				(TODO)
			</p>
			<form id="source-upload-form" action="" method="POST">
				<p><textarea name="source-upload-file"  cols="30" rows="4"></textarea>
				<input type="submit" name="source-upload-submit" value="Load it!"  /> </p>
			</form>
		</div>
		<div id="options-left">
			<h3>Source</h3>
			<form id="source-selector-form" action="">
				<div>
					<img src="img/ruby.png" width="48" height="48" alt="Ruby" />
					<img src="img/obj-c.png" width="48" height="48" alt="Obj C" />
					<img src="img/markdown.png" width="48" height="48" alt="Markdown" /><br />
					<select onchange="changeSource(); return false;" id="source-selector" name="source">
						<option value="text">---Plain Text---</option>
						<?php
							foreach($sources as $value) {
								$value = substr($value, 0, -5);
								echo('<option value="'.$value.'">'.ucfirst($value).'</option>');
							}
						?>
					</select>
					Choose sample source
				</div>
			</form>
			<h3>Theme</h3>
			<form id="code-style-form" action="">
				<div>
					<img src="img/TMTheme.png" width="48" height="48" alt="TMTheme" /><br />
					<select onchange="changeTheme(); return false;" id="style-selector" name="theme">
					<option value="none">---None---</option>
						<?php
							foreach($themes as $value) {
								$value = substr($value, 0, -4);
								echo('<option value="'.$value.'">'.ucwords(str_replace('_', ' ', $value)).'</option>');
							}
						?>
					</select>
					Choose a Theme
				</div>
			</form>
			<div id="options-right">
				<h3>Upload your theme</h3>
				<img src="img/theme.png" width="128" height="128" alt="Theme" style="float: left;"/>
				<p>
					Paste your theme in CSS form here<br />
					(TODO)
				</p>
				<form id="theme-upload-form" action="" method="POST">
					<p><textarea name="theme-upload-file"  cols="30" rows="4"></textarea>
					<input type="submit" name="theme-upload-submit" value="Style it!"  /></p>
				</form>
			</div>
			<h3>Font</h3>
			<form id="font-switcher-form" action="">
				<div>
					<select onchange="changeFont(); return false;" id="font-selector" name="font">
						<option value="monospace">---Browser monospaced font---</option>
						<?php
							foreach($fonts as $value) {
								echo('<option value="'.$value.'">'.ucfirst($value).'</option>');
							}
						?>
					</select> Choose a font (You must have them installed locally of course ;)
				</div>
			</form>
			<h3>Additional Options</h3>
			<form id="line-switcher-form" action="">
				<div>
					<input id="line-switcher" type="checkbox" name="Show Line Numbers" checked="checked" onchange="switchLineNumbers();" /> Show Line Numbers
				</div>
			</form>
			<form id="wrap-switcher-form" action="">
				<div>
					<input id="wrap-switcher" type="checkbox" name="Wrapping" checked="checked" onchange="toggleWrapping();" /> Wrapping (TODO) — <a href="?no-wrapping">No-wrap Version</a> (reloads the whole thing)
				</div>
			</form>
			<p>&nbsp;</p>
		</div>
	</div>
	<h2>TODO</h2>
	<div class="options">
		<h3>TODO</h3>

		<ul>
			<li>Credit the theme makers</li>
			<li>Provide download links for the themes</li>
			<li>Make it bookmarkable wrt current settings and let theme authors link to their themes specifically</li>
			<li>Font Text Input Field</li>
			<li>Printing Stylesheet</li>
			<li>User a $_COOKIE to store user settings</li>
			<li>Let users upload themes</li>
			<li>Tick off the TODO items around there ;)</li>
		</ul>
		
	</div>

<hr />
<p class="footer">A combined effort of Soryu, Brad Choate &amp; Jacob Rus</p>
</div>
</body>
</html>