<?php
if (function_exists('register_sidebar')) register_sidebar();

function my_attachment_link($id) {
	// grab file extension
	$bits = explode('.',get_attached_file($id));
	$ext = '.'.$bits[count($bits)-1].' format';
	// get the icon link
	$icon_link = wp_get_attachment_link($id,'thumbnail',false,true);
	// get the text link
	$text_link = wp_get_attachment_link($id,'',false,false);
	// get the filesize in kilobytes
	 $filesize = ceil(filesize(get_attached_file($id))/1024).'K';
	 echo $icon_link. ' '.$text_link.' <small>('.$ext.' '.$filesize.')</small>';
}

function my_download_link($my_mime_type) {
	// create download link based on attachment mime type
	$download='';
	switch (true) {
		case (stristr($my_mime_type,'pdf')):
		$download = '<a href="http://www.adobe.com/products/acrobat/readstep2.html">Adobe Acrobat Reader</a>';
		break;

		case (stristr($my_mime_type,'msword')):
		$download = 'Microsoft<sup>&reg;</sup> Word or <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=95e24c87-8732-48d5-8689-ab826e7b8fdf">Word Viewer 2003</a>';
		break;

		case (stristr($my_mime_type,'excel')):
		$download = 'Microsoft<sup>&reg;</sup> Excel or <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=c8378bf4-996c-4569-b547-75edbd03aaf0">Excel Viewer 2003</a>';
		break;

		case (stristr($my_mime_type,'powerpoint')):
		$download = 'Microsoft<sup>&reg;</sup> Powerpoint or <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=428D5727-43AB-4F24-90B7-A94784AF71A4">Powerpoint Reader</a>';
		break;

		case (stristr($my_mime_type,'quicktime')):
		$download = '<a href="http://www.apple.com/quicktime/download/">Quicktime</a>';
		break;
	}
	if($download !='') echo '<p><small>This file requires '.$download.'.</small></p>';
}

/*
enhanced_link_pages() function taken from ePage Links plugin by Rich Pedley (http://quirm.net/)
Alternative for wp_link_pages to be able to specifiy wrappers for each link. Simply use <code>enhanced_link_pages(array('blink'=&gt;'&lt;li&gt;','alink'=&gt;'&lt;/li&gt;','before' =&gt; '&lt;ul&gt;', 'after' =&gt; '&lt;/ul&gt;', 'next_or_number' =&gt; 'number'));</code> in place of wp_link_pages in your themes. 
eg. enhanced_link_pages(array('blink'=>'<li>','alink'=>'</li>','before' => '<ul>', 'after' => '</ul>', 'next_or_number' => 'number')); 
*/
function enhanced_link_pages($args = '') {
	global $post;

	if ( is_array($args) )
		$r = &$args;
	else
		parse_str($args, $r);

	$defaults = array('before' => '<p>' . __('Pages:'), 'after' => '</p>', 'next_or_number' => 'number', 'nextpagelink' => __('Next page'),
			'previouspagelink' => __('Previous page'), 'pagelink' => '%', 'more_file' => '', 'echo' => 1, 'blink'=>'','alink'=>'');
	$r = array_merge($defaults, $r);
	extract($r, EXTR_SKIP);

	global $id, $page, $numpages, $multipage, $more, $pagenow;
	if ( $more_file != '' )
		$file = $more_file;
	else
		$file = $pagenow;

	$output = '';
	if ( $multipage ) {
		if ( 'number' == $next_or_number ) {
			$output .= $before;
			for ( $i = 1; $i < ($numpages+1); $i = $i + 1 ) {
				$j = str_replace('%',"$i",$pagelink);
				$output .= ' ';
				if ( ($i != $page) || ((!$more) && ($page==1)) ) {
					if ( 1 == $i ) {
						$output .= $blink.'<a href="' . get_permalink() . '">'.$j.'</a>'.$alink;
					} else {
						if ( '' == get_option('permalink_structure') || 'draft' == $post->post_status )
							$output .= $blink.'<a href="' . get_permalink() . '&amp;page=' . $i . '">'.$j.'</a>'.$alink;
						else
							$output .= $blink.'<a href="' . trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged') . '">'.$j.'</a>'.$alink;
					}
				}else{
					$output .= $blink.'<span>'.$j.'</span>'.$alink;
				}
			}
			$output .= $after;
		} else {
			if ( $more ) {
				$output .= $before;
				$i = $page - 1;
				if ( $i && $more ) {
					if ( 1 == $i ) {
						$output .= $blink.'<a href="' . get_permalink() . '">' . $previouspagelink . '</a>'.$alink;
					} else {
						if ( '' == get_option('permalink_structure') || 'draft' == $post->post_status )
							$output .= $blink.'<a href="' . get_permalink() . '&amp;page=' . $i . '">' . $previouspagelink . '</a>'.$alink;
						else
							$output .= $blink.'<a href="' . trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged') . '">' . $previouspagelink . '</a>'.$alink;
					}
				}
				$i = $page + 1;
				if ( $i <= $numpages && $more ) {
					if ( 1 == $i ) {
						$output .= $blink.'<a href="' . get_permalink() . '">' . $nextpagelink . '</a>'.$alink;
					} else {
						if ( '' == get_option('permalink_structure') || 'draft' == $post->post_status )
							$output .= $blink.'<a href="' . get_permalink() . '&amp;page=' . $i . '">' . $nextpagelink . '</a>'.$alink;
						else
							$output .= $blink.'<a href="' . trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged') . '">' . $nextpagelink . '</a>'.$alink;
					}
				}
				$output .= $after;
			}
		}
	}

	if ( $echo )
		echo $output;

	return $output;
}

/* adds backwardscomments compatibility for pre-WP 2.7 Requires legacy.comments.php in theme */
add_filter( 'comments_template', 'legacy_comments' );
function legacy_comments($file) {
	if(!function_exists('wp_list_comments')) $file = TEMPLATEPATH . '/legacy.comments.php';
	return $file;
}

/* customised comment template */
function my_theme_comments($comment, $args, $depth) {
	$GLOBALS['comment'] = $comment;
	static $my_comment_count=0;
	$my_comment_count++;
	?>
	<li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">
	<div id="div-comment-<?php comment_ID(); ?>">

	<?php echo get_avatar($comment,$size='32'); ?><?php printf(('<cite>%s</cite>'), get_comment_author_link()) ?> - <span class="commentdata"><a href="<?php echo htmlspecialchars(get_comment_link($comment->comment_ID ) ) ?>"><?php printf(('%1$s at %2$s'), get_comment_date(), get_comment_time()) ?></a></span>

	<div class="comment_text"><?php comment_text() ?></div>
	
	<?php if ($comment->comment_approved == '0') : ?>	<p class="moderated"><strong>This comment is currently in a moderation queue.</strong></p><?php endif; ?>

	<ul class="reply-edit">
	<li class="reply_to"><?php comment_reply_link(array_merge( $args, array('add_below' => 'div-comment', 'depth' => $depth, 'max_depth' => $args['max_depth'], 'reply_text' => 'Reply to comment '.$my_comment_count))) ?></li>
	<?php edit_comment_link(('Edit comment '.$my_comment_count),'<li class="edit_comment">','</li>') ?>
	</ul>
	
	</div>
	<?php
}

remove_shortcode('gallery');
add_shortcode('gallery', 'my_gallery_shortcode');
function my_gallery_shortcode($attr) {
	global $post;

	// Allow plugins/themes to override the default gallery template.
	$output = apply_filters('post_gallery', '', $attr);
	if ( $output != '' )  return $output;

	// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
	if ( isset( $attr['orderby'] ) ) {
		$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
		if ( !$attr['orderby'] ) unset( $attr['orderby'] );
	}

	extract(shortcode_atts(array(
		'order'      => 'ASC',
		'orderby'    => 'menu_order ID',
		'id'         => $post->ID,
		'itemtag'    => 'dl',
		'icontag'    => 'dt',
		'captiontag' => 'dd',
		'columns'    => 3,
		'size'       => 'thumbnail'
	), $attr));

	$id = intval($id);
	$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );

	if ( empty($attachments) ) return '';

	if ( is_feed() ) {
		$output = "\n";
		foreach ( $attachments as $id => $attachment )
			$output .= wp_get_attachment_link($id, $size, true) . "\n";
		return $output;
	}

	$itemtag = tag_escape($itemtag);
	$captiontag = tag_escape($captiontag);
	$columns = intval($columns);
	$itemwidth = $columns > 0 ? floor(100/$columns) : 100;

	$output = '<div class="gallery">'."\n";

	$i = 0;
	foreach ( $attachments as $id => $attachment ) {
		$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
		$output .= "\n<".$itemtag.' class="gallery-item" style="width:'.$itemwidth.'%;">'."\n";
		$output .= '<'.$icontag.' class="gallery-icon">'.$link.'</'.$icontag.'>';
		if ( $captiontag && trim($attachment->post_excerpt) ) {
			$output .= '<'.$captiontag.' class="gallery-caption">'.$attachment->post_excerpt.'</'.$captiontag.'>';
		}
		$output .= '</'.$itemtag.'>'."\n";
		if ( $columns > 0 && ++$i % $columns == 0 )
			$output .= "\n".'<br class="clear" />'."\n";
	}

	$output .= "\n".'<br class="clear" />'."</div>\n";

	return $output;
}

?>