var SortableTablePager = Class.create({
	initialize: function(list, options, sortings) {
		this.options = {
			columns:        2,
			item_text:      'Artikel',
			no_items_text:  'keine Artikel',
			items_per_page: 5,
			page:           0,
			sorting:        0
		};
		Object.extend(this.options, options || { });

		this.sortings = sortings || { };
		this.list = $(list);
		this.body = $(list).select('tbody')[0];
		this.pager_top = new Element('div').addClassName('sortable_pager');
		this.pager_bottom = new Element('div').addClassName('sortable_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.body.select('td').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.sort_items();
		this.update();

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

	sort_items: function() {
		if(this.sortings.length == 0)
			return;

		var selector = this.sortings[this.options.sorting][1];
		var asc      = (this.sortings[this.options.sorting][2] == 'asc');
		var items    = [];

		this.body.select('td').each(function(e) {
			e.sorting_value = e.down(selector).innerHTML;
			items.push(e);
		});

		// bubble sort to avoid removing/inserting everything
		for(var i = items.length - 1; i > 0; i--) {
			for(var j = 0; j < i; j++) {
				if((asc && items[j].sorting_value > items[j + 1].sorting_value) || (!asc && items[j].sorting_value < items[j + 1].sorting_value)) {
					items[j].parentNode.insertBefore(items[j + 1], items[j]);

					var tmp = items[j];
					items[j] = items[j + 1];
					items[j + 1] = tmp;
				}
			}
		}
	},

	update: function() {
		this.total_items = this.body.select('td').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;
		var tds = this.body.select('td');
		this.body.update();
		var tr;
		var tr_columns = this.options.columns;
		tds.each(function(td, i) {
			if(i >= min && i < max) {
				if(tr_columns++ >= this.options.columns) {
					tr = new Element('tr');
					this.body.insert(tr);
					tr_columns = 1;
				}
				tr.insert(td);
			}
			else {
				var hidden_tr = new Element('tr');
				hidden_tr.style.display = "none";
				this.body.insert(hidden_tr);
				hidden_tr.insert(td);
			}
		}.bind(this));

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

	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('sortable_pager_top');
		var pager_bottom = new Element('div').addClassName('sortable_pager_bottom');

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

		if(this.total_items > 0) {
			var total = this.body.select('td').size();
			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.options.sorting = parseInt(this.options[this.selectedIndex].value);
				this.list_pager.sort_items();
				this.list_pager.update();
			}.bindAsEventListener(select);
		for(var i = 0; i < this.sortings.length; i++)
			select.options[select.options.length] = new Option(this.sortings[i][0], i, false, (this.options.sorting == i));
		pager_bottom.insert(new Element('span').addClassName('sortable_pager_select').insert('Sortierung: ').insert(select));

		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="sortable_pager_pages">Seite: ' + (this.current_page + 1) + ' von ' + this.total_pages + '</span>');
	}
});

