<ul class="chat-box">
<?php
// $messages = $this->support->messages();
$messages = Crunchbutton_Support_Message::byPhone( $this->support->phone );
$last_id_support_message = 0;
foreach( $messages as $message ){
	$last_id_support_message = $message->id_support_message;
	$date = $message->date();
	switch ( $message->from ) {
		case Crunchbutton_Support_Message::TYPE_FROM_CLIENT:
			?>
				<li class="arrow-box-left">
					<div class="info">
						<span class="name"><strong>
							<?php if( $message->name ) { echo $message->name; } else { echo '<i>No name</i>'; } ?>
						</strong> <span class="badge badge-blue">user</span></span>
						<span class="time" title="<?=$date->format('M jS Y - g:i:s A');?>"><?php echo $message->relativeTime( false )?></span>
					</div>
				<?php echo nl2br( $message->body ); ?>
				<?php
					if ($message->media) : ?>
    <? foreach ($message->media as $media) : ?>
        <img src="<?=$media?>" class="support-image-history">
    <? endforeach; ?>
<? endif ; ?>

				</li>
			<?php
			break;
		case Crunchbutton_Support_Message::TYPE_FROM_REP:
			switch ( $message->type ) {
				case Crunchbutton_Support_Message::TYPE_SMS:
					?>
						<li class="arrow-box-right gray">
							<div class="info">
								<span class="name">
									<strong>
										<?php echo $message->admin()->name; ?>
									</strong>
								<span class="badge badge-green">SMS</span>
								</span>
								<span class="time" title="<?=$date->format('M jS Y - g:i:s A');?>"><?php echo $message->relativeTime( false ); ?></span>
							</div>
							<?php echo nl2br( $message->body ); ?>
							<?php
					if ($message->media) : ?>
    <? foreach ($message->media as $media) : ?>
        <img src="<?=$media?>" class="support-image-history">
    <? endforeach; ?>
<? endif ; ?>

						</li>
					<?php
					break;

				case Crunchbutton_Support_Message::TYPE_NOTE:
					?>
						<li class="box-note">
							<div class="info">
								<span class="name">
									<strong>
										<?php echo $message->admin()->name; ?>
									</strong>
								<span class="badge badge-green">NOTE</span>
								</span>
								<span class="time" title="<?=$date->format('M jS Y - g:i:s A');?>"><?php echo $message->relativeTime( false ); ?></span>
							</div>
							<?php echo nl2br( $message->body ); ?>
							<?php
					if ($message->media) : ?>
    <? foreach ($message->media as $media) : ?>
        <img src="<?=$media?>" class="support-image-history">
    <? endforeach; ?>
<? endif ; ?>

						</li>
					<?php
					break;
			}

			break;

		case Crunchbutton_Support_Message::TYPE_FROM_SYSTEM:
			?>
				<li class="box-note-system">
					<div class="info">
						<span class="name">
							<strong>System</strong>
							<span class="badge badge-gray">Log</span>
							<?php if( $message->visibility == 'external' ){ echo '<i class="icon-mobile-phone" title="sms"></i>'; } ?>
						</span>
						<span class="time" title="<?=$date->format('M jS Y - g:i:s A');?>"><?php echo $message->relativeTime( false ); ?></span>
					</div>
					<?php echo nl2br( $message->body ); ?>
					<?php
					if ($message->media) : ?>
    <? foreach ($message->media as $media) : ?>
        <img src="<?=$media?>" class="support-image-history">
    <? endforeach; ?>
<? endif ; ?>

				</li>
			<?php
			break;
	}
}
?>
</ul>
<script type="text/javascript">
if( SupportChats.chats && SupportChats.chats[ <?php echo $this->support->id_support ?> ] ){
	SupportChats.chats[ <?php echo $this->support->id_support ?> ].setLastIdSupportMessage( '<?php echo $last_id_support_message; ?>0' );
}
</script>