//products.js
//Product viewer script.
//Copyright David Thomson at Hundredth Codemonkey.


//Initialises the products display page.
function initialiseProductsPage()
{
	updateProductDisplay(1, true);
}

//Updates the product display navigatin and content.
// - nDisplayProduct:			New product to display
// - bInitialise:				Gallery initialisation flag
// * Return:					VOID
function updateProductDisplay(nDisplayProduct, bInitialise)
{
	//Update holder value.
	document.getElementById('curr_product').value = nDisplayProduct.toString();

	//Update product navigation.
	var sNumProducts = document.getElementById('num_products').value;
	var nNumProducts = parseInt(sNumProducts, 10);

	//Back button.
	var oProductBack = document.getElementById('imgBack');
	if (nDisplayProduct == 1)
	{
		//Replace link with a b tag.
		var oNewProductBack = document.createElement("b");
		oNewProductBack.id = 'imgBack';
		oNewProductBack.appendChild(document.createTextNode(""));
		oProductBack.parentNode.replaceChild(oNewProductBack , oProductBack);
	}
	else
	{
		var nProductBack = nDisplayProduct - 1;

		//If the back button is a b tag replace it with a link.
		if (oProductBack.tagName == 'B')
		{
			var oNewProductBack = document.createElement("a");
			oNewProductBack.id = 'imgBack';
			oNewProductBack.href = '#';
			oNewProductBack.appendChild(document.createTextNode(String.fromCharCode(171)));
			oNewProductBack.appendChild(document.createElement("br"));
			oNewProductBack.appendChild(document.createTextNode("Back"));
			oProductBack.parentNode.replaceChild(oNewProductBack , oProductBack);
			oNewProductBack.onclick = function(){return updateProductDisplay(nProductBack, false); return false;};
		}
		//Otherwise simple update its event handler.
		else
		{
			//sProductBack = toString(nProductBack);
			oProductBack.onclick = function(){return updateProductDisplay(nProductBack, false); return false;};
		}
	}

	//Next Button.
	var oProductNext = document.getElementById('imgNext');
	if (nDisplayProduct == nNumProducts)
	{
		//Replace link with a b tag.
		var oNewProductNext = document.createElement("b");
		oNewProductNext.id = 'imgNext';
		oNewProductNext.appendChild(document.createTextNode(""));
		oProductNext.parentNode.replaceChild(oNewProductNext , oProductNext);
	}
	else
	{
		var nProductNext = nDisplayProduct + 1;

		//If the back button is a b tag replace it with a link.
		if (oProductNext.tagName == 'B')
		{
			var oNewProductNext = document.createElement("a");
			oNewProductNext.id = 'imgNext';
			oNewProductNext.href = '#';
			oNewProductNext.appendChild(document.createTextNode(String.fromCharCode(187)));
			oNewProductNext.appendChild(document.createElement("br"));
			oNewProductNext.appendChild(document.createTextNode("Next"));
			oProductNext.parentNode.replaceChild(oNewProductNext , oProductNext);
			oNewProductNext.onclick = function(){return updateProductDisplay(nProductNext, false); return false;};
		}
		//Otherwise simple update its event handler.
		else
		{
			//sProductNext = toString(nProductNext);
			oProductNext.onclick = function(){return updateProductDisplay(nProductNext, false); return false;};
		}
	}

	//Update product display.
	var aDivTags = document.getElementsByTagName('div');
	var nProductDisplayArea = 1;
	for (i = 0, j = aDivTags.length; i < j; i++)
	{
		//Match div tags of product display areas.
		var sDivId = aDivTags[i].id;
		if (sDivId.indexOf('product_') != -1)
		{
   			//If the area the current display value show it.
			if (nProductDisplayArea == nDisplayProduct)
				aDivTags[i].className = 'product_show';
			//Otherwise hide it.
			else
			{
				aDivTags[i].className = 'product_hide';
				if (bInitialise)
					aDivTags[i].style.display = 'none';
			}

			nProductDisplayArea++;
		}
	}

	//Smooth fade in and out onclick.
	if (!bInitialise)
	{
		$('.product_hide:visible').fadeOut('normal', function(){$('.product_show').fadeIn('normal');});
	}

}

