// Requires general.js
// Requires geometry.js
// Requires animation.js


function problemo (problem)
{
	var message = id("message");
	message.innerHTML = "";
	
	var paragraph1 = document.createElement("p");
	var message1 = document.createTextNode("We're sorry. The item you chose is ");
	paragraph1.appendChild(message1);
	var emphasized = document.createElement("em");
	var message2 = document.createTextNode(problem);
	emphasized.appendChild(message2);
	paragraph1.appendChild(emphasized);
	message.appendChild(paragraph1);
	
	message.style.height = "auto";
	var messageHeight = getElementDimensions(message).height;
	message.style.height = "0px";
	
	var duration = 0.6;
	var heightAnimation = { attribute: "height",
							startValue: null,
							endValue: messageHeight,
							unit: "px" };
	animateAttributes( message, [ heightAnimation ], duration );
	
	/*
	var errorButton = id("placeInShoppingBagError");
	var throbDuration = 1.0;
	var opacityAnimation = { attribute: "opacity",
							startValue: null,
							endValue: 1.0,
							unit: "" };
	var repeat = { iterations: -1,
				   delay: 0,
				   loop: "bounce" };
	errorButton.style.opacity = 0;
	errorButton.style.visibility = "visible";
	animateAttributes( errorButton, [ opacityAnimation], throbDuration, "ease-out", repeat );
	*/
}

function incompleteMessage(incompleteSelects)
{
	var message = id("message");
	message.innerHTML = "";
		
	for ( var select in incompleteSelects)
	{
		var paragraph = document.createElement("p");
		var emphasized = document.createElement("em");
		var label = id(incompleteSelects[select].id + "Label");
		var selectText = document.createTextNode(label.innerHTML);
		emphasized.appendChild(selectText);
		paragraph.appendChild(emphasized);
		var chosenText = document.createTextNode(" was not chosen.");
		paragraph.appendChild(chosenText);
		message.appendChild(paragraph);
	}
	
	message.style.height = "auto";
	var messageHeight = getElementDimensions(message).height;
	message.style.height = "0px";
	
	var duration = 0.6;
	var heightAnimation = { attribute: "height",
							startValue: null,
							endValue: messageHeight,
							unit: "px" };
	animateAttributes( message, [ heightAnimation ], duration );
}

function updateQuantity (quantity)
{
	var quantitySelect = document.getElementById("quantity");
	
	quantitySelect.options.length = 0; // Reset the quantity select.
	
	if (quantity > 0)
	{
		var i;
		for ( i = 1 ; i <= quantity ; i++ )
		{
			var quantityOption = document.createElement("option");
			quantityOption.value = i + "";
			quantityOption.innerHTML = i;
			quantitySelect.appendChild(quantityOption);
		}
		quantitySelect.disabled = false;
	}
	else if ( quantity == 0 )
	{

		var quantityOption = document.createElement("option");
		quantityOption.value = "0";
		quantityOption.innerHTML = "Not available";
		quantitySelect.appendChild(quantityOption);
		problemo("not available.");
		quantitySelect.disabled = false;
		var helpBox = id("help");
		if ( helpBox && ! helpBox.open )
		{
			helpBox.timeout = setTimeout( showHelpBox, 600.0 );
		}
 	}
	else
	{
		// Opera won't display the Select correctly unless it has at least one option.
		var quantityOption = document.createElement("option");
		quantitySelect.appendChild(quantityOption);
		quantitySelect.disabled = true;
	}
}

function updateOptions ()
{
	var problemMessage = id("message");
	problemMessage.style.height = "0px";
	problemMessage.innerHTML = "";
	
	var helpBox = id("help");
	if ( helpBox && helpBox.open )
	{
		showHelpBox();
	}
	/*
	var errorButton = id("placeInShoppingBagError");
	if ( errorButton && errorButton.animation )
	{
		stopAnimation(errorButton);
		errorButton.style.visibility = "hidden";
	}
	*/

	var variantid = false;
	for (var variant in variants)
	{// Search the variants array for the variant with the current combination of option choices.
		
		if (variants[variant].options.length == 0)
		{// Variant is empty. Skip.
			continue;
		}

		variantid = variant;
		for (var option in variants[variant].options)
		{// Compare each of the variant's option choices to see if they match the selected option choices.	
			var optionSelect = id("po" + option);
			
			var choice;
			if (optionSelect)
			{
				choice = optionSelect.options[optionSelect.selectedIndex].value;
			}

			if ( choice != variants[variant].options[option])
			{
				variantid = false;
				break;
			}
		}

		if (variantid)
		{// Variant identified. Break out of search.
			break;
		}
	}

	var quantity;
	if (variantid)
	{
		quantity = variants[variantid].quantity;
	}
	
	updateQuantity(quantity);	
}

function verifyChoice ()
{
	var quantitySelect = id("quantity");
	
	if (quantitySelect.disabled)
	{
		var optionSelects = id("productOptions").getElementsByTagName("select");

		var incompleteSelects = new Array;
		var i;
		for ( i = 0; i < optionSelects.length; i++ )
		{
			if ( optionSelects[i].selectedIndex == 0 )
			{
				incompleteSelects[incompleteSelects.length] = optionSelects[i];
			}
		}
		
		if ( incompleteSelects.length > 0 )
		{
			incompleteMessage(incompleteSelects);
			return false;
		}
	}
	else if ( quantitySelect.options[quantitySelect.selectedIndex].value == 0 )
	{
		problemo("not available.");
		var helpBox = id("help");
		if ( helpBox && ! helpBox.open )
		{
			helpBox.timeout = setTimeout( showHelpBox, 600.0 );
		}
		return false;
	}
	
	return true;
}

function ieAddToWishList ()
{
	if ( verifyChoice() )
	{
		id("hiddenMode").value = "add2wl";
		document.orderform.submit();
	}
}

function ieThumbnailHighlight ()
{
	this.nextSibling.style.borderColor = "#3c4b52";
}

function ieThumbnailUnhighlight ()
{
	this.nextSibling.style.borderColor = "white";
}


function productDetailsLoad ()
{
	document.orderform.onsubmit = verifyChoice;
	if ( id("productOptions") )
	{
		updateOptions();
	}
	
	
	if ( navigator.appName.search(/Microsoft Internet Explorer/i) >= 0)
	{
		var wishListButton = id("addToWishListButton");
		wishListButton.name = null;
		wishListButton.onclick = ieAddToWishList;
		
		var productThumbnails = id("productThumbnails")
		if (productThumbnails)
		{
			var thumbnailImages = productThumbnails.getElementsByTagName("img");
			var i;
			for ( i = 0 ; i < thumbnailImages.length ; i++ )
			{
				thumbnailImages[i].onmouseover = ieThumbnailHighlight;
				thumbnailImages[i].onmouseout = ieThumbnailUnhighlight;
			}
		}
	}
	

}
