<%@ page buffer="8kb" autoFlush="true" %>
<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="zm" uri="com.zimbra.zm" %>
<%@ taglib prefix="app" uri="com.zimbra.htmlclient" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="com.zimbra.i18n" %>
<app:handleError>
    <zm:getMailbox var="mailbox"/>
    <c:choose>
    <c:when test="${not empty mailbox.prefs.locale}">
        <fmt:setLocale value='${mailbox.prefs.locale}' scope='request' />
    </c:when>
    <c:otherwise>
        <fmt:setLocale value='${pageContext.request.locale}' scope='request' />
    </c:otherwise>
    </c:choose>
    <fmt:setBundle basename="/messages/ZhMsg" scope="request"/>

    <c:if test="${!empty param.doAction}">
        <zm:checkCrumb crumb="${param.crumb}"/>
        <app:folderAction/>
    </c:if>

    <%-- types selectedFolder for us --%>
    <c:set var="selectedFolder" value="${mailbox.inbox}"/>

    <c:choose>
        <c:when test="${not empty requestScope.currentFolderId}">
            <c:set var="selectedFolder" value="${zm:getFolder(pageContext, requestScope.currentFolderId)}"/>
        </c:when>
        <c:when test="${not empty requestScope.newlyCreatedFolderId}">
            <c:set var="selectedFolder" value="${zm:getFolder(pageContext, requestScope.newlyCreatedFolderId)}"/>
        </c:when>
        <c:when test="${not empty param.id}">
            <c:set var="selectedFolder" value="${zm:getFolder(pageContext, param.id)}"/>
        </c:when>
        <c:when test="${not empty param.sfi}">
            <c:set var="selectedFolder" value="${zm:getFolder(pageContext, param.sfi)}"/>
        </c:when>
    </c:choose>
    <c:if test="${empty selectedFolder}"><c:set var="selectedFolder" value="${mailbox.inbox}"/></c:if>
    <c:set var="newFolder" value="${zm:actionSet(param, 'actionNewFolder') or zm:actionSet(param, 'actionNewSearchFolder') or zm:actionSet(param, 'actionNewFeedFolder')}"/>

    <fmt:message var="title" key="manageFolders"/>
   <c:set var="isFolderUpdated" value="${zm:actionSet(param, 'actionSave')}" />
    <c:if test="${not empty param.sync and not isFolderUpdated}">
        <zm:syncFolder id="${param.sync}"/>
        <c:set var="folderName" value="${zm:getFolderName(pageContext, param.sync)}"/>
        <app:status>
            <fmt:message key="actionFolderReloaded">
                <fmt:param value="${folderName}"/>
            </fmt:message>
        </app:status>
    </c:if>

    <c:set var="selectedRow" value="${-1}"/>

</app:handleError>

<app:view mailbox="${mailbox}" title="${title}" context="${null}" selected='folders' folders="true" searches="true" editmode="true" keys="true"
        onload="var e=document.getElementById('newName'); if (e) e.focus();">

    <form action="" method="post" accept-charset="utf-8">
        <table width="100%" cellspacing="0" cellpadding="0">

            <tr>
                <td class='TbTop'>
                    <table width="100%" cellspacing="0">
                        <tr>
                            <td class='TbBt'>
                                <table cellspacing="0" cellpadding="0" class='Tb'>
                                    <tr>
                                        <td>
                                            <a id="OPCLOSE" href="<c:url value="/h/search"/>">
                                                <app:img src="common/ImgClose.png" alt="close"/><span style='padding-left:2px'><fmt:message key="close"/></span>
                                            </a>
                                        </td>
                                        <td><div class='vertSep'></div></td>
                                        <td>   
                                        <a id="actionNewFolder" href="<c:url value="/h/mfolders?actionNewFolder"/>">
                                                <app:img src="startup/ImgNewFolder.png" alt="folderNew"/><span style='padding-left:2px'><fmt:message key="folderNew"/></span>
                                        </a>
                                        </td>
                                       <%-- <app:button id="OPNEWFOLD" name="actionNewFolder" src="startup/ImgNewFolder.png" tooltip="folderNew" text="folderNew"/> --%>
                                        <td><div class='vertSep'></div></td>
                                       <%-- <app:button id="OPNEWRSS" name="actionNewFeedFolder" src="startup/ImgNewFolder.png" tooltip="folderNewRssAtomFeed" text="folderNewRssAtomFeed"/> --%>
                                        <td>
                                        <a id="actionNewFeedFolder" href="<c:url value="/h/mfolders?actionNewFeedFolder"/>">
                                                <app:img src="startup/ImgNewFolder.png" alt="folderNewRssAtomFeed"/><span style='padding-left:2px'><fmt:message key="folderNewRssAtomFeed"/></span>
                                        </a>
                                        </td>
                                        <td><div class='vertSep'></div></td>
                                        <%--<app:button id="OPNEWSEARCH" name="actionNewSearchFolder" src="startup/ImgNewFolder.png" tooltip="folderNewSearchFolder" text="folderNewSearchFolder"/>--%>
                                        <c:if test="${mailbox.features.savedSearches}">
                                            <td>
                                            <a id="actionNewSearchFolder" href="<c:url value="/h/mfolders?actionNewSearchFolder"/>">
                                                    <app:img src="startup/ImgNewFolder.png" alt="folderNewSearchFolder"/><span style='padding-left:2px'><fmt:message key="folderNewSearchFolder"/></span>
                                            </a>
                                            </td>
                                        </c:if>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>

            <tr>
                <td class='ZhAppContent'>
                    <table border="0" cellpadding="0" cellspacing="0" width="100%">
                        <tr>
                            <td width="200" class='List' valign='top'>
                                <table width="100%" cellpadding=2 cellspacing=0>
                                    <c:set var="index" value="${0}"/>
                                    <zm:forEachFolder var="folder">
                                        <c:if test="${(folder.isMailView and not folder.isChats and not folder.isSpam) or folder.isTrash or (mailbox.features.spam and folder.isSpam)}">
                                            <c:if test="${selectedFolder.id eq folder.id and not newFolder}"><c:set var="selectedRow" value="${index}"/></c:if>
                                            <tr <c:if test="${index eq selectedRow}">class='RowSelected'</c:if>>
                                                <td width="1%">
                                                    &nbsp;
                                                </td>
                                                <td nowrap class='Folder<c:if test="${folder.hasUnread}"> Unread</c:if>' style='padding-left: ${folder.depth*8}px'>
                                                    <c:set var="label" value="${zm:getFolderName(pageContext, folder.id)}"/>
                                                    <c:url var="selectUrl" value="/h/mfolders">
                                                        <c:param name="id" value="${folder.id}"/>
                                                    </c:url>
                                                    <a id="A${index}" href="${fn:escapeXml(selectUrl)}">
                                                        <app:img src="${folder.image}" alt="folder"/>
                                                            <span style='vertical-align:middle'>${label}
                                                            <c:if test="${folder.hasUnread}">(${folder.unreadCount}) </c:if>
                                                            </span>
                                                    </a>
                                                </td>
                                            </tr>
                                            <c:set var="index" value="${index+1}"/>
                                        </c:if>
                                    </zm:forEachFolder>
                                </table>
                            </td>
                            <td height="100%" class='ZhEditFolderContent' valign='top'>
                                <c:choose>
                                    <c:when test="${zm:actionSet(param, 'actionNewFolder')}">
                                        <app:newFolder/>
                                    </c:when>
                                    <c:when test="${zm:actionSet(param, 'actionNewSearchFolder')}">
                                        <app:newFolder search="true"/>
                                    </c:when>
                                    <c:when test="${zm:actionSet(param, 'actionNewFeedFolder')}">
                                        <app:newFolder url="true"/>
                                    </c:when>
                                    <c:otherwise>
                                        <table width="100%" cellspacing="0" cellpadding="0">
                                            <tr>
                                                <td>
                                                    <app:editFolder folder="${selectedFolder}"/>
                                                </td>
                                            </tr>
                                        </table>
                                    </c:otherwise>
                                </c:choose>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
        <input type="hidden" name="doAction" value="1"/>
        <input type="hidden" name="crumb" value="${fn:escapeXml(mailbox.accountInfo.crumb)}"/>
    </form>

    <SCRIPT TYPE="text/javascript">
    <!--
    var zrc = ${index};
    var zindex = ${selectedRow};
    var zclick = function(id) { var e2 = document.getElementById(id); if (e2) e2.click(); }
    var zget = function(i) { var e = document.getElementById("A"+i); if (e && e.href) window.location = e.href; }
    var zprev = function() { if (zindex > 0) zget(zindex-1);}
    var znext = function() { if (zindex+1 < zrc) zget(zindex+1);}

    //-->
    </SCRIPT>
    <app:keyboard cache="mail.mfolders" mailbox="${mailbox}" passspecial="true" globals="true">
        <zm:bindKey message="global.Cancel" id="OPCLOSE"/>
        <zm:bindKey message="mfolders.NewFolder" func="function() {zclick('SOPNEWFOLD');}"/>
        <zm:bindKey message="mfolders.NewSearchFolder" func="function() {zclick('SOPNEWSEARCH');}"/>
        <zm:bindKey message="mfolders.NewRssFeed" func="function() {zclick('SOPNEWRSS');}"/>
        <zm:bindKey message="mfolders.Save" func="function() {zclick('OPSAVE');}"/>
        <zm:bindKey message="mail.MarkAllRead" func="function() {zclick('OPMARKALLREAD');}"/>
        <zm:bindKey message="mfolders.EmptyFolder" func="function() {zclick('OPEMPTY');}"/>
        <zm:bindKey message="global.PreviousItem" func="zprev"/>
        <zm:bindKey message="global.NextItem" func="znext"/>
    </app:keyboard>
    
</app:view>
