<?php
//find first post that is a message
$this->thread = null;
Zend_Registry::get('log')->debug( "\n" . print_r($this->summary, true) . "\n" . __METHOD__ . ' #' . __LINE__ . ": \$this->summary \n");

foreach($this->summary['serviceData'] as $r)
	if(!empty($r['message']))
	{
		
		$this->thread = $r;
		$this->thread['pid'] = $this->pid;
		$this->thread['folder'] = $this->folder;
		break;
		
	}


?>
<div id="messageList" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
	<div id="primary_header">
	<span id="loading"></span>
	<h1>
		<span id="viewFolder">Facebook</span>
		<span id="viewmessageSubject">- Summary</span>
	</h1>
	<div id="top_right_button">
		<a href="#">
			<span class="stronglastword" id="newPostTopRight"><?php echo $this->translate('New Post') ?></span>
		</a>
	</div>
</div>

<style>
DIV#interior_search #closeButton {
position: absolute;
z-index: 999999;
top: 7px;
width: 20px;
left: 243px;
height: 24px;
cursor: pointer;
}
</style>

<div id="primary_content">
	<div id="primary_content_inner">
		<div id="sms_summary" class="mobileUI smsList facebookSummary">
			<form id='listfb'>
				<div id="interior_search">
					<div id="closeButton"></div>
					<input type="hidden" class="pid" name="pid" value="<?php echo $this->pid ?>" />
					<input type="hidden" class="folder" name="folder" value="<?php echo $this->folder ?>" />
					<input name="q" value="<?php echo $this->query ? $this->query : $this->translate('Search') ?>" />
				</div>
<?php //echo __FILE__ . "#" . __LINE__ . "\n<pre>" . print_r($this->summary,true) . "</pre>" ?>
<?php 	if( @empty($this->summary['data'][0])) : ?>
				<div style="margin: 10px;" id="noMessageDisplay">&nbsp;No messages.</div>	
<?php 	else : ?>
<?php 		foreach($this->summary['data'] as $k => $r) : ?>
<?php //skip unrecognised message types
	if( empty($r['message']) )
		continue;
?>
				<div from="<?php echo urlencode($r['from']['name']) ?>" class="messageRow mail_row read facebookSummaryRow<?php if($k == '0') echo ' selected' ?>" mid="<?php echo addslashes($r['id']) ?>">
                
                <div class="mailDate dateFormat1"><?php echo timeago( strtotime($r['created_time']) ) ?></div>
                
                <div class="mailPhoto">
                <span class="facebookPhotoLeft" title="<?php echo $this->thread['from']['name'] ?>" style="background: url('<?php echo "http://graph.facebook.com/" . $r['from']['id'] . "/picture/" ?>') !important;"></span>
                </div>
                
					<div class="mailFrom">
						<h3 title="<?php echo $r['from']['name'] ?>"><?php echo $r['from']['name'] ?></h3>
					</div>
					<div class="mailPreview"><?php echo iconv("UTF-8", "UTF-8//IGNORE", ((strlen($r['message']) > 70)? htmlentities(mb_substr($r['message'], 0, 70)) . '&hellip;' : htmlentities($r['message']))) ?></div>
	
				</div>
<?php 		endforeach ?>
<?php 	endif ?>
			</form>
		</div>

		<div id="mail_info">
			

		</div>
	</div>
</div>

<script language='Javascript'>

$(function() {

	myFbName = '<?php echo addslashes($this->myName) ?>'
	myid = '<?php echo addslashes($this->myId) ?>'
	myFbProfileImageUrl = '<?php echo $this->myProfileImageUrl ?>'
	
	enableMailActions();

	$("div.messageRow", ".facebookSummary").bind('click', function() {
		
		$('#mail_info').html('');
		$('#mail_info').block({ message: "<h1 class='loading-text'><img class='loadingimg' src='" + siteBaseUrl + "images/loading.gif' width='26' height='26'/><span class='loading'>Loading...</span></h1>", overlayCSS: {backgroundColor: 'transparent', opacity: 0.4}, css: {backgroundColor: 'transparent',color: '#575757',border: '0'} });
		
		var url = '<?php echo $this->moduleBaseUrl ?>/service/view/pid/<?php echo $this->pid ?>/folder/default/mid/' + $(this).attr('mid');
		$.php(url);
	
		$('.messageRow').removeClass('selected');
		$(this).addClass('selected'); 
	});
	//mark first as selected
	$("div.messageRow:first", ".facebookSummary").addClass('selected');
	
	if($("input[name=q]").val().length > 0)	{
		$("input[name=q]").focus();
	}
	
	$("input[name=q]").bind('click', function() {
		
		if($(this).val() == '<?php echo $this->translate("Search") ?>')
			$(this).val('');

	});

	$('#listfb').bind('submit', function() 
	{
		$('input[name=q]').searchMessages();
		return false;
	});

	var search_timeout = undefined;
	$('input[name=q]').bind('keyup', function() 
	{
		/*
		if ( search_timeout != undefined ) 
		{
			clearTimeout(search_timeout);
		}
		
		search_timeout = setTimeout(function() 
		{
			search_timeout = undefined;
			$('input[name=q]').search();
		}, 400);
		*/
	});
	
	$("DIV#interior_search #closeButton").bind('click', function()
	{
		
		$('input[name=q]').val('');
		$('input[name=q]').focus();	
	
		// Show results (all)
		$.php('<?php echo $this->moduleBaseUrl ?>/carrier/list/pid/<?php echo $this->pid ?>/folder/default');
		
	});
	$('#newPostTopRight').bind('click', function() {
		
		$.php('<?php echo $this->moduleBaseUrl ?>/service/new/pid/<?php echo $this->pid ?>/folder/default');
		
	})		
});

</script>