var cart_progress = false;
var belt_timer = null;

wmc_cart_add = function(e) {
		e.stop();
		if (cart_progress === true) return;
		cart_progress = true;
		clearTimeout(belt_timer);
		
		info = $('basket-info-template').clone();
		info.set('opacity', 0);
		info.removeClass('hid');
		info.inject($(document.body), 'inside');
		$$('div.baasket-info-finish').destroy();

		function info_close() {
			new Fx.Tween(info).start('opacity', 0);
			return false;
		}

		info.getElement('a.close').addEvent('click', info_close);
		info.addEvent('click', info_close);

		new Fx.Tween(info).start('opacity', 0.9);
		new Request.JSON({
			'url': $(this).get('action') + '?ajax=1',
			'onSuccess': function(responseJSON) {
				$('basket-wrapper').set('html', responseJSON.html);

				(function() {
					info.getElement('span.loading').addClass('hid');
					info.getElement('span.finish').removeClass('hid');
					info.addClass('baasket-info-finish');
					info.addClass('baasket-info-finish');
					var html = '';
					if(responseJSON.result) {
						html = info.getElement('span.finish').get('html').substitute({'product': responseJSON.product.title});
						new Fx.Tween(info).start('background-color', '#730028').chain(function() {
							cart_progress = false;
						});
						$('cart').addClass('cart-active');
					} else {
						new Fx.Tween(info).start('background-color', '#51001C').chain(function() {
							cart_progress = false;
						});
						html = responseJSON.product.title;
					}
					info.getElement('span.finish').set('html', html);
					belt_timer = setTimeout(info_close, 4000);
					slided_cart();
				}).delay(500);
			}
		}).post($(this));
		e.stop();
        return false;
	} 


window.addEvent('domready', function() {
    $$('form.product-add-cart').addEvent('submit', wmc_cart_add);
});

