var Pagination = new Class({
    opt: function(pages,handles,opt) {
		this.current = 0; 
		this.counter = null; 
		this.pages = pages; 
		this.handles = {
			'adv': null,
			'prev': null
		}; 
        Object.extend(this.handles,handles||{});
		this.opt = {
			'trans': null,
			'counter': null
		}; 
        Object.extend(this.opt,opt||{});
	},
	initialize: function(p,h,opt) {
        this.opt(p,h,opt);
		this.controls(); 
		this.count_setup(); 
		this.display(0,false); 
	},
	count_setup: function() {
		if(this.opt.counter!=undefined) { 
			this.counter = $E(this.opt.counter); 
			this.counter.setText('('+(this.current+1)+' of '+this.pages.length+')'); 
		}
	},
	count_update: function() {
		if(this.counter!=undefined) { 
			this.counter.setText('('+(this.current+1)+' of '+this.pages.length+')'); 
		}
	},
	controls: function() {
		if(this.handles.adv==undefined) { return true; }
		$(this.handles.adv).ref = this; 
		$(this.handles.adv).addEvent('click',function(ev){
			this.ref.next(); 
			this.ref.stopEv(ev); 
		}); 
		$(this.handles.prev).ref = this; 
		$(this.handles.prev).addEvent('click',function(ev){
			this.ref.prev(); 
			this.ref.stopEv(ev); 
		}); 
	},
	next: function() {
		this.current = (this.current+1)>=this.pages.length ? 0 : this.current+1;
		this.display(this.current,true); 
		this.count_update(); 
	},
	prev: function() {
		this.current = (this.current-1)<0 ? this.pages.length-1 : this.current-1;
		this.display(this.current,true); 
		this.count_update(); 
	},
	display: function(idx,trans) {
		(this.pages).each(function(item,index){
			var d = index!=idx ? 'none' : 'block'; 
			item.setStyle('display', d); 
		}); 
		if(trans && this.opt.trans!=undefined) { 
			var el = this.pages[idx]; 
			var tel = el.getElement(this.opt.trans); 
			if(tel) { 
				tel.setOpacity(0); 
				this.trans(tel); 
			}
		}
	},
	trans: function(el) {
		el.effect('opacity',{duration: 300}).start(0,1); 
	},
	stopEv: function(ev) {
		var event = new Event(ev);
			event.stop();
	}
});
