//Тип анимации, < 1 easy in, > 1 easy out
var powr = 2;
//Длительность (позиций)
var totalsteps = 40;
//Пункты меню
var lis;
// Хранит текущую вкладку
var current = {amimate: null};

/*Чтобы блоки сходились при быстром убирании мышки на mouseout добавить проверку !current.animate. 
Но тогда при быстром перемещении по вкладкам, интератор будет создаваться для каждой вкладки и плашки будут скакать. 
Чтобы избавится от этого создать объект current co свойствойм animate глобально. 
И перед присвоением current = this отменить animate.*/




window.onload = function() {
	if (document.getElementById("maincat")) {
	lis = document.getElementById("maincat").getElementsByTagName("li");
	for (var i=0; i<lis.length; i++) {
		lis[i].onmouseover = resizeitems;
		lis[i].onmouseout = stopresize;
		}
	}
	
	
	
	//Анимация лампы внизу
	document.getElementById("footer").onmouseover = function() {
		this.className = "lampaon";
	}
	document.getElementById("footer").onmouseout = function() {
		this.className = "lampaoff";
	}
	
	//Подгружаем картинку Лампы
	var pic = new Image();
	pic.src = "images/lampa.jpg";
	
}
function resizeitems() {
	var step = 0;
	clearInterval(current.animate);
	current = this;
	current.animate = setInterval(function(){
		for (var i=0; i<lis.length; i++) {
			var el = lis[i];
			if (current == el) {
				//элемент в фокусе
				el.firstChild.style.backgroundPosition = '106px';
				el.style.width = easeing(el,369,step) + 'px';
				//специальный стиль для последнего элемента
				if (i == lis.length - 1) {
					el.style.width = easeing(el,375,step) + 'px';
					var curwidth = Number(el.style.width.replace('px',''));
					el.style.backgroundPosition = curwidth - 379 + 'px';
					el.firstChild.style.backgroundPosition = 'right';
					el.firstChild.style.paddingLeft = curwidth - 100 + 'px';
				}
			} else {
				//элемнт вне фокуса
				el.style.width = easeing(lis[i],105,step) + 'px';
				//специальный стиль для последнего элемента
				if (i == lis.length - 1) {
					el.style.width = easeing(lis[i],109,step) + 'px';
					var curwidth = Number(el.style.width.replace('px',''));
					el.style.backgroundPosition = curwidth - 379 + 'px';
					el.firstChild.style.paddingLeft = curwidth - 100 + 'px';
				}
		}
	}
	step++;
	if (step >= totalsteps) {clearInterval(current.animate);};
	},10);
	
}


function easeing(obj,maxValue,actualStep) { 
	var minValue = obj.style.width ? Number(obj.style.width.replace('px','')) : 148;
    var delta = maxValue - minValue; 
    var stepp = minValue+(Math.pow((actualStep / totalsteps), powr) * delta); 
    return Math.ceil(stepp) 
    } 

function stopresize() {
}

