var ktcrpc = new rpc ();

var ktc = {
	url: '/index/kerrtotalcare-rpc-action',
	action: ktcrpc.action,
	item: false,

	toggle: function (id, name, thumb, qty) {
		ktc.item = id;
		ktcrpc.call (
			this.action ('toggle', [id, name, thumb, qty]),
			function (request) {
				res = eval (request.responseText);
				if (res == 'added') {
					jQuery('#added-' + ktc.item)
						.html ('Item added!')
						.show ()
						.animate ({opacity: 1.0}, 3000)
						.fadeOut ();
				} else {
					jQuery('#added-' + ktc.item)
						.html ('Item removed!')
						.show ()
						.animate ({opacity: 1.0}, 3000)
						.fadeOut ();
				}
				return false;
			}
		);
		return false;
	},

	updateQty: function (id, qty) {
		if (qty == 0) {
			return ktc.removeItem (id);
		}
		ktc.item = id;
		jQuery('#qty-' + ktc.item)
			.html ('Saving...')
			.show ();
		ktcrpc.call (
			this.action ('updateQty', [id, qty]),
			function (request) {
				jQuery('#qty-' + ktc.item)
					.html ('Saved!')
					.show ()
					.animate ({opacity: 1.0}, 3000)
					.fadeOut ();
				return false;
			}
		);
		return false;
	},

	removeItem: function (id) {
		ktc.item = id;
		jQuery('#remove-' + ktc.item).html ('Removing item...');
		ktcrpc.call (
			this.action ('removeItem', [id]),
			function (request) {
				jQuery('#remove-' + ktc.item).html ('Item removed!');
				jQuery('#item-' + ktc.item)
					.animate ({opacity: 1.0}, 3000)
					.fadeOut ();
				return false;
			}
		);
		return false;
	}
};
