CrossSells = { msgAdd: null, msgRemove: null, init: function() { if($("div#crossSells").length > 0) { CrossSells.initMessages(); CrossSells.initAjax(); CrossSells.initAddRemoveButtons(); CrossSells.initAddRemoveItemForms(); } }, initMessages: function() { if(CrossSells.msgAdd == null || CrossSells.msgRemove == null) { CrossSells.msgAdd = $("#msgAdd").val(); CrossSells.msgRemove = $("#msgRemove").val(); } }, initAjax: function() { $.ajaxSetup({ cache : false }); }, initAddRemoveButtons: function() { $("div#crossSells").find("a.btn-primary").click(function() { if($(this).parents("div.rounded-box").hasClass("highlighted")) { var removeForm = $("form#removeCrossSellFromOrder"); removeForm.find(".product-id").val($(this).data("prod-id")); removeForm.find(".sku-id").val($(this).data("sku-id")); removeForm.find(".commerce-id").val($(this).data("comm-id")); $("#submitRemoveCrossSell").click(); } else { var addForm = $("form#addCrossSellToOrder"); addForm.find(".product-id").val($(this).data("prod-id")); addForm.find(".sku-id").val($(this).data("sku-id")); $("#submitAddCrossSell").click(); } return false; }); }, initAddRemoveItemForms: function() { $("form#addCrossSellToOrder").ajaxForm({ async: true, dataType: 'json', success: function (data) { if(data.success == '1') { CrossSells.highlightItem(data.productId, data.commerceId); CrossSells.updateBasketCount(data.totalItems); } }, error: function (jqXHR, textStatus, errorThrown) { } }); $("form#removeCrossSellFromOrder").ajaxForm({ async: true, dataType: 'json', success: function (data) { if(data.success == '1') { CrossSells.dehighlightItem(data.productId); CrossSells.updateBasketCount(data.totalItems); } }, error: function (jqXHR, textStatus, errorThrown) { } }); }, highlightItem: function(productId, commerceId) { var link = $("a.btn-primary[data-prod-id='"+productId+"']"); link.data("comm-id", commerceId); link.parents("div.rounded-box").addClass("highlighted"); link.html(CrossSells.msgRemove); }, dehighlightItem: function(productId) { var link = $("a.btn-primary[data-prod-id='"+productId+"']"); link.parents("div.rounded-box").removeClass("highlighted"); link.html(CrossSells.msgAdd); }, updateBasketCount: function(totalCount) { $("span.basket-count").html(totalCount); } } $(document).ready(function() { CrossSells.init(); });