/**
 * This is a closure that runs immediately. It shields the $ object from other libraries.
 * It is important that this file be included before others.
 */
(function($) {
	/**
	 * This will change the value of the 'list' parameter in an Add to Cart URL.
	 * This will determine the value of the string that shows up in the popup layer.
	 * It will either say '... has been added to your list' or '... has been added to your cart'.
	 */

	$(document).ready(function() {

		$('a.add-to-cart').click(function(e) {
			e.preventDefault();
			$.log('clicked: '+$(this));

			var a = $(this),
			form = a.parents('form:first'),
			ajaxUrl;
			// This is the CartModifierFormHandler.sendToStore input.
			var input = form.find(':radio:checked');
			// If no radio buttons are checked then this causes a problem.
			// Fire a click event on the radio, and then re-issue the click
			// to $(this).
			if (input.length < 1) {
				form.find('input:radio:enabled:last').click();
			}

			input = form.find(':radio:checked');

			var replaceURLParameter = function(link, value) {
				link.attr('href', link.attr('href').replace(/=list/, ['=', value].join('')));
			};

			if (form.find('input:radio:first').is(':checked')){
				if(storeType === 'list'){
					ajaxUrl = '/autozone/common/shoppingListInfo.jsp';
					replaceURLParameter(a, 'list');
				} else {
					ajaxUrl = '/autozone/common/cartInfo.jsp';
					replaceURLParameter(a, 'list');
				}
			} else {
				ajaxUrl = '/autozone/common/cartInfo.jsp';
				replaceURLParameter(a, 'cart');
			}

			/* Track Add to Cart for Recommendations */
			// get itemid
			jQuery.log(jQuery(e.target).parents('form:first'));
			var itemid = jQuery(e.target).parents('form:first').find('input[name="/atg/commerce/order/purchase/CartModifierFormHandler.catalogRefId"]').val();
			resx.event = "shopping+cart";
			resx.itemid = itemid;
			certonaResx.run();

			jQuery.log(form);

			var goHere = a.attr('href');

			// Submit the form via AJAX
			var myData = form.serialize();
			jQuery.ajax({
				url: ajaxUrl,
				type: 'POST',
				cache: false,
				data: myData,
				dataType: "html",
				error: function(){
					if (a.parents('.product,.contentright').length > 0) {
						a.parents('.product:first,.contentright').prepend('<div class="errorMessages">There was a problem adding this to your cart.</div>');
					}
				},
				success: function(data){
					var d = jQuery(data);
					if (a.parents('.product,.contentright').length > 0) {
						a.parents('.product:first,.contentright').append('<div class="add-to-cart-message centered" style="width:421px;">');
						a.parents('.product:first,.contentright').find('.add-to-cart-message').load(goHere+ ' .rounded', function() {
							applyCorners('#added-to-cart');
						});
					}
					if(d.find('.cart.replaceThis').length > 0){
						$('.cart.replaceThis').replaceWith(d.find('.cart.replaceThis'));
					}
					if(d.find('.list.replaceThis').length > 0){
						$('.list.replaceThis').replaceWith(d.find('.list.replaceThis'));
					}
					form.find('a.add-to-cart').replaceWith('<div class="button-bar-msg-in-cart clearAsMud">IN YOUR CART</div><a href="/autozone/cart/cart.jsp"><img src="/autozone/images/common/btn_checkout.gif" alt="Proceed To Checkout" id="checkoutBtn"></a>');
					// Remove Add To Cart Message after a couple of seconds
					setTimeout(function() {
						jQuery('.product,.contentright').find('.add-to-cart-message').remove();
					}, 2500);

					var productString = jQuery('#shopping-cart-td .replaceThis').find('.productString').text();
					if (productString.length === 0){
						productString = jQuery('#store-list-td .replaceThis').find('.productString').text();
					}
					productString = jQuery.trim(productString);
					js_track.s_addCart(productString);
				}
			});

		});
	});

})(jQuery);
