var Pager = Class.create(
{
	initialize: function( p_container, p_options){
		this.id = p_container;
		this.container = $(p_container);
		this.currentIndex = 0;
		this.elements = [];
		this.currentDiv = 0;
		this.moving = false;
		this.nowarp = p_options && p_options.nowarp || false;
		this.vertical = p_options && p_options.vertical || false;

		for (var i = 0 ; i < this.container.childNodes.length; ++ i)
		{
			var l = this.container.childNodes[i];
			if (isElement(l))
			{
				$(l).identify();
				this.elements.push( l);
			}
		}
		
		if (this.elements.length <= 0){return;}
		this.size = p_options&&p_options.size?p_options.size:this.elements[0].getDimensions();
		this.csize = p_options&&p_options.csize?p_options.csize: this.container.getDimensions();
		this.numLines = Math.floor((this.csize.height -(this.vertical?64:0)) / this.size.height);
		this.numRows = Math.floor((this.csize.width-(this.vertical?0:64)) / this.size.width);
		this.numPerPage = this.numRows * this.numLines;
		this.numPages = 1+ Math.floor((this.elements.length-1) / this.numPerPage);
		this.travelWidth = this.size.width*this.numRows;
		this.travelHeight = this.size.height*this.numLines;
		this.offTop = (this.csize.height - this.size.height * this.numLines)/2;
		this.offLeft = (this.csize.width - this.size.width * this.numRows)/2;
		this.container.setStyle('overflow:hidden;');
		if (this.numPages <= 1){return;}
		
		for (var i = 0 ; i < this.elements.length ; ++ i)
		{
			this.elements[i].hide();
		}
		
		d = new Element( 'div', {id:this.id+'_Int0'});
		d.setStyle({display:'none',height:this.size.height * this.numLines+'px',position:'absolute',top:(this.offTop+'px'),left:this.offLeft+'px','text-align':'left',width:this.travelWidth+'px'});
		this.container.insert(d);
		
		d = new Element( 'div', {id:this.id+'_Int1'});
		d.setStyle({height:this.travelHeight+'px',position:'absolute',top:(this.offTop+'px'),left:this.offLeft+'px','text-align':'left',width:this.travelWidth+'px'});
		this.container.insert(d);

		{
			var a = new Element('span',{'class':'base'});
			var d = new Element( 'a', {href:'javascript:;'});
			d.onclick = this.PageUp.bind(this);
			var e = new Element('img',{id:'PageUp_'+this.id,'class':'i32',src:this.vertical?'/i/flechedown.png':'/i/flechedroite.png'});
			if (this.vertical)
			{
				a.setStyle('height:32px;width:100%;position:absolute;left:0;top:0');
				e.setStyle({position:'absolute',left:(this.csize.width-32)/2+'px',top:0});
			}
			else
			{
				a.setStyle('height:100%;width:32px;position:absolute;right:0;top:0');
				e.setStyle({position:'absolute',left:0,top:(this.csize.height-32)/2+'px'});
			}
			
			d.update(e);
			a.update(d);
			this.container.insert(a,{position:'before'});
		}

		{
			var a = new Element('span',{'class':'base'});
			var d = new Element( 'a', {href:'javascript:;'});
			d.onclick = this.PageDown.bind(this);
			var e = new Element('img',{id:'PageDown_'+this.id,'class':'i32',src:this.vertical?'/i/flecheup.png':'/i/flechegauche.png'});
			if (this.vertical)
			{
				a.setStyle('height:32px;width:100%;position:absolute;left:0;bottom:0');
				e.setStyle({position:'absolute',left:(this.csize.width-32)/2+'px',top:0});
			}
			else
			{
				a.setStyle('height:100%;width:32px;position:absolute;left:0;top:0');
				e.setStyle({position:'absolute',left:0,top:(this.csize.height-32)/2+'px'});
			}
			
			d.update(e);
			a.update(d);
			this.container.insert(a);
		}
		
		
		if (this.nowarp)
		{
			$('PageDown_'+this.id).hide();
		}
		this.ShowPage( 0);
	},
	ShowPage: function( p_arc){
		var l_pdiv = $(this.id+'_Int' + this.currentDiv);
		this.currentDiv = 1 - this.currentDiv;
		var l_div = $(this.id+'_Int' + this.currentDiv);
		l_div.update();
		for (var i = this.currentIndex ; i < this.elements.length && i < this.currentIndex+this.numPerPage ; ++ i)
		{
			l_div.insert( this.elements[i]);
			this.elements[i].show();
		}
		if (p_arc != 0)
		{
			this.moving = true;
			l_div.hide();
			l_pdiv.show();
			
			l_pdiv.setStyle('left:'+this.offLeft+'px');
			l_div.setStyle('left:' + (this.offLeft + (this.vertical?0:p_arc*this.travelWidth))+'px;top:'+ (this.offTop + (this.vertical?p_arc*this.travelHeight:0))+'px');
			new Effect.Parallel(
			[
				new Effect.Move(l_pdiv,{sync:true,mode:'absolute',y:this.offTop-(this.vertical?p_arc*this.travelHeight:0),x:this.offLeft-(this.vertical?0:p_arc*this.travelWidth)}),
				new Effect.Move(l_div,{sync:true,mode:'absolute',y:this.offTop,x:this.offLeft}),
				new Effect.Appear(l_div,{sync:true}),
				new Effect.Fade(l_pdiv,{sync:true})
			],{duration:0.5,afterFinish:this.EndMove.bind(this)});
		}
	},
	PageUp: function(){
		if(this.moving){return;}
		$('PageDown_'+this.id).show();
		this.currentIndex += this.numPerPage;
		if (this.nowarp && (this.currentIndex + this.numPerPage) >= this.elements.length)
		{
			$('PageUp_'+this.id).hide();
		}
		else if (!this.nowarp && (this.currentIndex) >= this.elements.length)
		{
			this.currentIndex = 0;
		}
		var t = this.ShowPage(1);
	},
	EndMove: function(){
		this.moving = false;
	},
	PageDown: function(){
		if(this.moving){return;}
		$('PageUp_'+this.id).show();
		this.currentIndex -= this.numPerPage;
		if (this.nowarp)
		{
			if (this.currentIndex - this.numPerPage < 0)
			{
				$('PageDown_'+this.id).hide();
			}
		}
		else if(this.currentIndex < 0)
		{
			this.currentIndex = this.numPerPage * (this.numPages - 1);
		}
		var t = this.ShowPage(-1);
	}
});

