<?php

	# Default style #
	$defaultStyle = array(
		'Background' => array(
			'body - background-color' => 'ffffff',
			'#content - background-color' => 'ffffff',
		),

		'Color 1' => array(
			'#main-menu - background-color' => '000000',
		),

		'Color 2' => array(
			'div.index table th - background-color' => 'D00010',
			'#sidebar, #content, #footer, #content .header - border-color' => '#D00010',
			'#main-menu li a:hover - background-color' => 'D00010',
			'div.message - background-color' => 'D00010',
		),

		'Color 3' => array(
			'#sidebar - background-color' => 'E1E5F3',
		),

		null => array(
			'#content .header - background-color' => 'F0F0F0',
			'#main-menu li a - color' => 'ffffff',
			'#main-menu li a:hover - color' => 'ffffff',

			'div.index table th, div.index table th a - color' => 'ffffff',
			'div.index table th a:hover - color' => 'ffffff',
			'div.index table th a:hover - border-bottom' => 'ffffff',

			'div.message - color' => 'ffffff'
		)
	);
	if(defined('CAKE')) return;

	# Get style from the database @
	require_once('../../config/database.php');
	$config = new DATABASE_CONFIG();
	$config = $config->default;
	mysql_connect($config['host'], $config['login'], $config['password']);
	mysql_select_db($config['database']);
	mysql_query('SET NAMES UTF-8');
	$result = mysql_fetch_assoc(mysql_query('SELECT `style` FROM `users` AS `User` WHERE MD5(`User`.`id` * Length(`User`.`username`) + Length(`User`.`full_name`)) = "'	 . mysql_real_escape_string(key($_GET)) . '"'));
	$style = @unserialize($result['style']);

	# If there was an error in unserialization #
	if(empty($style)) {
		$style = array();
	}

	# Generate user style #
	$css = array_merge($defaultStyle, $style);
	$custom = "\n\n\n/* User specific style */\n";
	foreach($css as $group) {
		foreach($group as $key => $value) {
			list($selector, $property) = explode(' - ', $key);
			if($selector{0} != '@') {
				$custom .= "{$selector} { {$property}: #{$value}; }\n";

			} else {
				switch($selector) {
					case '@bordered':
						$custom .= "#sidebar, #content, #footer { border-color: #{$value} }\n#content .header { border-left: 2px solid #d0d0d0; border-bottom: 2px solid #{$value}; }\n";
						break;
					default:
				}
			}
		}
	}

	# Echo style #
	//header("Date: " . date("D, j M Y G:i:s ", $templateModified) . 'GMT');
	header("Content-Type: text/css");
	header("Expires: " . gmdate("D, d M Y H:i:s", time() + 60*60*24) . " GMT");
	header("Cache-Control: max-age=86400, must-revalidate");
	header("Pragma: cache");
	require 'cake.generic.css';
	//echo $custom;
?>
