<%@ page buffer="8kb" autoFlush="true" %>
<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="zm" uri="com.zimbra.zm" %>
<%@ taglib prefix="mo" uri="com.zimbra.mobileclient" %>
<%@ 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" %>
<%@ taglib prefix="app" uri="com.zimbra.htmlclient" %>
<c:if test="${param.ui != null && param.ui != '1'}">
    <jsp:forward page="main"/>
</c:if>
<c:if test="${param.ui == null || param.ui == ''}">
    <c:set var="uiv" value="1" scope="session"/>
</c:if>
<c:if test="${sessionScope.limit == null}">
    <c:set var="limit" value="10" scope="session"/>
</c:if>
<c:if test="${(sessionScope.limit != param.limit && param.limit != null && param.limit != '')  }">
    <c:set var="limit" value="${zm:cook(param.limit)}" scope="session"/>
</c:if>
<script type="text/javascript">var selectedImgId = "mail_img";</script>
<c:set var="context_url" value="${requestScope.baseURL!=null?requestScope.baseURL:'mainx'}"/>
<c:if test="${param.loginOp=='logout'}">
    <c:set var="uiv" value="1" scope="session"/>
    <c:redirect url="/?loginOp=logout&client=mobile"/>
</c:if>

<mo: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"/>
    <mo:apptComposeCheck/>
    <mo:composeCheck/>
    <c:set var="action" value="${zm:cook(empty param.paction ? param.action : param.paction)}" scope="request"/>
</mo:handleError>
<mo:view title="${mailbox.name}" context="${null}" mailbox="${mailbox}">
<table border="0" cellpadding="0" cellspacing="0" width="100%" class="NavbarBg">
    <tr>
        <td width="5%" align="center" valign="top">
            <!-- Tabs starts here -->
            <c:set var="aurl" value="${context_url}?"/>
            <a id="mails" href="${aurl}"><img id="mail_img" alt="<fmt:message key="mail"/>"
                                              src="<app:imgurl value='mobile/Mail${param.showSearch!="1" && (empty param.st || param.st == "message" || param.st == "conversation") ? "-sel" : ""}.gif' />"
                                              border="0" width="55" height="48">
                    <%-- c:if test="${mailbox.inbox.hasUnread}">&nbsp;(${mailbox.inbox.unreadCount})</c:if --%>
            </a>
        </td>
        <td align="center" width="5%" valign="top">
            <c:if test="${mailbox.features.contacts}">
                <c:set var="aurl" value="${context_url}?st=contact"/>
                <a id="contacts" href="${aurl}"><img alt="<fmt:message key="contacts"/>" id="contact_img"
                                                     src="<app:imgurl value='mobile/Contacts${param.showSearch!="1" && (param.st eq "contact" || param.st eq "ab") ? "-sel" : ""}.gif' />"
                                                     border="0" width="55" height="48"></a>
            </c:if>
        </td>
        <td width="5%" align="center" valign="top">
            <c:if test="${mailbox.features.calendar}">
                <c:set var="aurl" value="${context_url}?st=cal"/>
                <a id="cal" href="${aurl}"><img alt="<fmt:message key="calendar"/>" id="cal_img"
                                                src="<app:imgurl value='mobile/Calendar${param.showSearch!="1" && param.st eq "cal" ? "-sel" : ""}.gif' />"
                                                border="0" width="55" height="48"></a>
            </c:if>
        </td>
        <td width="5%" align="center" valign="top">
            <c:set var="aurl" value="${context_url}?st=folders"/>
            <a id="folders" href="${aurl}"><img alt="<fmt:message key="folder"/>" id="folder_img"
                                                src="<app:imgurl value='mobile/Folders${param.showSearch!="1" && param.st eq "folders" ? "-sel" : ""}.gif' />"
                                                border="0" width="55" height="48"></a>
        </td>
        <td id="search_tab" width="5%" align="center" valign="top">
            <c:set var="surl" value="?"/>
            <c:if test="${not empty param.st}">
                <c:set var="surl" value="${surl}${fn:replace(pageContext.request.queryString,'showSearch=1','')}&"/>
            </c:if>
            <c:if test="${param.showSearch != '1'}">
                <c:set var="surl" value="${surl}showSearch=1"/>
            </c:if>
            <a onclick="return toggleSearch();" href="${zm:cook(surl)}"><img id="search_img" alt="<fmt:message key="search"/>" src="<app:imgurl value='mobile/Search${param.showSearch!="1"?"":"-sel"}.gif' />"
                 border="0" width="55" height="48"></a>
        </td>
    </tr>

</table>
<div class="View" style="${param.showSearch!='1'?'display:none;':''};margin:2px;padding:2px;" class="Stripes" id="SEARCH_BOX">
    <!--<table width="100%" align="center">
        <tr class="searchbar">
            <td>-->
                <form method="get" accept-charset="UTF-8" action="${context_url}" style=";"
                      onsubmit="if(!this.sq.value){var v = document.getElementById('d_msg');v.innerHTML='<div class=StatusWarning><fmt:message key="actionNoSearchQuerySpecified"/></div>';v.style.display='block';return false;}else{return true;}">
                    <c:if test="${not empty param.action}">
                        <input type="hidden" name="action" value="${zm:cook(param.action)}"/>
                    </c:if>
                    <c:if test="${empty param.view}">
                        <input type="hidden" name="view" value="month"/>
                    </c:if>
                    <c:if test="${not empty param.view}">
                        <input type="hidden" name="view" value="${zm:cook(param.view)}"/>
                    </c:if>
                    <c:if test="${not empty param.invId}">
                        <input type="hidden" name="invId" value="${zm:cook(param.invId)}"/>
                    </c:if>
                    <input type="hidden" name="showSearch" value="1"/>
                    <input type="hidden" name="crumb" value="${fn:escapeXml(mailbox.accountInfo.crumb)}"/>
                    <table cellspacing="0" cellpadding="0" width="100%">
                        <tr>
                            <td>
                                <table cellspacing="1" cellpadding="1" border="0">
                                    <tr>
                                        <td class="zo_m_list_row">
                                                <%--<fmt:message key="in"/>--%>
                                            <select name="st">
                                                <option value="${(mailbox.features.conversations ? mailbox.prefs.groupMailBy : 'message')}">
                                                    <fmt:message key="mail"/></option>
                                                <c:if test="${mailbox.features.contacts}">
                                                    <option value="contact"
                                                            <c:if test="${param.st == 'contact' || param.st == 'ab' }">selected='selected'</c:if> >
                                                        <fmt:message key="contacts"/></option>
                                                </c:if>
                                                <c:if test="${mailbox.features.calendar}">
                                                    <option value="cal"
                                                            <c:if test="${param.st == 'cal'}">selected='selected'</c:if>>
                                                        <fmt:message key="ALT_MSG_STATUS_APPT"/></option>
                                                </c:if>
                                                <option value="folders"
                                                        <c:if test="${param.st == 'folders'}">selected='selected'</c:if>>
                                                    <fmt:message key="folders"/></option>
                                            </select>
                                        </td>
                                        <td align="left" style="padding-right:2px;">
                                            <input type="text"  class="Textarea" name="sq" value="${zm:cook(param.sq)}"/>
                                        </td>
                                        <td align="right">
                                            <input type="submit" name="actionSearch"
                                                   value="<fmt:message key="search"/>"/>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="3" align="right">
                                            <a href="mainx?moquery"><fmt:message key="advancedSearch"/></a>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </form>
<%--
            </td>
        </tr>
    </table>--%>
</div>
<div style="padding:5px;display:none;" id="d_msg"></div>
<table border="0" cellpadding="0" cellspacing="0" width="100%" border="0">
    <tr>
        <td colspan="2" height="100%" valign="top">
            <mo:handleError>
                <c:choose>
                    <c:when test="${param.moquery != null}">
                        <c:import url="/m/moquery"/>
                    </c:when>
                    <c:when test="${(param.st != null && param.st == 'newmail') || (param.action=='compose')}">
                        <c:import url="/m/mocompose"/>
                    </c:when>
                    <c:when test="${param.st != null && (param.st == 'newappt' || param.st == 'editappt')}">
                        <c:import url="/m/moapptcompose"/>
                    </c:when>
                    <c:when test="${param.st == null || param.st == (mailbox.features.conversations ? mailbox.prefs.groupMailBy : 'message') || param.st == 'contact'}">
                        <c:if test="${param.st == 'contact'}">
                            <script type="text/javascript">selectedImgId = "contact_img";</script>
                        </c:if>
                        <c:import url="/m/mosearch"/>
                    </c:when>
                    <c:when test="${param.st != null && param.st == 'cal'}">
                        <c:choose>
                            <c:when test="${not empty param.tz}">
                                <fmt:setTimeZone var="tz" value="${param.tz}" scope="request"/>
                            </c:when>
                            <c:otherwise>
                                <c:set var="tz" value="${mailbox.prefs.timeZone}" scope="request"/>
                            </c:otherwise>
                        </c:choose>
                        <c:if test="${param.sq == null}">
                            <fmt:formatDate var="today" timeZone="${tz}" pattern="yyyyMMdd" value="${zm:getToday(tz).time}"/>
                            <c:set var="arg"
                                   value="${param.view==null?'view=month':''}&${param.date==null?'date=':''}${today}">
                            </c:set>
                        </c:if>
                        <c:if test="${not empty param.sq}">
                            <c:set var="arg" value="view=list"/>
                        </c:if>
                        <c:import url="/m/mocalendar?${arg}"/>
                        <script type="text/javascript">selectedImgId = "cal_img";</script>
                    </c:when>
                    <c:when test="${param.st != null && param.st == 'folders'}">
                        <c:import url="/m/mofolders"/>
                        <script type="text/javascript">selectedImgId = "folder_img";</script>
                    </c:when>
                    <c:when test="${param.st != null && param.st == 'ab'}">
                        <c:import url="/m/moaddrbooks"/>
                        <script type="text/javascript">selectedImgId = "contact_img";</script>
                    </c:when>

                </c:choose>
            </mo:handleError>
        </td>
    </tr>
    <tr style="font-size:small;height: 37px;">
        <td class="zo_m_list_frag" align="left">
            <c:url var="logoutUrl" value="${context_url}?loginOp=logout&client=mobile"/>
            <a href="${fn:escapeXml(logoutUrl)}" id="_logout">
                <fmt:message key="logOut"/>
            </a>
            (${mailbox.accountInfo.attrs["uid"][0]})
        </td>

        <td class="zo_m_list_frag" align="right">
            <fmt:message key="MO_quota"/>:
            <c:set var="max" value="${mailbox.attrs.zimbraMailQuota[0]}"/>
            <fmt:message var="unlimited" key="unlimited"/>
            <fmt:message key="MO_quotaUsage">
                <fmt:param value="${zm:displaySizeFractions(pageContext,mailbox.size,2)}"/>
                <fmt:param value="${max==0 ? unlimited : zm:displaySizeFractions(pageContext,max,2)}"/>
            </fmt:message>
        </td>
    </tr>
</table>
<hr size="1"/>
<div align="center" style="height:30px;">
    <a target="_blank" href="<fmt:message key="logoURL"/>"><fmt:message key="footerCopyright"/></a>
</div>
<script type="text/javascript" language="JavaScript">
   <%-- <c:if test="${param.showSearch == null || param.showSearch != '1'}">
    document.getElementById("SEARCH_BOX").style.display = "none";
    </c:if>--%>
    //document.getElementById("search_tab").style.display="";
    var toggleSearch = function() {
        if (document.getElementById("SEARCH_BOX").style.display == "block") {
            document.getElementById("SEARCH_BOX").style.display = "none";
            document.getElementById("search_img").src = "<app:imgurl value='mobile/Search.gif' />";
            document.getElementById(selectedImgId).src = document.getElementById(selectedImgId).src.replace("-sel", "").replace(".gif", "-sel.gif");
            ;
        } else {
            document.getElementById("SEARCH_BOX").style.display = "block";
            document.getElementById("search_img").src = "<app:imgurl value='mobile/Search-sel.gif' />";
            document.getElementById(selectedImgId).src = document.getElementById(selectedImgId).src.replace("-sel", "");
        }
        return false;
    };
    var changeListSize = function(size) {
        var url = window.location.href;
        url = url.replace(/sc=[0-9]+/i, "");
        if (url.indexOf("limit=") >= 0) {
            url = url.replace(/limit=[0-9]+/i, "limit=" + size);
        } else {
            url = url.replace("?", "?limit=" + size + "&");
        }
        if (url.indexOf("so=") >= 0) {
            url = url.replace(/so=[0-9]+/, "so=" + size);
        }
        window.location.href = url;
    };
</script>
</mo:view>
