<?php
function mr_admin_head() { ?>
	<style type="text/css">
		h2 { margin-bottom: 20px; }
		.title { margin: 0px !important; background: #D4E9FA; padding: 10px; font-family: Georgia, serif; font-weight: normal !important; letter-spacing: 1px; font-size: 18px; }
		.container { background: #EAF3FA; padding: 10px; }
		.maintable { font-family:"Lucida Grande","Lucida Sans Unicode",Arial,Verdana,sans-serif; background: #EAF3FA; margin-bottom: 20px; padding: 10px 0px; }
		.mainrow { padding-bottom: 10px !important; margin: 0px 10px 10px 10px !important; }
		.mainrow td { border-bottom: 1px solid #D4E9FA;padding:10px}
		.titledesc { font-size: 14px; font-weight:bold; width: 220px !important; margin-right: 20px !important; }
		.forminp { width: 700px !important; valign: middle !important; }
		.forminp input, .forminp select, .forminp textarea { margin-bottom: 9px !important; background: #fff; border: 1px solid #D4E9FA; width: 500px; padding: 4px; font-family:"Lucida Grande","Lucida Sans Unicode",Arial,Verdana,sans-serif; font-size: 12px; }
		.forminp span { font-size: 10px !important; font-weight: normal !important; ine-height: 14px !important; }
		.info { background: #FFFFCC; border: 1px dotted #D8D2A9; padding: 10px; color: #333; }
		.forminp .checkbox { width:20px }
		.info a { color: #333; text-decoration: none; border-bottom: 1px dotted #333 }
		.info a:hover { color: #666; border-bottom: 1px dotted #666; }
		.warning { background: #c3e3a7; border: 1px dotted #60982f; padding: 10px; color: #333; font-weight: bold; }
		em {font-size:12px}
	</style>
	<?php }

	/* ----------------------
		Variables
	------------------------ */

		/* Theme Name */
		$themename = "Theme";
		$shortname = "theme";
		
		$options = array();
		$template_path = get_bloginfo('template_directory');
		$mr_categories_obj = get_categories('hide_empty=0');
		$mr_categories = array();

		foreach ($mr_categories_obj as $mr_cat) {
			$mr_categories[$mr_cat->cat_ID] = $mr_cat->cat_name;
		}
		
		$yesno = array("Yes","No");
		$htmlxhtml = array("HTML 4.0 Strict","XHTML 1.0 Strict");
		$colours = array("Blue","Green");
		$gravatar = array("24","32","48","64","80","96","112","128");
		$categories_tmp = array_unshift($mr_categories, "Select a category:");
		
	/* ----------------------
		Options
	------------------------ */

	$options = array (
			array(	"name" => "General Settings",
					"type" => "heading"),

			array( 	"name" => "Markup Language",
					"desc" => "HTML 4 Strict or XHTML 1.0 Strict - the choice is yours!",
					"id" => $shortname."_html_or_xhtml",
					"std" => "HTML or XHTML:",
					"type" => "select",
					"options" => $htmlxhtml),
					
			array( 	"name" => "Color Scheme",
					"desc" => "Select the site color scheme",
					"id" => $shortname."_colour",
					"std" => "Color:",
					"type" => "select",
					"options" => $colours),
			
			array( 	"name" => "Book Now URL",
					"desc" => "The location this area links to",
					"id" => $shortname."_book_url",
					"std" => "#",
					"type" => "text"),
			
			array( 	"name" => "Photos URL",
					"desc" => "The location this area links to",
					"id" => $shortname."_photos_url",
					"std" => "#",
					"type" => "text"),
			
			array( 	"name" => "Testimonials URL",
					"desc" => "The location this area links to",
					"id" => $shortname."_testimonials_url",
					"std" => "#",
					"type" => "text"),
					
			
			array(	"name" => "Google Analytics",
					"desc" => "Please paste your Google Analytics (or other) statistics code here.",
					"id" => $shortname."_google_analytics",
					"std" => "",
					"type" => "textarea"),
			
			array(	"name" => "Comments",
					"type" => "heading"),
					
			array(	"name" => "Comments on Posts",
					"desc" => "Check this box if you wish to enable comments on posts",
					"id" => $shortname."_comments_posts",
					"std" => "false",
					"type" => "checkbox"),

			array(	"name" => "Comments on Pages",
					"desc" => "Check this box if you wish to enable comments on pages",
					"id" => $shortname."_comments_pages",
					"std" => "false",
					"type" => "checkbox"),
					
			array(	"name" => "Gravatars",
					"desc" => "Check this box to enable or disable gravatars in comments",
					"id" => $shortname."_gravatar",
					"std" => "false",
					"type" => "checkbox"),

			array( 	"name" => "Gravatar Size",
					"desc" => "Select the size you want Gravatars to display.",
					"id" => $shortname."_gravatar_size",
					"std" => "Gravatar Size:",
					"type" => "select",
					"options" => $gravatar),
					
			array(	"name" => "Sitemap",
					"type" => "heading",
					"desc" => "Controls what sections appear on the sitemap. To create a sitemap, make a new page and select 'Sitemap' from the page template list."),
					
			array(	"name" => "Display Feeds",
					"desc" => "",
					"id" => $shortname."_sitemap_feed",
					"std" => "true",
					"type" => "checkbox"),
					
			array(	"name" => "Display Pages",
					"desc" => "",
					"id" => $shortname."_sitemap_page",
					"std" => "true",
					"type" => "checkbox"),
					
			array(	"name" => "Display Posts",
					"desc" => "",
					"id" => $shortname."_sitemap_post",
					"std" => "true",
					"type" => "checkbox"),
					
			array(	"name" => "Display Category Archives",
					"desc" => "",
					"id" => $shortname."_sitemap_category",
					"std" => "true",
					"type" => "checkbox"),
					
			array(	"name" => "Display Author Archives",
					"desc" => "",
					"id" => $shortname."_sitemap_author",
					"std" => "true",
					"type" => "checkbox"),
					
			array(	"name" => "Display Yearly Archives",
					"desc" => "",
					"id" => $shortname."_sitemap_yearly",
					"std" => "true",
					"type" => "checkbox"),
					
			array(	"name" => "Display Monthly Archives",
					"desc" => "",
					"id" => $shortname."_sitemap_monthly",
					"std" => "true",
					"type" => "checkbox"),
					
			array(	"name" => "Display Weekly Archives",
					"desc" => "",
					"id" => $shortname."_sitemap_weekly",
					"std" => "true",
					"type" => "checkbox"),
					
			array(	"name" => "Display Daily Archives",
					"desc" => "",
					"id" => $shortname."_sitemap_daily",
					"std" => "true",
					"type" => "checkbox"),
					
			array(	"name" => "Display Tag Archives",
					"desc" => "",
					"id" => $shortname."_sitemap_tag",
					"std" => "true",
					"type" => "checkbox"),
	  );

	/* ----------------------
		Admin Panel
	------------------------ */

	function mr_add_admin() {

		global $themename, $options;
		
		if ( $_GET['page'] == basename(__FILE__) ) {	
			if ( 'save' == $_REQUEST['action'] ) {
		
					foreach ($options as $value) {
						if($value['type'] != 'multicheck'){
							update_option( $value['id'], $_REQUEST[ $value['id'] ] ); 
						}else{
							foreach($value['options'] as $mc_key => $mc_value){
								$up_opt = $value['id'].'_'.$mc_key;
								update_option($up_opt, $_REQUEST[$up_opt] );
							}
						}
					}

					foreach ($options as $value) {
						if($value['type'] != 'multicheck'){
							if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ]  ); } else { delete_option( $value['id'] ); } 
						}else{
							foreach($value['options'] as $mc_key => $mc_value){
								$up_opt = $value['id'].'_'.$mc_key;						
								if( isset( $_REQUEST[ $up_opt ] ) ) { update_option( $up_opt, $_REQUEST[ $up_opt ]  ); } else { delete_option( $up_opt ); } 
							}
						}
					}
					
					$options = array(
						'main_text'
						);
					foreach($options as $option)
					{
						update_option('theme_'. $option, (stripcslashes($_POST[$option])));
					}

					header("Location: admin.php?page=theme-panel.php&saved=true");								
				
				die;

			} else if ( 'reset' == $_REQUEST['action'] ) {				
				header("Location: admin.php?page=theme-panel.php&reset=true");
				die;
			}
		}

	add_menu_page($themename." Options", $themename." Options", 'edit_themes', basename(__FILE__), 'mr_page');
	}

	function mr_page (){
			global $options, $themename, $manualurl;
	?>

	<div class="wrap">

					<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">

							<h2><?php echo $themename; ?> Options</h2>

							<?php if ( $_REQUEST['saved'] ) { ?><div style="clear:both;height:20px;"></div><div class="warning"><?php echo $themename; ?> Options have been updated!</div><?php } ?>
							<?php if ( $_REQUEST['reset'] ) { ?><div style="clear:both;height:20px;"></div><div class="warning"><?php echo $themename; ?> Options have been reset!</div><?php } ?>
									
									<table class="maintable" width="100%">
								<?php foreach ($options as $value) { ?>
		
										<?php if ( $value['type'] <> "heading" ) { ?>
		
											<tr class="mainrow">
											<td class="titledesc"><?php echo $value['name']; ?></td>
											<td class="forminp">
			
										<?php } ?>		 
		
										<?php
											
											switch ( $value['type'] ) {
											case 'text':
			
										?>
										
												<input name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( get_settings( $value['id'] ) != "") { echo get_settings( $value['id'] ); } else { echo $value['std']; } ?>" />
			
										<?php
											
											break;
											case 'select':
			
										?>
			
											<select name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
											<?php foreach ($value['options'] as $option) { ?>
												<option<?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option>
											<?php } ?>
											</select>
			
										<?php
			
											break;
											case 'textarea':
											$ta_options = $value['options'];
			
										?>
										
											<textarea name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" cols="<?php echo $ta_options['cols']; ?>" rows="8"><?php  if( get_settings($value['id']) != "") { echo stripslashes(get_settings($value['id'])); } else { echo $value['std']; } ?></textarea>
			
										<?php
											
											break;
											case "radio":
			
											foreach ($value['options'] as $key=>$option) { 
					
														$radio_setting = get_settings($value['id']);
														
														if($radio_setting != '') {
															
															if ($key == get_settings($value['id']) ) { $checked = "checked=\"checked\""; } else { $checked = ""; }
														
														} else {
														
															if($key == $value['std']) { $checked = "checked=\"checked\""; } else { $checked = ""; }
										} ?>
										
										<input type="radio" name="<?php echo $value['id']; ?>" value="<?php echo $key; ?>" <?php echo $checked; ?> /><?php echo $option; ?><br />
			
										<?php }
			 
											break;
											case "checkbox":
											
											if(get_settings($value['id'])) { $checked = "checked=\"checked\""; } else { $checked = ""; }
										
										?>
										
										<input type="checkbox" class="checkbox" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> />
			
										<?php
			
											break;
											case "multicheck":
			
											foreach ($value['options'] as $key=>$option) {
											
													$pn_key = $value['id'] . '_' . $key;
													$checkbox_setting = get_settings($pn_key);
					
													if($checkbox_setting != '') {
						
															if (get_settings($pn_key) ) { $checked = "checked=\"checked\""; } else { $checked = ""; }
					
													} else { if($key == $value['std']) { $checked = "checked=\"checked\""; } else { $checked = ""; }
					
										} ?>
										
										<input type="checkbox" class="checkbox" name="<?php echo $pn_key; ?>" id="<?php echo $pn_key; ?>" value="true" <?php echo $checked; ?> /><label for="<?php echo $pn_key; ?>"><?php echo $option; ?></label><br />
										
										<?php }
			 
											break;
											case "heading":

										?>
										
											</table> 
											
													<h3 class="title"><?php echo $value['name']; ?></h3>
											
											<table class="maintable" width="100%">
												<?php if($value['desc']) { ?>
												<tr class="mainrow">
													<td colspan="2"><em><?php echo $value['desc']; ?></em></td>
												</tr>
												<?php } ?>
			
										<?php
											
											break;
											default:
											break;
										
										} ?>
		
										<?php if ( $value['type'] <> "heading" ) { ?>
		
											<?php if ( $value['type'] <> "checkbox" ) { ?><br/><?php } ?><span><?php echo $value['desc']; ?></span>
											</td></tr>
		
										<?php } ?>		
								<?php } ?>	
								
								</table>

								<p class="submit">
									<input name="save" type="submit" value="Save changes" />    
									<input type="hidden" name="action" value="save" />
								</p>							
								
								<div style="clear:both;"></div>						 
				</form>

	</div>

	<div style="clear:both;height:20px;"></div>

	<?php
	};

	function google_analytics () {
		echo stripslashes(get_option('theme_google_analytics'));
	}

	add_action('admin_menu', 'mr_add_admin');
	add_action('admin_head', 'mr_admin_head');	
	add_action('wp_footer', 'google_analytics');

?>