<?xml version="1.0"?>
<!DOCTYPE modification SYSTEM "http://www.simplemachines.org/xml/modification">
<modification xmlns="http://www.simplemachines.org/xml/modification" xmlns:smf="http://www.simplemachines.org/">
	<id>yodaofdarkness:themechanger</id>
	<version>1.0.2</version>

	<file name="$boarddir/index.php">
		<operation>
			<search position="before"><![CDATA[
loadSession();]]></search>
			<add><![CDATA[

// Simple Theme Changer
$request = db_query("
	SELECT *
	FROM {$db_prefix}themes
	WHERE variable = 'name'
		AND ID_MEMBER = 0
		AND ID_THEME IN ({$modSettings['knownThemes']})", __FILE__, __LINE__);

$context['themes'] = array();

while($row = mysql_fetch_assoc($request))
	$context['themes'][$row['ID_THEME']] = $row['value'];

mysql_free_result($request);
// End Simple Theme Changer]]></add>
		</operation>
	</file>

	<file name="$themedir/index.template.php">
		<operation>
			<search position="replace"><![CDATA[
		</table>';

		// Show the load time?]]></search>
			<add><![CDATA[
			<tr>
				<td colspan="3" style="text-align: right;">
					<select id="simple_theme_changer" onchange="switch_theme();">
						<optgroup label="', $txt['simple_theme_changer_name'], '">';

							foreach ($context['themes'] as $id => $theme)
								echo '
							<option value="', $id, '"', (($settings['theme_id'] == $id) ? ' selected="selected"' : ''), '>', $theme, '</option>';

					echo '
						</optgroup>
					</select>
				</td>
			</tr>
		</table>';

		// Show the load time?]]></add>
		</operation>  
	</file>

	<file name="$themedir/script.js">
		<operation>
			<search position="end" />
			<add><![CDATA[

function switch_theme(){
	theme_changer = document.getElementById('simple_theme_changer');
	var id = theme_changer.options[theme_changer.selectedIndex].value;
	var page = new String(window.location);
	var themereg = new RegExp("theme=([0-9]+);?");
	var hash = "";
	page = page.replace(themereg, "");

	// Remove anything after a hash... sorry
	if(page.search("#") != -1){
		hash = page.substr(page.search("#"));
		page = page.substr(0, page.search("#"));
	}

	// Remove any ending question marks...
	while (page.charAt(page.length-1) == "?" || page.charAt(page.length-1) == ";")
		page = page.substr(0, page.length - 1);

	// Are there already things in a query string?
	if(page.search("/index.php") < (page.length - 10) && page.search("/index.php") != -1)
		window.location = page + ";theme=" + id + hash;

	// or is index.php at the end?
	else if(page.search("/index.php") == (page.length - 10))
		window.location = page + "?theme=" + id + hash;

	// Are we ending with a slash?
	else if(page.charAt(page.length-1) == "/")
		window.location = page + "index.php?theme=" + id + hash;

	// or are we pretty much nowhere?
	else
		window.location = page + "/index.php?theme=" + id + hash;

	return false;
}]]></add>
		</operation>
	</file>
</modification>