var ListPager = Class.create({
	initialize: function(list, options) {
		this.options = {
			child_tag: 'li',
			item_text: 'Artikel',
			no_items_text: 'keine Artikel',
			items_per_page: 5,
			page: 0,
			ajax_notification: false // false or url
		};
		Object.extend(this.options, options || { });

		this.list = $(list);
		this.pager_top = new Element('div').addClassName('pager');
		this.pager_bottom = new Element('div').addClassName('pager');
		this.list.insert({ before: this.pager_top, after: this.pager_bottom });

		this.current_page = this.options.page;

		var scroll_to = null;
		if(location.hash.length >= 2) {
			var hash = location.hash.substr(1);
			var current_page = 0;
			var items_per_page = this.options.items_per_page;
			this.list.select(this.options.child_tag).each(function(li, i) {
				if(li.id == hash) {
					scroll_to = li;
					current_page = Math.floor(i / items_per_page);
				}
			});
			this.current_page = current_page;
		}

		this.update();

		if(scroll_to != null)
			$(scroll_to).scrollTo();
	},

	update: function() {
		this.total_items = this.list.select(this.options.child_tag).size();
		this.total_pages = Math.ceil(this.total_items / this.options.items_per_page);

		if(this.current_page < 0)
			this.current_page = 0;
		if(this.current_page >= this.total_pages)
			this.current_page = this.total_pages - 1;

		var min = this.current_page * this.options.items_per_page;
		var max = (this.current_page + 1) * this.options.items_per_page;
		this.list.select(this.options.child_tag).each(function(li, i) {
			var visible = (i >= min && i < max);
			li.style.display = visible ? 'block' : 'none';
		});

		this.update_pager(this.pager_top);
		this.update_pager(this.pager_bottom);
	},

	set_items_per_page: function(items_per_page) {
		this.options.items_per_page = items_per_page;
		if(this.options.ajax_notification) {
			new Ajax.Request(this.options.ajax_notification, {
				method: 'get',
				parameters: { items_per_page: this.options.items_per_page }
			});
		}
		this.update();
		this.pager_top.scrollTo();
	},

	set_page: function(page) {
		this.current_page = page;
		if(this.options.ajax_notification) {
			new Ajax.Request(this.options.ajax_notification, {
				method: 'get',
				parameters: { page: this.current_page }
			});
		}
		this.update();
		this.pager_top.scrollTo();
	},

	update_pager: function(pager) {
		pager.childElements().each(Element.remove);

		var pager_top = new Element('div').addClassName('pager_top');
		var pager_bottom = new Element('div').addClassName('pager_bottom');

		pager.insert(pager_top);
		pager.insert(pager_bottom);

		if(this.total_items > 0) {
			var total = this.list.select(this.options.child_tag).length;
			var min = this.current_page * this.options.items_per_page + 1;
			var max = Math.min(total, (this.current_page + 1) * this.options.items_per_page);
			pager_top.insert(this.options.item_text + ': ' + min + ' bis ' + max + ' von ' + total);
		}
		else
			pager_top.insert(this.options.no_items_text);

		var select = new Element('select');
		select.list_pager = this;
		select.onchange = function() {
				this.list_pager.set_items_per_page(parseInt(this.options[this.selectedIndex].value));
			}.bindAsEventListener(select);
		select.options[select.length] = new Option('1', '1', false, (this.options.items_per_page == 1));
		select.options[select.length] = new Option('5', '5', false, (this.options.items_per_page == 5));
		select.options[select.length] = new Option('15', '15', false, (this.options.items_per_page == 15));
		select.options[select.length] = new Option('alle', '99999999', false, (this.options.items_per_page == 99999999));
		pager_bottom.insert(new Element('span').addClassName('pager_select').insert(this.options.item_text + ': ').insert(select).insert('&nbsp;pro Seite'));

		if(this.total_pages > 1) {
			var list = new Element('ul');
			list.insert(new Element('li').insert(new Element('a', { 'href': '#' }).addClassName('first ico').update('&lt;&lt;').observe('click', function(event) { this.set_page(0); event.stop(); }.bindAsEventListener(this))));
			list.insert(new Element('li').insert(new Element('a', { 'href': '#' }).addClassName('prev ico').update('&lt;').observe('click', function(event) { this.set_page(this.current_page - 1); event.stop(); }.bindAsEventListener(this))));
			var begin = Math.max(0, Math.min(this.total_pages - 5, this.current_page - 2));
			var end = Math.min(this.total_pages - 1, begin + 4);
			for(var i = begin; i <= end; i++)
				list.insert(new Element('li').addClassName((i == this.current_page) ? 'active' : 'page').insert(new Element('a', { href: '#' }).update(i + 1).observe('click', function(event, i) { this.set_page(i); event.stop(); }.bindAsEventListener(this, i))));
			list.insert(new Element('li').insert(new Element('a', { 'href': '#' }).addClassName('next ico').update('&gt;').observe('click', function(event) { this.set_page(this.current_page + 1); event.stop(); }.bindAsEventListener(this))));
			list.insert(new Element('li').insert(new Element('a', { 'href': '#' }).addClassName('last ico').update('&gt;&gt;').observe('click', function(event) { this.set_page(9999999999); event.stop(); }.bindAsEventListener(this))));
			pager_bottom.insert(list);
		}

		if(this.total_pages > 0)
			pager_bottom.insert('<span class="pager_pages">Seite: ' + (this.current_page + 1) + ' von ' + this.total_pages + '</span>');
	}
});
