var navLeft = {
	initialize: function(){
		var ref = document.id('navA'), i, blocks, handle, active = false;

		if(ref){
			this.fx = [];
			blocks = ref.getElements('ul');

			for(i=0; i<blocks.length; i++){
				handle = blocks[i].getPrevious();
				this.fx[i] = new Fx.Slide(blocks[i].removeClass('preload')).hide();
				handle.addEvent('click',this.toggle.bindWithEvent(this,[i]));
				if(handle.getParent().hasClass('leading')){
					active = i;
				}
			}

			if(active !== false){
				this.fx[active].show();
			}
		}
	},

	toggle: function(e,i){
		new Event(e).stop();
		for(var j = 0; j < this.fx.length; j++){
			if(j === i){
				this.fx[j][this.fx[j].open ? 'slideOut' : 'slideIn']();
			}else if(this.fx[j].open){
				this.fx[j].slideOut();
			}
		}
	}
};

var navInt = {
	initialize: function(){
		var ref = document.id('navInt'), i, submenu;

		if(ref){
			this.displayed = false;
			this.current = false;
			this.preventer = false;

			this.blocks = [];
			this.handles = ref.getElement('ul').getChildren();
			this.total = this.handles.length;

			for(i=0; i<this.total; i++){
				submenu = this.handles[i].getElement('ul');

				if(submenu){
					this.blocks[i] = submenu.addEvents({
						mouseenter: this.prevent.bind(this),
						mouseleave: this.hide.bind(this)
					});
				}

				this.handles[i].addEvents({
					mouseenter: this.show.pass([i],this),
					mouseleave: this.hide.bind(this)
				});

				if(this.handles[i].hasClass('leading')){
					this.current = i;
				}
			}

			this.restore();
		}
	},

	show: function(j){
		this.prevent();
		this.displayed = j;

		for(var i=0; i<this.total; i++){
			this.handles[i].
				setOpacity(j === i ? 0 : 1)
				[(j === i ? 'add' : 'remove') + 'Class']('leading')
				[([0, j, j+1].contains(i) ? 'add' : 'remove') + 'Class']('noborder');

			if(this.blocks[i]){
				this.blocks[i].setStyle('display', j === i ? 'block' : 'none');
			}

			this.handles[j].fade('in');
		}
	},

	hide: function(){
		this.preventer = false;
		this.restore.delay(200,this);
	},

	prevent: function(){
		this.preventer = true;
	},

	restore: function(){
		if(this.current !== false && this.current !== this.displayed && !this.preventer){
			this.show(this.current);
		}
	}
};

(function(){
	document.getElements('a.blank').each(function(a){
		a.addEvent('click',function(e){
			new Event(e).stop();
			window.open(this.href);
		});
	});
})();

navLeft.initialize();
navInt.initialize();

