<%
	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;
  import org.apache.commons.lang.StringEscapeUtils;
  import org.exoplatform.wcm.webui.reader.ContentReader;
	
	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="<%=ContentReader.getXSSCompatibilityContent(originalNode.getName())%>"><%=ContentReader.getXSSCompatibilityContent(originalNode.getName())%></div>
			        <%
			        	if(contentNode.hasProperty("jcr:data")){
			        	  String iframeId = uiParent.getId() + "ifame" ;
			        	  def rqcontext = _ctx.getRequestContext() ;
			        	  def strContent = StringEscapeUtils.unescapeHtml(contentNode.getProperty("jcr:data").getString());	
			        	  strContent = ContentReader.getEscapeHtmlContent(Utils.sanitize(strContent));	        	  
			        %>
			        	<div style="text-align: center">
			        	  <textarea id="<%=iframeId%>"><%=strContent%></textarea> 
			        	  <script type="text/javascript">
				            eXo.ecm.WCMUtils.replaceToIframe('$iframeId');
				          </script>
			        	</div>
			        <%}%>
				    </div>   
	        </div>
	        <div class="UITabContent" style="display: none;"> 
	        	<div class="NavigationContainer">       
						<div class="TopTitle" title="<%=ContentReader.getXSSCompatibilityContent(originalNode.getName())%>"><%=ContentReader.getXSSCompatibilityContent(originalNode.getName())%></div>
		        	<div class="Content">
		        		<div class="TextContent">
					        <%
					        	if(contentNode.hasProperty("jcr:data")) {
					        	  def strContent = StringEscapeUtils.unescapeHtml(contentNode.getProperty("jcr:data").getString());	
			        	  		strContent = ContentReader.getEscapeHtmlContent(Utils.sanitize(strContent));
					        		println "<pre>" + strContent + "</pre>"; 
					        	}
					        %>
			        	</div>
		        	</div>				     
				  	</div>   
	        </div>
	      </div>  
	    </div>  
	  </div>  
	</div>  
<%} else {%>
  <%        
    if(!unsupportMimeTypeText.contains(mimeType) && contentNode.hasProperty("jcr:data")) {
      def strContent = StringEscapeUtils.unescapeHtml(contentNode.getProperty("jcr:data").getString());	
			strContent = ContentReader.getEscapeHtmlContent(Utils.sanitize(strContent));
      if(getFileSize(contentNode) < 1024) {
        println "<pre>" + strContent + "</pre>"; 
      } else {
   %>
        <div style="text-align:center; font-style:italic">
			    <%=_ctx.appRes("File.view.label.file-size-too-big")%>
			  </div>
   <%
      }
    } 
   %>
<%}%>
