
function switchDetail(button, index) {
	$$('.detailPicture').invoke('addClassName', 'hidden');
	Element.removeClassName($('detailImage_' + index), 'hidden');
	$('detailLinks').select('a').invoke('removeClassName', 'current');
	Element.addClassName(button, 'current');
	$('detailBlurb').innerHTML = detailImageText[index];
}

function subscribeProduct(id) {
	var aR = new Ajax.Request('/productInfoController.php',
		{
			method: 'post',
			parameters: { subscribeProduct: id },
			onSuccess: function(transport, json) {
				Element.addClassName($('subAdd'), 'hidden');
				Element.removeClassName($('productSubscribed'), 'hidden');
				Element.removeClassName($('subRemove'), 'hidden');				
			}
		}
	);
}

function unSubscribeProduct(id) {
	var aR = new Ajax.Request('/productInfoController.php',
		{
			method: 'post',
			parameters: { unSubscribeProduct: id },
			onSuccess: function(transport, json) {
				Element.addClassName($('subRemove'), 'hidden');
				Element.addClassName($('productSubscribed'), 'hidden');
				Element.removeClassName($('subAdd'), 'hidden');				
			}
		}
	);
}



function optionChange(id) {
	var options = $$('.optionSelect');
	var optionsValues = new Array();
	options.each(function(s) {
		optionsValues.unshift($H({
			optionID: s.id.substr(7),
			valueID: s.options[s.selectedIndex].value
		}));
	});

	var productShowPrice = productBasePrice;
	if (productOldPrice != null) {
		var productShowOldPrice = productOldPrice;
	}
	var productStockIDSansSize = new Array();
	var productStockID = new Array();

	optionsValues.each(function(s) {
		ovObject = s.toObject();
		//s = s.toObject();
		productShowPrice = eval(productShowPrice + productPrice[ovObject.optionID][ovObject.valueID]); // calculate price
		if (productShowOldPrice != null) {
			productShowOldPrice = eval(productShowOldPrice + productPrice[ovObject.optionID][ovObject.valueID]); // calculate price			
		}
		productStockID.push(ovObject.optionID + '-' + ovObject.valueID);
		if (ovObject.optionID != 1) { // ugh
			productStockIDSansSize.push(ovObject.optionID + '-' + ovObject.valueID);
		}
	});

	// format price and change price display
	var productShowPrice = new Number(productShowPrice);

	/*
	// oh god what was I thinking here
	productShowPrice = productShowPrice.toString();
	var priceSplit = productShowPrice.split(".", 2);

	var priceDollars = priceSplit[0];
	var priceCents = priceSplit[1];

	if (priceCents && priceCents.length == 1) {
		priceCents = priceCents + '0';
	} else if (!priceCents) {
		priceCents = '00';
	}

	if (priceCents.length > 2) {
		//priceCents = priceCents.substr(2);		
		//priceCents = Math.round(parseInt(priceCents));
		
	}
	*/
	
	// update price
	//$('productPrice').innerHTML = '$' + priceDollars + '.' + priceCents;
	$('productPrice').innerHTML = '$' + productShowPrice.toFixed(2);

	// if this product is on sale, there'll be productOldPrice, better update that too
	if (productShowOldPrice != null) {
		var oldPrice = new Number(productShowOldPrice);

		// update price
		$('oldPrice').innerHTML = '$' + oldPrice.toFixed(2);
		
	}

	
	// clear classes on size list
	// go through sizes and change classes based on stock
	/* // do I really need this? I think not. we don't have multiples
	$('sizesList').immediateDescendants().each(function(s) {
		var sizeValueID = s.id.substr(5);
		productStockIDSansSize.unshift('1-' + sizeValueID);

		productStock.each(function(a) {
			if (a.attributes == productStockIDSansSize.join(',')) { // oh god ow
				if (a.quantity == 0) {
					// item out of stock
					Element.addClassName(s, 'noStock');					
				} else {
					// item in stock
					Element.removeClassName(s, 'noStock');
				}
			}
		});
		productStockIDSansSize.shift(); // yukk
	});
	*/

	var stockExists = false;
	productStock.each(function(a) {
		var stockObject = a.toObject();
		if (stockObject.attributes == productStockID.join(',')) {
			stockExists = true;
		}
	});
	
	// is this current size in stock?

	if (stockExists == true) {
		productStock.each(function(a) {
			var stockObject = a.toObject();
			if (stockObject.attributes == productStockID.join(',') && stockExists == true) {
				if (stockObject.quantity >= 1) {
					// this is in stock
					Element.addClassName($('productOutOfStock'), 'hidden');
					Element.removeClassName($('productPriceContainer'), 'noStock');
					Element.removeClassName($('addToCartButton'), 'disabled');
					$('addToCartButton').enable();
				} else {
					// this isn't in stock
					Element.removeClassName($('productOutOfStock'), 'hidden');
					Element.addClassName($('productPriceContainer'), 'noStock');
					Element.addClassName($('addToCartButton'), 'disabled');
					$('addToCartButton').disable(); // disable this button so we can't add to cart				
				}
			}
		});
	} else {
		// this isn't in stock
		Element.removeClassName($('productOutOfStock'), 'hidden');
		Element.addClassName($('productPriceContainer'), 'noStock');
		Element.addClassName($('addToCartButton'), 'disabled');
		$('addToCartButton').disable(); // disable this button so we can't add to cart						
	}
}

function infoSwitch(button, target) {
	$('productTabs').immediateDescendants().invoke('removeClassName', 'current');
	Element.addClassName(button, 'current');
	Element.addClassName($('productMetaContainer'), 'hidden');
	Element.addClassName($('productReviewsContainer'), 'hidden');
	Element.removeClassName($(target), 'hidden');
}




