<?php
if ( ! isset( $content_width ) )
	$content_width = 521;

add_action( 'after_setup_theme', 'designtidbits_setup' );

if ( ! function_exists( 'designtidbits_setup' ) ):

function designtidbits_setup() {

	add_editor_style();

	add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );

	add_theme_support( 'post-thumbnails' );

	add_theme_support( 'automatic-feed-links' );

	load_theme_textdomain( 'designtidbits', TEMPLATEPATH . '/languages' );

	$locale = get_locale();
	$locale_file = TEMPLATEPATH . "/languages/$locale.php";
	if ( is_readable( $locale_file ) )
		require_once( $locale_file );

	register_nav_menus( array(
		'primary' => __( 'Primary Navigation', 'designtidbits' ),
	) );
}
endif;


function designtidbits_page_menu_args( $args ) {
	$args['show_home'] = true;
	return $args;
}
add_filter( 'wp_page_menu_args', 'designtidbits_page_menu_args' );

function designtidbits_excerpt_length( $length ) {
	return 40;
}
add_filter( 'excerpt_length', 'designtidbits_excerpt_length' );

function designtidbits_continue_reading_link() {
	return ' ';
}

function designtidbits_auto_excerpt_more( $more ) {
	return ' &hellip;' . designtidbits_continue_reading_link();
}
add_filter( 'excerpt_more', 'designtidbits_auto_excerpt_more' );


function designtidbits_custom_excerpt_more( $output ) {
	if ( has_excerpt() && ! is_attachment() ) {
		$output .= designtidbits_continue_reading_link();
	}
	return $output;
}
add_filter( 'get_the_excerpt', 'designtidbits_custom_excerpt_more' );


add_filter( 'use_default_gallery_style', '__return_false' );


function designtidbits_remove_gallery_css( $css ) {
	return preg_replace( "#<style type='text/css'>(.*?)</style>#s", '', $css );
}


if ( version_compare( $GLOBALS['wp_version'], '3.1', '<' ) )
	add_filter( 'gallery_style', 'designtidbits_remove_gallery_css' );

if ( ! function_exists( 'designtidbits_comment' ) ) :

function designtidbits_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>', 'designtidbits' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
		</div>
		<?php if ( $comment->comment_approved == '0' ) : ?>
			<em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'designtidbits' ); ?></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', 'designtidbits' ), get_comment_date(),  get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)', 'designtidbits' ), ' ' );
			?>
		</div>

		<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>
	</div>

	<?php
			break;
		case 'pingback'  :
		case 'trackback' :
	?>
	<li class="post pingback">
		<p><?php _e( 'Pingback:', 'designtidbits' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( '(Edit)', 'designtidbits' ), ' ' ); ?></p>
	<?php
			break;
	endswitch;
}
endif;

function designtidbits_widgets_init() {
	// Area 1, located at the top of the sidebar.
	register_sidebar( array(
		'name' => __( 'Primary Widget Area', 'designtidbits' ),
		'id' => 'primary-widget-area',
		'description' => __( 'The primary widget area', 'designtidbits' ),
		'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, located below the Primary Widget Area in the sidebar. Empty by default.
	register_sidebar( array(
		'name' => __( 'Secondary Widget Area', 'designtidbits' ),
		'id' => 'secondary-widget-area',
		'description' => __( 'The secondary widget area', 'designtidbits' ),
		'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', 'designtidbits_widgets_init' );


function designtidbits_remove_recent_comments_style() {
	add_filter( 'show_recent_comments_widget_style', '__return_false' );
}
add_action( 'widgets_init', 'designtidbits_remove_recent_comments_style' );

if ( ! function_exists( 'designtidbits_posted_on' ) ) :


function designtidbits_posted_on() {
	printf( __( '<span class="%1$s">Posted on</span> %2$s <span class="meta-sep">by</span> %3$s', 'designtidbits' ),
		'meta-prep meta-prep-author',
		sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
			get_permalink(),
			esc_attr( get_the_time() ),
			get_the_date()
		),
		sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
			get_author_posts_url( get_the_author_meta( 'ID' ) ),
			sprintf( esc_attr__( 'View all posts by %s', 'designtidbits' ), get_the_author() ),
			get_the_author()
		)
	);
}
endif;

if ( ! function_exists( 'designtidbits_posted_in' ) ) :


function designtidbits_posted_in() {
	
	$tag_list = get_the_tag_list( '', ', ' );
	if ( $tag_list ) {
		$posted_in = __( 'This entry was posted in %1$s and tagged %2$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'designtidbits' );
	} elseif ( is_object_in_taxonomy( get_post_type(), 'category' ) ) {
		$posted_in = __( 'This entry was posted in %1$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'designtidbits' );
	} else {
		$posted_in = __( 'Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'designtidbits' );
	}
	
	printf(
		$posted_in,
		get_the_category_list( ', ' ),
		$tag_list,
		get_permalink(),
		the_title_attribute( 'echo=0' )
	);
}
endif;

/*
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Plugin Name: Gravatar
Plugin URI: http://www.gravatar.com/implement.php#section_2_2
*/

function gravatar($rating = false, $size = false, $default = false, $border = false) {
	global $comment;
	$out = "http://www.gravatar.com/avatar.php?gravatar_id=".md5($comment->comment_author_email);
	if($rating && $rating != '')
		$out .= "&amp;rating=".$rating;
	if($size && $size != '')
		$out .="&amp;size=".$size;
	if($default && $default != '')
		$out .= "&amp;default=".urlencode($default);
	if($border && $border != '')
		$out .= "&amp;border=".$border;
	echo $out;
}

/*
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Plugin Name: Post Images
*/

function get_post_images($size = 'thumbnail',$post_id='') {   //thumbnail, medium, large or full
	global $wp_query, $wpdb, $post;
	if($post_id=='')	
		$post_id = $post->ID;
	
	$querystr = "
			    SELECT p.* 
			    FROM $wpdb->posts p
			    WHERE (p.post_mime_type='image/gif'
			    		or p.post_mime_type='image/png'
			    		or p.post_mime_type='image/jpeg'
			    		) and p.post_type='attachment' and post_parent = '$post_id' 
			    ORDER BY p.menu_order limit 1
			 ";
	
 	$photo = $wpdb->get_results($querystr, OBJECT);
 	
	
	if ($photo) {
		$att =  wp_get_attachment_image_src($photo[0]->ID, $size);
		
		//$alt = get_post_meta($photo[0]->ID, '_wp_attachment_image_alt', true);
		//$att[0]['title'] = $photo[0]->post_title;;
			
		return $att[0];
	}
	return false;
}

function content($limit) {
  $content = explode(' ', get_the_content(), $limit);
  if (count($content)>=$limit) {
    array_pop($content);
    $content = implode(" ",$content).'...';
  } else {
    $content = implode(" ",$content);
  }	
  $content = apply_filters('the_content', $content); 
  return $content;
}

function improved_trim_excerpt($text) {
        global $post;
        if ( '' == $text ) {
                $text = get_the_content('');
                $text = apply_filters('the_content', $text);
                $text = str_replace('\]\]\>', ']]&gt;', $text);
                $text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
                $text = strip_tags($text, '<p>'.'<small>'.'<strong>'.'<h1>'.'<h2>'.'<h3>'.'<h4>'.'<h5>'.'<h6>');
                $excerpt_length = 55;
                $words = explode(' ', $text, $excerpt_length + 1);
                if (count($words)> $excerpt_length) {
                        array_pop($words);
                        array_push($words, '[...]');
                        $text = implode(' ', $words);
                }
        }
        return $text;
}
remove_filter('the_excerpt', 'wp_trim_excerpt');
add_filter('the_excerpt', 'improved_trim_excerpt');

/*
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Plugin Name: DDThemes - Logo Options
Plugin URI: http://www.designdisease.com/
*/

//ADD OPTION PAGE
add_action('admin_menu', 'rtlogoop_admin');

//UPON ACTIVATION OR PREVIEWED
if ( $_GET['activated'] == 'true'  || $_GET['preview'] == 1 )
{
	rtlogoop_setup();
}

function rtlogoop_admin() 
{
	/* PROCESS OPTION SAVING HERE */
	if ( 'save' == $_REQUEST['action'] ) 
	{
		if ( $_REQUEST['savetype'] == 'header' )
		{
			update_option( 'rtlogoop_header', $_REQUEST['rtlogoop_header']);
		}

	}

	/* SHOW THEME CUSTOMIZE PAGE HERE */
	add_theme_page(__('Logo Options'), __('Logo Options'), 'edit_themes', basename(__FILE__), 'rtlogoop_headeropt_page');
}

function rtlogoop_headeropt_page()
{ ?>
<style type="text/css">
<!--
.select { background: #fff; padding: 10px; border: solid 1px #ccc;}
.hr { border: none; border-top:1px dotted #abb0b5; height : 1px;}
.note { color:#999; font-size: 11px;}
.note a, .note a:visited, .note a:hover { color:#999; text-decoration: underline;}
-->
</style>

<div class="wrap">
<div id="icon-themes" class="icon32"><br /></div>
<h2><a href="http://designtidbits.com/">Design Tidbits</a> - Logo Options</h2>
<hr class="hr" />

<?php
	if ( $_REQUEST['action'] == 'save' ) echo '<div id="message" class="updated fade"><p><strong>Settings saved.</strong></p></div>';
	?>
	<form method="post">
		<p class="select">
        <strong>Select Image Logo or Text Logo:</strong>&nbsp;&nbsp;<label for="rtlogoop_header_text"><input type="radio" name="rtlogoop_header" value="text" id="rtlogoop_header_text" <?php if ( get_option('rtlogoop_header') == 'text' ) echo 'checked="checked"'?> /> 
		  Text Logo</label> <label for="rtlogoop_header_logo">&nbsp;
	    <input type="radio" name="rtlogoop_header" value="logo" id="rtlogoop_header_logo" <?php if ( get_option('rtlogoop_header') == 'logo' ) echo 'checked="checked"'?> /> Image</label> 
		  Logo</p>
         <ul>
          <li>1. <strong>Text Logo</strong> displays your Site Title and Tagline as your logo.  You can change your Site Title and Tagline in your General Settings.  This option is chosen by default.</li>
          <li>2. <strong>Image Logo</strong> displays your custom made logo.  Simply upload your logo to the images folder of the theme and make sure it is named <strong>logo.png</strong>.</li>
      </ul>            
<p class="submit">
<input type="hidden" name="savetype" value="header" />
<input name="save" type="submit" value="Save changes" />
<input type="hidden" name="action" value="save" />
</p>
</form>
<hr class="hr" />
</div>


<?php } function rtlogoop_setup() 
{ if ( get_option('rtlogoop_header') == '' )
{ update_option('rtlogoop_header', 'text');}
}
?>