<?php
/**
 * @package WordPress
 * @subpackage Sapphire
 */

$themecolors = array(
	'bg' => 'ffffff',
	'text' => '000000',
	'link' => 'cc6600',
	'border' => '2b658b',
	'url' => 'ff9900',
);

$content_width = 450;

add_theme_support( 'automatic-feed-links' );

// Custom background
add_custom_background();

add_theme_support( 'print-style' );

function sapphire_custom_background() {
	if ( '' != get_background_color() && '' == get_background_image() ) { ?>
	<style type="text/css">
		body { background-image: none; }
	</style>
	<?php }
}
add_action( 'wp_head', 'sapphire_custom_background' );

function sapphire_widgets_init() {
	register_sidebars(1, array(
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div>'
	));
	function wp_widget_pages_control() {
		$options = $newoptions = get_option('widget_pages');
		if ( $_POST['pages-submit'] ) {
			$newoptions['title'] = strip_tags(stripslashes($_POST['pages-title']));

			$sortby = stripslashes( $_POST['pages-sortby'] );

			if ( in_array( $sortby, array( 'post_title', 'menu_order', 'ID' ) ) ) {
				$newoptions['sortby'] = $sortby;
			} else {
				$newoptions['sortby'] = 'menu_order';
			}

			$newoptions['exclude'] = strip_tags( stripslashes( $_POST['pages-exclude'] ) );
		}
		if ( $options != $newoptions ) {
			$options = $newoptions;
			update_option('widget_pages', $options);
		}
		$title = esc_attr( $options['title'] );
		$exclude = esc_attr( $options['exclude'] );
	?>
			<p><label for="pages-title"><?php esc_html_e('Title:'); ?> <input class="widefat" id="pages-title" name="pages-title" type="text" value="<?php echo esc_attr( $title ); ?>" /></label></p>
			<p>
				<label for="pages-sortby"><?php _e( 'Sort by:' ); ?>
					<select name="pages-sortby" id="pages-sortby" class="widefat">
						<option value="post_title"<?php selected( $options['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option>
						<option value="menu_order"<?php selected( $options['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option>
						<option value="ID"<?php selected( $options['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
					</select>
				</label>
			</p>
			<p>
				<label for="pages-exclude"><?php esc_html_e( 'Exclude:' ); ?> <input type="text" value="<?php echo esc_attr( $exclude ); ?>" name="pages-exclude" id="pages-exclude" class="widefat" /></label>
				<br />
				<small><?php _e( 'Page IDs, separated by commas.' ); ?></small>
			</p>
			<input type="hidden" id="pages-submit" name="pages-submit" value="1" />
	<?php
	}
	function widget_sapphire_pages($args) {
		extract( $args );
		$options = get_option( 'widget_pages' );

		$title = empty( $options['title'] ) ? __( 'Pages' , 'sapphire') : apply_filters('widget_title', $options['title']);
		$sortby = empty( $options['sortby'] ) ? 'menu_order' : $options['sortby'];
		$exclude = empty( $options['exclude'] ) ? '' : $options['exclude'];

		if ( $sortby == 'menu_order' ) {
			$sortby = 'menu_order, post_title';
		}

		$out = wp_list_pages( array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) );

		echo $before_widget;
		echo $before_title . $title . $after_title;
	?>
		<ul>
			<li class="page_item"><a href="<?php bloginfo('url'); ?>"><?php _e('Home'); ?></a></li>
			<?php echo $out; ?>
		</ul>
	<?php
		echo $after_widget;
	}

	unregister_widget('WP_Widget_Pages');
	wp_register_sidebar_widget('pages', __('Pages (Sapphire)'), 'widget_sapphire_pages', null, 'pages');
	wp_register_widget_control('pages', __('Pages (Sapphire)'), 'wp_widget_pages_control' );

}
add_action('widgets_init', 'sapphire_widgets_init');

define('HEADER_TEXTCOLOR', 'FFFFFF');
define('HEADER_IMAGE', '%s/images/sapphirehead.jpg'); // %s is theme dir uri
define('HEADER_IMAGE_WIDTH', 740);
define('HEADER_IMAGE_HEIGHT', 180);

function header_style() {
?>
<style type="text/css">
#header{
	background: url(<?php header_image() ?>) no-repeat center;
}
<?php if ( 'blank' == get_header_textcolor() ) { ?>
#header h1, #header .description {
	display: none;
}
<?php } else { ?>
#header h1 a, .description {
	color:#<?php header_textcolor() ?>;
}
<?php } ?>
</style>
<?php
}

function sapphire_admin_header_style() {
?>
<style type="text/css">
#headimg{
	background: url(<?php header_image() ?>) no-repeat;
	background-repeat: no-repeat;
	height: <?php echo HEADER_IMAGE_HEIGHT; ?>px;
	width:<?php echo HEADER_IMAGE_WIDTH; ?>px;
	font-family: Verdana, Arial, Sans-Serif;
}
#headimg h1{
font-size: 32px;
font-weight: bold;
padding-top: 60px;
text-align: center;
margin: 0;
}
#headimg h1 a{
	color:#<?php header_textcolor() ?>;
	text-decoration: none;
	border-bottom: none;
}
#headimg #desc{
	color:#<?php header_textcolor() ?>;
font-size: 15px;
text-align: center;
}

<?php if ( 'blank' == get_header_textcolor() ) { ?>
#headimg h1, #headimg #desc {
	display: none;
}
#headimg h1 a, #headimg #desc {
	color:#<?php echo HEADER_TEXTCOLOR ?>;
}
<?php } ?>

</style>
<?php
}

add_custom_image_header('header_style', 'sapphire_admin_header_style');

function sapphire_comment($comment, $args, $depth) {
	$GLOBALS['comment'] = $comment;
	extract($args, EXTR_SKIP);
?>
<li <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">
	<div id="div-comment-<?php comment_ID() ?>">
	<div class="comment-author vcard">
		<?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, $args['avatar_size'] ); ?>
		<cite class="fn"><?php comment_author_link() ?></cite> <?php _e('Says:', 'sapphire'); ?>
	</div>
	<?php if ($comment->comment_approved == '0') : ?>
		<em><?php _e('Your comment is awaiting moderation.', 'sapphire'); ?></em>
	<?php endif; ?>
	<br />

	<small class="comment-meta commentmetadata"><a href="#comment-<?php comment_ID() ?>" title=""><?php comment_date() ?> <?php _e('at', 'sapphire'); ?> <?php comment_time() ?></a> <?php edit_comment_link(__('e', 'sapphire'),'',''); ?></small>

	<?php comment_text(); ?>

	<div class="reply">
		<?php comment_reply_link(array_merge( $args, array('add_below' => 'div-comment', 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
	</div>
	</div>
<?php
}

/**
 * Enqueue scripts and styles
 */
function sapphire_scripts() {
	wp_enqueue_style( 'sapphire', get_stylesheet_uri() );

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) )
		wp_enqueue_script( 'comment-reply' );
}
add_action( 'wp_enqueue_scripts', 'sapphire_scripts' );

/**
 * Filters wp_title to print a neat <title> tag based on what is being viewed.
 *
 * @since Sapphire 1.0
 */
function sapphire_wp_title( $title, $sep ) {
	global $page, $paged;

	if ( is_feed() )
		return $title;

	// Add the blog name
	$title .= get_bloginfo( 'name' );

	// Add the blog description for the home/front page.
	$site_description = get_bloginfo( 'description', 'display' );
	if ( $site_description && ( is_home() || is_front_page() ) )
		$title .= " $sep $site_description";

	// Add a page number if necessary:
	if ( $paged >= 2 || $page >= 2 )
		$title .= " $sep " . sprintf( __( 'Page %s', 'sapphire' ), max( $paged, $page ) );

	return $title;
}
add_filter( 'wp_title', 'sapphire_wp_title', 10, 2 );