function ShowTshirtColors (sexid)
{
	if (!Tshirts[sexid]) return;

	for (id in Tshirts) {
		ToggleColors (Tshirts[id], id == sexid);
	}
}

function ToggleColors (colors, state)
{
	for (id in colors)
	{
		var current = document.getElementById ('CH-' + id);
		if (!current) continue;

		current.style.display = state ? 'block' : 'none';
		var input = current.getElementsByTagName('input');
		
		ToggleSizes (colors[id], state && input[0].checked);
	}
}

function ShowTshirtSizes (colorid)
{
	for (sx in Tshirts)
		for (cl in Tshirts[sx]) {
			ToggleSizes (Tshirts[sx][cl], cl == colorid);
		}
}

function ToggleSizes (sizes, state)
{
	for (id in sizes)
	{
		
		var current = document.getElementById ('CH-' + id);
		if (!current) continue;

		current.style.display = state ? 'block' : 'none';
	}
}

function ToggleSubProducts (id, level)
{
	switch (level)
	{
		case 1: 
			ShowTshirtColors (id);
			break;
		case 2: 
			ShowTshirtSizes (id);
			break;
		case 3: 
		//ChangeTshirtSizes (id);
		break;
	}

	ShowPreview();
}

function ShowPreview()
{
	var subproductID = 0;

	searchloop:
	for (sx in Tshirts)
	{
		var sxarea = document.getElementById ('CH-' + sx);
		if (!sxarea || sxarea.style.display == 'none') continue;

		for (cl in Tshirts[sx])
		{
			var clarea = document.getElementById ('CH-' + cl);
			if (!clarea || clarea.style.display == 'none') continue;

			for (sz in Tshirts[sx][cl])
			{
				var szarea = document.getElementById ('CH-' + sz);
				if (!szarea || szarea.style.display == 'none') continue;

				var szradio = document.getElementById ('RA-' + sz);
				if (szradio && szradio.checked)
				{
					subproductID = TshirtSubproductId[sz];						
					break searchloop;
				}
			}
		}
	}

	//alert(subproductID);
	var SubproductHiddenID = document.getElementById ('SubproductHiddenID');
	if (SubproductHiddenID) SubproductHiddenID.value = subproductID;

	var TshirtPreview = document.getElementById ('TshirtPreview');
	if (TshirtPreview && TshirtImages['src' + subproductID] && TshirtImages['src' + subproductID] != '') TshirtPreview.src = TshirtImages['src' + subproductID];

	var AddToChartButton = document.getElementById ('AddToChartButton');	
	if (AddToChartButton) AddToChartButton.value = TshirtSubproductOrderState['ID-' + subproductID] == 0 ? OrderText : PreorderText;
}

function InitVariants()
{

	var firstSx = true;
	for (var sx in Tshirts)
	{
		var radio = document.getElementById ('RA-' + sx);
		if (!radio) continue;
		radio.checked = 
			radio.style &&
			radio.style.display != 'none' &&
			firstSx;
		if (radio.checked) firstSx = false;

		var firstCl = true;
		for (var cl in Tshirts[sx])
		{
			var radio = document.getElementById ('RA-' + cl);
			if (!radio) continue;
			radio.checked = 
				radio.style &&
				radio.style.display != 'none' &&
				firstCl;
			if (radio.checked) firstCl = false;

			var firstSz = true;
			for (var sz in Tshirts[sx][cl])
			{
				var radio = document.getElementById ('RA-' + sz);
				if (!radio) continue;
				radio.checked = 
					radio.style &&
					radio.style.display != 'none' &&
					firstSz;
				if (radio.checked) firstSz = false;
			}
		}
	}

	//CurrentTshirtInit();
	RandomTshiftInit();
}

InitVariants();

window.onload = function()
{
	ShowPreview();
	var AddToChartButton = document.getElementById ('AddToChartButton');
	if (AddToChartButton) AddToChartButton.disabled = false;	
}

function RandomTshiftInit()
{
	var size = 0;
	for (var x in Tshirts) size++;
	var pos = parseInt (size * Math.random());

	var randSx = null;
	for (x in Tshirts) if (!pos--)
	{
		randSx = x;
		break;
	}

	size = 0;
	for (x in Tshirts[randSx]) size++;
	pos = parseInt (size * Math.random());

	var randCl = null;
	for (x in Tshirts[randSx]) if (!pos--)
	{
		randCl = x;
		break;
	}

	ShowTshirtColors (randSx);
	ShowTshirtSizes (randCl);

	var radioSx = document.getElementById ('RA-' + randSx);
	var radioCl = document.getElementById ('RA-' + randCl);

	if (radioSx) radioSx.checked = true;
	if (radioCl) radioCl.checked = true;
}

