/****************************************************************************
 Depends on: prototype.js, scriptaculous/builder.js, rock.js, rock-util.js
 
 The pagination control DOM is identified by "div.controlBar div.paginationControl".
 ****************************************************************************/
 
// define the namespace
rock.namespace("rock.pagination");

/*================================================
   Class PaginationControl
   data = {
      	itemPage: pageNumber
      }   
 =================================================*/
rock.pagination.PaginationControl = function(currPage, totalPages, totalItems, data, onPaginationTopic){
	var itemsPerPage = 10;
	var maxPages = 400;
	var lowSteps = 4;
	var highSteps = 4;
	var topicOnPagination = "/rock/pagination/onPagination";
	
	if(onPaginationTopic){
		topicOnPagination = onPaginationTopic;
	}
	
	// calc last page number
	var lastPage;
	if(totalPages >= maxPages){
		lastPage = maxPages;
	}else{
		lastPage = Math.floor(totalItems/itemsPerPage) + 1;
	}
	//rock.debug.msg('lastPage: ' + lastPage);	
		
	var prev = Builder.node('a', {href:'#'}, [
		Builder.node('span', {className: 'arrow'}, '< '),
		'Prev'
	]);	
	prev.onclick = gotoPrev;
	
	var next = Builder.node('a', {href:'#'}, [
		'Next',
		Builder.node('span', {className: 'arrow'}, ' >')
	]);
	next.onclick = gotoNext;
	
	var ctl = Builder.node('div', {className:'paginationControl'});						
							
	function gotoNext(){
		if(currPage < lastPage){
			currPage++;
			display();
			
			// publish event
			data.itemPage = currPage;
			jmaki.publish(topicOnPagination, data);
			//rock.debug.msg("published evt " + topicOnPagination);
		}
		return false;
	}
	
	function gotoPrev(){
		if(currPage > 1){
			currPage--;
			display();
			
			// publish event
			data.itemPage = currPage;
			jmaki.publish(topicOnPagination, data);			
		}
		return false;
	}
	
	function gotoPage(){
		var pageNumber = Number(this.getAttribute('pageNumber'));
		//rock.debug.msg('gotoPage(): pageNumber= ' + pageNumber);
		currPage = pageNumber;
		
		// publish event
		//rock.debug.msg('publishing evt topicOnPagination, currPage=' + currPage);
		data.itemPage = currPage;
		jmaki.publish(topicOnPagination, data);	
				
		display();

		return false;
	}	
			
	function display(){
		rock.util.DomUtil.removeAllChildren(ctl);
		
		var startingItem = (currPage-1) * itemsPerPage + 1;
		var endingItem = currPage * itemsPerPage;
		var msg = Builder.node('span', 'Items '+ startingItem + ' - ' +
					endingItem + ' of ' + totalItems + '.');
		ctl.appendChild(msg);
		ctl.appendChild(rock.util.DomUtil.createSeparator('&nbsp;&nbsp;&nbsp;'));
			
		// calc lowest steps
		var lowestStep = currPage - lowSteps;
		if(lowestStep < 1){
			lowestStep = 1;
		}
					
		if(currPage == 1){
			ctl.appendChild(Builder.node('span', {className:'currPage'}, currPage));
		}else if(currPage > 1){
			ctl.appendChild(prev);
			ctl.appendChild(rock.util.DomUtil.createSeparator());
			ctl.appendChild(rock.util.DomUtil.createSeparator());

			// if lowestStep > lowSteps, insert 1st page
			if(lowestStep > 2){
				var node = Builder.node('a', {href:'#', pageNumber: '1'}, '1');
				node.onclick = gotoPage;	
				ctl.appendChild(node);		
				ctl.appendChild(rock.util.DomUtil.createSeparator());				
				ctl.appendChild(Builder.node('span', '...'));
				ctl.appendChild(rock.util.DomUtil.createSeparator());			
			}
			
			// lower steps
			for(var i=lowestStep; i<=currPage-1; i++){
				var node = Builder.node('a', {href:'#', pageNumber: i}, i);
				node.onclick = gotoPage;
				ctl.appendChild(node);
				ctl.appendChild(rock.util.DomUtil.createSeparator());
			}
			
			ctl.appendChild(Builder.node('span',{className:'currPage'}, currPage));
		}

		// calc highest steps
		//rock.debug.msg('currPage=' + currPage + ',highSteps=' + highSteps + 
		//	',lowSteps=' + lowSteps + ', highSteps=' + highSteps + ', lowestStep=' + lowestStep);
		var highestStep = currPage + highSteps;
		var extraSteps = lowSteps - (currPage - lowestStep);
		highestStep += extraSteps;
		if(highestStep >= lastPage-1){
			highestStep = lastPage-1;
		}
		//rock.debug.msg('highestStep = ' + highestStep);
		
		// upper steps
		for(var ii=currPage+1; ii<=highestStep; ii++){
			//rock.debug.msg('ii=' + ii);
			ctl.appendChild(rock.util.DomUtil.createSeparator());	
			var node = Builder.node('a', {href:'#', pageNumber: ii}, ii);
			node.onclick = gotoPage;			
			ctl.appendChild(node);
			ctl.appendChild(rock.util.DomUtil.createSeparator());
		}
				
		// if currPage < lastPage, display "..."
		if(currPage < lastPage){
			ctl.appendChild(Builder.node('span', '...'));
			ctl.appendChild(rock.util.DomUtil.createSeparator());
		}		
		
		if(currPage < lastPage){
			var node = Builder.node('a', {href:'#', pageNumber: lastPage}, lastPage);
			node.onclick = gotoPage;	
			ctl.appendChild(node);
			ctl.appendChild(rock.util.DomUtil.createSeparator());
			ctl.appendChild(rock.util.DomUtil.createSeparator());
			ctl.appendChild(next);
		}	
	}
	
		
	display();
	//rock.debug.msg(ctl.innerHTML);
	return ctl;	
}
