
/*
 * 
<div class="theme-paging">
			<ul class="left">
				<li><span><strong>Results</strong>: 1</span></li>
				<li><span><strong>Page</strong>: 1 of 1</span></li>
			</ul>
			<ul class="right">
				<li><span class="current">1</span></li>
			</ul>
			</div>
 * 
 */
paginator = new Class({
	
	Implements: [Options, Events],
	
	options: {
		container_id: '',
		when: 'after',
		elem: false,
		per_page: 10,
		page_numbers_difference: 3,
		onPage: $empty /* fires when the page is changed */
	},
	initialize: function(options){
		this.setOptions(options);
		
		this.options.pager_id = this.options.container_id + '_' + Math.random();
	},
	/* returns the results applying limits */
	getResults: function(json){
		
		this.getPaging(json.length, 0);
	},
	getPaging: function(items, page){
		
		var root = this;

		this.items = items;
		this.page = page;
		this.per_page = this.options.per_page;
		this.pages = Math.ceil(this.items / this.per_page);
		
		this.page_start = (this.page * this.per_page) + 1;
		this.page_end = ((this.page > 0 && (this.page * this.per_page) == 0) || ((this.page * this.per_page) + this.per_page) > this.items ? this.items : (this.page * this.per_page) + this.per_page);
		
		if($defined($(this.options.pager_id))){
			$(this.options.pager_id).dispose();
		}
		
		var div = new Element('div', {
			'class': 'theme-paging',
			'id': this.options.pager_id
		});
		
		var ul_details = new Element('ul', {'class': 'left'});
		
		if(items == 0){	
			var li = new Element('li', {
				'class': 'info',
				'html': 'No results'  
			}).inject(ul_details, 'inside');
			
			var span = new Element('span', {
				'html': 'No results'  
			}).inject(li, 'inside');
			
			ul_details.inject(div);
		}
		else{
			
			var ul_nav = new Element('ul', {'class': 'right'});
			
			var li = new Element('li', {
				'class': 'info'
			}).inject(ul_details, 'inside');
			
			var span = new Element('span', {
				'html': 'Results: ' + this.page_start + ' - ' + this.page_end + ' of ' + this.items
			}).inject(li, 'inside');
			
			this.pagingBack().inject(ul_nav);
						
			var ret = '';
			
			var d = this.options.page_numbers_difference;
			
			var i = (this.page - d > 0 ? this.page - d : 0);
			var to = ((this.page + d + 1) < this.pages ? (this.page + d + 1) : this.pages);
		
			for(i; to > i; i++){
				var li = new Element('li', {
					'class': 'info'
				}).inject(ul_nav);
				
				var span = new Element((this.page == i ? 'span' : 'a'), {
					'class': (this.page == i ? 'current' : ''),
					'html': (i + 1),
					'events': {
						'click': function(){
							root.fireEvent('onPage', (this.innerHTML - 1));
						}
					}
				}).inject(li, 'inside');
			}
			
			this.pagingNext().inject(ul_nav);
			
			ul_details.inject(div);
						
			ul_nav.inject(div);
		}
		
		if($defined($(this.options.container_id))){
			div.inject($(this.options.container_id), this.options.when);
		}
	},
	/* returns next page button or not */
	pagingNext: function(){
		var root = this;
		
		if((this.items / this.per_page) > (this.page + 1)){
			var span = new Element('a', {
				'html': 'Next',
				'events': {
					'click': function(){
						root.fireEvent('onPage', (root.page + 1));
					}
				}
			});
		}
		else{
			var span = new Element('span', {
				'html': 'Next'
			});
		}
		
		var li = new Element('li', {
			'class': 'info'
		});
		
		span.inject(li);
		
		return li;
		
	}, 
	/* returns back page button or not */
	pagingBack: function(){
		var root = this;
		
		if(this.page > 0){
			var span = new Element('a', {
				'html': 'Back',
				'events': {
					'click': function(){
						root.fireEvent('onPage', root.page - 1);
					}
				}
			});
		}
		else{
			var span = new Element('span', {
				'html': 'Back'
			});
		}
		
		var li = new Element('li', {
			'class': 'info'
		});
		
		span.inject(li);
		
		return li;
	}
});	

paginator.JSON = new Class({
	
	Extends: paginator,

	options: {
		url: '',
		url_page: 'C_page',
		current_page: 0,
		result_selection: false, /* does this query return all results or a selection, ie its limited server side */
		onPage: function(page){
			this.options.current_page = page;
			
			this.request.send(this.options.request_str + '&' + this.options.url_page + '=' + page);
		}
	},
	 
	initialize: function(options){
		this.setOptions(options);
		this.parent(options);
	},
	
	send: function(request){
		
		if(request != this.options.request_str){
			this.options.request_str = request;
			this.options.current_page = 0;
		}	
		
		this.request = new Request.JSON({
			url: this.options.url, 
			onSuccess: this.sendSuccess.bind(this)
		});
		
		this.request.send(this.options.request_str);
	},
	
	sendSuccess: function(json, text){
		
		if(json){
			/* setup the paging */
			this.getPaging(json.info.total_results, this.options.current_page);
			
			/* send success event */
			this.fireEvent('onSuccess', [json.results, text]);
		}
	}
});	
