window.arivis = window.arivis || {};
window.arivis.website = window.arivis.website || {};

/**
 * @constructor Navigation init
 */
arivis.website.Navigation = function() {

	this.init = function(){
		var mainNavElem = $$('#nav_main a');
		for (var i=0; i < mainNavElem.length; i++){
			if (!mainNavElem[i].hasClassName('current')){
				mainNavElem[i].addClassName('jsanimate');
				mainNavElem[i].navigate = new arivis.website.elemNavigation(mainNavElem[i]);
			}
		}
	};
};

// init Nativation
arivis.website.navigation = new arivis.website.Navigation();
Event.observe(window, 'load', function(){ arivis.website.navigation.init(); });

/**
 * @constructor Navigation init
 * @param {object} parameter
 */
arivis.website.elemNavigation = function(elem) {

	this.timeId = 0;
	this.timer = 30;
	this.step = 1.5;
	this.breaking = 0.3;
	this.breakPoint = 4;
	this.maxDist = 10;
	this.position = 0;
	this.dir = 0;
	

	var instance = this;
	elem.observe("mouseover", function(e){ instance.start(e); });
	elem.observe("mouseout", function(e){ instance.start(e); });
};

arivis.website.elemNavigation.prototype.start = function(e){
		var elem = Event.element(e);
		this.dir = (e.type == "mouseover") ? 1 : -1;
		this.run(e);
	};

arivis.website.elemNavigation.prototype.run = function(e){
		window.clearTimeout(this.timeId);
		if (this.setDist(e)){
			var instance = this;
			this.timeId = window.setTimeout( function(){
				instance.run(e);
			}, this.timer);
		}
	};
	
arivis.website.elemNavigation.prototype.setDist = function(e){
		var elem = Event.element(e);
		if (typeof(elem) != "object") return;
		var position = this.getNewPosition();
			elem.style.marginTop = (-1*position)+"px";
			elem.style.borderBottom = position+"px solid #FFF";
		if (position <= 0 || position >= this.maxDist){
			this.position = (this.dir >=0) ? this.maxDist : 0;
			return false;
		}
		return true;
	};

arivis.website.elemNavigation.prototype.getNewPosition = function(){
		var step = (this.dir >= 0)
			? (this.maxDist-this.position)*this.breaking
			: this.position*this.breaking;
		step = Math.min(step, this.step);
		this.position += (this.dir >= 0) ? step : (step*-1);
		this.position = Math.min(this.position, this.maxDist);
		this.position = Math.max(this.position, 0);
		return Math.round(this.position);
	};
