<?php
/*
Plugin Name: Theme Options
Author: Nathan Rice
Author URI: http://www.nathanrice.net/

NOTE: this file requires WordPress 2.7+ to function
*/
$settings = 'mods_'.get_current_theme(); // do not change!

$defaults = array( // define our defaults
		'header_blog_title' => 'Image',
		'blog_layout' => 'Right',
		'hp_main' => 1,
		'video' => 'No',
		'video_bottom' => 'No',
		'hp_bottom' => 1,
		'hp_bottom_thumb_width' => 200,
		'hp_bottom_thumb_height' => 140,
		'blog_cat' => 1,
		'blog_cat_num' => 5,
		'analytics' => 'Yes' // <-- no comma after the last option
);

//	push the defaults to the options database,
//	if options don't yet exist there.
add_option($settings, $defaults, '', 'yes');

/*
///////////////////////////////////////////////
This section hooks the proper functions
to the proper actions in WordPress
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
*/
//	this function registers our settings in the db
add_action('admin_init', 'register_theme_settings');
function register_theme_settings() {
	global $settings;
	register_setting($settings, $settings);
}
//	this function adds the settings page to the Appearance tab
add_action('admin_menu', 'add_theme_options_menu');
function add_theme_options_menu() {
	add_submenu_page('themes.php', 'Executive '.__('Theme Options','studiopress'), 'Executive '.__('Theme Options','studiopress'), 8, 'theme-options', 'theme_settings_admin');
}

/*
///////////////////////////////////////////////
This section handles all the admin page
output (forms, update notifications, etc.)
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
*/
function theme_settings_admin() { ?>
<?php theme_options_css_js(); ?>
<div class="wrap">
<?php
	// display the proper notification if Saved/Reset
	global $settings, $defaults;
	if(get_theme_mod('reset')) {
		echo '<div class="updated fade" id="message"><p>'.__('Theme Options', 'studiopress').' <strong>'.__('RESET TO DEFAULTS', 'studiopress').'</strong></p></div>';
		update_option($settings, $defaults);
	} elseif($_REQUEST['updated'] == 'true') {
		echo '<div class="updated fade" id="message"><p>'.__('Theme Options', 'studiopress').' <strong>'.__('SAVED', 'studiopress').'</strong></p></div>';
	}
	// display icon next to page title
	screen_icon('options-general');
?>
	<h2><?php echo get_current_theme() . ' '; _e('Theme Options', 'studiopress'); ?></h2>
	<form method="post" action="options.php">
	<?php settings_fields($settings); // important! ?>

	<?php // first column ?>
    
	<div class="metabox-holder">
    
		<div class="postbox">
		<h3><?php _e("StudioPress QuickDash", 'studiopress'); ?></h3>
			<div class="inside">
				<p>Do you want access to all StudioPress Themes? <br /> Purchase the <a rel="nofollow" href="http://www.studiopress.com/themes/pro-plus">Pro-Plus package</a> for only $149.95.<br /><strong><small>(Use SPF25 as the discount code and save $50.00!)</small></strong></p>
                <p style="border-top: 1px solid #DFDFDF; padding-top: 10px;"><a target="_blank" href="http://feeds2.feedburner.com/studiopress"><img style="vertical-align:middle;margin:0px 3px 3px 0px;" src="http://www.studiopress.com/images/rss.gif" alt="Subscribe to StudioPress" /></a><a target="_blank" href="http://feeds2.feedburner.com/studiopress">Subscribe to RSS Feed</a><a target="_blank" href="http://feedburner.google.com/fb/a/mailverify?uri=studiopress"></a>&nbsp;&nbsp;<img style="vertical-align:middle;margin:0px 3px 3px 0px;" src="http://www.studiopress.com/images/rss.gif" alt="Subscribe to StudioPress" /></a><a target="_blank" href="http://feedburner.google.com/fb/a/mailverify?uri=studiopress">Subscribe via Email</a></p>

			</div>
		</div> 
                                    
		<div class="postbox">
		<h3><?php _e("Header Blog Title", 'studiopress'); ?></h3>
			<div class="inside">
				<p><?php _e("Select from the following:", 'studiopress'); ?><br />
				<select name="<?php echo $settings; ?>[header_blog_title]">
					<option style="padding-right:10px;" value="Image" <?php selected('Image', get_theme_mod('header_blog_title')); ?>><?php _e("Use an image logo", 'studiopress'); ?></option>
					<option style="padding-right:10px;" value="Text" <?php selected('Text', get_theme_mod('header_blog_title')); ?>><?php _e("Use dynamic text", 'studiopress'); ?></option>
				</select></p>
			</div>
		</div>
        
		<div class="postbox">
		<h3><?php _e("Blog Layout", 'studiopress'); ?></h3>
			<div class="inside">
				<p><?php _e("Select from the following:", 'studiopress'); ?><br />
				<select name="<?php echo $settings; ?>[blog_layout]">
					<option style="padding-right:10px;" value="Left" <?php selected('Left', get_theme_mod('blog_layout')); ?>><?php _e("Sidebar/Content", 'studiopress'); ?></option>
					<option style="padding-right:10px;" value="Right" <?php selected('Right', get_theme_mod('blog_layout')); ?>><?php _e("Content/Sidebar", 'studiopress'); ?></option>
					<option style="padding-right:10px;" value="Split" <?php selected('Split', get_theme_mod('blog_layout')); ?>><?php _e("Sidebar/Content/Sidebar", 'studiopress'); ?></option>
				</select></p>
			</div>
		</div>
        
		<div class="postbox">
		<h3><?php _e("Homepage Main", 'studiopress'); ?></h3>
			<div class="inside">
				<p><?php _e("Select which <strong>page</strong> you want displayed on the top section of the homepage:", 'studiopress'); ?><br />
				<?php wp_dropdown_pages(array('selected' => get_theme_mod('hp_main'), 'name' => $settings.'[hp_main]' )); ?></p>
                
				<p><?php _e("Replace the photo on the homepage with a video?", 'studiopress'); ?><br />
				<select name="<?php echo $settings; ?>[video]">
					<option style="padding-right:10px;" value="Yes" <?php selected('Yes', get_theme_mod('video')); ?>><?php _e("Yes", 'studiopress'); ?></option>
					<option style="padding-right:10px;" value="No" <?php selected('No', get_theme_mod('video')); ?>><?php _e("No", 'studiopress'); ?></option>
				</select></p>
                
				<p><?php _e("If you select yes, then you will need to add your embedded video code just below the standard text editor box while creating a page.", 'studiopress'); ?><br /><strong><small>(<?php _e("Recommended video size is 400px wide x 270px high", 'studiopress'); ?>)</small></strong></p>
        	</div>
		</div>
                        
		<div class="postbox">
		<h3><?php _e("Homepage Bottom", 'studiopress'); ?></h3>
			<div class="inside">
				<p><?php _e("Select which <strong>category</strong> you want to display at the bottom of the homepage.", 'studiopress'); ?><br />
    			<?php wp_dropdown_categories(array('selected' => get_theme_mod('hp_bottom'), 'name' => $settings.'[hp_bottom]', 'orderby' => 'Name' , 'hierarchical' => 1, 'show_option_all' => __("All Categories", 'studiopress'), 'hide_empty' => '0' )); ?></p>
				<p><?php _e("Thumbnail dimensions (Width x Height)", 'studiopress'); ?><br />
				<input type="text" name="<?php echo $settings; ?>[hp_bottom_thumb_width]" value="<?php echo get_theme_mod('hp_bottom_thumb_width'); ?>" size="3" /> x <input type="text" name="<?php echo $settings; ?>[hp_bottom_thumb_height]" value="<?php echo get_theme_mod('hp_bottom_thumb_height'); ?>" size="3" /></p>
                
				<p><?php _e("Replace the photos on the homepage with videos?", 'studiopress'); ?><br />
				<select name="<?php echo $settings; ?>[video_bottom]">
					<option style="padding-right:10px;" value="Yes" <?php selected('Yes', get_theme_mod('video_bottom')); ?>><?php _e("Yes", 'studiopress'); ?></option>
					<option style="padding-right:10px;" value="No" <?php selected('No', get_theme_mod('video_bottom')); ?>><?php _e("No", 'studiopress'); ?></option>
				</select></p>
                
				<p><?php _e("If you select yes, then you will need to add your embedded video code just below the standard text editor box while creating a post.", 'studiopress'); ?><br /><strong><small>(<?php _e("Recommended video size is 200px wide x 140px high", 'studiopress'); ?>)</small></strong></p>
        	</div>
		</div>
                             
	</div>
    
	<?php // end first column ?>
    
	<?php // second column ?>
            
	<div class="metabox-holder">
    
		<div class="postbox">
		<h3><?php _e("Blog Page", 'studiopress'); ?></h3>
			<div class="inside">
				<p><?php _e("Select which category you want displayed:", 'studiopress'); ?><br />
    			<?php wp_dropdown_categories(array('selected' => get_theme_mod('blog_cat'), 'name' => $settings.'[blog_cat]', 'orderby' => 'Name' , 'hierarchical' => 1, 'show_option_all' => __("All Categories", 'studiopress'), 'hide_empty' => '0' )); ?></p>
				
				<p><?php _e("Number of Posts to Show"); ?>:<br />
				<input type="text" name="<?php echo $settings; ?>[blog_cat_num]" value="<?php echo get_theme_mod('blog_cat_num'); ?>" size="3" /></p>
			</div>
		</div>
                
		<div class="postbox">
		<h3><?php _e("Analytics/Stat Tracker Code", 'studiopress'); ?></h3>
			<div class="inside">
				<p><?php _e("Do you want to include analytics/stat tracker code?", 'studiopress'); ?><br />
				<select name="<?php echo $settings; ?>[analytics]">
					<option style="padding-right:10px;" value="Yes" <?php selected('Yes', get_theme_mod('analytics')); ?>>Yes</option>
					<option style="padding-right:10px;" value="No" <?php selected('No', get_theme_mod('analytics')); ?>>No</option>
				</select></p>
                
				<p><?php _e("Enter your analytics/stat tracker code:", 'studiopress'); ?><br />
				<textarea name="<?php echo $settings; ?>[analytics_code]" cols=40 rows=5><?php echo stripslashes(get_theme_mod('analytics_code')); ?></textarea></p>
            </div>
		</div>
        		
		<p>
			<input type="submit" class="button-primary" value="<?php _e('Save Settings') ?>" />
			<input type="submit" class="button-highlighted" name="<?php echo $settings; ?>[reset]" value="<?php _e('Reset Settings'); ?>" />
		</p>
            
	</div>
    
	<?php // end second column ?>
    
	</form>

</div>
<?php }

// add CSS and JS if necessary
function theme_options_css_js() {
echo <<<CSS

<style type="text/css">
	.metabox-holder { 
		width: 350px; float: left;
		margin: 0px; padding: 0px 10px 0px 0px;
	}
	.metabox-holder .postbox .inside {
		padding: 0px 10px 0px 10px;
	}
</style>

CSS;
echo <<<JS

<script type="text/javascript">
jQuery(document).ready(function($) {
	$(".fade").fadeIn(1000).fadeTo(1000, 1).fadeOut(1000);
});
</script>

JS;
}
?>