




$(document).ready(function() {



	curr = $('#shop-cart-currency');
    US.Currency.init({name: curr.attr('name'), course: curr.attr('course'), decimals : curr.attr('decimals'), dsep: curr.attr('dsep'), tsep: curr.attr('tsep')});

/*
	// Начальное состояние
    US.Cart.init([
    	{'id' : 1, 'name' : 'el1', 'price' : 100},
    	{'id' : 2, 'name' : 'el2', 'price' : 200, 'count': 5}
    ]);
*/



    US.Cart.onsave = function(){    	count = this.getCount();
		price = this.getPrice();

		if (count)
		{			$('.cart-isempty').hide();
			$('.cart-isnotempty').show();

			$('.cart-count').html(count + '&nbsp;' + US.ruscomp(count, 'товаров,товар,товара'));
			$('.cart-price').html(US.formatPrice(price) + '&nbsp;' + US.Currency.name);

			$('#cart-total').html( US.money(price, 'strong') );
		}
		else
		{			$('.cart-isempty').show();
			$('.cart-isnotempty').hide();		}
    };


    // Из cookie
    US.Cart.init();



	$('a[rel=fancybox]').fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600,
		'speedOut'		:	200,
		'overlayShow'	:	false
	});

	$('#cip-main a').click(function(){
		$('#cip-thumbs li:first a').trigger('click');
		return false;
	});




	$('.product-variant').bind('change', function(){		var s = $(this);
		var price = $(s.attr('rel'));

		$('option', s).each(function(){			o = $(this);
			if (o.attr('value') == s.attr('value'))
			{				price.html( US.money( o.attr('price'), 'strong') );			}		});
	}).trigger('change');
	$('.addtocart-variant a.button').bind('click', function(){		$($(this).attr('rel')).submit();	});



	$('.addtocart').bind('click', function(){

		t = $(this);

		US.Cart.add(US.checkId(t.attr('productid')), 1, parseFloat(t.attr('productprice')));
		count = US.Cart.getCount();
		price = US.Cart.getPrice();

		if (count)
		{			if (t.attr('show')) $(t.attr('show')).show();
			if (t.attr('hide')) $(t.attr('hide')).hide();
		}
		else
		{			// Кривой шаблон, не добавилось		}

		return false;
	});



	$('.cart-delete').bind('click', function(){
		t = $(this);

		if (confirm('Удалить этот товар из заказа?'))
		{
			US.Cart.del(US.checkId(t.attr('productid')));

			t.parent().parent().empty();
		}

		return false;
	});



	$('.cart-count-changer').bind('keyup', function(){
		// <input value="{ $item.qty }" price="{ $item.price }" productid="{ $item.id }" class="cart-count-changer" update=".pv{$item.id}" />
		var t = $(this);

		var id = US.checkId(t.attr('productid'));
		var price = parseFloat(t.attr('price'));

		var count = parseInt(t.attr('value'));
		count = count ? count : 1;
		t.attr('value', count);


		US.Cart.update(id, count, price);

		$(t.attr('update')).html(   US.money(price * count, 'strong')   );

	}).trigger('keyup');


});
