<%
	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.ecm.webui.utils.Utils;
  import org.exoplatform.services.jcr.RepositoryService;   
  import org.exoplatform.wcm.webui.reader.ContentReader;
  
	
	UIComponent uiParent = uicomponent.getParent();
  RepositoryService rService = uicomponent.getApplicationComponent(RepositoryService.class);
  String repository = rService.getCurrentRepository().getConfiguration().getName();	
	
	public boolean isVersionable(Node node) throws Exception {
    return node.isNodeType("mix:versionable");
  }		

  def originalNode = uiParent.getOriginalNode();  
  def currentNode = uicomponent.getFileLangNode(uiParent.getNode());
  def contentNode = currentNode.getNode("jcr:content") ;
  def mimeType = contentNode.getProperty("jcr:mimeType").getString() ;
   public String formatNodeName(String text) {
    return text.replaceAll("'", "\\\\'") ;
  }
  def currentPage = uicomponent.getPageNumber();
  def maximumPage = uicomponent.getMaximumOfPage();
  def currentRotation = uicomponent.getCurrentRotation();
  def currentScale = uicomponent.getCurrentScale();
  def metadatas = uicomponent.getMetadataExtraction();
  def rqcontext = _ctx.getRequestContext() ;
  rqcontext.getJavascriptManager().addJavascript('eXo.ecm.ECMUtils.onKeyPDFViewerPress();') ;
%>
<head>
  <style>

.UIPDFViewerContainer {
	border: 1px solid #d1d1d1;
} 

.UIPDFViewerContainer .PDFViewerBar {
	background: url('/eXoDMSResources/skin/images/file/PDFViewerBar.gif') repeat-x ;
	height: 28px; 
}

.UIPDFViewerContainer .PDFViewerBar .ControlButton {
	float: left;
	line-height: 28px;
	margin-left: 8px;
}


.UIPDFViewerContainer .PDFViewerBar .ControlButton .ButtonBack{
	background: url('/eXoDMSResources/skin/images/file/ButtonBack.gif') no-repeat left center ;
	height: 28px;
	width: 18px;
	cursor: pointer;
}

.UIPDFViewerContainer .PDFViewerBar .ControlButton .ButtonNext{
	background: url('/eXoDMSResources/skin/images/file/ButtonNext.gif') no-repeat left center ;
	height: 28px;
	width: 18px;
	cursor: pointer;
}

.UIPDFViewerContainer .PDFViewerBar .ControlButton .ZoomIn{
	background: url('/eXoDMSResources/skin/images/file/ZoomIn.gif') no-repeat left center ;
	height: 28px;
	width: 18px;
	cursor: pointer;
}


.UIPDFViewerContainer .PDFViewerBar .ControlButton .ZoomOut{
	background: url('/eXoDMSResources/skin/images/file/ZoomOut.gif') no-repeat left center ;
	height: 28px;
	width: 18px;
	cursor: pointer;
}

.UIPDFViewerContainer .PDFViewerBar .ControlButton .RotateLeft{
	background: url('/eXoDMSResources/skin/images/file/HeightToWidth.gif') no-repeat left center ;
	height: 28px;
	width: 18px;
	cursor: pointer;
}

.UIPDFViewerContainer .PDFViewerBar .ControlButton .RotateRight{
	background: url('/eXoDMSResources/skin/images/file/WidthToHeight.gif') no-repeat left center ;
	height: 28px;
	width: 18px;
	cursor: pointer;
}

.UIPDFViewerContainer .PDFViewerBar .ControlButton .InfoIcon{
	background: url('/eXoDMSResources/skin/images/file/InfoIcon.gif') no-repeat left center ;
	height: 28px;
	width: 18px;
	cursor: pointer;
}

.UIPDFViewerContainer .PDFViewerBar .DownLoadIcon{
	background: url('/eXoDMSResources/skin/images/file/DownLoadIcon.gif') no-repeat left center ;
	Line-height: 28px;
	cursor: pointer;
	float: right;
	margin-right: 8px;
	color: #4f4f4f;
	padding-left: 22px;
}

.UIPDFViewerContainer .PDFViewerContent {
	padding-top: 12px;
}

.UIPDFViewerContainer .PDFViewerContent .ImageView{
	margin:auto;
	height: 100%;
	text-align: center;
}

.UIPDFViewerContainer .PDFViewerBar .ControlButton .PageNumber{
	height: 20px;
	width: 28px;
	margin: 4px 0px 0px 0px;
	line-height: 20px;
}

.UIPDFViewerContainer .PDFViewerBar .ControlButton .PageNumber input{
	width: 28px;
	border: 1px solid #c7c7c7;
	text-align: center;
}

.UIPDFViewerContainer .PDFViewerBar .ControlButton .SeparatorLine{
	height: 28px;
	border-left: 1px solid #c7c7c7;
}

.UIPDFViewerContainer .InfoPopup{
	padding: 5px;
	background: #fafafa;
	border: 1px solid #c7c7c7;
}

.UIPDFViewerContainer .InfoPopup .metaKey{
	font-weight: bold;
}

.UIPDFViewerContainer .InfoPopup .metaValue{
	margin-left: 5px;
}

.UIPDFViewerContainer .InfoPopup .Metadata{
	margin: 5px 5px 0px 0px;
}


  </style>  
</head>
<%uiform.begin()%>
<%
	if (maximumPage > 0) {
%>    
	<div class="UIPDFViewerContainer">   
 	 <div class="PDFViewerBar ClearFix">
		<div class="ControlButton">
			<a href="<%=uicomponent.event("PreviousPage")%>" title="<%=uicomponent.getResourceBundle("PDFViewer.label.previousPage")%>"><div class="ButtonBack"><span></span></div></a>
		</div>
		<div class="ControlButton">
			<div class="Lable"><%=uicomponent.getResourceBundle("PDFViewer.label.page")%></div>
		</div>
		<div class="ControlButton" id="PageControl">
		  <a href="<%=uicomponent.event("GotoPage")%>" id="GotoPage"></a>
			<div class="PageNumber"><%uiform.renderField(uicomponent.getChildById("pageNumber"))%></div>
		</div>
		<div class="ControlButton">
			<div class="Lable"><%=uicomponent.getResourceBundle("PDFViewer.label.of")%> $maximumPage</div>
		</div>
		<div class="ControlButton">
			<a href="<%=uicomponent.event("NextPage")%>" title="<%=uicomponent.getResourceBundle("PDFViewer.label.nextPage")%>">
			  <div class="ButtonNext"><span></span></div>
			</a>
		</div>
		<div class="ControlButton">
			<div class="SeparatorLine"><span></span></div>
		</div>
		<div class="ControlButton">
			<a href="<%=uicomponent.event("ZoomOutPage")%>" title="<%=uicomponent.getResourceBundle("PDFViewer.label.zoomOutPage")%>">
			  <div class="ZoomOut"><span></span></div>
			</a>
		</div>
		<div class="ControlButton">
			<a href="<%=uicomponent.event("ZoomInPage")%>" title="<%=uicomponent.getResourceBundle("PDFViewer.label.zoomInPage")%>">
			  <div class="ZoomIn"><span></span></div>
			</a>
		</div>
		<div class="ControlButton">
		   <div class="selectbox"><%uiform.renderField(uicomponent.getChildById("scalePage"))%></div>
		</div>
		<div class="ControlButton">
			<a href="<%=uicomponent.event("RotateLeftPage")%>" title="<%=uicomponent.getResourceBundle("PDFViewer.label.rotateLeft")%>">
			  <div class="RotateLeft"><span></span></div>
			</a>
		</div>
		<div class="ControlButton">
			<a href="<%=uicomponent.event("RotateRightPage")%>" title="<%=uicomponent.getResourceBundle("PDFViewer.label.rotateRight")%>"><div class="RotateRight"><span></span></div></a>
		</div>
		<div class="ControlButton">
			<div class="InfoIcon" onclick="javascript:eXo.ecm.ECMUtils.showDocumentInformation(this, event);" title="<%=uicomponent.getResourceBundle("PDFViewer.label.viewInfor")%>"><span></span></div>
		</div>
		<a href="javascript: void(0)"><div class="DownLoadIcon" onclick="<%=uicomponent.event("DownloadFile")%>"><%=uicomponent.getResourceBundle("PDFViewer.label.downloadFile")%></div></a>
		
	</div>
	<div class="InfoPopup" id="metadatas" style="display: none;position: absolute;">
		<div class="Metadata"><span class="metaKey"><%=uicomponent.getResourceBundle("PDFViewer.metadata.fileName")%></span>: <span class="metaValue"><%=ContentReader.getXSSCompatibilityContent(originalNode.getName())%></span></div>
		<%for(key in metadatas.keySet()) {%>
			<div class="Metadata"><span class="metaKey"><%=uicomponent.getResourceBundle("PDFViewer.metadata." + key)%>: </span><span class="metaValue"><%=metadatas.get(key)%></span></div>
		<%}%>
	</div>
	<div class="PDFViewerContent">
  	<div class="ImageView" style="overflow-x:auto; overflow-y:hidden" id ="pdf_viewer_image" name="pdf_viewer_image">
		<%
			String pdfPageImage = "/" + uiParent.getPortalName() + "/" + Utils.getRestContextName(uiParent.getPortalName()) + 
			                        "/pdfviewer/" + repository + "/" + uiParent.getWorkspaceName() + 
			                        "/${currentPage}/${currentRotation}/${currentScale}/" + currentNode.getUUID() + "?" +
			                        originalNode.getProperty("jcr:content/jcr:lastModified").getString();				  
		%>
			  <a href="$pdfPageImage" style="display:inline-block" title="<%=uicomponent.getResourceBundle("PDFViewer.label.viewFullPage")%>"><img src="$pdfPageImage" /></a>
		</div>
	</div>
  </div>
<% }else {%>	
	  <div style="text-align:center; font-style:italic">
	    <%=_ctx.appRes("File.view.label.not-viewable")%>
	  </div>
<% }%>
<%uiform.end()%>		
