var akrScroller = Class.create();
akrScroller.prototype = {
	item_position: 0,
	item_max: 0,
	item_perpage: 0,
	page_total: 0,
	page_current: 0,
	container0ready: false,
	container2ready: false,
	working: false,
	url: '',
	whnd: '',
	nextdirection: 0,
	containerheight: 0,
	container0: '',
	container1: '',
	container2: '',
	callback: false, // function that gets CANMOVEPREVIEWS,CANMOVENEXT and CURRENTITEM
	initialize: function(c0,c1,c2,height) {
		height = parseInt(height);
		this.containerheight = height;
		this.container0 = $(c0);
		this.container0.style.position = 'absolute';
		this.container0.style.top = '-'+height+'px';
		this.container0.style.height = height+'px';
		this.container1 = $(c1);
		this.container1.style.position = 'absolute';
		this.container1.style.top = '0px';
		this.container1.style.height = height+'px';
		this.container2 = $(c2);
		this.container2.style.position = 'absolute';
		this.container2.style.top = height+'px';
		this.container2.style.height = height+'px';
	},
	start: function() {
		this.page_total = Math.floor(this.item_max/this.item_perpage);
		this.page_current = (this.item_position-1)/this.item_perpage;
	},
	move: function(direction,appendQuery) {
		if (this.working) return false; // loading/moving
		if (direction == -1 && this.page_current <= 0) return false; // already at start
		if (direction == 1 && this.page_current>=this.page_total) return false; // already at top
		this.nextdirection = direction;
		if (direction == -1 && this.container0ready) { // move up and ready
			this.moveGo();
		} else if (direction == 1 && this.container2ready) { // move down and ready
			this.moveGo();
		} else if (direction == -1) { // move up
			this.working = true;
			if (!appendQuery) appendQuery = "";
			url = this.url+(this.item_position-this.item_perpage);
			new Ajax.Updater(this.container0,url,{method:'post',
									    		  parameters:appendQuery,
												  asynchronous:true,
				  								 onComplete:this.moveGo.bind(this)
												});
		} else { // move down
			this.working = true;
			url = this.url+((this.item_position+this.item_perpage));
			if (!appendQuery) appendQuery = "";
			new Ajax.Updater(this.container2,url,{method:'post',
												  parameters:appendQuery,
										          asynchronous:true,
												 onComplete:this.moveGo.bind(this)
												});
		}
		this.updateiface();
		return true;
	},
	moveGo: function() {
		this.working = true;
		if (this.nextdirection==-1) {
			this.container0.style.top = '-'+this.containerheight+"px";
			this.container0.style.display = ''
			new Effect.Move(this.container0, { x: 0, y: 0, mode: 'absolute' 
								});
			new Effect.Move(this.container1, { x: 0, y: this.containerheight, mode: 'absolute' 
								});
		} else {
			this.container2.style.top = this.containerheight+"px";
			this.container2.style.display = ''
			new Effect.Move(this.container2, { x: 0, y: 0, mode: 'absolute' 
								});
			new Effect.Move(this.container1, { x: 0, y: -this.containerheight, mode: 'absolute' 
								});
		}
		setTimeout(this.moveEnd.bind(this),'1500');
	},
	moveEnd: function() {
		tmp = this.container1.innerHTML; // switch content with ajax container
		this.container1.style.top = '0px';
		this.container1.innerHTML = this.nextdirection==-1?this.container0.innerHTML:this.container2.innerHTML; // put ajax container in the middle
		this.container0.style.top = '-'+this.containerheight+'px';
		this.container2.style.top = this.containerheight+'px';
		if (this.nextdirection==-1) {
			this.container2.innerHTML = tmp;
			this.container0.innerHTML = '';
			this.item_position -= this.item_perpage;
			this.page_current--;
		} else {
			this.container0.innerHTML = tmp;
			this.container2.innerHTML = '';
			this.item_position += this.item_perpage;
			this.page_current++;
		}
		this.container0ready = this.nextdirection==1;
		this.container2ready = this.nextdirection==-11;
		tmp = "";
		this.working = false;
		this.nextdirection = 0;
		this.updateiface();
	},
	updateiface: function() {
		if (this.callback) {
			canMovePreviews = !this.working && this.page_current > 0;
			canMoveNext = !this.working && this.page_current < this.page_total;
			this.callback (canMovePreviews,canMoveNext,this.item_position,this.whnd,this.item_perpage);
		}
	}
}