var tree = [];

function catTree(val,tmp,a,x,i)
{
	if(val === -1 || val === '-1')
	{
		return false;
	}
	document.getElementById('advmentCat').value = val; tmp = val; unsetAdvmentPremium();
	a = ['<li><a href="#" onclick="return catTree(\''+val+'\');">'+tree[val][0]+'</a></li>'];
	while(tmp = findParent(tmp))
	{
		a[a.length] = '<li><a href="#" onclick="return catTree(\''+tmp+'\');">'+tree[tmp][0]+'</a></li>';
	}
	a.reverse();
	a = a.join('');
	if(tree[val] && tree[val][1] === null)
	{
		a += '<li><select onchange="return setAdvmentPremium(this.options[this.selectedIndex].value);">';
		a += '<option value="0">-- wybierz typ ogłoszenia --</option>';
		a += '<option value="1">Ogłoszenie Standard</option>';
		a += '<option value="2">Ogłoszenie Premium</option>';
	}
	else
	{
		a += '<li><select onchange="return catTree(this.options[this.selectedIndex].value);">';
		if(tmp = findParent(val))
		{
			a += '<option value="-1">-- wybierz podkategorię --</option>';
			a += '<option value="'+tmp+'">-- wróć do ostatniej kategorii --</option>';
		}
		else
		{
			a += '<option value="-1">-- wybierz kategorię --</option>';
		}
		for(i in x = tree[val][1].split('|').slice(1,-1))
		{
			a += '<option value="'+x[i]+'">'+tree[x[i]][0]+'</option>';
		}
	}
	a += '</select></li>';
	document.getElementById('category-path').innerHTML = a;
	return false;
}

function findParent(val,i)
{
	if(val === '0')
	{
		return false;
	}
	for(i in tree)
	{
		if(tree[i][1] && tree[i][1].indexOf('|'+val+'|') != -1)
		{
			return i;
		}
	}
	return false;
}

function setAdvmentPremium(val)
{
	document.getElementById('advmentPremium').value = val;
	document.getElementById('category-path').innerHTML = document.getElementById('category-path').innerHTML.substring(0, document.getElementById('category-path').innerHTML.toLowerCase().lastIndexOf('<li>'))+'<li class="advmentPremium">'+((val == '1') ? 'Ogłoszenie Standard' : 'Ogłoszenie Premium')+'</li>';
	document.getElementById('prev-step').style.display = 'none';
	document.getElementById('next-step').style.display = 'block';
	return false;
}

function unsetAdvmentPremium()
{
	document.getElementById('advmentPremium').value = '';
	if(document.getElementById('prev-step')) document.getElementById('prev-step').style.display = 'block';
	if(document.getElementById('next-step')) document.getElementById('next-step').style.display = 'none';
	return false;
}