$(document).ready(function(){
	basket_recalc();
	
	$(".rmFromBasket").click(function() {
		var els = $(".rmFromBasket").parents('tr').size();
		var thisEl = $(this);
		
		$.ajax({
	        type: "GET",
	        url: "/basket?remove="+thisEl.attr('id'),
	        dataType: "json",
	        success: function(data){
	            if (data.success == true) {            		
	            	if (els > 1) {
	            		thisEl.parents('tr').remove();
	            		basket_recalc();
	        		} else {
	        			thisEl.parents('table').remove();
	        			$('.block_title').after('<p>Корзина пуста</p>');
	        		}
	            }
	            var count = 0;
	    		$(".value").each(function(idx,val){
	    			thsEl = $(this).text();
	    			count = count + parseInt( thsEl );
	    		});
	    		$(".count b").text(count);
	        }
	    });
	});
	
	$("#clearAll").click(function() {
		var thisEl = $(this);
		
		$.ajax({
	        type: "GET",
	        url: "/basket?clean=true",
	        dataType: "json",
	        success: function(data){
	            if (data.success == true) {
	        		thisEl.parents('table').remove();
	        		$(".count b").text(0);
	        		$('.block_title').after('<p>Корзина пуста</p>');
	            }
	        }
	    });
	});

	$(".buy").click(function() {
			
			$(this).parent().append('<div class="tip">В корзине</div>');
			$(".tip").animate({ opacity: "hide" }, 2500);
	});

	$(".up").click(function() {

		var v = $(this).next("div").text();
		v = parseInt(v);
		$(this).next("div").text( v + 1 );

		coun = $(".count b").text();		

		id = $(this).parent().parent().attr("id");
		id = id.replace("product_", "");
		
		$.ajax({
			type: "GET",
			url: "/basket?up="+id,
			success: function(msg){
				basket_recalc();
				coun++;
				$(".count b").text(coun);
			}
		});	
	});
	
	
	$(".down").click(function() {
		var v = $(this).prev("div").text();
		v = parseInt(v);
		if (v > 1) {
			$(this).prev("div").text( v - 1 );

			coun = $(".count b").text();

			id = $(this).parent().parent().attr("id");
			id = id.replace("product_", "");

			$.ajax({
				type: "GET",
				url: "/basket?down="+id,
				success: function(msg){
	
						basket_recalc();
						coun--;
            $(".count b").text(coun);
				}
			});	

		}

	});	
	
});


function basket_recalc()
{	
	var n = 0;
	var total = 0;
	var end = false;

	while(end == false) {
		n++;	

		var price = $("#price_"+n).text();
		var col = $("#col_"+n).text();

		price = price.replace(" руб", "");
		price = price.replace(" ", "");			

		$("#sum_"+n).text(formatPrice(price*col) + " руб");

		total = total + price*col;

		z = n+1;
		if ($("#name_"+z).text() == '') { end = true; }
	}
	
	$("#total").text(formatPrice(total) + " руб");

}

function addToBasket(id, parent) {

				$.ajax({
					type: "GET",
					url: "/basket?add="+id+"&parent="+parent,
					success: function(msg){
	
						coun = $(".count b").text();			
						
						coun = parseInt($(".count b").text());
            coun++;
            $(".count b").text(coun);

					}
				});
}

function formatPrice (price) {
	
		var new_str = '';
		var z = 0;

		price = String(price);

		for (i=price.length-1; i > -1; i--) {
	
			if ( (z%3) == 0) {
				new_str = price[i] + " " + new_str;
			} else {
				new_str = price[i] + new_str;
			}
			
			z++;
		}

		return new_str;	
}
