<%
	import java.util.List;
	import java.util.ArrayList;
	import javax.jcr.Node;
	import javax.jcr.NodeIterator;
	import org.exoplatform.webui.core.UIPopupWindow;
	import org.exoplatform.web.application.Parameter;
	
	public Node getFileLangNode(Node currentNode) throws Exception {
    if(currentNode.isNodeType("nt:unstructured")) {
      if(currentNode.getNodes().getSize() > 0) {
        NodeIterator nodeIter = currentNode.getNodes() ;
        while(nodeIter.hasNext()) {
          Node ntFile = nodeIter.nextNode() ;
          if(ntFile.getPrimaryNodeType().getName().equals("nt:file")) {
            return ntFile ;
          }
        }
        return currentNode ;
      }
    }
    return currentNode ;
  }
	
	UIPopupWindow uiPopupWindow = uicomponent.getAncestorOfType(UIPopupWindow.class);
  def resizeBlock = "class=\"UIResizableBlock\"";
  if(uiPopupWindow != null) resizeBlock = ""; 

  def originalNode = uicomponent.getOriginalNode();
  def currentNode = getFileLangNode(uicomponent.getNode()) ;
  def contentNode = currentNode.getNode("jcr:content") ;
  def mimeType = contentNode.getProperty("jcr:mimeType").getString() ;
  List<String> unsupportMimeTypeText = new ArrayList<String>();
  unsupportMimeTypeText.add("text/rtf");
  public String formatNodeName(String text) {
    return text.replaceAll("'", "\\\\'") ;
  }
%>
<div $resizeBlock>
	<style>
    <% _ctx.include(uicomponent.getTemplateSkin("nt:file", "Stylesheet")); %>
  </style>
	<div class="FileContent">
	  <%
	    if(mimeType.equals("application/rss+xml")) mimeType="text/html"; 
	    def fileComponent = uicomponent.getUIComponent(mimeType);
	    if(fileComponent != null) {
	      uicomponent.renderUIComponent(fileComponent);
	    } else {
	  %>
			<div class="NavigationContainer">       
			  <div class="TopTitle">$originalNode.name</div>
				<div class="Content">
			  	<div class="TextContent">
						<div style="text-align:center; font-style:italic">
						  <%=_ctx.appRes("File.view.label.not-viewable")%>
						</div>
					</div>
				</div>
			</div>
		<%}%>   	      		
	  <div class="UIAction">                                          
		  <table class="ActionContainer">  
		  	<tr>
		  		<td>
			      <div onclick="javascript:eXo.ecm.ECMUtils.generateWebDAVLink('<%=uicomponent.getWebDAVServerPrefix()%>','<%=uicomponent.getPortalName()%>','<%=uicomponent.getRepository()%>','<%=uicomponent.getWorkspaceName()%>','<%=formatNodeName(currentNode.getPath())%>','<%=mimeType%>')" class="ActionButton LightBlueStyle">
			        <div class="ButtonLeft">
			          <div class="ButtonRight">
			            <div class="ButtonMiddle">
			              <a href="javascript:void(0);"><%=_ctx.appRes("File.view.label.webDAV")%></a>
			            </div>
			          </div>
			        </div>
			      </div>
			      <div onclick="<%=uicomponent.event("Download")%>" class="ActionButton LightBlueStyle">
			        <div class="ButtonLeft">
			          <div class="ButtonRight">
			            <div class="ButtonMiddle" title="<%=_ctx.appRes("File.view.label.download")%>">
			              <a href="javascript:void(0);"><%=_ctx.appRes("File.view.label.download")%></a>
			            </div>
			          </div>
			        </div>
			      </div>
		  		</td>
		  	</tr>          
		  </table>
		</div>                  
	  <div><% _ctx.include(uicomponent.getViewTemplate("mix:votable", "view1")); %></div>
	  <div><% _ctx.include(uicomponent.getViewTemplate("exo:comments", "view1")); %></div>
	</div>
</div>