<%@ 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"/>
    
    <zm:fileUploader var="uploader"/>

    <c:if test="${uploader.isUpload}">
        <zm:checkCrumb crumb="${uploader.paramValues.crumb[0]}"/>
        <app:calendarAction/>
    </c:if>


    <c:choose>
        <c:when test="${not empty requestScope.newlyCreatedCalendarId}">
            <c:set var="selectedFolder" value="${zm:getFolder(pageContext, requestScope.newlyCreatedCalendarId)}"/>
        </c:when>
        <c:when test="${not empty param.id}">
            <c:set var="selectedFolder" value="${zm:getFolder(pageContext, param.id)}"/>
        </c:when>
    </c:choose>

    <c:if test="${empty selectedFolder or not selectedFolder.isAppointmentView}"><c:set var="selectedFolder" value="${mailbox.calendar}"/></c:if>
    <c:set var="newShared" value="${not empty uploader.params.actionNewSharedCalendar or param.action eq 'newShared'}"/>
    <c:set var="newCalendar" value="${not empty uploader.params.actionNewCalendar or not empty uploader.params.actionNewSubscribedCalendar or newShared}"/>
    <c:set var="sendFreeBusy" value="${not empty uploader.params.actionSendFreeBusyLink}"/>

    <c:if test="${sendFreeBusy}" >
        <c:set var="calName" value="/${mailbox.calendar.name}"/>
        <c:set var="bodyCont" value="${fn:substringBefore(fn:escapeXml(zm:getFolderRestURL(mailbox, mailbox.calendar)), calName)}" />
        <c:set var="bodyCont" value="${bodyCont}?fmt=freebusy" />
          <c:redirect url="/h/search?action=compose">
			  <c:param name="body" value="${bodyCont}" />
		  </c:redirect>
    </c:if>

    <fmt:message var="title" key="manageCalendars"/>

    <c:if test="${not empty uploader.params.sync}">
        <zm:syncFolder id="${uploader.params.sync}"/>
        <c:set var="folderName" value="${zm:getFolderName(pageContext, uploader.params.sync)}"/>
        <app:status>
            <fmt:message key="actionCalendarReloaded">
                <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' calendars="true" minical="false" editmode="true" keys="true"
     onload="var e=document.getElementById('newName'); if (e) e.focus();">

    <form action="" method="post" enctype="multipart/form-data" 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/calendar"/>">
                                                <app:img src="common/ImgClose.png" alt="close"/><span style='padding-left:5px'><fmt:message key="close"/></span>
                                            </a>
                                        </td>
                                        <td><div class='vertSep'></div></td>
                                        <app:button id="OPNEWCAL" name="actionNewCalendar" src="calendar/ImgNewAppointment.png" tooltip="calendarNew" text="calendarNew"/>
                                        <td><div class='vertSep'></div></td>
                                        <app:button id="OPNEWSUB" name="actionNewSubscribedCalendar" src="calendar/ImgNewAppointment.png" tooltip="calendarNewSubscribed" text="calendarNewSubscribed"/>
                                        <c:if test="${mailbox.features.sharing}">
                                            <td><div class='vertSep'></div></td>
                                            <app:button id="OPNEWLINK" name="actionNewSharedCalendar" src="calendar/ImgNewAppointment.png" tooltip="calendarNewShared" text="calendarNewShared"/>
                                        </c:if>
                                        <td><div class='vertSep'></div></td>
                                        <app:button id="OPFREEBUSYLINK" name="actionSendFreeBusyLink" src="calendar/ImgNewAppointment.png" tooltip="sendFreeBusyLink" text="sendFreeBusyLink"/>
                                    </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">
                                        <fmt:message var="colorMsg" key="${folder.rgbColorMsg}"/>
                                        <c:if test="${folder.isAppointmentView}">
                                            <c:set var="rowColor" value="${zm:lightenColor(not empty folder.rgb ? folder.rgb : colorMsg)}"/>
                                            <c:set var="selected" value="${selectedFolder.id eq folder.id and not newCalendar}"/>
                                            <c:if test="${selected}"><c:set var="selectedRow" value="${index}"/></c:if>                                            
                                            <tr>
                                                <td style='background-color:${rowColor}' width="1%">
                                                    &nbsp;
                                                </td>
                                                <td style='background-color:${rowColor}'>
                                                    <c:set var="label" value="${zm:getFolderName(pageContext, folder.id)}"/>
                                                    <c:url var="selectUrl" value="">
                                                        <c:param name="id" value="${folder.id}"/>
                                                    </c:url>
                                                    <a id="A${index}" href="${selectUrl}">
                                                        <app:img src="${folder.image}" alt="folder"/>
                                                        <span <c:if test="${selected}">class='ZhTISelected'</c:if> style='vertical-align:middle'>${label}</span>
                                                    </a>
                                                </td>
                                            </tr>
                                            <c:set var="index" value="${index+1}"/>
                                        </c:if>
                                    </zm:forEachFolder>
                                </table>
                            </td>
                            <td class='ZhEditCalendarContent' valign='top'>
                                <c:choose>
                                    <c:when test="${not empty uploader.params.actionNewCalendar}">
                                        <app:newFolder calendar="true"/>
                                    </c:when>
                                    <c:when test="${newShared}">
                                        <app:newFolder calendar="true"  link="true"/>
                                    </c:when>
                                    <c:when test="${not empty uploader.params.actionNewSubscribedCalendar}">
                                        <app:newFolder calendar="true" url="true"/>
                                    </c:when>
                                    <c:otherwise>
                                        <app:editFolder folder="${selectedFolder}"/>
                                    </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="cal.mcalendars" mailbox="${mailbox}" globals="true" passspecial="true">
        <zm:bindKey message="global.Cancel" id="OPCLOSE"/>
        <zm:bindKey message="mcalendars.NewCalendar" func="function() {zclick('SOPNEWCAL');}"/>
        <zm:bindKey message="mcalendars.NewSubscription" func="function() {zclick('SOPNEWSUB');}"/>
        <zm:bindKey message="mcalendars.NewLink" func="function() {zclick('SOPNEWLINK');}"/>
        <zm:bindKey message="mfolders.Save" func="function() {zclick('OPSAVE');}"/>        
        <zm:bindKey message="global.PreviousItem" func="zprev"/>
        <zm:bindKey message="global.NextItem" func="znext"/>
    </app:keyboard>
</app:view>
