// JavaScript Document


PageTurner = Class.create();
Object.extend(PageTurner.prototype, {
	element: 'pageturner'
	,buttonPrefix: 'page'
	,totalItems: 1
	,currentItem: 1
	,viewSize: 1
	,duration: .7
	,pauseLength: 5000
	,timer: 0
	,transition: Effect.Transitions.sinoidal
	,direction: 1  //1 for horizontal, 2 for vertical
	,offset: 0 //in pixels
	,inactiveCssClass: ''
	,activeCssClass: 'active'
	,isAutoscroll: false
	,isStopped: false
	,isMoving: false
	,isPagesWrapped: true
	,debug: false
	,noNumbers: false 
	
	,initialize: function() {
		this.options = Object.extend({
			element: 'pageturner'
			,buttonPrefix: 'page'
			,totalItems: 1
			,viewSize: 1
			,duration: .7
			,pauseLength: 5000
			,timer:0
			,transition: Effect.Transitions.sinoidal
			,direction: 1
			,offset: 0
			,inactiveCssClass: ''
			,activeCssClass: 'active'
			,isAutoscroll: false
			,isPagesWrapped: true
			,debug: false
			,noNumbers: false 
		}, arguments[0] || {});
		
		this.element = this.options.element;
		this.buttonPrefix = this.options.buttonPrefix
		this.totalItems = this.options.totalItems; 
		this.viewSize = this.options.viewSize;
		this.duration = this.options.duration;
		this.pauseLength = this.options.pauseLength;
		this.timer = this.options.timer;
		this.transition = this.options.transition;
		this.direction = this.options.direction;
		this.offset = this.options.offset;
		this.inactiveCssClass = this.options.inactiveCssClass;
		this.activeCssClass = this.options.activeCssClass;
		this.isAutoscroll = this.options.isAutoscroll;
		this.isPagesWrapped = this.options.isPagesWrapped;
		this.debug = this.options.debug;
		this.noNumbers = this.options.noNumbers;
		
		if(this.isAutoscroll){
			this.start();
		}
	}
	
	,start: function() {
		this.isStopped = false;
		this.interval = setInterval(function(){ this.showNext(true) }.bind(this), this.pauseLength);
	}
	
	,stop: function() {
		this.isStopped = true;
		clearInterval(this.interval);
	}
	
	,reset: function(){
		this.stop();
		this.start();
	}

	,showPrevious: function(isAuto) 
	{	
		if (this.currentItem > 1) 
		{
			var x,y;
			(this.direction == 1) ? x = this.offset : x = 0;
			(this.direction == 2) ? y = this.offset : y = 0;
			
			if(this.debug){ alert("Showing Previous | Current Item: " + this.currentItem + " x: " + x + " y:" + y)}
			new Effect.Move(this.element, { 
				x: x
				,y: y
				,duration: this.duration
				,beforeStart: function() {
					if(!this.isMoving){
						if (!this.noNumbers)
						{
							$(this.buttonPrefix + this.currentItem).className = this.inactiveCssClass;	
							$(this.buttonPrefix + (this.currentItem-1)).className = this.activeCssClass;
						}
						this.isMoving = true;
					}
					if(!isAuto){ this.stop(); }
				}.bind(this)
				,afterFinish: function(e) { 
					this.currentItem--;
					this.isMoving = false;
					if(this.isAutoscroll && !this.isStopped){ this.reset(); }
				}.bind(this)
				,transition: this.transition
				,queue: { 
					position: 'end'
					,scope: this.element
					,limit: 1 
				}
			});
		}
		else{
			if(this.isPagesWrapped){
				this.showPage(this.totalItems);
			}
		}
	}
	,showNext: function(isAuto) 
	{	
		if (this.currentItem < this.totalItems) 
		{
			var x,y;
			(this.direction == 1) ? x = -1*this.offset : x = 0;
			(this.direction == 2) ? y = -1*this.offset : y = 0;
			if(this.debug){ alert("Showing Next | Current Item: " + this.currentItem + " x: " + x + " y:" + y)}
			new Effect.Move(this.element, { 
				x: x
				,y: y
				,duration: this.duration
				,beforeStart: function() {
					if(!this.isMoving){
						if (!this.noNumbers)
						{
							$(this.buttonPrefix + this.currentItem).className = this.inactiveCssClass;	
							$(this.buttonPrefix + (this.currentItem+1)).className = this.activeCssClass;
						}
						this.isMoving = true;
					}
					if(!isAuto){ this.stop(); }
				}.bind(this)
				,afterFinish: function(e) { 
					this.currentItem++;
					this.isMoving = false;
					if(this.isAutoscroll && !this.isStopped){ this.reset(); }
				}.bind(this)
				,transition: this.transition
				,queue: { 
					position: 'end'
					,scope: this.element
					,limit: 1 
				}
			});
		}
		else{
			if(!isAuto){ var isAuto = false; }
			if(this.isPagesWrapped){
				this.showPage(1, isAuto);
			}
		}
	}
	,showPage: function(num, isAuto)
	{		
		var x,y;
		(this.direction == 1) ? x = ((num - this.currentItem)*(-1 * this.offset)) : x = 0;
		(this.direction == 2) ? y = ((num - this.currentItem)*(-1 * this.offset)) : y = 0;
		if(this.debug){ alert("Showing Page " + num + " | Current Item: " + this.currentItem + " x: " + x + " y:" + y)}
		new Effect.Move(this.element, { 
			x: x
			,y: y
			,duration: this.duration
			,beforeStart: function() {
				if(!this.isMoving){
					if (!this.noNumbers)
					{
						$(this.buttonPrefix + this.currentItem).className = this.inactiveCssClass;
						$(this.buttonPrefix + num).className = this.activeCssClass;
					}
					this.isMoving = true;
				}
				if(!isAuto){ this.stop(); }
			}.bind(this)
			,afterFinish: function(e) { 
				this.currentItem = num;
				this.isMoving = false;
				if(this.isAutoscroll && !this.isStopped){ this.reset(); }
			}.bind(this)
			,transition: this.transition
			,queue: { 
				position: 'end'
				,scope: this.element
				,limit: 1 
			}
		});
	}
});
