<%
	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.ecm.webui.utils.Utils;	
	import org.exoplatform.services.wcm.core.NodeLocation;
	import org.exoplatform.ecm.utils.text.Text;
	
	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 ;
  }
	
  def rcontext = _ctx.getRequestContext() ;	
  rcontext.getJavascriptManager().importJavascript('eXo.ecm.ECMUtils','/ecm-wcm-extension/javascript/');
  rcontext.getJavascriptManager().importJavascript('eXo.ecm.DMSBrowser','/ecm-wcm-extension/javascript/');
  rcontext.getJavascriptManager().addJavascript("eXo.ecm.DMSBrowser.managerResize();");
	
	
  UIPopupWindow uiPopupWindow = uicomponent.getAncestorOfType(UIPopupWindow.class);
  def resizeBlock = "class=\"UIResizableBlock\"";
  if(uiPopupWindow != null) resizeBlock = ""; 
  
  def currentNode = getFileLangNode(uicomponent.getNode()) ;
  currentNode = org.exoplatform.wcm.webui.Utils.getRealNode(currentNode);
  def currentNodePath = org.exoplatform.wcm.webui.Utils.getRealNodePath(currentNode);
  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">
			<div class="NavigationContainer">       
			  <div class="TopTitle" style="float:left;">
			   <%=Text.unescapeIllegalJcrChars(currentNode.name) %>
			  </div>
        <div style="float:right;padding: 4px 10px;" onclick="javascript:eXo.ecm.ECMUtils.generateWebDAVLink('<%=uicomponent.getWebDAVServerPrefix()%>','<%=uicomponent.getPortalName()%>','<%=Utils.getRestContextName(uicomponent.getPortalName())%>','<%=uicomponent.getRepository()%>','<%=uicomponent.getWorkspaceName()%>','<%=formatNodeName(currentNodePath)%>','<%=mimeType%>')" class="ActionButton LightBlueStyle">
          <a href="javascript:void(0);"><%=_ctx.appRes("File.view.label.download")%></a>
        </div>			  
        <div style="clear:both"></div>
				<div class="Content">
        <%
          def fileComponent = uicomponent.getUIComponent(mimeType);
          if(fileComponent != null) {
            uicomponent.renderUIComponent(fileComponent);
          } else {
        %>
					  <%=_ctx.appRes("File.view.label.not-viewable")%>
    		<%}%>   	      		
				</div>
		</div>
	  <div><% _ctx.include(uicomponent.getViewTemplate("mix:votable", "view1")); %></div>
	  <div><% _ctx.include(uicomponent.getViewTemplate("exo:comments", "view1")); %></div>
	</div>
</div>