<?php
$this->log = Zend_Registry::get('log');

if( $this->testing) : ?>

<link rel="stylesheet" href="<?php echo $this->siteBaseUrl ?>/css/common.css" type="text/css" media="screen, print" />
<?php endif; ?>
<ul id="nav_secondary">
	
<li class="nav-category"><h1><?php print $this->translate('Mailboxes')?></h1></li>	
	
<?php

// first level of folders are namespaces so handle differently
$namespaces = $this->foldersObject;


foreach( $this->mainFolders as $mainFolderGlobal )
{

	$folderObject = $this->AtmailMailStorageMain->getFolderObject( $mainFolderGlobal );

	$localName = $folderObject->getLocalName();
	$localNameSelectorSafe = strtolower($localName);
	$globalName = $folderObject->getGlobalName();
	$urlSafeGlobalName = urlencode( urlencode( $globalName ) ); //ZF decodes before looking for actions so double encode

	if($localName == 'INBOX')
		$localName = 'Inbox';

	if( isset($this->folderTotals[$globalName]['exists']) )
	{

		$totalMessages = $this->folderTotals[$globalName]['exists'];

	}
	else
	{

		$totalMessages = 0;

	}

	$isSelectable = $folderObject->isSelectable()?'true':'false';

?>
			<li>
				<div isSelectable="<?php echo $isSelectable ?>" isMainFolder="true" foldernameglobal="<?php echo $urlSafeGlobalName ?>" id="folder_<?php echo $localNameSelectorSafe ?>"<?php echo ($this->currentFolder==$globalName?' class="current"':'') ?>>
					<a tabindex="0" href="<?php echo $this->moduleBaseUrl ?>/mail/listfoldermessages/selectFolder/<?php echo $urlSafeGlobalName ?>" onclick="prepareMailList($(this).parent())">
						<span class="label"><?php echo htmlspecialchars( $this->translate( $this->AtmailMailStorageMain->decodeUTF7($localName) ) ) ?></span>
						<?php if($this->currentFolder==$globalName): ?>
						<span class="offscreen" id="nav2-marker"> - selected</span>
						<?php endif ?>
						<span class="unread"<?php echo ($totalMessages==0 ? ' style="display:none;"' : '') ?>><strong><?php echo $totalMessages ?></strong></span>
					</a>
				</div>
			</li>
<?php
}

 ?>
<?php if( $this->UCDBEnabled ) : ?>
<li class="filter nav-category"><span><?php print $this->translate('PHONE') . ($this->selectedSIPAccount['PhoneNumber']!='null'?' ( <div id="sip_folder_title_phonenumber" style="display:inline;">' . $this->selectedSIPAccount['PhoneNumber'] . '</div> )':'') ?></span></li>	
<?php echo $this->partial('global/voicemail.phtml', $this); ?>
<?php endif ?>

<li id="folder_actions" class="nav-category"><h1><?php print $this->translate('Folders')?></h1>
	<span tabindex="0" class="folder-remove" title="<?php echo $this->translate('Delete Current Folder')?>"><?php echo $this->translate('Delete Current Folder')?></span>
	<span tabindex="0" class="folder-add" title="<?php echo $this->translate('Add New Folder')?>"><?php echo $this->translate('Add New Folder')?></span>
</li>

<?php


$namespaces->rewind();

$recursionArgs = array(
	'moduleBaseUrl' => $this->moduleBaseUrl,
	'AtmailMailStorageMain' => $this->AtmailMailStorageMain,
	'currentFolder' => $this->currentFolder,
	'folderChildren' => $namespaces,
	'mainFolders' => $this->mainFolders,
	'renderMainFolders' => false
);

echo $this->partial('mail/indexfolderlistnoderecursive.phtml', $recursionArgs );

$namespaces->next();

while( $namespaces->current() != false )
{

    $recursionArgs = array(
		'moduleBaseUrl' => $this->moduleBaseUrl,
		'AtmailMailStorageMain' => $this->AtmailMailStorageMain,
		'currentFolder' => $this->currentFolder,
		'folderChildren' => $namespaces->current(),
		'mainFolders' => $this->mainFolders,
		'renderMainFolders' => false
	);
	echo $this->partial('mail/indexfolderlistnoderecursive.phtml', $recursionArgs );
	$namespaces->next();

}

?>

<!--
<li class="filter nav-category"> <h4><?php print $this->translate('Filters')?></h4> </li>	

<li class="filter">
	<div class="unread notifications" isseletable="true" isfilter="true" filtertype="unread">
		 <a href="#" tabindex="0">
			<span class="label"><?php print $this->translate('Unread')?></span>
		</a> 
	</div>
</li>

<li class="filter">
	<div class="flagged" isseletable="true" isfilter="true" filtertype="flagged">
		 <a href="#" tabindex="0">
			<span class="label"><?php print $this->translate('Flagged')?></span>
		</a> 
	</div>
</li>

<li class="filter">
	<div class="purchases" isseletable="true" isfilter="true" filtertype="purchases">
		 <a><span class="label"><?php print $this->translate('Purchases')?></span></a> 
	</div>
</li>

<li class="filter">
	<div class="friends" isseletable="true" isfilter="true" filtertype="friends">
		 <a><span class="label"><?php print $this->translate('Friends')?></span></a> 
	</div>
</li>

<li class="filter">
	<div class="newsletters" isseletable="true" isfilter="true" filtertype="newsletters">
		<a href="#">
			 <span class="label"><?php print $this->translate('Newsletters')?></span> 
		</a>
	</div>
</li>

<li class="filter">
	<div class="notifications" isseletable="true" isfilter="true" filtertype="notifications">
		<a href="#">
			 <span class="label"><?php print $this->translate('Notifications')?></span> 
		</a>
	</div>
</li>
-->


</ul>

<div id="flash"></div><div id="errors"></div><div id="notices"></div>


<ul id="folder_actions">
	<li id="folder_add" title='<?php print $this->translate('Add New Folder')?>'><a><?php print $this->translate('FOLDERS')?></a></li>


	<li id="folder_remove" title='<?php print $this->translate('Delete Folder')?>'><a class="<?php echo $this->currentGroup=='All'?'disabled':'enabled' ?>"><?php print $this->translate('Delete Folder')?></a></li>
	<li id="sub_folder_add" title='<?php print $this->translate('Add New Subfolder')?>'><a><?php print $this->translate('Add New Subfolder')?></a></li>
	<li id="exclamation" style="display:none;"><a><?php echo $this->translate('Error') ?></a></li>

</ul>

<?php 	if( $this->SIPEnabled ) echo $this->partial('global/webphoneminiUI.phtml', $this); ?>

<script type="text/javascript" language="JavaScript">
	//CONSIDER: difficult to prevent a click outside of the folder editing form (on another folder) from listing that folder after form submit. possible look at simple solutions to prevent this
	var currentlyEditingAFolder = false
    var clickedFolder = null
	var doubleClickWaitTimer = null
	var folderTreeView = null

	function initTreeView(node)
	{

		if( node == undefined )
		{

			node = $("ul#nav_secondary", '#Email');

		}
		var simpleTreeViewOptions = {
			open: '',
			close: '',
			classOpen: 'folderOpened',
			classClose: 'folderClosed',
			slide: false,
			speed: 'fast',
			collapsed: true,
			expand: '0'
		};
		folderTreeView = node.simpletreeview( simpleTreeViewOptions );

	}
 
	$( function() {
    	//this accesses context variable waitingForSecondClick, so will have to stay here till we contextualize all methods properly
		doubleClickWaitTimer = null
	  	$('#nav_secondary a', '#Email').bind('click', function(e)
	  	{

			clickedFolderAnchor = this;
			applyFolderCustomizations( $(this) );
			if( doubleClickWaitTimer == null ) {
				doubleClickWaitTimer = setTimeout( function() {
					folderListMessages(clickedFolderAnchor)
					doubleClickWaitTimer = null
				}, 200);
			}
			return false;

		} );

		$('#nav_secondary a', '#Email').bind("dblclick", function()
		{
			window.clearTimeout(doubleClickWaitTimer)
			doubleClickWaitTimer = null
			folderRename(this)

		} )

/*
		$('#nav_secondary a', '#Email').each(function(){
			if($(this).parent().attr('ismainfolder')!='true' && $(this).parent().attr('isfilter')!='true')
			{
				$(this).draggable({

					helper: function() { return dragMailFolder( $(this).parent()) },
					stop: function() { return dragMailFolderStop( $(this).parent() ) },
					appendTo:'#draggableMessagesContainer',
					cursor:'move',
					scroll:false,
					containment: 'body',
					distance:10,
					cursorAt:{top:15,left:20},
					revert: "invalid",
				  	revertDuration: 300
				})
			}

		})
   */

		if( $('body').hasClass('fresh') )
			$('span.folder-add', '#folder_actions').click( function(e) { folderCreate($('#Email #nav_secondary')) } );
		else
			$('span.folder-add', '#folder_actions').click( function() { folderCreate( $('#Email #nav_secondary'), true ) } )
		
		
		$('#folder_actions #sub_folder_add a').click( function() { folderCreate( $('#Email #nav_secondary'), false ) } )

		$('span.folder-remove', '#folder_actions').click( function()
		{

			if( $('.current', '#Email #nav_secondary').attr('ismainfolder') == 'true' )
			{

				alert('<?php echo $this->translate("Main folders can not be removed.") ?>');
				return false;

			}
			else if( $(this).hasClass('disabled') )
			{

				alert('<?php echo $this->translate("Can not remove this folder.") ?>');
				return false;

			}
			else
			{

				folderRemove( $('.current', '#Email #nav_secondary').parent() )
				return false

			}

		} )

		initTreeView();

		// ensure currently select item has its tree expanded
		folderTreeView.expand( $('.current', '#Email #nav_secondary').parent().parent() );

		// if current selected folder is a non-root folder ..
		if( $('.current', '#Email #nav_secondary').attr('ismainfolder') != 'true' )
		{
			// .. ensure subfolder button is enabled
		    $('#folder_actions #sub_folder_add a').show();
		}
		else
		{
			// .. else its a main folder - make sure that the remove button is disabled
		    $('#folder_actions #folder_remove a').removeClass('enabled').addClass('disabled')
		}

		$('#Email #secondary').scrollTo($('.current', '#Email #nav_secondary').parent());
		applyFolderCustomizations( $('.current', '#Email #nav_secondary').children('a') );
		
		$('.filter span.label', '#nav_secondary').bind('click', function(e) {
			var $thisFilter = $(this);
			e.preventDefault();
			e.stopPropagation();

			if( getSelectedCallFolder() != null) {
				return;
			}

			if( $thisFilter.attr('loading') == 'true' ) {
				return;
			}


			$('.filter-current').removeClass('filter-current');

		    $thisFilter.addClass('filter-current');
		    $thisFilter.attr('loading', 'true');

		    setTimeout(function() {$thisFilter.removeAttr('loading')}, 1000);
			
			var filtertype = $thisFilter.parent().parent().attr('filtertype');
			
			$('input[name=searchQuery]','#search_anything').val('');

			if(filtertype == 'flagged') {
				$('input[name=flagged]','#search_anything').attr('checked', 'checked');
			}
			
			if(filtertype == 'unread') {
				$('input[name=unseen]','#search_anything').attr('checked', 'checked');
			}			

			$('input[name=filter]','#search_anything').val(filtertype);
			
			$('#searchForm').submit();
			
		});

	})
  $('li#folder_actions span').keydown(function(e){
		if(e.which==ENTER_KEY){
			e.preventDefault();
			e.stopPropagation();
			$(this).click();
		}
	});
</script>


