<?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>fustrate:themechanger</id>
	<version>1.0.3</version>

	<file name="$boarddir/index.php">
		<operation>
			<search position="before"><![CDATA[
loadSession();]]></search>
			<add><![CDATA[

// Simple Theme Changer
$request = $smcFunc['db_query']('', '
	SELECT *
	FROM {db_prefix}themes
	WHERE variable = {string:name}
		AND id_member = 0
		AND id_theme IN ({raw:known})',
	array('name' => 'name', 'known' => $modSettings['knownThemes'])
);

$context['themes'] = array();

while ($row = $smcFunc['db_fetch_assoc']($request))
	$context['themes'][$row['id_theme']] = $row['value'];

$smcFunc['db_free_result']($request);
// End Simple Theme Changer]]></add>
		</operation>
	</file>

	<file name="$themedir/index.template.php">
		<operation error="ignore">
			<search position="replace"><![CDATA[
</body>]]></search>
			<add><![CDATA[
<div id="theme_changer">
	<form id="theme_form" action="', $scripturl, '?action=profile;area=theme" method="post">
		<select name="new_theme" id="new_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>
		<input type="submit" name="save" id="theme_submit" value="Submit" class="button_submit" />
	</form>
</div>]]></add>
		</operation>
	</file>

	<file name="$sourcedir/Profile-Modify.php">
		<operation>
			<search position="after"><![CDATA[

	loadThemeOptions($memID);]]></search>
			<add><![CDATA[

	if (isset($_POST['new_theme']))
	{
		$new_theme = (int) $_POST['new_theme'];

		// Umm, no, I don't want to set a non-existent theme!
		if (!in_array($new_theme, explode(',', $modSettings['knownThemes'])))
			die('Hacking attempt...');

		updateMemberData($memID, array('id_theme' => $new_theme));

		if (isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']))
		{
			$parts = explode('?', preg_replace('~theme=([0-9]+)(&|;)?~', '', $_SERVER['HTTP_REFERER']));

			redirectexit(count($parts) > 1 ? $parts[1] : '');
		}
	}]]></add>
		</operation>
	</file>

	<file name="$themedir/scripts/script.js">
		<operation>
			<search position="end" />
			<add><![CDATA[

function theme_change()
{
	var oSubmitButton = document.getElementById('theme_submit');
	var oDropdown = document.getElementById('new_theme');

	oDropdown.onchange = function()
	{
		var oThemeForm = document.getElementById('theme_form');

		oThemeForm.submit();
	}

	oSubmitButton.style.display = 'none';
	oSubmitButton.style.visibility = 'hidden';
}

addLoadEvent(theme_change);]]></add>
		</operation>
	</file>

	<file name="$themedir/css/index.css">
		<operation>
			<search position="end" />
			<add><![CDATA[

#theme_changer
{
	text-align: right;
	padding: 10px 10px 0 0;
}]]></add>
		</operation>
	</file>
</modification>