var AquilaNavigator = new Class({
	
	initialize: function(options)
	{
		this.list = $(options.list);
		
		if(!this.list) return false;
		
		this.childs = this.list.getChildren('li');
		this.numChilds = this.childs.length;
		this.size = this.list.getSize().y;
		this.max = this.list.getScrollSize().y - this.size;
		if(this.max <= 0)
		{
			$(options.prev).destroy();
			$(options.next).destroy();
			return false;
		}
		this.current = this.list.getScroll().y;
		this.index = 0;
		this.working = false;
		
		this.fx = new Fx.Scroll(this.list, {
			'onComplete': function() {
				this.current = this.list.getScroll().y;
				this.working = false;
			}.bind(this)
		});
		
		$(options.prev).addEvent('click', this.prev.bindWithEvent(this));
		$(options.next).addEvent('click', this.next.bindWithEvent(this));
		
		this.checkInitial(options.list);
	},
	
	checkInitial: function(id)
	{
		var active = $$('#' + id + ' a.active')[0];
		
		if(active)
		{
			var li = active.getParent('li');
			
			this.index = this.childs.indexOf(li);
			if(this.index == 0) return;
			
			var pos = this.childs[this.index].getPosition(this.list).y;
			
			var delta = pos - this.current;
			
			if(delta > this.max)
			{
				do
				{
					this.index--;
					pos = this.childs[this.index].getPosition(this.list).y;
					delta = pos - this.current;
					
				} while(delta > this.max && this.index > 0);
				if(this.index == 0) this.index = 1;
			}
			this.doScroll();
		}
	},
	
	prev: function(e)
	{
		e.stop();
		if(this.current > 0 && !this.working)
		{
			this.index--;
			if(this.index < 0) { this.index = 0; }
			this.doScroll();
		}
	},
	
	next: function(e)
	{
		e.stop();
		if(this.current < this.max && !this.working)
		{
			this.index++;
			this.doScroll();
		}
	},
	
	doScroll: function()
	{
		this.working = true;
		this.fx.toElement(this.childs[this.index]);
	}
	
});