<?php

function theme_vars_settings(){
	global $theme_key;
	
	$vars = theme('custom_variables');
	if(!$vars){
		$form['theme_vars'] = array(
			'#type' => 'markup',
			'#value' => '<p>This theme defines no custom variables.</p>'
		);
		return $form;
	}
	
	$form['theme_vars'] = array(
		'#type' => 'fieldset',
		'#title' => $theme_key . ': Theme Variables',
		'#collapsible' => FALSE,
		'#description' => t('Custom variables specific to this theme.'),
	);
	
	foreach($vars as $var_name=>$form_field){
		$var_key = join('_',array('theme_vars',$theme_key,$var_name));
		$form_field['#default_value'] = variable_get($var_key, $form_field['#default_value']);
		$form['theme_vars'][$var_key] = $form_field;
	}
	return $form;
}

function theme_vars_retrieve(){
	global $theme_key;
	
	$vars = theme('custom_variables');
	if(!$vars){
		return array();
	}
	foreach($vars as $var_name=>$form_field){
		$var_key = join('_',array('theme_vars',$theme_key,$var_name));
		$default_value = $form_field['#default_value'];
		$vars[$var_name] = variable_get($var_key,$default_value);
	}
	return $vars;
}