/**
 * This function is called for changing the style
 * of the pagination link selected by the user (search-results.xhtml page).
 * When the user clicks in a page link, this link has to stay selected
 * till the user clicks again in other link: the style of the clicked
 * link has to be set up to 'paginationClassSelected' and all the other
 * links of the pagination to 'paginationClass'
 * @param linkObj representing the current link object clicked
 * @param index is the property value of linkObj (as it is converted into a tag span in html, 
 *   the value can not be retrieved)
 **/
function changeCSSLinkOverview(linkObj,index) {  
	
	 if (linkObj) {
		 linkObj.className = 'paginationClassSelected'; 
		 var index = document.getElementById(linkObj.id);
		 if (index) {
			 if (index.firstChild.nodeValue > 0){
				 /** Note that, in order to control the current page, for manipulating the
				  * first, previous, next and last elements, we have to store the current page value in
				  * some place that we can retrieve after ... this place is a hidden input used to store
				  * the page current 
				  **/
				  var currentPage = document.getElementById('formOverview:currentPage');	
				  if (currentPage) {
					  /** the first page for the user is 1 (what is show in the interface) 
					   * but for the search method is 0. It is why we decrease always 1 **/
					  if (index.firstChild.nodeValue.match('^(0|[1-9][0-9]*)$')) {
					      currentPage.value = parseInt(index.firstChild.nodeValue) - 1;
					  }
				  }
			 };
		 }
	   	 linkObj.blur();
 	 }
	 /** Put all other links in the list of links with 'normal' style **/
	 var links = document.getElementById('formOverview:navigationLinks');		
	 if (links) { 
		  for (var i = 0; i < links.childNodes.length; i++) {
	    	  var link = links.childNodes.item(i);
	    	   if (link.id != null && link.id != linkObj.id) {
	    		   link.className  = 'paginationClass'; 
	    	   }
	      }
	 }	 
}

/** NAVIGATION function implementations */

/**
 * This function implements the first control : set up the window 
 * pagination from 1 to size window (5, for instance). Two hidden files
 * contain the firstPageWindow value and the lastPageWindow value. These 
 * values have to be updated in each function (first, previous, next, last).
 * 
 * @param sizeWindow is the current set up size of the pagination window (ex: 5 = 1|2|3|4|5 )
 * @param countPages is number total of results page
 */
function firstOverview(sizeWindow,countPages) {
	 /** Retrieves the value of a hidden field that indicates the current last page **/
	  var firstPageWindow = document.getElementById('firstPageWindow');	
	  if (firstPageWindow) {
		  /** Only decreases each link (1,2,3,4,5 pages) if the firstPage is > 1 **/
		  if (firstPageWindow.value > 1) {
			  firstPageWindow.value = 1;
			  /** when click in the first, the last page of the window is the size of the window **/
			  var lastPageWindow = document.getElementById('lastPageWindow');
			  if (lastPageWindow) {
				  lastPageWindow.value = sizeWindow;
			  }
			  /** Retrieves all the links in the pagination bar and add 1 **/
		      var links = document.getElementById('formOverview:navigationLinks');	
	  	      if (links) { 
	  	    	 /** For each link **/
		  		  var cont = 0;
	              for (var i = 0; i < links.childNodes.length; i++) {
	    	          var link = links.childNodes.item(i);
	    	          /** If the current link has a child **/
	    	          if (link.id != null && link.hasChildNodes()) {
	    	    	       /** if it is a 'numerical link', then add 1 (next behavior) **/
	    	        	   /** it is needed because formOverview:navigationLinks contain >, >>, or 'next' ... **/
				    	   if (link.firstChild.nodeValue.match('^(0|[1-9][0-9]*)$')) {
				    		   cont = cont + 1;
				    		   link.firstChild.nodeValue = cont;
				    	   };
				       };
			       };
	    	  };
	    	  /** When click in the first, last and next become visible and first and previous are hidden **/
	    	  changeClassLastNextOverview('paginationClassVisible');
			  changeClassFirstPreviousOverview('paginationClassHidden');
	    };
	  };
};

/**
 * This function implements the previous control. 
 * Two hidden files contain the firstPageWindow value and the lastPageWindow value. These 
 * values have to be updated in each function (first, previous, next, last).
 * @param sizeWindow is the current set up size of the pagination window (ex: 5 = 1|2|3|4|5 )
 * @param countPages is number total of results page
 */
function previousOverview(sizeWindow,countPages) {
	  /** Retrieves the value of a hidden field that indicates the current last page **/
	  var firstPageWindow = document.getElementById('firstPageWindow');	
	  if (firstPageWindow) {
		  /** Only decreases to previous if the firstPageWindow is > 1 **/
		  if (firstPageWindow.value > 1) {
			  firstPageWindow.value = parseInt(firstPageWindow.value) - 1;
			  /** Decreases also the last page because the window has moved - 1 **/
			  var lastPageWindow = document.getElementById('lastPageWindow');
			  if (lastPageWindow) {
				  lastPageWindow.value = parseInt(lastPageWindow.value) - 1;
			  }
			  /** As the window moves left, makes last and next visible **/
			  changeClassLastNextOverview('paginationClassVisible');
			  /** Retrieves all the links in the pagination bar and add 1 **/
			  var links = document.getElementById('formOverview:navigationLinks');	
		  	  if (links) { 
		  		/** For each link **/
	              for (var i = 0; i < links.childNodes.length; i++) {
	    	          var link = links.childNodes.item(i);
	    	          /** If the current link has a child **/
	    	          if (link.id != null && link.hasChildNodes()) {
	    	        	  /** if it is a 'numerical link', then add 1 (next behavior) **/
	    	        	  /** it is needed because formOverview:navigationLinks contain >, >>, or 'next' ... **/
				    	   if (link.firstChild.nodeValue.match('^(0|[1-9][0-9]*)$')) {
				    		   link.firstChild.nodeValue = parseInt(link.firstChild.nodeValue) - 1;
				    	   };
				       };
			       };
	    	  };
		  };
	  };
};

/**
 * This function implements the next function.
 * Two hidden files contain the firstPageWindow value and the lastPageWindow value. These 
 * values have to be updated in each function (first, previous, next, last).
 * @param sizeWindow is the current set up size of the pagination window (ex: 5 = 1|2|3|4|5 )
 * @param countPages is number total of results page
 */
function nextOverview(sizeWindow,countPages) {
	  /** Retrieves the value of a hidden field that indicates the current last page **/
	  var lastPageWindow = document.getElementById('lastPageWindow');	
	  if (lastPageWindow) {
		  /** if the last page is < than the total number of results page, then modes the window right **/
		  if (lastPageWindow.value < countPages) {
			  lastPageWindow.value = parseInt(lastPageWindow.value) + 1;
			  /** moving the window right, moves also the first page**/
			  var firstPageWindow = document.getElementById('firstPageWindow');
			  if (firstPageWindow) {
				  firstPageWindow.value = parseInt(firstPageWindow.value) + 1;
			  }
			  /** moving the window right, makes visible the first and previous controls **/
			  changeClassFirstPreviousOverview('paginationClassVisible');
              /** Retrieves all the links in the pagination bar and add 1 **/
			  var links = document.getElementById('formOverview:navigationLinks');	
		  	  if (links) { 
		  		  /** For each link **/
	              for (var i = 0; i < links.childNodes.length; i++) {
	    	          var link = links.childNodes.item(i);
	    	          /** If the current link has a child **/
	    	          if (link.id != null && link.hasChildNodes()) {
	    	    	       /** if it is a 'numerical link', then add 1 (next behavior) **/
	    	        	  /** it is needed because formOverview:navigationLinks contain >, >>, or 'next' ... **/
				    	   if (link.firstChild.nodeValue.match('^(0|[1-9][0-9]*)$')) {
				    		   link.firstChild.nodeValue = parseInt(link.firstChild.nodeValue) + 1;
				    	   }
				       };
			       };
	    	  };
		  };
	  };
};


/**
 * This function implements the 'last' control.
 * @param sizeWindow is the current set up size of the pagination window (ex: 5 = 1|2|3|4|5 )
 * @param countPages is number total of results page
 */
function lastOverview(sizeWindow,countPages) {
	  /** Retrieves the value of a hidden field that indicates the current last page **/
	  var lastPageWindow = document.getElementById('lastPageWindow');	
	  if (lastPageWindow) {
		  /** Goes to last page if the current last page is < total numer of available pages **/
		  if (lastPageWindow.value < countPages) {
		      lastPageWindow.value = countPages;
		      /**going to last implies to change the first window  **/
		      var firstPageWindow = document.getElementById('firstPageWindow');
			  if (firstPageWindow) {
				  firstPageWindow.value =(parseInt(countPages) - parseInt(sizeWindow)) + 1;
			  }
			  
		      /** Retrieves all the links in the pagination bar and add 1 **/
		      var links = document.getElementById('formOverview:navigationLinks');	
	  	      if (links) { 
		  		  /** For each link **/
		  		  var cont = 0;
	              for (var i = 0; i < links.childNodes.length; i++) {
	    	           var link = links.childNodes.item(i);
	    	           /** If the current link has a child **/
	    	           if (link.id != null && link.hasChildNodes()) {
	    	    	       /** if it is a 'numerical link', then add 1 (next behavior) **/
	    	        	   /** it is needed because formOverview:navigationLinks contain >, >>, or 'next' ... **/
				    	   if (link.firstChild.nodeValue.match('^(0|[1-9][0-9]*)$')) {
				    		   cont = cont + 1;
				    		   link.firstChild.nodeValue = (parseInt(countPages) - parseInt(sizeWindow)) + cont;
				    	   }
				       };
			       };
	    	  };
	    	  /** Change**/
	    	  changeClassFirstPreviousOverview('paginationClassVisible');
		      changeClassLastNextOverview('paginationClassHidden');
	    };
	  };
	  
};

/**
 * This is a auxiliary method used to change the css class
 * of specific components: first and previous controls
 */
function changeClassFirstPreviousOverview(className) {
	 // Makes first and previous controls visible
	  var first = document.getElementById('formOverview:first');	
	  if (first) {
		  first.className = className;
	  }	  
	  
	  var previous = document.getElementById('formOverview:previous');	
	  if (previous) {
		  previous.className = className;
	  };		
};

/**
 * This is a auxiliary method used to change the css class
 * of specific components: last and next controls
 */
function changeClassLastNextOverview(className) {
	 // Makes first and previous controls visible
	  var last = document.getElementById('formOverview:last');	
	  if (last) {
		  last.className = className;
	  }	  
	  var next = document.getElementById('formOverview:next');	
	  if (next) {
		  next.className = className;
	  };		
	  return true;
};