var snails;
var trail;
var timer;
var wtimer;
var wtimerTime = 100;
var ms = 50;
var init_trailWidth;
var home_trailWidth;
var target_width;
var speedMin = 2;
var speedCoef = 6;
var decalage = 140;

function mv() {
	speed = Math.abs(trail.offsetWidth - target_width)/speedCoef;
	if (speed < speedMin) speed = speedMin;
	if (trail.offsetWidth < target_width) {
		if ((target_width - trail.offsetWidth) < speed) {
			nwdth = target_width;
		} else {
			nwdth = trail.offsetWidth + speed;
		}
		trail.style.width = nwdth + "px";
	} else if (trail.offsetWidth > target_width) {
		if ((trail.offsetWidth - target_width) < speed) {
			nwdth = target_width;
		} else {
			nwdth = trail.offsetWidth - speed;
		}
		trail.style.width = nwdth + "px";
	}
	if (trail.offsetWidth != target_width) {
		clearTimeout(timer);
		timer = setTimeout("mv()", ms);
	}
}

function ctw() {
	clearTimeout(wtimer);
}

function move_snails(tw) {
	clearTimeout(wtimer);
	target_width = tw;
	mv();
}

function soon_move_back_snails() {
	wtimer = setTimeout("move_back_snails()", wtimerTime);
}

function move_back_snails() {
	target_width = home_trailWidth;
	mv();
}

function init_snails_move() {
	snails = document.getElementById("snails");
	snails.onmouseover = ctw;
	snails.onmouseout = soon_move_back_snails; 
	trail = document.getElementById("snails_trail");
	for (i=0; i<=3; i++) {
		btn = document.getElementById("menu_"+(i+1));
		btn.onmouseover = new Function("move_snails(" + i*decalage + ");");
		btn.onmouseout = soon_move_back_snails;
		if (i == 0) {
			init_trailWidth = trail.offsetWidth;
			home_trailWidth = 0;
		} 
		if (btn.className.match("active") != null) {
			home_trailWidth = i*decalage;
			target_width = i*decalage;
		} 
	}
	mv();
}

