(function($) {
	
	// cart form submission (e.g. cart, wishlist)
	$.fn.submitCartForm = function(formContainer, action, errorFunction, successFunction) {
		var form = formContainer.find('form');
		form.hide();
		var loadingMsg = formContainer.find('.loading-image');
		loadingMsg.css('height', form.height());
		loadingMsg.css('width', form.width());
		loadingMsg.show();
		$.ajax({
			url: action,
			data: form.serialize(),
			type: 'POST',
			complete: function(html) {
				loadingMsg.hide();
				if($(html.responseText).find('.errorList, .error-message, .formError').length){
					//there was an error
					var validatedForm = $(html.responseText).find('form.order-form');
					formContainer.find('.form-container').html(validatedForm);
					errorFunction();
				}else{
					//If this is the product detail page, reload the form to clear any previous errors
					if ($('div.product-detail').length) {
						var validatedForm = $(html.responseText).find('form.order-form');
						formContainer.find('.form-container').html(validatedForm);
					}
					successFunction();
				}		
			}
		});
	};
	
	// inventory notification form submission
	$.fn.submitInventoryForm = function(productId, formContainer, action, outOfStockList) {
		formContainer.find('form').hide();
		var loadingMsg = formContainer.find('.loading-image');
		loadingMsg.css('height', formContainer.find('form').height());
		loadingMsg.css('width', formContainer.find('form').width());
		loadingMsg.show();
		$.ajax({
			url: action,
			data: formContainer.find('form').serialize(),
			success: function(html) {
				loadingMsg.hide();
				var validatedForm = $(html).find('form.order-form');
				formContainer.find('.form-container').html(validatedForm);
			},
			complete: function(html) {
				$.fn.inventory(productId, true, formContainer, outOfStockList);
				$.fn.colorbox.resize();
			}
		});
	};
	
	$.fn.attributeButton = function() {
		var button = $(this);
		var attrValue = button.find('span').text();
		//var attrValue = button.val(); can't use this because IE7/IE6 does not support it
		button.parentsUntil('ul').find('input[name=' + button.attr('name') + ']').val(attrValue);
		button.parentsUntil('ul').find('button.selected').removeClass('selected');
		button.addClass('selected');
		button.blur();
		
		// select coordinating select box option
		var option = button.parentsUntil('ul').find('option[value=' + attrValue + ']');
		option.parentsUntil('ul').find('option.selected').removeClass('selected');
		option.addClass('selected');
		option.attr('selected', 'selected');
	};
	
	$.fn.attributeSelectOption = function() {
		var currOption = $(this);
		currOption.parent().find('.selected').removeClass('selected');
		currOption.addClass('selected');
		
		// if this is the color select box
		if (currOption.hasClass('color-swatch')) {
			var attrValue = currOption.val();
			currOption.parent().find('input[name=' + currOption.attr('name') + ']').val(attrValue);
			if (attrValue == "") {
				//clear selection
				var buttons = currOption.parent().find('button');
				buttons.removeClass('selected');
			} else {
				//select coordinating button
				var button = currOption.parent().find('button[class *=' + attrValue + ']');
				button.parent().find('button.selected').removeClass('selected');
				button.addClass('selected');
			}
		}
	};
	
})(jQuery)

