var static_prefix = '';
var div = document.getElementById('lovetree_image');
var min_y = 50;
var offset = 120;
var limit = 20;
var timeout = 150;

var items = new Array();
var branches = new Array();

function i(code, x, y)
{
	items.push([ code, x, y ]);

	if (y < min_y)
		min_y = y;
}

function b(x, y, angle, variant)
{
	branches.push([ x, y, angle, variant ]);

	if (y < min_y)
		min_y = y;
}

function tick()
{
	if (branches.length) {
		var index_from = (branches.length > limit) ? branches.length - limit : 0;
		for (var i = index_from; i < branches.length; i++) {
			var branch = branches[i];
			var x = branch[0];
			var y = branch[1];
			var angle = branch[2];
			var variant = branch[3];
			var age = branches.length - i;
			var thickness1 =
				(age > 25000) ? 16 :
				(age > 15000) ? 15 :
				(age > 10000) ? 14 :
				(age > 4000) ? 13 :
				(age > 2000) ? 12 :
				(age > 1000) ? 11 :
				(age > 500) ? 10 :
				(age > 300) ? 9 :
				(age > 200) ? 8 :
				(age > 150) ? 7 :
				(age > 120) ? 6 :
				(age > 70) ? 5 :
				(age > 50) ? 4 :
				(age > 20) ? 3 :
				(age > 5) ? 2 :
				1;
			var thickness2 = Math.round((branches.length - i) * 16 / branches.length);
			if (thickness2 < 1)
				thickness2 = 1;
			var thickness = (thickness1 < thickness2) ? thickness1 : thickness2;
			var el = document.createElement('IMG');
			el.src = static_prefix + '/st/branches/' + angle + '-' + thickness + '-' + variant + '.gif';
			el.style.position = 'absolute';
			el.style.left = (x + 135) + 'px';
			el.style.top = (y + offset - min_y) + 'px';
			div.appendChild(el);
		}
		branches.splice(index_from, branches.length - index_from);
	}

	if (items.length) {
		var index_from = (items.length > limit) ? items.length - limit : 0;
		for (var i = index_from; i < items.length; i++) {
			var item = items[i];
			var code = item[0];
			var x = item[1];
			var y = item[2];
			var el = document.createElement('IMG');
			el.src = static_prefix + '/st/loveitems/' + code + '.gif';
			el.style.position = 'absolute';
			el.style.left = (x + 135) + 'px';
			el.style.top = (y + offset - min_y) + 'px';
			div.appendChild(el);
		}
		items.splice(index_from, items.length - index_from);
	}
	
	if (branches.length || items.length) {
		setTimeout(tick, timeout);
	}
}

function render()
{
	var height = offset - min_y + 220;
	div.style.height = height + 'px';
	tick();
}

