var CoreshopResultPaginator = new Class({
	Implements: [Options, Events],
	options: {
		pagedItemClassName: '',
		numItemsPerPage: 20,
		shopPageCtrlEles: ''
	},
	initialize: function(options) {
		this.setOptions(options);
		this.pagedItems = $$('.'+this.options.pagedItemClassName);
		

		this.setupPages(this.options.numItemsPerPage);
		this.pagedItems.each(function(item) {
			item.each(function(item2) {
				item2.setStyle('display', 'none');
			});
		});
		
		this.pagedItems[this.currentPageNum].each(function(item) {
			item.setStyle('display', 'block');
		});
		
		this.setupClickHandlers(this.options.shopPageCtrlEles);
	},
	numPages: 0,
	currentPageNum: 0,
	pagedItems: [],
	setupClickHandlers: function(passedHandels) {
		passedHandels["PageLinks"].each(function(item) {
			item.each(function(item2, index) {
				item2.addEvent('click', this.gotoPage.bind(this, index));
			}.bind(this));
		}.bind(this));
		
		passedHandels["NextLink"].each(function(item) {
			item.addEvent('click', function() { this.nextPage(); return false; }.bind(this));
		}.bind(this));
		
		passedHandels["PrevLink"].each(function(item) {
			item.addEvent('click', function() { this.prevPage(); return false; }.bind(this));
		}.bind(this));
	},
	updateClickHandlers: function(passedHandels) {
		if(this.currentPageNum > 0)
			passedHandels["PrevLink"].each(function(item) { item.setStyle('display', 'inline'); });
		else
			passedHandels["PrevLink"].each(function(item) { item.setStyle('display', 'none'); });
		
		//alert(this.currentPageNum-1);
		//alert(this.numPages);
		if(this.currentPageNum == this.numPages)
			passedHandels["NextLink"].each(function(item) { item.setStyle('display', 'none'); });
		else
			passedHandels["NextLink"].each(function(item) { item.setStyle('display', 'inline'); });
		passedHandels["PageLinks"].each(function(item) {
				item.each(function(item2, index) {
					
					if(index == this.currentPageNum) {
						item2.setStyles({
							color: '#000000',
							textDecoration: 'none'});
						item2.addEvent('click', function(){ return false; });
					}
					else {
						item2.setStyles({
							color: '#3399ff',
							textDecoration: 'underline',
							paddingLeft: '4px'});
						
						item2.addEvent('click', function(index){
							this.gotoPage.bind(this, index);
							return false;
						}.bind(this));
					}
					
				}.bind(this));
			}.bind(this));
		
	},
	setupPages: function(numPerPage) {
		var myNewPages = [];
		var tmpCurrPagedItems = this.pagedItems;
		
		var totalItmCnt = 0;
		var tmpItmCnt = 0;
		var tmpCurrPgItems = [];
		tmpCurrPagedItems.each(function(item) {
			totalItmCnt++;
			tmpCurrPgItems.include(item);
			tmpItmCnt++;
			if(tmpItmCnt == numPerPage) {
				myNewPages.include(tmpCurrPgItems);
				tmpCurrPgItems = [];
				tmpItmCnt = 0;
			}
		});
		
		this.numPages = parseInt(this.pagedItems.length/this.options.numItemsPerPage);
		
		
		if(tmpCurrPgItems.length < numPerPage)
			myNewPages.include(tmpCurrPgItems);
		this.pagedItems = myNewPages;
	},
	nextPage: function() {
		if(this.currentPageNum < this.numPages) {
			this.pagedItems[this.currentPageNum].each(function(item) {
				item.setStyle('display', 'none');
			});
			this.currentPageNum++;
			this.pagedItems[this.currentPageNum].each(function(item) {
				item.setStyle('display', 'block');
			});
			this.fireEvent('pageChanged'); //child class can bind a func to this event later
		}
		return false;
	},
	prevPage: function() {
		if(this.currentPageNum > 0) {
			this.pagedItems[this.currentPageNum].each(function(item) {
				item.setStyle('display', 'none');
			});
			this.currentPageNum--;
			this.pagedItems[this.currentPageNum].each(function(item) {
				item.setStyle('display', 'block');
			});
			this.fireEvent('pageChanged'); //child class can bind a func to this event later
		}
		return false;
	},
	gotoPage: function(passedCurrPageNum) {
		
		//event.stop();
		passedCurrPageNum = parseInt(passedCurrPageNum);
		if(this.currentPageNum != passedCurrPageNum) {
			this.pagedItems[this.currentPageNum].each(function(item) {
				item.setStyle('display', 'none');
			});
			this.currentPageNum = passedCurrPageNum;
			this.pagedItems[this.currentPageNum].each(function(item) {
				item.setStyle('display', 'block');
			});
			this.fireEvent('pageChanged'); //child class can bind a func to this event later
		}	
		//alert('HERE');
		return false;
	}
});
