$(document).ready(function () {
	var cartDialogElem = $('<div/>').attr('id','cart_dialog');
  $('body').append(cartDialogElem);
  $('#cart_dialog').dialog({
    title: 'Added To Cart',
    bgiframe: false,
    width: 360,
    position: 'center',
    buttons: {
      "Checkout": function() { location = 'index.php?route=checkout/checkout'; },
      "View Cart": function() { location = 'index.php?route=checkout/cart'; },
      "Continue Shopping": function() { $(this).dialog("close"); }
    },
    height: 170,
    resizable: false,
    modal: false,
    autoOpen: false
  });
});

function addToCart(product_id) {
	$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information, .error').remove();

			if (json['redirect']) {
				location = json['redirect'];
			}

			if (json['error']) {
				if (json['error']['warning']) {
					$('#notification').html('<div class="warning" style="display: none;">' + json['error']['warning'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');

					$('.warning').fadeIn('slow');

					$('html, body').animate({ scrollTop: 0 }, 'slow');
				}
			}

			if (json['success']) {
				$('#cart_total').html(json['total']);

        $('#cart_dialog').html(json['success_dialog']);
        $('#cart_dialog').dialog("open");
			}
		}
	});
}
