<?php
if ( !function_exists( 'grain_init' ) ) {
/* Set the file path based on whether the Grain Theme is a parent theme or child theme */
if ( STYLESHEETPATH == TEMPLATEPATH ) {
	define('GRAIN_URL', TEMPLATEPATH . '/options/');
	define('GRAIN_DIRECTORY', get_template_directory_uri() . '/options/');
} else {
	define('GRAIN_URL', STYLESHEETPATH . '/options/');
	define('GRAIN_DIRECTORY', get_stylesheet_directory_uri() . '/options/');
}
require_once (GRAIN_URL . 'grain-options-framework.php');
}
/* 
 * This is an example of how to add custom scripts to the options panel.
 * This one shows/hides the an option when a checkbox is clicked.
 */
add_action('grain_custom_scripts', 'grain_custom_scripts');
function grain_custom_scripts() { ?>
<script type="text/javascript">
jQuery(document).ready(function() {
	jQuery('#example_showhidden').click(function() {
  		jQuery('#section-example_text_hidden').fadeToggle(400);
	});
	
	if (jQuery('#example_showhidden:checked').val() !== undefined) {
		jQuery('#section-example_text_hidden').show();
	}
});
</script>
<?php
}

	if ( grain_get_option('layout', 'narrow-sidebar' ) == "wide-sidebar"){
		if ( ! isset( $content_width ) ){
			$content_width = 528;
		} 
		elseif( ! isset( $content_width ) ){
			$content_width = 653;
		}
	}
add_action( 'after_setup_theme', 'grain_setup' );
function grain_setup() {
add_theme_support( 'post-thumbnails' );
// link the post thumbnails to the post
add_filter( 'post_thumbnail_html', 'grain_post_image_html', 10, 3 );
function grain_post_image_html( $html, $post_id, $post_image_id ) {
	$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '">' . $html . '</a>';
	return $html;
}
add_custom_background();
if ( function_exists( 'register_nav_menu' ) ) {
	register_nav_menu( 'main-menu', 'Main Menu' );
}
add_theme_support('automatic-feed-links');
add_editor_style();
function grain_excerpt_length($length) {
	return 50;
}
add_filter('excerpt_length', 'grain_excerpt_length');
function grain_excerpt_more($more) {
       global $post;
	return '<a href="'. get_permalink($post->ID) . '"> Read On..</a>';
}
add_filter('excerpt_more', 'grain_excerpt_more');
	// Your changeable header business starts here
	if ( ! defined( 'HEADER_TEXTCOLOR' ) )
		define( 'HEADER_TEXTCOLOR', '' );
	// No CSS, just IMG call. The %s is a placeholder for the theme template directory URI.
	if ( ! defined( 'HEADER_IMAGE' ) )
		define( 'HEADER_IMAGE', '%s/images/headers/grass.png' );
	// The height and width of your custom header. You can hook into the theme's own filters to change these values.
	// Add a filter to grain_header_image_width and grain_header_image_height to change these values.
	define( 'HEADER_IMAGE_WIDTH', apply_filters( 'grain_header_image_width', 960	) );
	define( 'HEADER_IMAGE_HEIGHT', apply_filters( 'grain_header_image_height', 216 ) );
	// We'll be using post thumbnails for custom header images on posts and pages.
	// We want them to be 940 pixels wide by 216 pixels tall.
	// Larger images will be auto-cropped to fit, smaller ones will be ignored. See header.php.
	set_post_thumbnail_size( HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, true );
	// Don't support text inside the header image.
	if ( ! defined( 'NO_HEADER_TEXT' ) )
		define( 'NO_HEADER_TEXT', true );
	// Add a way for the custom header to be styled in the admin panel that controls
	// custom headers. See grain_admin_header_style(), below.
	add_custom_image_header( '', 'grain_admin_header_style' );
	// ... and thus ends the changeable header business.
	register_default_headers( array(
		'grass' => array(
			'url' => '%s/images/headers/grass.png',
			'thumbnail_url' => '%s/images/headers/grass-thumbnail.png',
			/* translators: header image description */
			'description' => __( 'grass', 'grain' )
		),
		'scene' => array(
			'url' => '%s/images/headers/scene.png',
			'thumbnail_url' => '%s/images/headers/scene-thumbnail.png',
			/* translators: header image description */
			'description' => __( 'scene', 'grain' )
		),
		'cat' => array(
			'url' => '%s/images/headers/cat.png',
			'thumbnail_url' => '%s/images/headers/cat-thumbnail.png',
			/* translators: header image description */
			'description' => __( 'cat', 'grain' )
		),
		'leaf' => array(
			'url' => '%s/images/headers/leaf.png',
			'thumbnail_url' => '%s/images/headers/leaf-thumbnail.png',
			/* translators: header image description */
			'description' => __( 'leaf', 'grain' )
		),
		'mount' => array(
			'url' => '%s/images/headers/mount.png',
			'thumbnail_url' => '%s/images/headers/mount-thumbnail.png',
			/* translators: header image description */
			'description' => __( 'mount', 'grain' )
		),
		'sky' => array(
			'url' => '%s/images/headers/sky.png',
			'thumbnail_url' => '%s/images/headers/sky-thumbnail.png',
			/* translators: header image description */
			'description' => __( 'sky', 'grain' )
		),
		'path' => array(
			'url' => '%s/images/headers/path.png',
			'thumbnail_url' => '%s/images/headers/path-thumbnail.png',
			/* translators: header image description */
			'description' => __( 'Path', 'grain' )
		),
		'sunset' => array(
			'url' => '%s/images/headers/sunset.png',
			'thumbnail_url' => '%s/images/headers/sunset-thumbnail.png',
			/* translators: header image description */
			'description' => __( 'Sunset', 'grain' )
		)
	) );
}	
	if ( ! function_exists( 'grain_admin_header_style' ) ) :
function grain_admin_header_style() {
?>
<style type="text/css">
/* Shows the same border as on front end */
#headimg {
	border-bottom: 1px solid #000;
	border-top: 4px solid #000;
	margin:0 auto;
}
/* If NO_HEADER_TEXT is false, you would style the text with these selectors:
	#headimg #name { }
	#headimg #desc { }
*/
</style>
<?php
}
endif;
// Make theme available for translation
// Translations can be filed in the /languages/ directory
load_theme_textdomain( 'grain', TEMPLATEPATH . '/languages' );
$grain_locale = get_locale();
$grain_locale_file = TEMPLATEPATH . "/languages/$grain_locale.php";
if ( is_readable($grain_locale_file) )
	require_once($grain_locale_file);
// Get the page number
function grain_get_page_number() {
    if (get_query_var('paged')) {
        print ' | ' . __( 'Page ' , 'grain') . get_query_var('paged');
    }
} // end get_page_number
function grain_styles(){
	wp_enqueue_style( 'grain_bangers', 'http://fonts.googleapis.com/css?family=Bangers' );
	wp_enqueue_style( 'grain_rainbow', 'http://fonts.googleapis.com/css?family=Over+the+Rainbow' );
	wp_enqueue_style( 'grain_shanti', 'http://fonts.googleapis.com/css?family=Shanti' );
	wp_enqueue_style( 'grain_custom', get_template_directory_uri() . '/custom.css' );
}
add_action( 'wp_print_styles', 'grain_styles' );
// For category lists on category archives: Returns other categories except the current one (redundant)
function grain_cats_meow($grain_glue) {
	$grain_current_cat = single_cat_title( '', false );
	$grain_separator = "\n";
	$grain_cats = explode( $grain_separator, get_the_category_list($grain_separator) );
	foreach ( $grain_cats as $grain_i => $grain_strings ) {
		if ( strstr( $grain_strings, ">$grain_current_cat<" ) ) {
			unset($grain_cats[$grain_i]);
			break;
		}
	}
	if ( empty($grain_cats) )
		return false;
	return trim(join( $grain_glue, $grain_cats ));
} // end cats_meow
// For tag lists on tag archives: Returns other tags except the current one (redundant)
function grain_tag_ur_it($grain_glue) {
	$grain_current_tag = single_tag_title( '', '',  false );
	$grain_separator = "\n";
	$grain_tags = explode( $grain_separator, get_the_tag_list( "", "$grain_separator", "" ) );
	foreach ( $grain_tags as $grain_i => $grain_strings ) {
		if ( strstr( $grain_strings, ">$grain_current_tag<" ) ) {
			unset($grain_tags[$grain_i]);
			break;
		}
	}
	if ( empty($grain_tags) )
		return false;
	return trim(join( $grain_glue, $grain_tags ));
} // end tag_ur_it

// Register widgetized areas
function grain_widgets_init() {
	// Area 1
  register_sidebar( array (
  'name' => 'Primary Widget Area',
  'id' => 'primary-widget-area',
  'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  'after_widget' => "</li>",
	'before_title' => '<h3 class="widget-title">',
	'after_title' => '</h3>',
  ) );


	// Area 2
  register_sidebar( array (
  'name' => 'Secondary Widget Area',
  'id' => 'secondary-widget-area', 
  'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  'after_widget' => "</li>",
	'before_title' => '<h3 class="widget-title">',
	'after_title' => '</h3>',
  ) );

	// Area 3
  register_sidebar( array (
  'name' => 'Teritiary Widget Area',
  'id' => 'teritiary-widget-area', 
  'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  'after_widget' => "</li>",
	'before_title' => '<h3 class="widget-title">',
	'after_title' => '</h3>',
  ) );

  // Footer column 1
    register_sidebar( array (
  'name' => 'first widget column',
  'id' => 'column-one', 
  'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  'after_widget' => "</li>",
	'before_title' => '<h3 class="widget-title">',
	'after_title' => '</h3>',
  ) );
  // Footer column 2
    register_sidebar( array (
  'name' => 'second widget column',
  'id' => 'column-two', 
  'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  'after_widget' => "</li>",
	'before_title' => '<h3 class="widget-title">',
	'after_title' => '</h3>',
  ) );
  // Footer column 3
    register_sidebar( array (
  'name' => 'third widget column',
  'id' => 'column-three', 
  'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  'after_widget' => "</li>",
	'before_title' => '<h3 class="widget-title">',
	'after_title' => '</h3>',
  ) );
} // end theme_widgets_init

add_action( 'widgets_init', 'grain_widgets_init' );


// Pre-set Widgets
$grain_preset_widgets = array (
	'primary-aside'  => array( 'search', 'pages', 'categories', 'archives' ),
	'secondary-aside'  => array( 'links', 'meta' )
);
if ( isset( $_GET['activated'] ) ) {
	update_option( 'sidebars_widgets', $grain_preset_widgets );
}


// Check for static widgets in widget-ready areas
function grain_is_sidebar_active( $grain_index ){
  global $wp_registered_sidebars;

  $grain_widgetcolums = wp_get_sidebars_widgets();
		 
  if (isset($grain_widgetcolums[$grain_index]) && $grain_widgetcolums[$grain_index]) return true;
  
	return false;
} // end grain_is_sidebar_active

// Produces an avatar image with the hCard-compliant photo class
function grain_commenter_link() {
	$commenter = get_comment_author_link();
	if ( ereg( '<a[^>]* class=[^>]+>', $commenter ) ) {
		$commenter = ereg_replace( '(<a[^>]* class=[\'"]?)', '\\1url ' , $commenter );
	} else {
		$commenter = ereg_replace( '(<a )/', '\\1class="url "' , $commenter );
	}
	$avatar_email = get_comment_author_email();
	$avatar = str_replace( "class='avatar", "class='photo avatar", get_avatar( $avatar_email, 80 ) );
	echo $avatar . ' <span class="fn n">' . $commenter . '</span>';
} // end grain_commenter_link

// Custom callback to list pings
function grain_custom_pings($comment, $args, $depth) {
       $GLOBALS['comment'] = $comment;
        ?>
    		<li id="comment-<?php comment_ID() ?>" class="<?php comment_class() ?>">
    			<div class="comment-author"><?php printf(__('By %1$s on %2$s at %3$s', 'grain'),
    					get_comment_author_link(),
    					get_comment_date(),
    					get_comment_time() );
    					edit_comment_link(__('Edit', 'grain'), ' <span class="meta-sep">|</span> <span class="edit-link">', '</span>'); ?></div>
    <?php if ($comment->comment_approved == '0') _e('\t\t\t\t\t<span class="unapproved">Your trackback is awaiting moderation.</span>\n', 'grain') ?>
            <div class="comment-content">
    			<?php comment_text() ?>
			</div>
<?php } // end grain_custom_pings

