var Cart =
{
	run: function ()
	{
		$(".cart_trigger").click(function(){
			Cart.addItem($('#_pst').val());
			return false;
		});

		if ($('#cart_table').length > 0) {

			$('input[name=pstg]').bind('click', function () {
				Cart.setPostage ($('input[@name=pstg]:checked').val());
			});
			$('#ccont').bind ('click', function () {
				Cart.setPostage ($('input[@name=pstg]:checked').val());
			});
			if ($('input[@name=pstg]:checked').length)
				Cart.setPostage ($('input[@name=pstg]:checked').val());
			Cart.recalculateTotals ();
		}
	},
	addItem: function (pid)
	{
		var bezier_params = {
			start: {
				x: $('.product_picture .thumb').offset().left,
				y: $('.product_picture .thumb').offset().top,
				angle: -45
			},
			end: {
				x:$('#head_cart').offset().left,
				y:$('#head_cart').offset().top,
				angle: 45,
				length: 0.33
			}
		}

		$.ajax ({
			url: Core.currenturl,
			dataType: 'json',
			type: 'GET',
			data: {
				module: 'cart',
				mode: 'aptc',
				pst: pid
			},
			success: function (data) {
				if (data['error'] == 1) {
					alert (data['description']);
				} else {
					$('.product_picture .thumb')
					.clone()
					.css({position: 'absolute', top: $('.product_picture .thumb').offset().top, left: $('.product_picture .thumb').offset().left}).appendTo('body')
					.animate({path : new $.path.bezier(bezier_params), width: '10px', height: '10px', opacity: 0 }, 800, function () {
						$('#head_cart span.itms').html(data.ni);
						$('#head_cart span.csum').html(data.sum);
						Product.hideBuyButton ();
					});
				}
			}
		});
		return false;
	},
	removeItem: function (pid)
	{
		$.ajax ({
			url: Core.currenturl,
			dataType: 'json',
			type: 'GET',
			data: {
				module: 'cart',
				mode: 'rpfc',
				pst: pid
			},
			success: function (data) {
				if (data['error'] == 1) {
					alert (data['description']);
				} else {
					location.reload();
//					$('tr#ci' + data.pid).fadeOut().remove();
//					$('input[name=tp]').val(data.sum);
//					Cart.recalculateTotals ();
				}
			}

		});
		return false;
	},
	setPostage: function (pstg)
	{
		$.ajax ({
			url: Core.currenturl,
			dataType: 'json',
			type: 'GET',
			data: {
				module: 'cart',
				mode: 'setPostage',
				pstg: pstg
			},
			success: function (data) {
				if (data['error'] == 1) {
					alert (data['description']);
					return false;
				} else {
					Cart.recalculateTotals ();
				}
			}
		});
	},
	recalculateTotals: function ()
	{
		$('span.total_price').html(parseFloat (parseFloat ($('input[name=tp]').val().replace(",", ".")) + parseFloat ($('input[@name=pstg]:checked').attr('rel').replace(",", "."))).toFixed (2));
		
		total_price = parseFloat ($('span.total_price').html());
		tax_rate = parseFloat ($('input[name=tax]').val().replace(",", "."));
		total_tax = total_price - (total_price / ((100+tax_rate)/100));
		//$('span.total_tax').html((parseFloat ($('input[name=tp]').val().replace(",", ".")) / 100 * parseFloat ($('input[name=tax]').val().replace(",", "."))).toFixed (2));
		$('span.total_tax').html((total_tax).toFixed (2));

		return false;
	},
	recalculateItems: function ()
	{
		$.ajax ({
			url: Core.currenturl,
			dataType: 'json',
			type: 'GET',
			data: {
				module: 'cart',
				mode: 'rpic',
				items: JSON.stringify ($('.cartwrap .item input').serializeArray())
			},
			success: function (data) {
				if (data['error'] == 1) {
					alert (data['description']);
				} else {
					location.reload();
//					$('input[name=tp]').val(data.sum);
//					Cart.recalculateTotals ();
				}
			}
		});

		return false;
	}
}
StartUp (Cart);

