// JavaScript Document

function switchProduct(productID) {
	if (productID > -1 && productID != lastProductID)
	{
		hideProduct(lastProductID);
		displayProduct(productID);
		lastProductID = productID;
	}
}

function displayProduct(productID) {
	var currentProduct = getProduct(productID);
	if (currentProduct != null)
	{
		document.getElementById("quantity").value = 1;
		document.getElementById("quantity").style.visibility = "visible";
		document.getElementById("submitButton").style.visibility = "visible";
		document.images["thumbnail"].src = currentProduct.thumbnail.src;
		document.images["fullsize"].src = currentProduct.fullsize.src;
		var currentOptions = getOptions(productID);
		for (var i = 0; i < currentOptions.length; i++)
		{
			createFormItems(currentOptions[i]);
		}
		playProductQuote(currentProduct.productID);
	}
}

function hideProduct() {
	soundManager.stopAll();
	document.images["thumbnail"].src = "transparent.gif";
	document.images["fullsize"].src = "transparent.gif";
	for (var i = 0; i < optionElems.length; i++)
	{
		removeFormItems(optionElems[i]);
	}
	optionElems.length = 0;
	document.getElementById("quantity").style.visibility = "hidden";
	document.getElementById("submitButton").style.visibility = "hidden";
}

function getProduct(productID){
	for (var i = 0; i < products.length; i++)
	{
		if (products[i].productID == productID)
			return products[i];
	}
}

function getOptions(productID){
	var currentOptions = new Array();
	for (var i = 0; i < productOptions.length; i++)
	{
		if (productOptions[i].productID == productID)
			currentOptions = currentOptions.concat(productOptions[i]);
	}
	return currentOptions;
}

function createFormItems(currentOption)
{
	var sibling = document.getElementById("quantity");
	var newElem = document.createElement("select");
	newElem.setAttribute("name", "options[]");
	var elem = document.getElementById("productOptions").insertBefore(newElem,sibling);
	optionElems[optionElems.length] = elem;
	elem.options.length = 0;
	var currentIds = currentOption.ids.split(',');
	var currentValues = currentOption.values.split(',');
	for (i = 0; i < currentIds.length; i++)
	{
		elem.options[i] = new Option(currentValues[i], currentIds[i], false, false);
	}
}

function removeFormItems(currentOption)
{
	currentOption.parentNode.removeChild(currentOption);
}

function startAnimation()
{
	document.getElementById("jesusRest").style.display = "none";
	document.getElementById("jesusSpeaks").style.display = "block";
}

function stopAnimation()
{
	document.getElementById("jesusSpeaks").style.display = "none";
	document.getElementById("jesusRest").style.display = "block";
}

function playProductQuote(productID) 
{
	soundManager.play("product" + productID);
}

function product(productID, imgThumb, imgFull, quipAudio) {
	this.productID = productID;
	this.thumbnail = new Image();
	this.thumbnail.src = "../products/" + imgThumb;
	this.fullsize = new Image();
	this.fullsize.src = "../products/" + imgFull;
	this.quipAudio = "../audio/" + quipAudio;
}

function productOption(productID, sortOrder, name, ids, values) {
	this.productID = productID;
	this.sortOrder = sortOrder;
	this.name = name;
	this.ids = ids;
	this.values = values;
}
