$(function(){
	/* отработка события кнопки "купить" */
	$('a.btn-buy, a.hpprobook-btn-buy').live('click', function(e){
		var posx = e.pageX;
		var posy = e.pageY;

		$.getJSON ($(this).attr ('href') + '?ajax=true', function (data) {
			data.article_title_full = data.article_title_full.replace (/\//g, ' / ');
			data.article_title_full = data.article_title_full.replace (/,/g, ', ');

			//заполняем данные о добавленном товаре в окошко
			$('#cart-add-position-title-full').html (data.article_title_full);
			$('#cart-add-position-price').html (data.article_price_retail_cash);

			//заполняем сводную информацию корзины - корзина в шапке
			$('#cart-goods-count').html (data.cart_goods_count);
			$('#cart-goods-count-declension').html (data.cart_goods_count_declension);
			$('#cart-total-price-cash').html (data.cart_total_price_cash);

			//отображения окошка
			$('.popup').css({'top':posy-120,'left':posx-180}).fadeIn(200);

			//отображение корзины в шапке
			$('#header-cart-empty').hide ();
			$('#header-cart-full').show ();
		});

		return false;
	});

	/* закрытие попапа */
	$('.popup .close').click(function(){
		$(this).parents('.popup').fadeOut(100);
		return false;
	});

	$('.slide-hold').each(function(){
		var _this = $(this);
		var link = _this.find('a.opener');
		var slide = _this.find('.accessory');
		var h = slide.children().eq(0).outerHeight();
		var mainH = slide.height();
		if(link.parents('.title').hasClass('title-active')) slide.height('auto');
		else slide.height(h);
		link.click(function(){
			if(link.parents('.title').hasClass('title-active')){
				link.parents('.title').removeClass('title-active');
				slide.animate({height:h}, {queue:false, duration:300});
			}
			else{
				link.parents('.title').addClass('title-active');
				slide.animate({height:mainH}, {queue:false, duration:300});
			}
			return false;
		});
	});

	$(function() {
		$('a[rel*=facebox]').fancybox({
			'transitionIn'		: 'fade',
			'transitionOut'		: 'fade',
			'titlePosition' 	: 'over',
			'speedIn'			: 200,
			'speedOut'			: 200,
			'changeSpeed'		: 200,
			'changeFade'		: 200,
			'scrolling' 		: 'yes'
		});
	});
});

/**
 * Расставить пробелы между тысячами
 *
 * @param int price
 * @return string
 */
function priceRur (price)
{
	price = new String (parseInt (price)).split ('');
	var result = '';
	for (var i = price.length - 1; i >= 0; i --) {
		result = price[i] + result;
		if ((price.length - i) % 3 === 0) {
			result = ' ' + result;
		}
	}
	return result;
}
