<?php

if (!is_admin())
        add_action('wp_enqueue_scripts', 'photologger_js');
	function photologger_js() {
        wp_enqueue_style( 'photologger-style', get_stylesheet_uri() );
        if ( is_singular() && get_option( 'thread_comments' ) )
	wp_enqueue_script( 'comment-reply' );
}

add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 190, 130, true );

function photologger_main_image() {
$files = get_children('post_parent='.get_the_ID().'&post_type=attachment
&post_mime_type=image&order=desc');
  if($files) :
    $keys = array_reverse(array_keys($files));
    $j=0;
    $num = $keys[$j];
    $image=wp_get_attachment_image($num, 'large', true);
    $imagepieces = explode('"', $image);
    $imagepath = $imagepieces[1];
    $main=wp_get_attachment_url($num);
	$template=get_template_directory();
	$the_title= the_title_attribute( 'echo=0');
	print "<img src='$main' alt='$the_title' class='frame' />";
  endif;
}

function photologger_menu() {
  register_nav_menus(
    array( 'header-menu' => __( 'Header Menu', 'photologger' ) )
  );
}
add_action( 'init', 'photologger_menu' );


	$custom_header_support = array(
		'default-image'          => get_template_directory_uri() . '/headers/001.jpg',
		'width' => apply_filters( 'photologger_header_image_width', 966 ),
		'height' => apply_filters( 'photologger_header_image_height', 221 ),
		'header-text'            => false,
	);	
	
	add_theme_support( 'custom-header', $custom_header_support );
	
	register_default_headers( array(
		'bluesky' => array (
                'url' => '%s/headers/001.jpg',
                'thumbnail_url' => '%s/headers/thumbnails/001_thumb.jpg',
                'description' => __( 'Blue Sky', 'photologger' )
            ),
                'grass' => array (
                'url' => '%s/headers/002.jpg',
                'thumbnail_url' => '%s/headers/thumbnails/002_thumb.jpg',
                'description' => __( 'Grass and Sky', 'photologger' )
            ),
            	'wave' => array (
                'url' => '%s/headers/003.jpg',
                'thumbnail_url' => '%s/headers/thumbnails/003_thumb.jpg',
                'description' => __( 'Wave', 'photologger' )
            ),
	) );


add_theme_support( 'custom-background', array(
	'default-image' => get_stylesheet_directory_uri() . '',
	'default-color' => 'FFFFFF'
) );


add_filter('the_title', 'photologger_title');
function photologger_title($title) {
if ($title == '') {
return 'Untitled';
} else {
return $title;
}
}

function photologger_custom_excerpt_length( $length ) {
	return 15;
}
add_filter( 'excerpt_length', 'photologger_custom_excerpt_length', 999 );

function photologger_replace_excerpt($content) {
       return str_replace(' [...]',
               '...',
               $content
       );
}
add_filter('the_excerpt', 'photologger_replace_excerpt');


function photologger_widgets_init() {

		register_sidebar( array(
			'name' => __( 'Footer Left Area', 'photologger' ),
			'id' => 'footer-left-area',
			'description' => __( 'The left footer widget area.', 'photologger' ),
			'before_widget' => '<div class="footerwidget">',
			'after_widget' => '</div>',
			'before_title' => '<h2>',
			'after_title' => '</h2>',
		) );
		
		register_sidebar( array(
			'name' => __( 'Footer Right Area', 'photologger' ),
			'id' => 'footer-right-area',
			'description' => __( 'The right footer widget area.', 'photologger' ),
			'before_widget' => '<div class="footerwidget">',
			'after_widget' => '</div>',
			'before_title' => '<h2>',
			'after_title' => '</h2>',
		) );
}


function photologger_comment( $comment, $args, $depth ) {
	$GLOBALS['comment'] = $comment;
	switch ( $comment->comment_type ) :
		case '' :
	?>
	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
		<div id="comment-<?php comment_ID(); ?>">
		<div class="comment-author vcard">
			<?php echo get_avatar( $comment, 40 ); ?>
			<?php printf( __( '%s <span class="says">says:</span>', 'photologger' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
		</div><!-- .comment-author .vcard -->
		<?php if ( $comment->comment_approved == '0' ) : ?>
			<em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'photologger' ); ?></em>
			<br />
		<?php endif; ?>

		<div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
			<?php
				/* translators: 1: date, 2: time */
				printf( __( '%1$s at %2$s', 'photologger' ), get_comment_date(),  get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)', 'photologger' ), ' ' );
			?>
		</div><!-- .comment-meta .commentmetadata -->

		<div class="comment-body"><?php comment_text(); ?></div>

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

	<?php
			break;
		case 'pingback'  :
		case 'trackback' :
	?>
	<li class="post pingback">
		<p><?php _e( 'Pingback:', 'photologger' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( '(Edit)', 'photologger' ), ' ' ); ?></p>
	<?php
			break;
	endswitch;
}


//Required by WordPress
	add_theme_support('automatic-feed-links');
	
	
	//CONTENT WIDTH
		if ( ! isset( $content_width ) ) $content_width = 930;


//LOCALIZATION
	
	//Enable localization
		load_theme_textdomain('photologger',get_template_directory() . '/languages');
		
	
// filter function for wp_title
function photologger_filter_wp_title( $old_title, $sep, $sep_location ){
		// add padding to the sep
		$ssep = ' ' . $sep . ' ';
			
		// find the type of index page this is
		if( is_category() ) 
				$insert = $ssep . __( 'Category', 'photologger' );
		elseif( is_tag() ) 
				$insert = $ssep . __( 'Tag', 'photologger' );
		elseif( is_author() ) 
				$insert = $ssep . __( 'Author', 'photologger' );
		elseif( is_year() || is_month() || is_day() ) 
				$insert = $ssep . __( 'Archives', 'photologger' );
		else 
				$insert = NULL;
			
		// get the page number we're on (index)
		if( get_query_var( 'paged' ) )
				$num = $ssep . 'page ' . get_query_var( 'paged' );
			
		// get the page number we're on (multipage post)
		elseif( get_query_var( 'page' ) )
				$num = $ssep . 'page ' . get_query_var( 'page' );
			
		// else
		else $num = NULL;
			
		// concoct and return new title
return get_bloginfo( 'name' ) . $insert . $old_title . $num;
}

require_once ( get_stylesheet_directory() . '/theme-options.php' );

add_filter( 'wp_title', 'photologger_filter_wp_title', 10, 3 );
add_action( 'widgets_init', 'photologger_widgets_init' );
?>