Index: plugins/attachments/_admin.php
===================================================================
--- plugins/attachments/_admin.php	(revision 1537)
+++ plugins/attachments/_admin.php	(revision 1501)
@@ -12,51 +12,56 @@
 if (!defined('DC_CONTEXT_ADMIN')) { return; }
 
-$core->addBehavior ('adminPostFormSidebar',array('attachmentAdmin','adminPostFormSidebar'));
+$core->addBehavior ('adminPostFormItems',array('attachmentAdmin','adminPostFormItems'));
 $core->addBehavior ('adminPostAfterForm',array('attachmentAdmin','adminPostAfterForm'));
+$core->addBehavior('adminPostHeaders',array('attachmentAdmin','postHeaders'));
 
 class attachmentAdmin
 {
-	public static function adminPostFormSidebar($post) 
+	public static function postHeaders()
+	{
+		return 
+		'<script type="text/javascript" src="index.php?pf=attachments/js/post.js"></script>';
+	}
+	public static function adminPostFormItems($main,$sidebar,$post) 
 	{
 		if ($post !== null)
 		{
 			$core =& $GLOBALS['core'];
-			if ( isset($core->media) ) {
-				$post_media = $core->media->getPostMedia($post->post_id);
-				echo
-				'<h3 class="clear">'.__('Attachments').'</h3>';
-				foreach ($post_media as $f)
-				{
-					$ftitle = $f->media_title;
-					if (strlen($ftitle) > 18) {
-						$ftitle = substr($ftitle,0,16).'...';
-					}
-					echo
-					'<div class="media-item">'.
-					'<a class="media-icon" href="media_item.php?id='.$f->media_id.'">'.
-					'<img src="'.$f->media_icon.'" alt="" title="'.$f->basename.'" /></a>'.
-					'<ul>'.
-					'<li><a class="media-link" href="media_item.php?id='.$f->media_id.'" '.
-					'title="'.$f->basename.'">'.$ftitle.'</a></li>'.
-					'<li>'.$f->media_dtstr.'</li>'.
-					'<li>'.files::size($f->size).' - '.
-					'<a href="'.$f->file_url.'">'.__('open').'</a>'.'</li>'.
-					
-					'<li class="media-action"><a class="attachment-remove" id="attachment-'.$f->media_id.'" '.
-					'href="post_media.php?post_id='.$post->post_id.'&amp;media_id='.$f->media_id.'&amp;remove=1">'.
-					'<img src="images/check-off.png" alt="'.__('remove').'" /></a>'.
-					'</li>'.
-					
-					'</ul>'.
-					'</div>';
+			$post_media = $core->media->getPostMedia($post->post_id);
+			$nb_media = count($post_media);
+			$title = !$nb_media ? __('Attachments') : sprintf(__('Attachments (%d)'),$nb_media);
+			$item = '<h5 class="clear s-attachments">'.$title.'</h5>';
+			foreach ($post_media as $f)
+			{
+				$ftitle = $f->media_title;
+				if (strlen($ftitle) > 18) {
+					$ftitle = substr($ftitle,0,16).'...';
 				}
-				unset($f);
+				$item .=
+				'<div class="media-item s-attachments">'.
+				'<a class="media-icon" href="media_item.php?id='.$f->media_id.'">'.
+				'<img src="'.$f->media_icon.'" alt="" title="'.$f->basename.'" /></a>'.
+				'<ul>'.
+				'<li><a class="media-link" href="media_item.php?id='.$f->media_id.'" '.
+				'title="'.$f->basename.'">'.$ftitle.'</a></li>'.
+				'<li>'.$f->media_dtstr.'</li>'.
+				'<li>'.files::size($f->size).' - '.
+				'<a href="'.$f->file_url.'">'.__('open').'</a>'.'</li>'.
+				
+				'<li class="media-action"><a class="attachment-remove" id="attachment-'.$f->media_id.'" '.
+				'href="post_media.php?post_id='.$post->post_id.'&amp;media_id='.$f->media_id.'&amp;remove=1">'.
+				'<img src="images/check-off.png" alt="'.__('remove').'" /></a>'.
+				'</li>'.
+				
+				'</ul>'.
+				'</div>';
 			}
+			unset($f);
 			
 			if (empty($post_media)) {
-				echo '<p>'.__('No attachment.').'</p>';
-			} else {
-			}
-			echo '<p><a class="button" href="media.php?post_id='.$post->post_id.'">'.__('Add files to this entry').'</a></p>';
+				$item .= '<p class="form-note s-attachments">'.__('No attachment.').'</p>';
+			} 
+			$item .= '<p class="s-attachments"><a class="button" href="media.php?post_id='.$post->post_id.'">'.__('Add files to this entry').'</a></p>';
+			$sidebar['metas-box']['items']['attachments']= $item;
 		}
 	}
Index: plugins/attachments/_public.php
===================================================================
--- plugins/attachments/_public.php	(revision 1266)
+++ plugins/attachments/_public.php	(revision 1280)
@@ -40,16 +40,12 @@
 		"<?php\n".
 		'if ($_ctx->posts !== null && $core->media) {'."\n".
-		'$_ctx->attachments = $core->media->getPostMedia($_ctx->posts->post_id, null, true);'."\n".
+			'$_ctx->attachments = new ArrayObject($core->media->getPostMedia($_ctx->posts->post_id));'."\n".
 		"?>\n".
 		
-		'<?php $attach_i = 0; ?>'. // LEGACY
-		'<?php while ($_ctx->attachments->fetch()) : ?>'.
-		
-		'<?php $GLOBALS[\'attach_i\'] = $attach_i; $attach_f = $_ctx->attachments; $GLOBALS[\'attach_f\'] = $attach_f;'. // LEGACY
-		'$_ctx->file_url = $attach_f->file_url; ?>'. // LEGACY
+		'<?php foreach ($_ctx->attachments as $attach_i => $attach_f) : '.
+		'$GLOBALS[\'attach_i\'] = $attach_i; $GLOBALS[\'attach_f\'] = $attach_f;'.
+		'$_ctx->file_url = $attach_f->file_url; ?>'.
 		$content.
-		'<?php $attach_i++; ?>'. // LEGACY
-		'<?php endwhile; '.
-		'$_ctx->attachments = null; ?>'.
+		'<?php endforeach; $_ctx->attachments = null; unset($attach_i,$attach_f,$_ctx->file_url); ?>'.
 		
 		"<?php } ?>\n";
@@ -64,5 +60,5 @@
 	{
 		return
-		"<?php if (\$_ctx->attachments->isStart()) : ?>".
+		"<?php if (\$attach_i == 0) : ?>".
 		$content.
 		"<?php endif; ?>";
@@ -75,5 +71,5 @@
 	{
 		return
-		"<?php if (\$_ctx->attachments->isEnd()) : ?>".
+		"<?php if (\$attach_i+1 == count(\$_ctx->attachments)) : ?>".
 		$content.
 		"<?php endif; ?>";
@@ -97,15 +93,15 @@
 		if (isset($attr['is_image'])) {
 			$sign = (boolean) $attr['is_image'] ? '' : '!';
-			$if[] = $sign.'$_ctx->attachments->media_image';
+			$if[] = $sign.'$attach_f->media_image';
 		}
 		
 		if (isset($attr['has_thumb'])) {
 			$sign = (boolean) $attr['has_thumb'] ? '' : '!';
-			$if[] = $sign.'isset($_ctx->attachments->media_thumb[\'sq\'])';
+			$if[] = $sign.'isset($attach_f->media_thumb[\'sq\'])';
 		}
 		
 		if (isset($attr['is_mp3'])) {
 			$sign = (boolean) $attr['is_mp3'] ? '==' : '!=';
-			$if[] = '$_ctx->attachments->type '.$sign.' "audio/mpeg3"';
+			$if[] = '$attach_f->type '.$sign.' "audio/mpeg3"';
 		}
 		
@@ -113,7 +109,7 @@
 			$sign = (boolean) $attr['is_flv'] ? '' : '!';
 			$if[] = $sign.
-				'($_ctx->attachments->type == "video/x-flv" || '.
-				'$_ctx->attachments->type == "video/mp4" || '.
-				'$_ctx->attachments->type == "video/x-m4v")';
+				'($attach_f->type == "video/x-flv" || '.
+				'$attach_f->type == "video/mp4" || '.
+				'$attach_f->type == "video/x-m4v")';
 		}
 		
@@ -131,5 +127,5 @@
 	{
 		$f = $GLOBALS['core']->tpl->getFilters($attr);
-		return '<?php echo '.sprintf($f,'$_ctx->attachments->type').'; ?>';
+		return '<?php echo '.sprintf($f,'$attach_f->type').'; ?>';
 	}
 	
@@ -140,5 +136,5 @@
 	{
 		$f = $GLOBALS['core']->tpl->getFilters($attr);
-		return '<?php echo '.sprintf($f,'$_ctx->attachments->media_type').'; ?>';
+		return '<?php echo '.sprintf($f,'$attach_f->media_type').'; ?>';
 	}
 	
@@ -149,5 +145,5 @@
 	{
 		$f = $GLOBALS['core']->tpl->getFilters($attr);
-		return '<?php echo '.sprintf($f,'$_ctx->attachments->basename').'; ?>';
+		return '<?php echo '.sprintf($f,'$attach_f->basename').'; ?>';
 	}
 	
@@ -162,7 +158,7 @@
 		$f = $GLOBALS['core']->tpl->getFilters($attr);
 		if (!empty($attr['full'])) {
-			return '<?php echo '.sprintf($f,'$_ctx->attachments->size').'; ?>';
-		}
-		return '<?php echo '.sprintf($f,'files::size($_ctx->attachments->size)').'; ?>';
+			return '<?php echo '.sprintf($f,'$attach_f->size').'; ?>';
+		}
+		return '<?php echo '.sprintf($f,'files::size($attach_f->size)').'; ?>';
 	}
 	
@@ -173,5 +169,5 @@
 	{
 		$f = $GLOBALS['core']->tpl->getFilters($attr);
-		return '<?php echo '.sprintf($f,'$_ctx->attachments->media_title').'; ?>';
+		return '<?php echo '.sprintf($f,'$attach_f->media_title').'; ?>';
 	}
 	
@@ -184,6 +180,6 @@
 		return
 		'<?php '.
-		'if (isset($_ctx->attachments->media_thumb[\'sq\'])) {'.
-			'echo '.sprintf($f,'$_ctx->attachments->media_thumb[\'sq\']').';'.
+		'if (isset($attach_f->media_thumb[\'sq\'])) {'.
+			'echo '.sprintf($f,'$attach_f->media_thumb[\'sq\']').';'.
 		'}'.
 		'?>';
@@ -196,5 +192,5 @@
 	{
 		$f = $GLOBALS['core']->tpl->getFilters($attr);
-		return '<?php echo '.sprintf($f,'$_ctx->attachments->file_url').'; ?>';
+		return '<?php echo '.sprintf($f,'$attach_f->file_url').'; ?>';
 	}
 	
Index: plugins/attachments/js/post.js
===================================================================
--- plugins/attachments/js/post.js	(revision 1606)
+++ plugins/attachments/js/post.js	(revision 1606)
@@ -0,0 +1,6 @@
+$(function() {
+	$('h5.s-attachments').toggleWithLegend($('.s-attachments').not('h5'),{
+		cookie: 'dcx_attachments',
+		legend_click: true
+	});
+});
