<%
  import java.text.SimpleDateFormat ;
  import org.exoplatform.ecm.webui.utils.PermissionUtil;
  import org.exoplatform.ecm.webui.utils.Utils ;
  import org.exoplatform.web.application.Parameter ;
  import org.exoplatform.webui.core.UIPopupWindow;
  import org.exoplatform.services.jcr.RepositoryService;
	
	UIPopupWindow uiPopupWindow = uicomponent.getAncestorOfType(UIPopupWindow.class);
  def resizeBlock = "class=\"UIResizableBlock\"";
  if(uiPopupWindow != null) resizeBlock = ""; 
  SimpleDateFormat dateFormat = new SimpleDateFormat() ;
  Parameter[] params;
  
  RepositoryService rService = uicomponent.getApplicationComponent(RepositoryService.class);
  String repository = rService.getCurrentRepository().getConfiguration().getName();  
%>
<style>
  <% _ctx.include(uicomponent.getTemplateSkin("exo:event", "Stylesheet")); %>
</style>
<div id="$uicomponent.id" $resizeBlock>
	<%
	 def node = uicomponent.getNode() ;		
	 def relations = uicomponent.getRelations() ;
	 def attachments = uicomponent.getAttachments() ;
	 if(node.hasProperty("exo:title")) {
	%>
		<div class="UIArticle">

      <%if(node.hasProperty("exo:endEvent")) {
        Calendar cal = node.getProperty("exo:endEvent").getDate();
        def day = cal.get(Calendar.DAY_OF_MONTH);
        dateFormat.applyPattern("MMM") ;
        def month = dateFormat.format(cal.getTime());       
        def year = cal.get(Calendar.YEAR);
      %>
      <div class="EndDateLabel">
        <div class="datetime"> 
        <div class="top"><p><%=month%></p></div> 
        <p class="day"><%=day%></p> 
        <p class="yr"><%=year%></p> 
        </div>
      </div>
      <div class="ArrowDate" height="30px"><span></span></div>
      <%}%>     

      <%if(node.hasProperty("exo:startEvent")) {
        Calendar cal = node.getProperty("exo:startEvent").getDate();
        def day = cal.get(Calendar.DAY_OF_MONTH);
        dateFormat.applyPattern("MMM") ;
        def month = dateFormat.format(cal.getTime());       
        def year = cal.get(Calendar.YEAR);
      %>
      <div class="StartDateLabel">
        <div class="datetime"> 
        <div class="top"><p><%=month%></p></div> 
        <p class="day"><%=day%></p> 
        <p class="yr"><%=year%></p> 
        </div>
      </div>
      <%}%>     
      
      <%if(node.hasProperty("exo:location")) { %>
      <div class="LocationLabel"><% print uicomponent.getInlineEditingField(node, "exo:location");%></div>
      <%}%>           

      <div class="TitleLabel">
        <%if(node.hasProperty("exo:url")) { 
        	def strURL = node.getProperty("exo:url").getString();
        %>
        <a onclick="javascript:window.open('$strURL');" style="cursor:pointer">
        <%}%>
        <%=node.getProperty("exo:title").getString()%>
        <%if(node.hasProperty("exo:url")) { %>
        </a>
        <%}%>           
			</div>
			
			<div style="clear:both"></div>
												
			<div class="ArticleFrame">
        <% if(node.hasProperty("exo:summary")) { %>
          <div class="LabelContent">
            <%
              print uicomponent.getInlineEditingField(node, "exo:summary", "", "WYSIWYG", "Summary", "Content", true, "toolbar=SuperBasicWCM");
            %>
          </div>
        <% } %>
        <% if(node.hasProperty("exo:text")) { %>
          <div class="LabelContent">
            <%
              print uicomponent.getInlineEditingField(node, "exo:text", "", "WYSIWYG", "Text", "Content", true);
            %>
          </div>
        <% } %>
			</div>

      <% if(node.hasProperty("exo:maps") && node.getProperty("exo:maps").getBoolean()) { %>
        <div class="MapsFrame">
        <% System.out.println("http://maps.google.com/maps?q=" + node.getProperty("exo:location").getString() + "&output=embed"); %>
          <iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" 
          src="http://maps.google.com/maps?q=<%=node.getProperty("exo:location").getString()%>&output=embed">
          </iframe><br />
          <small><a href="http://maps.google.com/maps?q=<%=node.getProperty("exo:location").getString()%>" style="color:#0000FF;text-align:left" target="_maps">View Larger Map</a>
          </small>
        </div>            
      <% } %>

						
      <%if(relations.size() > 0) {%>
				<div class="ArticleFrame">
				
				  <div class="TopLeftArticleFrame">
				    <div class="TopRightArtictleFrame">
				      <div class="TopCenterArticleFrame">
				        <div class="ArticleField">
		              <div class="FieldIcon SummaryIcon"><span></span></div>
		              <div class="FieldLabel"><%=_ctx.appRes("Article.view.label.link")%> :</div>
		              <div style="clear: left;"><span></span></div>
                </div>
				      </div>
				    </div>
				  </div>

				  <div class="MiddleLeftArticleFrame">
				    <div class="MidlleRightArticleFrame">
	            <div class="ArticleFrameContainer">
	              <div class="FieldContent">
	              	<div class="FieldContainerContent">

                    <div class="LabelContent">
                      <div class="Links">
                          <%
                            for(rel in relations) {
                              if(rel != null) {
                                String wsName = rel.getSession().getWorkspace().getName() ;
                                String actionLink ;
                                String relPath = rel.getPath() ;
                                String relName = relPath.substring(relPath.lastIndexOf("/") + 1, relPath.length()) ; 
                                if(!uicomponent.isRssLink()) { 
                                  params  = [new Parameter("workspaceName", wsName), new Parameter("findDrive","true")];
                                  actionLink = uicomponent.event("ChangeNode", Utils.formatNodeName(relPath), params);
                                } else { 
                                  actionLink = uicomponent.getRssLink() + wsName + relPath ;
                                }
                              
                          %>
                          <div> - <a onclick="$actionLink" style="cursor: pointer;">$relName</a></div>
                        <%    }
                          }%>
                      </div>
                    </div>

									</div>	
	              </div>
	            </div>
	          </div>
	        </div>
	        
        </div>
      <%}%>
			        
			        

      <% if(attachments.size() > 0) { %>
        <div style="height: 10px;"><span></span></div>
        <div class="ArticleFrame">
				  <div class="TopLeftArticleFrame">
				    <div class="TopRightArtictleFrame">
				      <div class="TopCenterArticleFrame">
			          <div class="ArticleField">
		              <div class="FieldIcon AttachmentsIcon"><span></span></div>
		              <div class="FieldLabel"><%=_ctx.appRes("Article.view.label.attachment")%> :</div>
				          <div style="clear: left;"><span></span></div>
				        </div>
	            </div>
	          </div>
			    </div> 
			    <div class="MiddleLeftArticleFrame">
				    <div class="MidlleRightArticleFrame">
	            <div class="ArticleFrameContainer">
	              <div class="FieldContent">
	              	<div class="FieldContainerContent">
	              	<%
					        def removeattachcomponent = uicomponent.getRemoveAttach();
					        String wsName = "";
													for (att in attachments) {
														if(att != null) {
							  							wsName = att.getSession().getWorkspace().getName();
							  							params  = [new Parameter("workspaceName", wsName), new Parameter("findDrive","true")];
							  							def viewAttachmentLink =  uicomponent.getAttachmentURL(att, params);
							  							def linkActionName = (viewAttachmentLink.startsWith("http")) ? "href" : "onclick";
												%>
												  <div class="AttachmentsContainer">	
					                  <div class="AttachmentsContentIcon">
					                  <a $linkActionName="$viewAttachmentLink" style="cursor: pointer;"><%=att.getName()%></a>
					                  </div>
					                  <% if (PermissionUtil.canRemoveNode(att)) {
					                       params = [new Parameter("workspaceName", wsName), new Parameter("repository", repository)];
					                    %>
					                    <a onclick="<%=removeattachcomponent.event("RemoveAttach", Utils.formatNodeName(att.getPath()), params)%>" style="cursor: pointer;" title="<%=_ctx.appRes(uicomponent.getId() + ".tooltip.remove-attachment")%>">
					                      <div class="RemoveAttachments"><span></span></div>
					                    </a>
                            <% } %>
					                  <div style="clear: left;"><span></span></div>
					                </div>
				              <%	}
				              	} %>
									</div>
								</div>
						  </div>
					  </div>
          </div>
        </div>
        <div style="height: 10px;"><span></span></div>
				              	 
			<% }%>
			
  		<% _ctx.include(uicomponent.getViewTemplate("mix:votable", "view1")); %> 
	
		</div>

		<% _ctx.include(uicomponent.getViewTemplate("exo:comments", "view1")); %>

  <%} else {%>
    <div class="UIArticle">
      <div class="FieldLabel"><%=_ctx.appRes("Article.msg.no-view")%> </div>
    </div>
  <%}%>
</div>
