<?php

load_theme_textdomain( 'nest', get_template_directory() . '/languages' );

/* ========== Theme parts ========== */

function nest_part_page_meta() {
	global $post;
	if( is_archive() || is_search() ) {
?>
		<div class="page-meta">
		<?php if( is_category() ) : ?>
			<?php printf( __( 'Category Archives: %s', 'nest' ), '<span>'.single_cat_title( '', false ) . '</span>' ); ?>
			<?php $category_description = category_description();
				if ( ! empty( $category_description ) )
					echo '<p>' . $category_description . '</p>';
			?>
		<?php elseif( is_tag() ) : ?>
			<?php printf( __( 'Tag Archives: %s', 'nest' ), '<span>' . single_tag_title( '', false ) . '</span>' ); ?>
		<?php elseif( is_author() ) : ?>
			<?php printf( __( 'Author Archives: %s', 'nest' ), '<span>' . get_the_author_meta( 'display_name', get_query_var( 'author' ) ) . '</span>' ); ?>
		<?php elseif( is_search() ) : ?>
			<?php printf( __( 'Search Results for: %s', 'nest' ), '<span>' . get_search_query() . '</span>' ); ?>
		<?php elseif( is_day() ) : ?>
			<?php printf( __( 'Daily Archives: %s', 'nest' ), '<span>' . get_the_date() . '</span>' ); ?>
		<?php elseif( is_month() ) : ?>
			<?php printf( __( 'Monthly Archives: %s', 'nest' ), '<span>' . get_the_date( 'F Y' ) . '</span>' ); ?>
		<?php elseif( is_year() ) : ?>
			<?php printf( __( 'Yearly Archives: %s', 'nest' ), '<span>' . get_the_date( 'Y' ) . '</span>' ); ?>
		<?php else : ?>
			<?php printf( __( 'Blog Archives', 'nest' ) ); ?>
		<?php endif; ?>
			<div class="clearfix"></div>
		</div><!-- .page-meta -->
<?php
	}
} // nest_part_page_meta();

function nest_part_by_nc_sa() {
	global $nest_options;
	if( is_single() && $nest_options['is_display_by-nc-sa'] ) {
?>
	<div class="page-meta">
		<?php printf( __( '<strong>Notice:</strong> This work is licensed under a <a rel="license nofollow external" href="http://creativecommons.org/licenses/by-nc-sa/3.0/"><abbr title="Attribution-NonCommercial-ShareAlike">BY-NC-SA</abbr></a>. Permalink: %s', 'nest'), '<a href="' . get_permalink().'">' . get_the_title() . '</a>' ); ?>
	</div>
<?php
	}
} // nest_part_by_nc_sa();

function nest_part_author_info() {
	global $nest_options;
	if( is_single() && get_option('show_avatars') && $nest_options['is_display_author_info'] ) {
?>
	<div class="post-author-info">
		<div class="author-avatar">
			<?php echo get_avatar( get_the_author_meta( 'user_email' ), 120 ); ?>
		</div><!-- .author-avatar -->
		<div class="author-description">
			<h3 class="author-description-title"><?php printf( esc_attr__( 'About %s', 'nest' ), get_the_author() ); ?></h3>
			<div class="author-bio"><?php the_author_meta( 'description' ); ?></div>
			<div class="author-link">
				<a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>">
					<?php printf( __( 'View all posts by %s', 'nest' ).'<span class="meta-nav">&rarr;</span>', get_the_author() ); ?>
				</a>
			</div><!-- .author-link -->
		</div><!-- .author-description -->
	</div><!-- .post-author-info -->
<?php
	}
} // nest_part_author_info();

function nest_part_structure_class() {
	global $nest_options, $nest_options_default_data, $post;
	if( get_post_meta( $post->ID, '_wp_page_template', true ) == 'template-fullwidth.php' ) {
		echo 'sidebar-none';
	} elseif( !empty( $nest_options['sidebar_pos'] ) ) {
		if( $nest_options['sidebar_pos'] == 'right' ) {
			echo 'sidebar-right';
		} elseif( $nest_options['sidebar_pos'] == 'left' ) {
			echo 'sidebar-left';
		} elseif( $nest_options['sidebar_pos'] == 'none' ) {
			echo 'sidebar-none';
		}
	} else {
		if( $nest_options_default_data['sidebar_pos'] == 'right' ) {
			echo 'sidebar-right';
		} elseif( $nest_options_default_data['sidebar_pos'] == 'left' ) {
			echo 'sidebar-left';
		} elseif( $nest_options_default_data['sidebar_pos'] == 'none' ) {
			echo 'sidebar-none';
		}
	}
} // nest_part_structure_class();

/* ========== Theme options ========== */

$nest_options_items = array (
	array(
		'id' => 'sidebar_pos',
		'name' => __( 'Sidebar Position', 'nest' ),
		'type' => 'select',
		'data' => array( array( 'name' => __( 'On the left', 'nest' ), 'value' => 'left' ), array( 'name' => __( 'On the right', 'nest' ), 'value' => 'right' ), array( 'name' => __( 'Hide sidebar (wide column)', 'nest' ), 'value' => 'none' ) ),
		'desc' => __( 'Select where your sidebar will be.', 'nest' )
	),
	array(
		'id' => 'logo_url',
		'name' => __( 'Logo URL', 'nest' ),
		'type' => 'text',
		'desc' => __( 'Put your website logo URL here. (leave it blank for displaying site name and site description.)', 'nest' )
	),
	array(
		'id' => 'menu_effect',
		'name' => __( 'Multi-level Menu Effect', 'nest' ),
		'type' => 'select',
		'data' => array( array( 'name' => __( 'None', 'nest' ), 'value' => 'none' ), array( 'name' => __( 'Fade', 'nest' ), 'value' => 'fade' ), array( 'name' => __( 'Flexible', 'nest' ), 'value' => 'flexible' ) ),
		'desc' => __( 'Select the effect of multi-level navigation menu.', 'nest' )
	),
	array(
		'id' => 'is_display_author_info',
		'name' => __( 'Author Info', 'nest' ),
		'type' => 'checkbox',
		'data' => __( 'Display Author Info', 'nest' ),
		'desc' => __( 'Enabling this will display your author information in a single post page. (avatars visibility required)', 'nest' )
	),
	array(
		'id' => 'is_display_by-nc-sa',
		'name' => __( 'BY-NC-SA License Notice', 'nest' ),
		'type' => 'checkbox',
		'data' => __( 'Display BY-NC-SA License Notice', 'nest' ),
		'desc' => __( 'Enabling this will display the BY-NC-SA License Notice in a singular page.', 'nest' )
	),
	array(
		'id' => 'is_display_random_posts_widget',
		'name' => __( 'Random Posts Widget', 'nest' ),
		'type' => 'checkbox',
		'data' => __( 'Display random posts widget', 'nest' ),
		'desc' => __( 'Enabling this will display the random posts widget in the sidebar of homepage.', 'nest' )
	),
	array(
		'id' => 'is_display_post_meta_in_page',
		'name' => __( 'Post meta in pages', 'nest' ),
		'type' => 'checkbox',
		'data' => __( 'Display post meta-information in pages', 'nest' ),
		'desc' => __( 'Enabling this will display the post meta-information under the post title in pages.', 'nest' )
	),
	array(
		'id' => 'is_display_posts_navi_links_in_single',
		'name' => __( 'Posts navigation links in single posts', 'nest' ),
		'type' => 'checkbox',
		'data' => __( 'Display posts navigation in single posts', 'nest' ),
		'desc' => __( 'Enabling this will display the posts navigation links (previous and next post) in a single post.', 'nest' )
	),
	array(
		'id' => 'is_display_header_menu_arrow',
		'name' => __( 'Header parent menu arrow mark ', 'nest' ),
		'type' => 'checkbox',
		'data' => __( 'Display the arrow mark of parent menu items', 'nest' ),
		'desc' => __( 'Enabling this will display the arrow mark of parent menu items containing sub-menu.', 'nest' )
	),
	array(
		'id' => 'rss_url',
		'name' => __( 'RSS URL', 'nest' ),
		'type' => 'text',
		'desc' => __( 'Put your full RSS subscribe URL here. (with http://, leave it blank for displaying the standard URL.)', 'nest' )
	),
	array(
		'id' => 'twitter_url',
		'name' => __( 'Twitter URL', 'nest' ),
		'type' => 'text',
		'desc' => __( 'Put your full Twitter URL here. (with http://, leave it blank for displaying none.)', 'nest' )
	),
	array(
		'id' => 'facebook_url',
		'name' => __( 'Facebook URL', 'nest' ),
		'type' => 'text',
		'desc' => __( 'Put your full Facebook URL here. (with http://, leave it blank for displaying none.)', 'nest' )
	),
	array(
		'id' => 'sina_url',
		'name' => __( 'Sina WeiBo URL', 'nest' ),
		'type' => 'text',
		'desc' => __( 'Put your full Sina WeiBo URL here. (with http://, leave it blank for displaying none.)', 'nest' )
	),
	array(
		'id' => 'code_siteinfo',
		'name' => __( 'Custom Website Info String', 'nest' ),
		'type' => 'textarea',
		'desc' => __( 'Some HTML code replaced the footer\'s website information.', 'nest' )
	)
);
// default values of theme options
$nest_options_default_data = array(
	'sidebar_pos' => 'right',
	'menu_effect' => 'fade',
	'is_display_author_info' => '0',
	'is_display_by-nc-sa' => '1',
	'is_display_post_meta_in_page' => '0',
	'is_display_posts_navi_links_in_single' => '1',
	'is_display_header_menu_arrow' => '1'
);

add_action( 'admin_init', 'nest_options_init' );
add_action( 'admin_menu', 'nest_options_add_page' );
add_action( 'init', 'nest_options_default' );

function nest_options_init(){
	register_setting( 'nest_theme_options', 'nest_theme_options', 'nest_options_validate' );
}
function nest_options_add_page() {
	add_theme_page( __( 'Theme Options', 'nest' ), __( 'Theme Options', 'nest' ), 'edit_theme_options', 'theme_options', 'nest_options_do_page' );
}
function nest_options_validate( $input ) {
	global $nest_options_items;
	foreach ( $nest_options_items as $item ) {
		switch ( $item['type'] ) {
			case 'text':
				$input[$item['id']] = wp_filter_nohtml_kses( $input[$item['id']] );
				break;
			case 'textarea':
				$input[$item['id']] = htmlspecialchars( $input[$item['id']] );
				break;
			case 'checkbox':
				$input[$item['id']] = ( $input[$item['id']] == 1 ? 1 : 0 );
				break;
		}
	}
	return $input;
}
function nest_options_default() {
	global $nest_options_items, $nest_options_default_data;
	$nest_options = get_option( 'nest_theme_options' );
	foreach ( $nest_options_items as $item ) {
		if ( ! isset( $nest_options[$item['id']] ) ) {
			if ( isset ( $nest_options_default_data[$item['id']] ) ) {
				$nest_options[$item['id']] = $nest_options_default_data[$item['id']];
			} else {
				$nest_options[$item['id']] = '';
			}
		}
	}
	update_option( 'nest_theme_options', $nest_options );
}
// display admin theme options page
function nest_options_do_page() {
	global $nest_options_items;

	if ( ! isset( $_GET['settings-updated'] ) ) $_GET['settings-updated'] = false;

	?>
	<div class="wrap">
		<?php screen_icon(); echo "<h2>" . get_current_theme() . ' ' . __( 'Theme Options', 'nest' ) . "</h2>"; ?>

		<?php if ( false !== $_GET['settings-updated'] ) : ?>
		<div class="updated fade"><p><strong><?php _e( 'Options saved.', 'nest' ); ?></strong></p></div>
		<?php endif; ?>

		<form method="post" action="options.php">
			<?php settings_fields( 'nest_theme_options' ); ?>
			<?php $nest_options = get_option( 'nest_theme_options' ); ?>

			<table class="form-table">
				<?php foreach( $nest_options_items as $item ) { ?>
				<tr valign="top">
					<th scope="row"><?php echo $item['name']; ?></th>
					<td>
						<?php if( $item['type'] == 'text' ) : ?>
						<input name="<?php echo 'nest_theme_options['.$item['id'].']'; ?>" type="text" value="<?php if( $nest_options[$item['id']] ) esc_attr_e( $nest_options[$item['id']] ); ?>" size="80" />
						<?php elseif( $item['type'] == 'textarea' ) : ?>
						<textarea name="<?php echo 'nest_theme_options['.$item['id'].']'; ?>" rows="5" cols="60"><?php if( $nest_options[$item['id']] ) echo html_entity_decode( $nest_options[$item['id']] ); ?></textarea>
						<?php elseif( $item['type'] == 'select' ) : ?>
						<select name="<?php echo 'nest_theme_options['.$item['id'].']'; ?>">
						<?php
							foreach( $item['data'] as $select_data ) {
								echo '<option value="'.$select_data['value'].'"';
								if( $nest_options[$item['id']] == $select_data['value'] ) echo ' selected';
								echo '>'.$select_data['name'].'</option>';
							}
						?>
						</select>
						<?php elseif( $item['type'] == 'checkbox' ) : ?>
							<label for="<?php echo 'nest_theme_options['.$item['id'].']'; ?>"><input name="<?php echo 'nest_theme_options['.$item['id'].']'; ?>" id="<?php echo 'nest_theme_options['.$item['id'].']'; ?>" type="checkbox" value="1"<?php checked( '1', $nest_options[$item['id']] ); ?> /> <?php echo $item['data']; ?></label>
						<?php endif; ?>
						<?php if( ! empty( $item['desc'] ) ) : ?>
						<br/><label class="description" for="<?php echo 'nest_theme_options['.$item['id'].']'; ?>"><?php echo $item['desc']; ?></label>
						<?php endif; ?>
					</td>
				</tr>
				<?php } // endforeach; ?>
			</table>

			<p class="submit">
				<input type="submit" class="button-primary" value="<?php _e( 'Save Changes', 'nest' ); ?>" />
			</p>
		</form>
	</div>
	<?php
}

// initialize theme options variable array
$nest_options = get_option( 'nest_theme_options' );

/* ========== Theme widgets ========== */

function nest_widgets_init() {
	if ( function_exists( 'register_sidebar' ) ) {
		// Area 1, located at the top of the sidebar.
		register_sidebar( array(
			'name' => __( 'Primary Widget Area', 'nest' ),
			'id' => 'primary-widget-area',
			'description' => __( 'The primary widget area', 'nest' ),
			'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
			'after_widget' => '</li>',
			'before_title' => '<h3 class="widget-title">',
			'after_title' => '</h3>',
		) );
	}
}
add_action( 'widgets_init', 'nest_widgets_init' );

/* ========== Theme Setup ========== */

function nest_theme_setup() {

	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'automatic-feed-links' );

	register_nav_menus( array( 'primary' => __( 'Primary Navigation', 'nest' ) ) );

}
add_action( 'after_setup_theme', 'nest_theme_setup' );

function nest_init_scripts() {
	global $nest_options;
	wp_enqueue_script( 'jquery' );
	if( $nest_options['is_display_header_menu_arrow'] ) {
		wp_register_script( 'nest-menu-arrow', get_template_directory_uri() . '/scripts/nest-menu-arrow.js', array( 'jquery' ) );
		wp_enqueue_script( 'nest-menu-arrow' );
	}
	switch( $nest_options['menu_effect'] ) {
		case 'fade':
			wp_register_script( 'nest-menu-effect-fade', get_template_directory_uri() . '/scripts/nest-menu-effect-fade.js', array( 'jquery' ) );
			wp_enqueue_script( 'nest-menu-effect-fade' );
			break;
		case 'flexible':
			wp_register_script( 'nest-menu-effect-flexible', get_template_directory_uri() . '/scripts/nest-menu-effect-flexible.js', array( 'jquery' ) );
			wp_enqueue_script( 'nest-menu-effect-flexible' );
			break;
		default:
			wp_register_script( 'nest-menu-effect-none', get_template_directory_uri() . '/scripts/nest-menu-effect-none.js', array( 'jquery' ) );
			wp_enqueue_script( 'nest-menu-effect-none' );
			break;
	}
}
add_action('wp_enqueue_scripts', 'nest_init_scripts');

// set variable $content_width ( WP required )
if( $nest_options['sidebar_pos'] == 'none' ) {
	$content_width = 930;
} else {
	$content_width = 630;
}

// define post thumbnail image HTML
function nest_the_thumbnail() {
	global $post;
	$id = (int) $post->ID;
	if ( ! $id ) return false;
	$html = get_the_post_thumbnail( $id, array(180, 180) );
	if( ! empty( $html ) ){
		echo '<div class="thumbnail"><a href="' . get_permalink($id) . '">' . $html . '</a></div>';
	}
}

// redefine search form code ( HTML validation required )
function nest_my_search_form( $form ) {
	$form = '<form method="get" id="searchform" action="' . home_url( '/' ) . '" >
		<div>
		<input type="text" value="' . get_search_query() . '" name="s" id="s" />
		<input type="submit" id="searchsubmit" value="'. esc_attr__( 'Search' ) .'" />
		</div>
		</form>';
	return $form;
}
add_filter( 'get_search_form', 'nest_my_search_form' );

add_filter( 'use_default_gallery_style', '__return_false' );

?>
