<?php

// Custom Theme Options
$radios = array('accordian', 'pt_crop');
$txts = array();
$ints = array('pt_width', 'pt_height', 'custom_excerpt');

$theme_options = array();
if( get_option('vx_options') ) {
	$theme_options = get_option('vx_options');
}
else {
	foreach( $radios as $key ) {
		$theme_options[$key] = 0;
	}
	foreach( $txts as $key ) {
		$theme_options[$key] = '';
	}
	foreach( $ints as $key ) {
		$theme_options[$key] = 0;
	}
}

// Custom theme options CSS
function add_custom_css() {
	$css_file = THEMELIB . '/custom-options.css';
	$css_url = THEMELIB_URI . '/custom-options.css';
	if ( file_exists($css_file) ) {
		wp_register_style('custom-options', $css_url, '', '', 'screen');
	}
}
add_action('admin_init', 'add_custom_css');

function enqueue_css() {
	wp_enqueue_style('custom-options');
}

// Delete theme options
function delete_theme_options($theme_options) {
	delete_option($theme_options);
	echo '<div id="message" class="updated fade"><p>'.__('Your theme options have been deleted.','victorian-xmas').'</p></div>'."\n";
}

// Update theme options
function update_theme_options($theme_options) {
	update_option('vx_options', $theme_options);
	echo '<div id="message" class="updated fade"><p>'.__('Your theme has been updated.','victorian-xmas').'</p></div>'."\n";
}

add_action('admin_menu', 'theme_admin');
if (!function_exists('theme_admin')) {
    // used by the admin panel hook
    function theme_admin() {    
        if (function_exists('add_menu_page')) {
			$page = add_theme_page(__('Theme Options','victorian-xmas'), __('Theme Options','victorian-xmas'),7, basename('theme_options.php'),'theme_admin_style');
			add_action('admin_print_styles-'.$page, 'enqueue_css');
    	}        
    }
}

function theme_admin_style() {
	?>
	<div class="wrap">
	<?php
	echo '<h2>'.__('Theme Options','victorian-xmas').'</h2>'."\n";
	global $wpdb,$radios,$txts,$ints,$theme_options;

	if( isset( $_POST['theme_options_submit'] ) && check_admin_referer($action ='update_options', $query_arg = 'theme_options_form') ) {
		if( $_POST['theme_options_delete'] == 1 ) {
			delete_theme_options('vx_options');
			$theme_options = array();
		}
		else {
			foreach( $radios as $key ) {
				$theme_options[$key] = $_POST[$key];
			}
			foreach( $txts as $key ) {
				$theme_options[$key] = $_POST[$key];
			}
			foreach( $ints as $key ) {
				$theme_options[$key] = absint( intval($_POST[$key]) );
			}
			$theme_options = theme_stripslashes_array($theme_options);
			$theme_options = theme_sanitise_array($theme_options);
			update_theme_options($theme_options);
		}
	}
	?>
	<form method="post" action="" id="theme-options"><div>
	<?php wp_nonce_field( $action = 'update_options', $name = 'theme_options_form', $referer = true , $echo = true) ;?>

	<fieldset>
	<legend><?php _e('Collapsing sub-menus','victorian-xmas'); ?></legend>
	<input type="radio" id="theme_accordian_yes" name="accordian" value="0" 
	<?php if ($theme_options['accordian'] != 1) echo ' checked="checked"';?>
	/> <label for="theme_accordian_yes"><?php _e('Enabled','victorian-xmas'); ?></label> 
	
	<input type="radio" id="theme_accordian_no" name="accordian" value="1" 
	<?php if ($theme_options['accordian'] == 1) echo ' checked="checked"';?>
	/> <label for="theme_accordian_no"><?php _e('Disabled','victorian-xmas'); ?></label>
	</fieldset>

	<fieldset>
	<legend><?php _e('Post Thumbnails','victorian-xmas');?></legend>
	<div><label class="pt" for="pt_width"><?php _e('Thumbnail Width:','victorian-xmas');?></label>
	<input class="pt" type="text" id="pt_width" name="pt_width" maxlength="3" value="<?php if( $theme_options['pt_width'] =='' ) echo CUSTOM_THUMB;else echo $theme_options['pt_width'];?>" /><br />
	<label class="pt" for="pt_height"><?php _e('Thumbnail Height:','victorian-xmas');?></label>
	<input class="pt" type="text" id="pt_height" name="pt_height" maxlength="3" value="<?php if( $theme_options['pt_height'] =='' ) echo CUSTOM_THUMB;else echo $theme_options['pt_height'];?>" /><div>
	
	<p><input type="radio" id="pt_crop_soft" name="pt_crop" value="0" <?php if($theme_options['pt_crop'] == 0) echo ' checked="checked"';?> /> <label for="pt_crop_soft"><?php _e('Soft (proportional) crop','victorian-xmas');?></label><br /> 
	<input type="radio" id="pt_crop_hard" name="pt_crop" value="1" <?php if($theme_options['pt_crop'] != 0) echo ' checked="checked"';?>  /> <label for="pt_crop_hard"><?php _e('Hard (fixed) crop','victorian-xmas');?></label></p>
	</fieldset>

	<fieldset>
	<legend><?php _e('Custom Excerpt','victorian-xmas');?></legend>
	<label for="custom_excerpt"><?php _e('Length (in characters):','victorian-xmas');?></label> 
	<input class="excerpt" type="text" id="custom_excerpt" name="custom_excerpt" maxlength="3" value="<?php if( $theme_options['custom_excerpt'] =='' ) echo '100' ;else echo $theme_options['custom_excerpt'];?>" />
	</fieldset>

	<fieldset>
		<legend><?php _e('Reset Theme Options', 'victorian-xmas');?></legend>
	<input type="checkbox" id="theme_options_delete" name="theme_options_delete" value="1" /> 
	<label for="theme_options_delete"><?php _e('Yes','victorian-xmas');?> <strong>(<?php _e('Ticking this box will remove all saved theme options from your database','victorian-xmas');?>)</strong></label>
	</fieldset>
	
	<div>
	<input type="hidden" name="action" value="update" />
	</div>

	<p class="submit"><input type="submit" name="theme_options_submit" class="button-primary" value="<?php _e('Update Theme','victorian-xmas') ?>" /></p>
	</div></form>
</div>
	<?php
}

?>