function UlPager(jo, opts) {/*{{{*/
	this.jo = jo;
	this.ch = jo.children();

	this.init_fields(opts);

	this.size = this.ch.size();
	this.pages_count = Math.ceil(this.size / this.per_page);

	//FIXME
	if (this.size <=  this.per_page) {
		this.pages_bar.parents().eq(0).hide();

		try {
			this.n.hide();
			this.p.hide();
		} catch(err){
		}
	} else {
		this.pages_bar.parents().eq(0).show();

		try {
			$(this.n).show();
			$(this.p).show();
		} catch(err){
		}
	}

	this.draw_pages_bar();
	this.page_hook();
	this.next_hook();
	this.prev_hook();
}/*}}}*/

UlPager.prototype = {/*{{{*/
	start_page: 1,/*{{{*/
	per_page: 1,
	cur_page: 1,
	ch: null,
	size: null,
	pages_count: 0,
	n: null,
	p: null,
	jo: null,
	pages_bar: null,
	spread: 10,
	n_inact_class: "inactive",
	p_inact_class: "inactive",/*}}}*/

	redraw_if_spread: function() {/*{{{*/
		if(this.spread <= this.cur_page) {
			this.start_page = this.spread - 4;
			this.spread += 4;
			this.reinit();
		}

		if(this.start_page == this.cur_page && this.start_page > 1) {
			this.start_page = this.spread - 4*2;
			this.spread  -= 4*2

			if(this.spread < 10) this.spread = 10;
			if(this.start_page < 10) this.start_page = 1;

			this.reinit();
		}
	},/*}}}*/

	reinit: function() {/*{{{*/
		if(this.n_was_autocreated) this.n = null;
		if(this.p_was_autocreated) this.p = null;

		this.draw_pages_bar();
		this.page_hook();

		this.next_hook(true);
		this.prev_hook(true);
	},/*}}}*/

	init_fields: function(opts) {/*{{{*/
		for(i in opts) {
			if(i in this) this[i] = opts[i];
		}
	},/*}}}*/

	change_cur_page: function(dest) {/*{{{*/
		pbc = this.pages_bar.children();
		cur_pbc_pos = this.cur_page;
		o = this;

		pbc.each(function() {
			if(dest) {
				repl_pbc_pos = cur_pbc_pos - dest;
				if(this.id == repl_pbc_pos) {
					$(this).removeClass("active");
					$(this).html("<a href=''>"+(o.cur_page - dest)+"</a>");
				}
			}

			if(this.id == cur_pbc_pos) {
				$(this).addClass("active");
				$(this).html("<span>"+o.cur_page+"</span>");
			}
		});

		this.inactivated_p_if_first();
		this.inactivated_n_if_last();

		this.redraw_if_spread();
	},/*}}}*/

	inactivated_p_if_first: function() {/*{{{*/
		if(this.p) {
			if(this.cur_page == 1) {
				this.p.addClass(this.p_inact_class);
			} else {
				this.p.removeClass(this.p_inact_class);
			}
		}
	},/*}}}*/

	inactivated_n_if_last: function() {/*{{{*/
		if(this.n) {
			if(this.cur_page == this.pages_count) {
				this.n.addClass(this.n_inact_class);
			} else {
				this.n.removeClass(this.n_inact_class);
			}
		}
	},/*}}}*/

	draw_pages_bar: function() {/*{{{*/
		this.pages_bar.empty();

		/*this.pages_count = 100;*/

		var end  = this.pages_count > this.spread ? this.spread : this.pages_count;
		var cont = (end == this.pages_count) ? null : (this.pages_count - this.spread);

		for(k=this.start_page; k<=end; k++) {
			this.pages_bar.append("<li id='"+k+"'><a href=\"#\">"+k+"</a></li>");
		}

		if(this.start_page > 1) {
			this.pages_bar.prepend("<li class=\"nolink\">...</li>");
		}

		if(cont)  {
			this.pages_bar.append("<li class=\"nolink\">...</li>");
		}

		if(!this.n) {
			this.pages_bar.append("<li>&raquo;</li>");
		}

		if(!this.p) {
			this.pages_bar.prepend("<li>&laquo;</li>");
		}

		this.change_and_compute();
	},/*}}}*/

	compute_pages_elements: function() {/*{{{*/
		this.ch.hide();

		begin = (end = (this.cur_page * this.per_page)) - this.per_page;

		for(k=begin; k<end; k++) {
			$(this.ch[k]).show().trigger("resizefont");
		}
	},/*}}}*/

	change_and_compute: function(dest) {/*{{{*/
		try {
			this.change_cur_page(dest);
			this.compute_pages_elements();
		} catch(err) {
		}
	},/*}}}*/

	page_hook: function() {/*{{{*/
		var o = this;

		this.pages_bar.children().click(function(e){
			var id = $(this).attr('id');
			if(!id) return false;

			var old_cur_page = o.cur_page;
			o.cur_page = id;

			if(old_cur_page != o.cur_page) {
				o.change_and_compute(o.cur_page - old_cur_page);
			}

			return false;
		});
	},/*}}}*/

	next_hook: function(without_compute) {/*{{{*/
		var o = this;

		if(this.n) {
			this.n.click(function() {
				if(o.cur_page == o.pages_count) return;

				if(!without_compute) {
					o.cur_page++;
					o.change_and_compute(1);
				}

				return false;
			});

			this.inactivated_n_if_last();

		} else {
			pbc = this.pages_bar.children();
			this.n = $(pbc[pbc.size() - 1]);
			this.n_was_autocreated  = true;
			this.next_hook();
		}
	},/*}}}*/

	prev_hook: function(without_compute) {/*{{{*/
		var o = this;

		if(this.p) {
			this.p.click(function() {
				if(o.cur_page  == 1) return;

				if (!without_compute) {
					o.cur_page--;
					o.change_and_compute(-1);
				}

				return false;
			});

			this.inactivated_p_if_first();

		} else {
			pbc = this.pages_bar.children();
			this.p = $(pbc[0]);
			this.p_was_autocreated  = true;
			this.prev_hook();
		}
	}/*}}}*/
}
/*}}}*/

jQuery.fn.ulpager = function(opts) {/*{{{*/
	this.each(function(){
		new UlPager($(this), opts);
	});
}/*}}}*/
