<?php
$this->log = Zend_Registry::get('log');
//$this->log->debug( "\n\n" . print_r($this->folderChildren, true) . "\n" . __METHOD__ . ' #' . __LINE__ . ": \$this->foldersChildren \n\n");

$this->log = Zend_Registry::get('log');
$requiredArgs = array('moduleBaseUrl', 'currentFolder', 'folderChildren', 'mainFolders', 'renderMainFolders'); //N.B. currentFolder = currently selected mailbox folder, not current iteration folder
foreach( $requiredArgs as $requiredArg )
{
	if( !isset($this->{$requiredArg} ) )
	{
		
		throw new Exception('Required argument missing: ' . $requiredArg );
	}

}

if( !isset($this->depth) ) 
{
	
	$this->depth = 0;
	
}

$tabPrepend = '';
for( $a = 0 ; $a < $this->depth ; $a++ )
{
	
	$tabPrepend .= "\t";
	
}

foreach( $this->folderChildren as $folderChild ) :
	
	$localName = $folderChild->getLocalName();
	
	$globalName = $folderChild->getGlobalName();
	
	if( in_array($globalName, $this->mainFolders) )
	{
		
		$isMainFolder = true;
		
	}
	else
	{
		
		$isMainFolder = false;
		
	}
	if( isset($this->mainFolders[$localName]) && $this->mainFolders[$localName] == $globalName ) 
	{

		if($localName == 'INBOX')
		{
		 
			$localName = 'Inbox';
			
		}
		$localNameSelectorSafe = strtolower( $localName );
		
	}
	else
	{
	
		$localNameSelectorSafe = safeId( $globalName );
		
	}
	$urlSafeGlobalName = urlencode(urlencode($globalName)); //ZF decodes before looking for actions so double encode 
	if( isset($this->folderTotals[$globalName]['exists']) )
	{
		
		$totalMessages = $this->folderTotals[$globalName]['exists'];
		
	}
	else
	{
		
		$totalMessages = 0;
		
	}
	
	$isSelectable = $folderChild->isSelectable()?'true':'false';
	
	if( ($isMainFolder && $this->renderMainFolders) || (!$isMainFolder && !$this->renderMainFolders) )
	{
		
		echo "\n" . $tabPrepend ?>
			<li class="custom-folder"><span class="spinner"></span>
				<div isSelectable="<?php echo $isSelectable ?>"<?php echo ($isMainFolder?' isMainFolder="true"':'')?> foldernameglobal="<?php echo $urlSafeGlobalName ?>" id="folder_<?php echo $localNameSelectorSafe ?>"<?php echo ($this->currentFolder==$globalName?' class="current"':'') ?>>     <?php echo "\n" . $tabPrepend ?>
				<?php if( $folderChild->isSelectable() ) : ?> 
					<a href="<?php echo $this->moduleBaseUrl ?>/mail/listfoldermessages/selectFolder/<?php echo $urlSafeGlobalName ?>" tabindex="0" onclick="prepareMailList($(this).parent());">	<?php echo "\n" . $tabPrepend ?>
						<span class="label"><?php echo htmlspecialchars( $this->AtmailMailStorageMain->decodeUTF7($localName) ) ?></span>	<?php echo "\n" . $tabPrepend ?>
						<?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>	<?php echo "\n" . $tabPrepend ?>
					</a>
				<?php else: ?>
					<a>	<?php echo "\n" . $tabPrepend ?>
						<span class="label"><?php echo htmlspecialchars( $this->AtmailMailStorageMain->decodeUTF7($localName) ) ?></span>	<?php echo "\n" . $tabPrepend ?>
						<?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>	<?php echo "\n" . $tabPrepend ?>
					</a>
		        <?php endif; ?>
				</div>
	<?php
	
		if( !$folderChild->isLeaf() )
		{
	
			echo $tabPrepend . "\t\t\t\t<ul class='subfolder'>\n";
			//recursivelyRenderFolderList($folderChild, $renderRoot, false, $_depth+1);
			$recursionArgs = array(
				'moduleBaseUrl' => $this->moduleBaseUrl, 
				'AtmailMailStorageMain' => $this->AtmailMailStorageMain, 
				'currentFolder' => $this->currentFolder, 
				'folderChildren' => $folderChild, 
				'folderTotals' => $this->folderTotals, 
				'depth' => ($this->depth+2), 
				'mainFolders' => $this->mainFolders, 
				'renderMainFolders' => $this->renderMainFolders
			);
			echo $this->partial('mail/indexfolderlistnoderecursive.phtml', $recursionArgs );
			echo $tabPrepend . "\t\t</ul>\n";
		
		}
?>
			</li>
			
<?php 
	}
	else
	{
		
		if( !$folderChild->isLeaf() )
		{
	
			//recursivelyRenderFolderList($folderChild, $renderRoot, false, $_depth+1);
			$recursionArgs = array(
				'moduleBaseUrl' => $this->moduleBaseUrl, 
				'AtmailMailStorageMain' => $this->AtmailMailStorageMain, 
				'currentFolder' => $this->currentFolder, 
				'folderChildren' => $folderChild, 
				'folderTotals' => $this->folderTotals, 
				'depth' => ($this->depth+2), 
				'mainFolders' => $this->mainFolders, 
				'renderMainFolders' => $this->renderMainFolders
			);
			echo $this->partial('mail/indexfolderlistnoderecursive.phtml', $recursionArgs );
			
		}		
		
		
		
		
		
		
	} 

endforeach;  
