var Pager = new Class({
	Implements: [Options, Events],
	
	options: {
		'start': 0,
		'zindex': 100,
		'handlers' : false,
		'handler_events': true,
		'effect': 'scroll',
		'width': 100,
		'height': 100,
		'orientation': 'horizontal',
		'page_effect': {
			'duration': 750,
			'link': 'cancel',
			'transition': Fx.Transitions.Expo.easeInOut
		},
		'onPageMoveBefore': $empty,
		'onPageMoveComplete': $empty,
		'onPageMoveIn': $empty,
		'onPageMoveOut': $empty,
		'onHandlerClick': $empty
	},
	
	initialize: function(pages, options){
		this.pages = $$(pages);
		this.setOptions(options);

		if(!this.pages) return;
		
		//ff win fix[hack]
		if(Browser.Platform.win && Browser.Engine.gecko){
		    this.options.effect = 'fade';
		}
		
		this.container = this.pages[0].getParent();
		this.page_count = this.pages.length;
		this.current_page = this.options.start;
		this.previous_page = 0;//this.options.start;
		this._build();
	},
	
	_build: function(){
		this.pages.each(function(page, i){
			var fx = new Fx.Morph(page, this.options.page_effect);
			
			page.store('fx', fx);
			
			switch(this.options.effect){
			    case 'scroll':
			        if(i != this.options.start){
			            this._placePage(i);
			        }
			
			        break;
			        
			    case 'fade':
			        if(i != this.options.start){
    			        page.setStyles({
    			            'z-index': (this.options.zindex - 1),
    			            'opacity': 0
    			        });
    			    }
    			    
			        break;
			}
			
			this.fireEvent('pageMoveIn', [this.current_page, this.previous_page]);
		}, this);
		
		if(this.options.handlers && this.options.handler_events){
            this.options.handlers = $$(this.options.handlers);

			this.options.handlers.each(function(handle, i){
				handle.addEvent('click', function(e){
					e.stop();
					this.goTo(i);
					this.fireEvent('handlerClick', [this.current_page, this.previous_page])
				}.bind(this));
			}, this);
		}
		
		return this;
	},
	
	_movePages: function(page_in, page_out){
		var page_in_fx = this.pages[page_in].retrieve('fx'),
    		page_out_fx = this.pages[page_out].retrieve('fx'),
    		location = '';
		
		/**
		 * fix the page placement for the first and last page while looping
		 */
		if(page_in === 0 && page_out === this.page_count - 1){
			location = 'after';
		}else if(page_in === this.page_count - 1 && page_out === 0){
			location = 'before';
		}else{
			location = page_in <= page_out ? 'before' : 'after';
		}
		
		this._placePage(page_in, location);
		
		this.pages[page_in].setStyle('overflow-y', 'auto');
		this.pages[page_out].setStyle('overflow', 'hidden');

		page_in_fx.start({
			'left': 0
		});
		
		this.fireEvent('pageMoveIn', [this.current_page, this.previous_page]);
		
		page_out_fx.start({
			'left': [0, location == 'before' ? this.options.width : -this.options.width]
		}).chain(function(e){
    		this.fireEvent('pageMoveOut', [this.current_page, this.previous_page]);
		}.bind(this));
		
		return this;
	},
	
	_fadePages: function(page_in, page_out){
	    var page_in_fx = this.pages[page_in].retrieve('fx'),
    		page_out_fx = this.pages[page_out].retrieve('fx');
    	
		this.fireEvent('pageMoveIn', [this.current_page, this.previous_page]);

    	this.pages[page_in].setStyles({
    	    'z-index': ++this.options.zindex
    	});
    	
    	this.pages[page_out].setStyles({
    	    'top': 0,
    	    'left': 0
    	});
    	
    	page_out_fx.start({'opacity': [100, 0]});    	
	    page_in_fx.start({'opacity': [0, 100]}).chain(function(e){
    		this.fireEvent('pageMoveOut', [this.current_page, this.previous_page]);
		}.bind(this));
	    
	    return this;
	},
	
	_placePage: function(index, location){
		var left = 0,
		    top = 0;
		
		switch(location){
			case 'before':
				left = -this.options.width;
				break;
				
			case 'after':
			default:
				left = this.options.width;
				break;
		}
		
		this.pages[index].setStyles({
			'top': top,
			'left': left,
			'z-index': ++this.options.zindex
		});
		
		return this;
	},
	
	goToTitle: function(title){
	    for(var x = 0; x < this.page_count; x++){
	        if(this.pages[x].get('title') == title){
	            this.goTo(x);
	            break;
	        }
	    }
	    
	    return this;
	},
	
	goTo: function(index){
		this.fireEvent('pageMoveBefore');
		
		if(index > this.page_count){
			return;
		}else if(index != this.current_page){
			this.previous_page = this.current_page;
			this.current_page = index;
            
            switch(this.options.effect){
                case 'scroll':
 			        this._movePages(index, this.previous_page);
 			        break;
 			    
 			    case 'fade':
 			        this._fadePages(index, this.previous_page);
 			        break;
 			}
 			
			this.fireEvent('pageMoveComplete');
		}
		
		return this;
	},
	
	next: function(){
		var next = 1 + this.current_page < this.page_count ? 1 + this.current_page : 0;

		this.goTo(next);
		
		return this;
	},
	
	prev: function(){
		var prev = this.current_page -1 < 0 ? this.page_count - 1 : this.current_page - 1;
		
		this.goTo(prev);
		
		return this;
	}
});