﻿var showTopArticles=0;
var currentPosition=0;

function gE(id) {
	return document.getElementById(id);
}

function circleTopArticles(diff)
{
	currentPosition += diff;
	if (currentPosition < 0) currentPosition += topcars.length;
	if (currentPosition >= topcars.length) currentPosition -= topcars.length;
	var num=currentPosition;
	for (var i=0; i<showTopArticles; i++)
	{
		try {
		if (topcars[num])
		{
			gE('tc_img'   + i).src  = topcars[num]['img'];
			gE('tc_link'  + i).href = topcars[num]['link'];
			gE('tc_link_' + i).href = topcars[num]['link'];
			
			var modelElement = gE('tc_link_' + i);
			var newModelElement = document.createTextNode(topcars[num]['model']);
			if (!modelElement.firstChild) 
			{
				modelElement.appendChild(newModelElement);
			} 
			else 
			{
				modelElement.replaceChild(newModelElement, modelElement.firstChild);
			}
			
			var priceElement = gE('tc_price' + i);
			var newPriceElement = document.createTextNode(topcars[num]['price']);
			if (!priceElement.firstChild) 
			{
				priceElement.appendChild(newPriceElement);
			} 
			else 
			{
				priceElement.replaceChild(newPriceElement, priceElement.firstChild);
			}
			
			
			
			
			var dateElement = gE('tc_date' + i);
			var newDateElement = document.createTextNode(topcars[num]['date']);
			if (!dateElement.firstChild) 
			{
				dateElement.appendChild(newDateElement);
			} 
			else 
			{
				dateElement.replaceChild(newDateElement, dateElement.firstChild);
			}
			
			num++;
		}
		} catch (e) { }
		if (num >= topcars.length) num = 0;
	}
}

function initTopArticles(_showTopArticles)
{
	if (gE('tc_img0')==null)
	{
		window.setTimeout('initTopArticles('+_showTopArticles+');',100);
	}
	else
	{
		showTopArticles=_showTopArticles;
		circleTopArticles(0);
	}
}
