(function($) { var ShoppingCart = window.ShoppingCart = function() { var errorMessage = ""; var cartAmount = ""; var cartItems = ""; return { addItem : function(itemNumber, quantity) { if ($("#uc-content").is(":visible")) { $("#uc-content").stop().remove(); } var params = "itemNumber=" + itemNumber + "&quantity=" + quantity; $.ajax({ type: "POST", url: "//www.swstrings.com/shopping/cart/add", data: params, async: false, dataType: "json", success: function (data) { errorMessage = data.errorMessage; }, error: function(XMLHttpRequest, textStatus, errorThrown) { errorMessage = "An Error Occurred while adding the item to your cart."; } }); ShoppingCart.summary.update(); }, removeItem : function(itemNumber) { $.ajax({ type: "POST", url: "//www.swstrings.com/shopping/cart/remove", data: {itemNumber:itemNumber}, async: false, dataType: "json", success: function (data) { errorMessage = data.errorMessage; }, error: function(XMLHttpRequest, textStatus, errorThrown) { errorMessage = "An Error Occurred while removing the item from your cart."; } }); $("#mc-cart-amount").html(ShoppingCart.summary.getCartAmount()); ShoppingCart.summary.update(); }, error : function() { return errorMessage }, showCart : function() { // If already visible, leave var path = $(location).attr("href"); if ($("#uc-content").is(":visible") || path.match(/cart$/)) { return; } ($("#wrapper").append("
")); $("#uc-content").append(""); $("#uc-content").append("
Loading Shopping Cart...
").slideToggle("slow", function(){ var timestamp = new Date().getTime(); $.ajax({ type: "GET", url: "//www.swstrings.com/shopping/cart/mini", async: false, dataType: "html", data: {ts:timestamp}, success: function (data) { $("#uc-content").html(data); }, error: function(XMLHttpRequest, textStatus, errorThrown) { errorMessage = "An Error Occurred while adding the item to your cart."; } }); }); }, hideCart : function() { $("#uc-content").fadeOut("slow", function() { $(this).remove(); }); }, summary : { update : function() { var timestamp = new Date().getTime(); $("#us-cart").slideToggle("slow", function(){ $.get("//www.swstrings.com/shopping/cart/summary", {ts:timestamp}, function(data) { var path = window.location.pathname; cartItems = data.itemCount; cartAmount = data.cartAmount; $("#us-cart #cart-items").text(cartItems); $("#us-cart #cart-amount").text(cartAmount); if ($("#uc-content").is(":visible") || path.match(/cart$/)) { $("#uc-content #mc-cart-amount").fadeOut("slow", function(){$("#mc-cart-amount").text(cartAmount).fadeIn("slow");}); }; }, "json"); }).slideToggle("slow"); }, getCartAmount : function() { return cartAmount; } }, autoHide : function() { $("#uc-content").mouseleave(function() { ShoppingCart.hideCart(); }); } }; }($); })($);