aParams = location.hash.split('/');
// alert(aParams);
var animMode = aParams[0] ? aParams[0] : '';//location.hash;
var animDuration = aParams[1] ? aParams[1] / 1000 : 0.4;//0.4;
var stepTime = aParams[2] ? aParams[2] : 300;//300;
var group = aParams[3] ? aParams[3] : 3;//3;
function getParentByClassName(obj, className) {
	obj = $(obj);
	while (obj && obj.hasClassName && !obj.hasClassName(className)) {
		obj = $(obj.parentNode);
	}
	return obj && obj.hasClassName && obj.hasClassName(className) ? $(obj) : null;
}
function animStep(startPrices, endPrices, className, counters, n) {
	if (animMode == '#scoreboard') {
		var digits = counters[0].getElementsByTagName('span');
		var step = $(digits[0]).getHeight() / 22;
		for (var i = n*group; i < (n+1)*group; i++) {
			if (!startPrices[i]) break;
			startPrices[i].parentNode.removeClassName('f');
			startPrices[i].parentNode.removeClassName('l');
			startPrices[i].parentNode.addClassName('a');
			var digits = counters[i].getElementsByTagName('span');
			var stratPrice = parseInt(startPrices[i].innerHTML);
			var soffs4 = (tmp = Math.floor(stratPrice / 1000) % 10) ? (tmp + 1) * step : 0;
			var soffs3 = (tmp = Math.floor(stratPrice / 100) % 10) ? (tmp + 1) * step : (soffs4 ? step : 0);
			var soffs2 = (tmp = Math.floor(stratPrice / 10) % 10) ? (tmp + 1) * step : (soffs4 || soffs3 ? step : 0);
			var soffs1 = (tmp = stratPrice % 10) ? (tmp + 1) * step : step;
			
			var endPrice = parseInt(endPrices[i].innerHTML);
			var eoffs4 = (tmp = Math.floor(endPrice / 1000) % 10) ? (tmp + 1) * step : 0;
			var eoffs3 = (tmp = Math.floor(endPrice / 100) % 10) ? (tmp + 1) * step : (eoffs4 ? step : 0);
			var eoffs2 = (tmp = Math.floor(endPrice / 10) % 10) ? (tmp + 1) * step : (eoffs4 || eoffs3 ? step : 0);
			var eoffs1 = (tmp = endPrice % 10) ? (tmp + 1) * step : step;
			
			digits[0].style.top = ('-' + soffs1 + 'px');
			digits[1].style.top = ('-' + soffs2 + 'px');
			digits[2].style.top = ('-' + soffs3 + 'px');
			digits[3].style.top = ('-' + soffs4 + 'px');
			
			if (eoffs1 != soffs1)
				$(digits[0]).morph('top:-' + (eoffs1 + (eoffs1 < soffs1 ? step * 11 : 0)) + 'px', {duration:animDuration});
			if (eoffs2 != soffs2)
				$(digits[1]).morph('top:-' + (eoffs2 + (eoffs2 < soffs2 ? step * 11 : 0)) + 'px', {duration:animDuration});
			if (eoffs3 != soffs3)
				$(digits[2]).morph('top:-' + (eoffs3 + (eoffs3 < soffs3 ? step * 11 : 0)) + 'px', {duration:animDuration});
			if (eoffs4 != soffs4)
				$(digits[3]).morph('top:-' + (eoffs4 + (eoffs4 < soffs4 ? step * 11 : 0)) + 'px', {duration:animDuration,
				afterFinish:function() {
					// $('price_cont').removeClassName('a');
					this.parentNode.removeClassName(className);
					this.parentNode.removeClassName(className == 'l' ? 'f' : 'l');
					// $('price_cont').addClassName(className);
				}
			});
		}
	} else {
		for (var i = n*group; i < (n+1)*group; i++) {
			if (!startPrices[i]) break;
			if (animMode != '#appearOnly') {
				new Effect.Fade(startPrices[i], {duration:animDuration, afterFinish:function(className) {
					$(this).setOpacity(0);
					new Effect.Appear(this, {duration:animDuration});
					$(this.parentNode).removeClassName(className == 'l' ? 'f' : 'l');
					$(this.parentNode).addClassName(className);
				}.bind(endPrices[i], className)});
			} else {
				$(endPrices[i]).setOpacity(0);
				new Effect.Appear(endPrices[i], {duration:animDuration});
				$(endPrices[i].parentNode).removeClassName(className == 'l' ? 'f' : 'l');
				$(endPrices[i].parentNode).addClassName(className);
			}
		}
	}
}
function animate(startPrices, endPrices, className) {
	var counters = $('price_cont').getElementsByClassName('counter');
	//Второй вид анимации включается добавлением #scoreboard к адресу
	if (animMode == '#appearOnly') {
		for (i = 0; i < startPrices.length; i++) {
			startPrices[i].style.display = 'none';
		}
	}
	for (i = 0; i < startPrices.length/group; i++) {
		setTimeout(function(startPrices, endPrices, className, i) {
			animStep(startPrices, endPrices, className, this, i);
		}.bind(counters, startPrices, endPrices, className, i), stepTime*i);
	}
}
function changePrice(ev) {
	var trgObj = Event.element(ev);
	if(trgObj.tagName.toLowerCase() != 'a') return;
	var obj = null;
	var allLPrices = $('price_cont').getElementsBySelector('span.l');
	var allFPrices = $('price_cont').getElementsBySelector('span.f');
	if (obj = getParentByClassName(trgObj.parentNode, 'l')) {
		animate(allLPrices, allFPrices, 'f');
		obj.removeClassName('l');
		obj.addClassName('f');
	} else if (obj = getParentByClassName(trgObj.parentNode, 'f')){
		animate(allFPrices, allLPrices, 'l');
		obj.removeClassName('f');
		obj.addClassName('l');
		// $('price_cont').removeClassName('f');
		// $('price_cont').addClassName('l');
	}
	return false;
}

function act(ev) {
	var trgObj = Event.element(ev);
	location.href = getParentByClassName(trgObj.parentNode, 'content').getElementsByTagName('a')[0].href;
	return false;
}
