<%
	import java.util.List;
	import java.util.ArrayList;
	import javax.jcr.Node;
	import javax.jcr.NodeIterator;
	import org.exoplatform.webui.core.UIComponent;
  import org.exoplatform.services.jcr.util.Text;	
  import org.exoplatform.wcm.webui.Utils;
	
	UIComponent uiParent = uicomponent.getParent();

  def originalNode = uiParent.getOriginalNode();
  def currentNode = uiParent.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("'", "\\\\'") ;
  }
  
  public long getFileSize(Node contentNode) throws Exception {
    long size = contentNode.getProperty("jcr:data").getLength()/1024;      	        
    return size;
  }
  
  if(mimeType.equals("text/html") || mimeType.equals("application/rss+xml")) { 
%>

	<div class="UITabPane">
	  <div class="TabPaneContent">
	    <div class="WorkingAreaWithHelp">
	      <div class="UIHorizontalTabs">
	        <div class="LeftHorizontalTabs">
		     	  <div class="RightHorizontalTabs">
		     	    <div class="CenterHorizontalTabs">
				        <div class="TabsContainer">
				         
				            <div class="UITab NormalTabStyle">
				              <div class="SelectedTab">
				                <div class="LeftTab">
				                  <div class="RightTab">                
				                    <div class="MiddleTab" onClick="eXo.webui.UIHorizontalTabs.displayTabContent(this)"><%=_ctx.appRes("File.view.label.htmlview")%></div>
				                  </div>
				                </div>
				              </div>
				            </div> 
				            
				            <div class="UITab NormalTabStyle">
				              <div class="NormalTab">
				                <div class="LeftTab">
				                  <div class="RightTab">                
				                    <div class="MiddleTab" onClick="eXo.webui.UIHorizontalTabs.displayTabContent(this)"><%=_ctx.appRes("File.view.label.plaintextview")%></div>
				                  </div>
				                </div>
				              </div>
				            </div>   
				                         
				        </div>
				      </div>
				    </div>
				  </div>
	      </div>
	      <div class="UITabContentContainer">
	        <div class="UITabContent" style="display: block;"> 
		        <div class="NavigationContainer">       
	    				<div class="TopTitle" title="<%=Text.unescapeIllegalJcrChars(originalNode.getName())%>"><%=Text.unescapeIllegalJcrChars(originalNode.getName())%></div>
			        <%
			        	if(contentNode.hasProperty("jcr:data")){
			        	  String iframeId = uiParent.getId() + "ifame" ;
			        	  def rqcontext = _ctx.getRequestContext() ;
			        	  rqcontext.getJavascriptManager().addCustomizedOnLoadScript("eXo.ecm.ECMUtils.replaceToIframe('$iframeId');");			        	  
			        %>
			        	<div style="text-align: center">
			        	<textarea id="<%=iframeId%>"><%=Utils.sanitize(contentNode.getProperty("jcr:data").getString())%></textarea> 
			        	</div>
			        <%}%>
				    </div>   
	        </div>
	        <div class="UITabContent" style="display: none;"> 
	        	<div class="NavigationContainer">       
						<div class="TopTitle" title="$originalNode.name"><%=Text.unescapeIllegalJcrChars(originalNode.getName())%></div>
		        	<div class="Content">
		        		<div class="TextContent">
					        <%
					        	if(contentNode.hasProperty("jcr:data")) {
					        		println "<pre>" + uiParent.encodeHTML(contentNode.getProperty("jcr:data").getString()) + "</pre>"; 
					        	}
					        %>
			        	</div>
		        	</div>				     
				  	</div>   
	        </div>
	      </div>  
	    </div>  
	  </div>  
	</div>  
<%} else {%>
  <%        
    if(!unsupportMimeTypeText.contains(mimeType) && contentNode.hasProperty("jcr:data")) {
      if(getFileSize(contentNode) < 1024) {
        println "<pre>" + uiParent.encodeHTML(contentNode.getProperty("jcr:data").getString()) + "</pre>"; 
      } else {
   %>
        <div style="text-align:center; font-style:italic">
			    <%=_ctx.appRes("File.view.label.file-size-too-big")%>
			  </div>
   <%
      }
    } 
   %>
<%}%>
