var Roller = {
	settings: {
		height: 35,
		totalHeight: 102,
		step: 12,
		persistence: 5
	},
	change: function(div, a)
	{
		var img = a.firstChild.nextSibling || a.firstChild;
		var zboziContent = div.parentNode.parentNode;

//		zboziContent.className = "zbozi " + img.className;
		zboziContent.getElementsByTagName("img")[0].src = img.src.replace(/\/mini\//, "/thumbs/");

		zboziContent.getElementsByTagName("a")[0].href = a.href;
		zboziContent.getElementsByTagName("a")[1].href = a.href;
	},
	init: function(div)
	{
		var wrapper = document.createElement("div");
		var active = {};
		var n = 0;
		for(var i = 0, a; a = div.firstChild; i++)
		{
			wrapper.appendChild(a);
			if(a.tagName)
			{
				n++;
				a.onmouseover = function()
				{
					active.className = null;
					active = this;
					active.className = "roller-active";
					Roller.change(div, this);
				};
				if(a.className == "roller-active") active = a;
			}
		}
		div.appendChild(wrapper);

		var above = n * Roller.settings.height - Roller.settings.totalHeight;
		if(above > 0)
		{
			var delta = 0;
			var finalDelta = 0;
			var position = 0;
			var arrows = document.createElement("span");
			var move = function()
			{
				delta = delta * (1 - 1 / Roller.settings.persistence) + finalDelta * (1 / Roller.settings.persistence);				
				if(!Math.round(delta) && !finalDelta)
				{
					delta = 0;
					return;
				}
				position = Math.max(Math.min(position + delta, above), 0);
				wrapper.style.marginTop = Math.round(-position) + "px";
				setTimeout(move, 60);
				var arrowStatus = (position != above ? position != 0 ? "" : "roller-most-top" : "roller-most-bottom");
				if(arrows.className != arrowStatus) arrows.className = arrowStatus;
			};
			for(var i = 0; i < 2; i++)
			{
				var arrow = document.createElement("span");
				arrow.className = "roller-arrow roller-arrow-" + ["top", "bottom"][i];
				arrow.delta = [-1, 1][i] * Roller.settings.step;
				arrow.onmouseover = function()
				{
					finalDelta = this.delta;
					if(delta == 0) move();
				};
				arrow.onmouseout = function()
				{
					finalDelta = 0;
				};
				arrows.appendChild(arrow);
			}
			div.appendChild(arrows);
			arrows.className = "roller-most-top";
		}
	}
}

document.documentElement.className = "js";

for(var i = 0, divy = document.getElementsByTagName("div"), d; d = divy[i]; i++)
	if(d.className.substr(0, 6) == "roller") Roller.init(d);


