var gCurrentCategory = null;

function displayCategory(index)
{
	if (gCurrentCategory)
	{
		if (gCurrentCategory == index)
		{
			hideCategory(index);
		}
		else
		{
			hideShowCategory(index);
		}
	}
	else
	{
		showCategory(index);
	}
}

function hideCategory(index)
{
	$("#productlist"+index).hide(800, function(){
		scrollWindow(0);
	});
	gCurrentCategory = null;
}

function hideShowCategory(index)
{
	if (gCurrentCategory)
	{
		$("#productlist"+gCurrentCategory).hide(800, function(){
			showCategory(index);
		});
	}
	else
	{
		showCategory(index);
	}
}

function showCategory(index)
{
	if (index)
	{
		$("#productlist"+index).show(800, function(){
			scrollWindow(index);
		});
		gCurrentCategory = index;
	}
}

function scrollWindow(index)
{
	if (index)
	{
		var arPos = findPos(document.getElementById("catHeader"+index));
		$.scrollTo(''+arPos[1]+'px',800);
	}
	else
	{
		$.scrollTo('0px',800);
	}
}

function findPos(obj)
{
	var curleft = curtop = 0;

	if (obj.offsetParent)
	{
		do
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}

	return [curleft,curtop];
}

function addToCart(index)
{
	var qty = document.getElementById('qty' + index);
	if ((qty.value == "") || (qty.value < 0))
	{
		alert("Please enter a quantity");
		qty.focus();
	}
	else
	{
		var productId = document.getElementById('productId' + index);
		location.replace(location.href + "?cart_activity=add&pid=" + productId.value + "&qty=" + qty.value);
	}
}
