(function($){

	Scroller = function(options){		
		$.extend(this, options);		
		return this;
	};
	
	Scroller.prototype = {	
	
		// Properties
		buttons    : {},
		callbacks  : {},
		current    : 0,
		length     : 0,
		page       : null,
		scrollable : null,
		sections   : null,
		selector   : '.page',
		
		// Methods
		init : function ()
		{
			this.page.find(this.selector).wrapInner('<div class="scrollable"/>');	
			
			this.scrollable = this.page.find('.scrollable');					
			this.sections   = this.scrollable.find('>div');
			this.length     = this.sections.length;

			this.sections.filter(':gt(0)').css({opacity: .25});

			this.controls();
			this.events();
			
			if (this.callbacks.init !== undefined) this.callbacks.init();
		},
		
		controls : function ()
		{
			this.buttons.previous = $('<span class="up"><span></span></span>');
			this.buttons.next     = $('<span class="down active"><span></span></span>');

			this.buttons.previous.insertAfter(this.scrollable);
			this.buttons.next.insertAfter(this.scrollable);
		},
		
		events : function ()
		{
			this.buttons.previous.bind('click', $.proxy(this, 'previous'));
			this.buttons.next.bind('click', $.proxy(this, 'next'));
		},
		
		next : function ()
		{
			if (this.current++ < this.length-1) {
				this.scrollable.animate({top: "-="+this.sections.eq(this.current-1).outerHeight()}, {
					duration: 600, 
					easing: 'easeInOutExpo'
				});
				this.sections.eq(this.current-1).animate({opacity: 0.25}, {queue: false, duration: 300});
				this.sections.eq(this.current).animate({opacity: 1}, {queue: false, duration: 600});
			} else {
				this.current--;
			}
			this.update();
		},
		
		previous : function ()
		{
			if (this.current-- > 0) {
				this.scrollable.animate({top: "+="+this.sections.eq(this.current).outerHeight()}, {
					duration: 600, 
					easing: 'easeInOutExpo'
				});
				this.sections.eq(this.current).animate({opacity: 1}, {queue: false, duration: 600});
				this.sections.eq(this.current+1).animate({opacity: 0.25}, {queue: false, duration: 300});
			} else {
				this.current++;
			}		
			this.update();
		},
	
		update : function ()
		{
			if (this.current == 0) {
				this.buttons.next.addClass('active');
				this.buttons.previous.removeClass('active');
			} else if(this.current == this.length-1) {
				this.buttons.next.removeClass('active');
				this.buttons.previous.addClass('active');
			} else {
				this.buttons.next.addClass('active');
				this.buttons.previous.addClass('active');
			}
		}
		
	};

})(jQuery);
