<%
	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;
	import org.exoplatform.services.jcr.RepositoryService;
  RepositoryService rService = uicomponent.getApplicationComponent(RepositoryService.class);
  String repository = rService.getCurrentRepository().getConfiguration().getName();	
	
	
	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">                                          
          <a href="javascript:void(0);" onclick="javascript:eXo.ecm.ECMUtils.generateWebDAVLink('','<%=uicomponent.getPortalName()%>','${repository}','<%=uicomponent.getWorkspaceName()%>','<%=formatNodeName(currentNode.getPath())%>','<%=mimeType%>')" class="ActionButton LightBlueStyle"><%=_ctx.appRes("File.view.label.webDAV")%></a>
          <a href="javascript:void(0);" title="<%=_ctx.appRes("File.view.label.download")%>" onclick="<%=uicomponent.event("Download")%>" class="ActionButton LightBlueStyle"><%=_ctx.appRes("File.view.label.download")%></a>
		</div>                  
	  <div><% _ctx.include(uicomponent.getViewTemplate("mix:votable", "view1")); %></div>
	  <div><% _ctx.include(uicomponent.getViewTemplate("exo:comments", "view1")); %></div>
	</div>
</div>