motorola.controls.Pagination = new Class(function() {
	var template;
	function setupVars() {
		template = $('allProductsPaginationTemplate').getFirst();
	}
	window.addEvent('productsready',setupVars);
	return {
		Implements: [Events,Options],
		options: {
			maxPerPage: 20,
			startPage: 1,
			pagesShown: 5
		},
		items: [],
		numPages: null,
		currentPage: 1,
		pages: [],
		pager: null,
		inTransition: false,
		initialize: function(items, options) {
			this.setOptions(options);
			this.items = items;
			this.numPages = Math.ceil(items.length / this.options.maxPerPage);
			this.currentPage = this.options.startPage;
			var i, halfShown, current, start, end;
			if (this.numPages > this.options.pagesShown) {
				halfShown = Math.floor(this.options.pagesShown / 2);
				current = this.currentPage;
				start = 1 + halfShown;
				end = this.numPages - halfShown;
				if (current < start) {
					start = 1;
				} else if (current > end) {
					start = end - halfShown;
				} else {
					start = current - halfShown;
				}
				for (i = 0; i < this.options.pagesShown; i = i + 1) {
					this.pages.push(start + i);
				}
			} else {
				for (i = 1; i <= this.numPages; i = i + 1) {
					this.pages.push(i);
				}
			}
			this.refreshPager();
		},
		refreshPager: function() {
			var pager, first, previous, pageLink, next, last;
			var allProductsText, replacer;
			if (this.options.maxPerPage >= this.items.length) {
				replacer = this.items.length;
				pager = template.clone();
				allProductsText = pager.get('html');
				while (allProductsText.indexOf('{max}') !== -1) {
					allProductsText = allProductsText.replace('{max}',replacer);
				}
				pager.set('html',allProductsText);
			} else {
				pager = new Element('div',{
					'class':'pagination'
				});
				first = new Element(this.hasPrevious() ? 'a' : 'span',{
					'class':'first control',
					'html':'&laquo;',
					'href':'#',
					'events':{
						'click':this.first.bind(this)
					}
				});
				first.inject(pager);
				previous = new Element(this.hasPrevious() ? 'a' : 'span',{
					'class':'previous control',
					'html':'&lt;',
					'href':'#',
					'events':{
						'click':this.previous.bind(this)
					}
				});
				previous.inject(pager);
				this.pages.each(function(page) {
					pageLink = new Element(page === this.currentPage ? 'span' : 'a',{
						'class':'page control',
						'html':page,
						'href':'#',
						'title':page,
						'events':{
							'click':this.setPage.bindWithEvent(this,[page])
						}
					});
					pageLink.inject(pager);
				},this);
				next = new Element(this.hasNext() ? 'a' : 'span',{
					'class':'next control',
					'html':'&gt;',
					'href':'#',
					'events':{
						'click':this.next.bind(this)
					}
				});
				next.inject(pager);
				last = new Element(this.hasNext() ? 'a' : 'span',{
					'class':'last control',
					'html':'&raquo;',
					'href':'#',
					'events':{
						'click':this.last.bind(this)
					}
				});
				last.inject(pager);
			}
			this.pager = pager;
		},
		hasNext: function() {
			var newPage = this.currentPage + 1;
			return newPage <= this.numPages;
		},
		next: function(e) {
			var items;
			if (this.hasNext()) {
				items = this.setPage(e,this.currentPage + 1);
				this.fireEvent('onNext');
			}
			return items;
		},
		hasPrevious: function() {
			var newPage = this.currentPage - 1;
			return newPage >= 1;
		},
		previous: function(e) {
			var items;
			if (this.hasPrevious()) {
				items = this.setPage(e,this.currentPage - 1);
				this.fireEvent('onPrevious');
			}
			return items;
		},
		first: function(e) {
			var items;
			if (this.hasPrevious()) {
				items = this.setPage(e,1);
				this.fireEvent('onFirst');
			}
			return items;
		},
		last: function(e) {
			var items;
			if (this.hasNext()) {
				items = this.setPage(e,this.numPages);
				this.fireEvent('onLast');
			}
			return items;
		},
		setPage: function(e,pageNum) {
			var evt, pages, i, halfShown, current, start, end, items;
			if (e) {
				evt = new Event(e);
				evt.stop();
			}
			if (!this.inTransition && pageNum !== this.currentPage) {
				this.currentPage = pageNum;
				pages = this.pages;
				pages.empty();
				if (this.numPages > this.options.pagesShown) {
					halfShown = Math.floor(this.options.pagesShown / 2);
					current = this.currentPage;
					start = 1 + halfShown;
					end = this.numPages - halfShown;
					if (current < start) {
						start = 1;
					} else if (current > end) {
						start = end - halfShown;
					} else {
						start = current - halfShown;
					}
					for (i = 0; i < this.options.pagesShown; i = i + 1) {
						this.pages.push(start + i);
					}
				} else {
					for (i = 1; i <= this.numPages; i = i + 1) {
						this.pages.push(i);
					}
				}
				items = [];
				for (i = (this.currentPage - 1) * this.options.maxPerPage;
						i < this.items.length && i < this.currentPage * this.options.maxPerPage; i = i + 1) {
					items.push(this.items[i]);
				}
				this.refreshPager();
				this.fireEvent('onSet',[pageNum,items]);
				return items;
			} else if (pageNum !== this.currentPage) {
				this.fireEvent('onInTransition');
			}
		},
		destroy: function() {
			this.items = null;
			this.pages = null;
			if (this.pager && this.pager.parentNode) {
				this.pager.destroy();
			}
			this.pager = null;
		}
	};
}());