ps = new Object();
ps.o = new Object();

ps.Paging  = new Class({
	Implements: [Events, Options, Chain],
	options: {
		classimages:'images',
		classbackbtt:'',
		classforwardbtt:'',
		classpagebtt:'',
		classselectedPageLink:'selected'
	},
	initialize: function(el,options){
		this.setOptions(options);
		
		// check data
		if($type($(el)) == 'element'){
			this.el = $(el);
		}else{
			// exit quietly
			return false;
		}
		
		this.pageObjs = this.el.getElements('.' + this.options.classimages);
		
		this.pageCount = this.pageObjs.length;
		if(this.pageCount <= 1){
			return false;
		}
		this.currPage = null;
		this.swPage = 0;
		
		this.el.getElement('.' + this.options.classbackbtt).addEvent('click', this.gotopage.bindWithEvent(this,'-'));
		this.el.getElement('.' + this.options.classforwardbtt).addEvent('click', this.gotopage.bindWithEvent(this,'+'));
		
		this.pageLinks = new Array(1);
		this.el.getElements('.' + this.options.classpagebtt).each(function(el,i){
			if($defined(el.get('rel'))){
				iPos = el.get('rel').toInt();
			}else{
				iPos = i+1;
			}
			el.addEvent('click', this.gotopage.bindWithEvent(this,['x',iPos]));
			this.pageLinks[iPos-1] = el;
		},this);
		
		this.pageObjs.each(function(el,i){
			el.set('tween',{property:'opacity',startvalue:1});
			if(i == this.swPage){
				el.get('tween').set(1);
			}else{
				el.get('tween').set(0);
			}
		},this);
		
		this.gotopage({},'x',this.swPage);
	},
	gotopage: function(evnt,type,page){
		if(type == '+'){
			// next page
			this.swPage ++;
			if(this.swPage+1 > this.pageCount){
				this.swPage = 0;
			}
			this._showPageChain();
		}
		if(type == '-'){
			// last page
			this.swPage --;
			if(this.swPage < 0){
				this.swPage = this.pageCount-1;
			}
			this._showPageChain();
		}
		if(type == 'x'){
			// open defined page
			if($defined(page) && page <= this.pageCount && page > 0){
				this.swPage = page-1;
			}
			this._showPageChain();
		}
	},
	_onBeforShow: function(){
		this.fireEvent('beforeShow',[this.pageObjs[this.currPage], this.pageObjs[this.swPage]]);
	},
	_onAfterShow: function(){
		this.fireEvent('beforeShow',[this.pageObjs[this.currPage], this.pageObjs[this.swPage]]);
	},
	_showPageChain: function(){
		if(this.swPage != this.currPage){
			
			this.chain(
				this._onBeforShow(),
				this._showPage(),
				this._onAfterShow()
			 );
			 this.callChain();
			
			this.currPage = this.swPage;
		}
	},
	_showPage: function(){
		if($defined(this.currPage)){
			this.pageObjs[this.currPage].get('tween').set(0);
			this.pageObjs[this.currPage].setStyle('display','none');
			this.el.getElements('.' + this.options.classpagebtt).removeClass(this.options.classselectedPageLink);
		}
		this.pageObjs[this.swPage].setStyle('display','block');
		this.pageObjs[this.swPage].get('tween').start(1);
		this.pageLinks[this.swPage].addClass(this.options.classselectedPageLink);
	}
});