var MenuFollower = new Class({
	Implements:Options,
	
	options : {
		container : '.menumain',
		follower : '.follower',
		following : 'a'
	},
	
	initialize : function(options){
		this.setOptions(options);
		this.initElements();
		this.getTarget();
		this.setInitPosition();
		this.setBehavior();
	},
	
	initElements : function(){
		this.container = $$(this.options.container)[0];
		this.follower = this.container.getElement(this.options.follower);
		this.following = this.container.getElements(this.options.following);
	},
	
	getTarget : function(){
		//get last from path
		this.selected = false;
		this.tPath = $$('.path')[0].getLast().getFirst().get('html');
		this.following.each(function(el){
			//test el against path
			if (el.get('html') == this.tPath) {
				this.selected = el;
			}
		}, this);
		
	},
	
	setInitPosition : function(){
		if(!this.selected) {
			this.initPos = false;
			this.follower.setStyle('opacity', 0);
			var fx = new Fx.Tween(this.follower, {property : 'opacity', wait: false});
			this.container.addEvents({
				'mouseenter' : function(){
					fx.start(1);
				},
				'mouseleave' : function(){
					fx.start(0);
				}
			});
		} else {
			this.initPos = this.selected.getCoordinates(this.container);
			this.follower.setStyle('left', this.initPos.left + this.initPos.width / 2 - 6)
		}
		this.container.addEvents({
			'mouseleave' : function(){
				if(this.selected) this.followerFx.start(this.initPos.left + this.initPos.width / 2 - 6)
			}.bind(this)
		});
	},
	
	setBehavior : function(){
		this.followerFx = new Fx.Tween(this.follower, {
			property : 'left',
			wait : false
		});
		
		this.following.each(function(el){
			var coord = el.getCoordinates(this.container);
			el.addEvents({
				'mouseenter' : function(){
					if(!this.selected && !this.follower.getStyle('opacity')) {
						this.follower.setStyle('left', coord.left + coord.width / 2 - 6)
					} else {
						this.followerFx.start(coord.left + coord.width / 2 - 6);
					}
					
				}.bind(this)
			});
		}, this);
	}
});
