<?php
function newsmin_theme_options_init() {
	wp_enqueue_style( 'newsmin-theme-options-style', get_template_directory_uri() . '/settings/options.css' );
}
add_action( 'admin_init', 'newsmin_theme_options_init' );

global $newsmin_options;
$settings = get_option( 'newsmin_options', $newsmin_options );
$shortname = "newsmin";


$newsmin_options = array(
	'newsmin_favicon_url' => '',
);


function newsmin_default_options() {
     global $newsmin_options;
     $newsmin_options_temp = $newsmin_options;
     $options = get_option( 'newsmin_options', $newsmin_options );
	foreach ( $newsmin_options as $newsmin_option_key => $newsmin_option_value ) {
		if ( isset($options[$newsmin_option_key])) {
			$newsmin_options[$newsmin_option_key] = $options[$newsmin_option_key];
		}
	}     
     update_option( 'newsmin_options', $newsmin_options );
}
add_action( 'init', 'newsmin_default_options' );

function newsmin_validate_options( $input ) {
	global $newsmin_options;	

	$settings = get_option( 'newsmin_options', $newsmin_options );
	
	if ( ! isset( $input['newsmin_favicon_url'] ) )
	$input['newsmin_favicon_url'] = null;
	$input['newsmin_favicon_url'] = esc_url_raw( $input['newsmin_favicon_url'] );

	return $input;
}


if ( is_admin() ) : 

//register settings and call sanitation functions
function newsmin_register_settings() {
	register_setting( 'newsmin_theme_options', 'newsmin_options', 'newsmin_validate_options' );
}
add_action( 'admin_init', 'newsmin_register_settings' );

function newsmin_theme_options() {
	add_theme_page('newsmin'.__('Theme Options','newsmin'), 'Newsmin '.__('Theme Options','newsmin'), 'edit_theme_options', 'theme-options', 'newsmin_theme_options_page' );
}
add_action( 'admin_menu', 'newsmin_theme_options' );



//generate options page
function newsmin_theme_options_page() {
	global $newsmin_options;
	if ( ! isset( $_REQUEST['settings-updated'] ) )
		$_REQUEST['settings-updated'] = false;
	if( isset( $_REQUEST['action'])&&('reset' == $_REQUEST['action']) ) 
		delete_option( 'newsmin_options' );
?>

	<div id="admin" class="wrap">

	
	
	<div class="options-form">
	
	
	<?php screen_icon(); echo "<h2>" . __( 'Theme Options' ,'newsmin' ) . "</h2>"; ?>
	<?php if ( isset( $_REQUEST['action'])&&('reset' == $_REQUEST['action']) ) : ?>
	<div class="updated_status fade"><?php _e( 'Options reset successfully. Remember to save the default settings!','newsmin' ); ?></div>
	<?php elseif ( $_REQUEST['settings-updated'] ) : ?>
	<div class="updated_status fade"><?php _e( 'Options saved successfully!','newsmin' ); ?></div>
	<?php endif;?>
	
	<form method="post" action="options.php">

	<?php $settings = get_option( 'newsmin_options', $newsmin_options ); ?>	
	<?php settings_fields( 'newsmin_theme_options' ); ?>
		
		
	<div class="field">
		<label><?php _e( 'Favicon URL','newsmin' ); ?></label>
       	<input class="input" id="newsmin_favicon_url" name="newsmin_options[newsmin_favicon_url]" type="text" value="<?php echo esc_url($settings['newsmin_favicon_url']); ?>" />
		<span><?php _e( 'Enter full URL for favicon starting with <strong>http:// </strong>.','newsmin' ); ?></span>
	</div>		
	
	<a href="https://twitter.com/wpmole" class="twitter-follow-button" data-show-count="false">Follow @wpmole</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script> <a href="http://www.facebook.com/pages/WPMole/142454599181539" target="_blank"><img src="<?php echo get_template_directory_uri(); ?>/img/facebook.png" /></a>  <a href="http://wpmole.com" target="_blank">WPMole</a>
	
	</div> <!-- /greenpage_options -->
	<!---- /form fields ---->
	
	
	<?php submit_button(); ?>
	</form>
	
	<form method="post">
		<p class="submit">
			<input class="button" name="reset" type="submit" value="Reset All Settings" />
			<input type="hidden" name="action" value="reset" />
		</p>
	</form>

	</div>

	<?php
}

endif;  // EndIf is_admin() ?>
