/*cart*/

$(document).ready(function() {
	
	initCartLoading();
	
	$.getJSON("/module.shop_cart.cartJSON/", {}, function(resp){
		rebuildCart(resp);
		if ($.cookie('cart_h')) {
			$('#cart .panner').addClass('closed').html('+').attr('title', 'Развернуть');
			$('#cart .list').slideUp(0);
			$('#cart').css({bottom:-11})
		}		
	});
	
	$('.buyButton').click(function(e){
		//if (e) e.stopPropagation();
		//var cloneItem = $('.imgInBox .img').clone().css();
		var params = this.href.match(/tocart\/([\d]*)/);
		itemParams = {
			'add': {'id':params[1], 'num':1}
		};
		$('#cart').addClass('loading');
		$.getJSON("/module.shop_cart.processor/", itemParams, function(resp){
			rebuildCart(resp);
		})
		
		$('.buyButton img').animate({'opacity':0}, {duration:200}).animate({'opacity':1}, {duration:200})
		
		location.hash = 'item-added-'+params[1];
		
		return false;
	});
	
	$('#cart .panner').click(function(){
		if (!$(this).hasClass('closed')) {
			$.cookie('cart_h', 1, {expires: 365, path: '/'});
			$(this).addClass('closed').html('+').attr('title', 'Свернуть');
			$('#cart .list').slideUp(500);
			$('#cart').animate({bottom:-11}, 500)
		} else {
			$.cookie('cart_h', 0, {expires: -1, path: '/'});
			$(this).removeClass('closed').html('&ndash;').attr('title', 'Развернуть');
			$('#cart .list').slideDown(400);
			$('#cart').animate({bottom:20}, 400)
		}
	})

})

var cartBody;
var initCartLoading = function() {
	cartBody = $('#cart .body');
	$('<img />').attr('src', '/images/cart-loader.gif');
}

var recountCartValues = function(itemsData) {
	var cart = $('#cart');
	cart.removeClass('empty').removeClass('loading');
	var numSummary = 0;
	var priceSummary = 0;
	$(itemsData).each(function(i){
		numSummary += this.num * 1;
		priceSummary += this.num * this.item.price;
	})
	
	var tEnd;
	var l = numSummary;
	if (l > 0 && l <= 1) {
		tEnd = '';
	} else if (l > 1 && l <= 4) {
		tEnd = 'а';
	} else if (l > 4 && l <= 20) {
		tEnd = 'ов';
	} else {
		tEnd = 'ов';
	}
	
	
	cart.find('.summary .num').html(numSummary);
	cart.find('.summary .numWordEnd').html(tEnd);
	cart.find('.summary .price .num').html(priceSummary);	
}

var rebuildCart = function(itemsData){
	var cart = $('#cart');
	if(itemsData.length) {	
		cart.removeClass('empty').removeClass('loading');
		var cartItemsListBox = cart.find('.list');
		var list = "<table>";
		var numSummary = 0;
		var priceSummary = 0;
		$(itemsData).each(function(i){
			list += '<tr class="row"><td class="img"><div><a href="/shop/categories/'+this.item.path+'"><img src="/userfiles/image_main_xsmall/'+this.item.image+'" alt="" width="30" /></a></div></td><td class="t"><a id="itemId_'+this.id+'" class="mainLink" href="/shop/categories/'+this.item.path+'">'+this.item.title+'</a>'+((this.num > 1)?'&nbsp;<span class="num"><span class="in">x&nbsp;<span class="dec">'+this.num+'</span></span><span class="renumBox"></span></span>':'')+'</td><td>'+this.item.price+'<span class="rur">Р<i>█</i></span></td><td class="del"><div title="Удалить"></div></td></tr>';
			
			numSummary += this.num * 1;
			priceSummary += this.num * this.item.price;
		})
		list += "</table>";
		cartItemsListBox.html(list);
		
		var tEnd;
		var l = numSummary;
		if (l > 0 && l <= 1) {
			tEnd = '';
		} else if (l > 1 && l <= 4) {
			tEnd = 'а';
		} else if (l > 4 && l <= 20) {
			tEnd = 'ов';
		} else {
			tEnd = 'ов';
		}
		
		
		cart.find('.summary .num').html(numSummary);
		cart.find('.summary .numWordEnd').html(tEnd);
		cart.find('.summary .price .num').html(priceSummary);
		
		cart.find('td.del div').click(function(){
			var linkId = $(this).parents('.row').find('.mainLink').attr('id');
			$('#cart').addClass('loading');
			$.getJSON("/module.shop_cart.processor/", {
				'recount':{
					'id': linkId.split("_")[1],
					'num': 0
				}
			}, function(resp){
				rebuildCart(resp);
			})
		})
		
		$('#cart .img img').hover(
			function(){
				$(this).animate({width:95}, {duration: 300,queue:false})
				//$('#cart').animate({width:280}, {duration: 300})
			},
			function(){
				$(this).animate({width:30}, {duration: 300,queue:false})
				//$('#cart').animate({width:280}, {duration: 300})
			}
		);
		$('#cart .t .num').click(function(){
			var el = $(this);
			var nDec = el.find('.dec');
			var currentNum = parseInt(nDec.html(), 10);
			if(!el.hasClass('active')) {
				el.addClass('active');
				var select = '<div class="renum"><div class="up"></div><div class="rDec">x&nbsp;<span class="rDecNum">'+parseInt(nDec.html(), 10)+'</span></div><div class="down"></div></div>';
				el.find('.renumBox').html(select);
				var dec = el.find('.rDecNum');
				el.find('.up').click(function(){
					var nnum = parseInt(dec.html(), 10) + 1;
					dec.html(nnum);
					nDec.html(nnum);
					return false;
				})
				el.find('.down').click(function(){
					if(dec.html() > 1) {
						var nnum = parseInt(dec.html(), 10) - 1;
						dec.html(nnum);
						nDec.html(nnum);
					}

				})
				
				el.find('.renum').mouseover(function(){
					el.addClass('active');
					$(this).css('display', 'block');
				}).mouseout(function(){
					el.removeClass('active');
					$(this).css('display', 'none');
					var nnum = parseInt(dec.html(), 10);
					
					if(currentNum != nnum) {
						var linkId = el.parent('.t').find('.mainLink').attr('id');
						$('#cart').addClass('loading');
						$.getJSON("/module.shop_cart.processor/", {
							'recount':{
								'id': linkId.split("_")[1],
								'num': nnum
							}
						}, function(resp){
							//alert(resp)
							recountCartValues(resp);
						})
					}
				})
				
			} else {
				//el.find('.renumBox').html('');
			}
		})
	} else {
		cart.addClass('empty');
	}
}


