<%

import org.exoplatform.ecm.webui.component.explorer.sidebar.UITreeExplorer;
import org.exoplatform.webui.core.UIRightClickPopupMenu ;
import org.exoplatform.ecm.jcr.model.Preference;
import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
import org.exoplatform.ecm.webui.component.explorer.UIWorkingArea;

	def totalPage = uicomponent.getAvailablePage();
	def currentPage = uicomponent.getCurrentPage();

  def rcontext = _ctx.getRequestContext() ;	
  UITreeExplorer uiTreeExplorer = uicomponent.getAncestorOfType(UITreeExplorer.class);
  def focusNode = uiTreeExplorer.getEncodeCurrentPath();
  def uiExplorer = uicomponent.getAncestorOfType(UIJCRExplorer.class);
  def pref = uiExplorer.getPreference();
  def enableDragAndDrop = pref.isEnableDragAndDrop();

  String uiWorkingAreaId = uicomponent.getAncestorOfType(UIWorkingArea.class).getId(); 
  rcontext.getJavascriptManager().addJavascript("eXo.ecm.UIListView.initDragDropForTreeEvent('$uiWorkingAreaId', '$enableDragAndDrop');");
  rcontext.getJavascriptManager().addOnLoadJavascript('eXo.ecm.ECMUtils.loadEffectedItemsInSideBar');
  rcontext.getJavascriptManager().addJavascript("eXo.ecm.ECMUtils.loadEffectedSideBar('$focusNode');") ;  
  
%>

<div class="UITreeNodePageIterator">
	<table class="NodePageIterator">
		<tr>
			<td>
		    <%if( currentPage - 1 > 0 ){%>        
		    	  <a href="<%=uicomponent.event("ShowPage","${currentPage - 1}")%>" class="Icon LastPage" title="<%=_ctx.appRes("UIPageIterator.label.previous")%>"><span></span></a>
		    <% } else {%>
		    	  <a class="Icon LastPage DisablePage" title="<%=_ctx.appRes("UIPageIterator.label.previous")%>"><span></span></a>
		    <%}%>
		  </td>
      <%
      int min = 1;
      int max = totalPage;
      if(totalPage > 5 && currentPage > 2){
        min = currentPage - 2;
        max = currentPage + 3;
        if (max > totalPage) max = totalPage;
      } else if( totalPage > 5 && currentPage < 3 ){
        max = 6;
        min = 1;
      } else {
        min = 1;
        max = totalPage;
      }
      for( i in min..max) { %>
      	<td>
	      <%if( i == currentPage) { %>
	          <a href="<%=uicomponent.event("ShowPage","$i")%>" class="PageSelected"><%=i%></a>
	      <%} else { %>
	          <a href="<%=uicomponent.event("ShowPage","$i")%>"><%=i%></a>
	      <%} %>
      	</td> 
      <%}%>
	    <td>
		    <% if(currentPage +1 <= totalPage) { %>
		    	   <a href="<%=uicomponent.event("ShowPage","${currentPage + 1}")%>" class="Icon NextPage" title="<%=_ctx.appRes("UIPageIterator.label.next")%>"><span></span></a>
		    <% } else { %>
		    	   <a class="Icon NextPage DisablePage" title="<%=_ctx.appRes("UIPageIterator.label.next")%>"><span></span></a>
		    <%}%>
	    </td>	  
		</tr>
	</table>
</div>