// JavaScript Document
jQuery(document).ready(function(){
	jQuery('.prod-listbox-details-button button').click(
		function()
		{
			var parentRow = jQuery(this).parents('.prod-listbox');
			var isVisible = jQuery('.prod-listbox-itemdetails:visible', parentRow).length > 0;
			if(isVisible)
			{
				jQuery(this).html('DETAILS');
				jQuery('.prod-listbox-itemdetails', parentRow).animate({height:'hide', opacity:'hide'},'fast');
			}
			else
			{
				jQuery(this).html('HIDE');
				jQuery('.prod-listbox-itemdetails', parentRow).animate({height:'show', opacity:'show'},'fast');
			}
		}
	);
	
	jQuery('.cartUpdateForm').submit(
		function()
		{
			var theForm = jQuery(this);
			var productId = jQuery('.itemId', theForm).attr('value');
			var isClay = jQuery('.isClay', theForm).length > 0;
			var isMasonStain = jQuery('.isMasonStain', theForm).length > 0;
			var qtyBox = jQuery('.prod-listbox-qty input[name=qty]', theForm);
			var qty = (isMasonStain) ? parseFloat(qtyBox.attr('value')) : parseInt(qtyBox.attr('value'));
			var qtyReminder = '';

			if(isClay && qty % 25 != 0)
			{
				var remainder = qty % 25;
				qty = qty + (25 - remainder);
				qtyBox.attr('value', qty);
				qtyReminder = 'The quantity for the clay you added to your cart has been adjusted to the next highest multiple of 25.  Please note that clays must be purchased in 25 lb. increments.';
			}

			if(isMasonStain && qty % .25 != 0)
			{
				var remainder = ((qty * 100) % 25) / 100;
				qty = qty + (.25 - remainder);
				qtyBox.attr('value', qty);
				qtyReminder = 'The quantity for the mason stain you added to your cart has been adjusted to the next highest multiple of 0.25.  Please note that mason stains must be purchased in 0.25 lb. increments.';
			}

			var submitButton = jQuery('.prod-listbox-cart-button button', theForm);
			if(isNaN(productId))
			{
				displayAjaxError('Invalid product selected');
				return false;
			}
			else if(isNaN(qty))
			{
				displayAjaxError('Invalid quantity entered');
				return false;
			}
			
			//get all the fields
			var attributes = jQuery('.attribute-values', theForm);
			var cart_values = '';
			var attrib_error = false;
			if(attributes.length > 0)
			{
				attributes.each(function(){
					var curVal = jQuery(this).attr('value');
					if(isNaN(curVal) || curVal == '')
					{
						attrib_error = true;
						jQuery(this).css('border', '2px solid #f00');
					}
					else
					{
						jQuery(this).css('border', '');
						cart_values += '&'+jQuery(this).attr('name')+'='+curVal;
					}
				});
				if(attrib_error)
				{
					var isVisible = jQuery('.prod-listbox-itemdetails:visible', theForm).length > 0;
					
					jQuery('.prod-listbox-details-button button', theForm).html('HIDE');
					jQuery('.prod-listbox-itemdetails', theForm).animate({height:'show', opacity:'show'},'fast');
					
					displayAjaxError('One or more attributes have not been selected');
					
					return false;
				}
			}
			
			submitButton.html('<img src="/images/updating-cart.gif" style="display:inline; " />');
			jQuery.ajax({
				url: '/catalog_ajax.php?ajax=updateCart&id='+productId+'&qty='+qty+cart_values,
				dataType: 'json',
				success: function(json)
				{
					if(json.error && json.error != '')
					{
						displayAjaxError(json.error);
					}
					else
					{
						submitButton.html('UPDATED!');
						if(json.status == 'removed')
						{
							setTimeout(function() { submitButton.html('ADD TO CART'); }, 1500);
						}
						else
						{
							setTimeout(function() { submitButton.html('UPDATE CART'); }, 1500);
						}
						
						if(!isNaN(json.numItemsInCart))
						{
							jQuery('#cart a').html(json.numItemsInCart+' item'+((json.numItemsInCart == 1) ? '' : 's')+' in your cart');
							if(json.numItemsInCart > 0)
							{
								if(qtyReminder != '')
								{
									var buttonHtml = '<div style="width:370px; float:left; text-align:left;">'+qtyReminder+'</div><input onclick="javascript:window.location=\'http://www.kentuckymudworks.com/catalog_check_out.php\';" type="button" name="null" value="" class="btnCheckoutNow" /><div style="clear:both;"></div>';
									jQuery('#checkout-button-top > div, #checkout-button-bottom > div').html(buttonHtml);
								}
								else
								{
									jQuery('#checkout-button-top > div, #checkout-button-bottom > div').html('<input onclick="javascript:window.location=\'http://www.kentuckymudworks.com/catalog_check_out.php\';" type="button" name="null" value="" class="btnCheckoutNow" />');
								}
								jQuery('#checkout-button-top:hidden, #checkout-button-bottom:hidden').slideDown('fast');
							}
							else
							{
								jQuery('#checkout-button-top:visible, #checkout-button-bottom:visible').slideUp('fast');
							}
						}
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown)
				{
					displayAjaxError('An error has occurred:\n'+textStatus+'\n'+errorThrown);
				}
			});
			return false;
		}
	);
	jQuery('#ajaxError .close-error').click(function(){jQuery('#ajaxError, #errorBlanket').animate({opacity:'hide'}, 'fast');});
});

function displayAjaxError(error)
{
	var errorBox = jQuery('#ajaxError');
	var errorBlanket = jQuery('#errorBlanket:hidden');
	var docHeight = jQuery(document).height();
	if(errorBox.filter(':visible') > 0)
	{
		errorBox.animate({opacity:'hide'}, 'fast');
	}
	jQuery('.error-text', errorBox).text(error);
	errorBox.css('top', (getScrollHeight()+200)+'px').animate({opacity:'show'}, 'fast');
	
	errorBlanket.css('height', docHeight+'px').animate({opacity:'show'}, 'fast');
}

function getScrollHeight() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}