$(function() { $('a.add_item').click(function(){ var el = $(this); var token = $(this).attr('rel'); $.ajax({ type: 'POST', url: '/store/add/', dataType: 'json', data: {'token': token}, success: function(data) { if (data.success) { el.addClass('none'); el.next('span').removeClass('none'); var price = $('.cart_price').text(); var item_price = $('.item_price').text(); price = parseFloat(price) + parseFloat(item_price); $('.cart_price').text(price); } else { // el.next().next('div').empty(); // $.each(data.errors, function(key, value) { // el.next().next('div').html('
' + value); // }); } }, error: function() { // el.next().next('div').text('Error'); } }); return false; }); $('a[rel="#showCart"]').overlay({ expose: { color: '#666', loadSpeed: 200, opacity: 0.7 }, effect: 'apple', onBeforeLoad: function(event) { updateCart(); } }); $('#flushCart').click(function(e) { flushCart(); return e.preventDefault(); }); $('.deleteItem').live('click', function(e) { deleteItem($(this).attr('href'), $(this).attr('rel')); return e.preventDefault(); }); }); function updateCart() { $.ajax({ type: 'POST', url: '/store/cart/', dataType: 'json', success: function(data) { var cartContent = ''; if (data.length) { cartContent += '' var total = 0; $.each(data, function(key, value) { cartContent += ''; cartContent += ''; cartContent += ''; cartContent += ''; total = total + Math.round(value['price']); $('#cart').css('display', 'inline'); }); if (total > 0) { cartContent += ''; } cartContent += '
Delete item ' + value['name'] + '$' + value['price'] + '

Total:   

$' + total + '
'; $('.cart_price').text(total); } else { $('.cart_price').text(0); cartContent = 'Your shopping cart is empty!'; // $('a[rel="#showCart"]').each(function() { // $(this).overlay().close(); // }); } $('#cartContent').html(cartContent); }, error: function() { $('#response').html('Error'); } }); } function flushCart() { $.ajax({ url: '/store/flush/', dataType: 'json', success: function(data) { //$('#response').html(data); //$('#cart').css('display', 'none'); $('.cart_price').text(0); $('.add_item').each(function() { $(this).removeClass('none'); $(this).next('span').addClass('none'); }); $('a[rel="#showCart"]').each(function() { $(this).overlay().close(); }); }, error: function() { // $('#response').html('Error'); } }); } function deleteItem(deleteUrl, token) { $.ajax({ url: deleteUrl, dataType: 'json', success: function(data) { $('a[rel='+token+']').each(function(){ if($(this).hasClass('add_item')) { $(this).removeClass('none'); $(this).next('span').addClass('none'); } }); //$(this).addClass('none'); updateCart(); }, error: function() { $('#response').html('Error'); } }); }