function open_wind(id)
{
    var h;
    h = document.getElementById('wrapper').offsetHeight;
    document.getElementById('alles').style.height = h+ 'px';
    document.getElementById('alles').style.display = 'block';
    document.getElementById('popup_'+id).style.display = 'block';
}

function close_wind(id)
{
    document.getElementById('alles').style.display = 'none';
    document.getElementById('popup_'+id).style.display = 'none';
}

function update_popup_cart()
{
	$.get("/netcat/modules/default/cart.php?ajax=1&a=popup", function(html){
		var cartPopup = $("#cartPopup").html();
		$("#cartPopup").html(html);
		if ( ! cartPopup) {
			$("#cartPopup").css("display", "none").fadeIn();
		}
	});
}

function cart_put()
{
	// Кол-во товаров
	var count = $("input[name=count]", this).attr("value");
	count = parseInt(count);
	if ( !count || count<0) count = '-';
	$("input[name=count]", this).attr("value", count);
	
	// Отправка запроса
	$.post("/netcat/modules/default/cart.php?ajax=1&a=put", $(this).serializeArray(), function(result){
		if ( result.count) {
			$("#item_"+result.id).addClass("active");
		} else {
			$("#item_"+result.id).removeClass("active");
			$("#sub_products_"+result.id).html("");
			add_sub_products_select(result.id);
		}
		update_popup_cart();
	}, 'json');
	 
	return false;
}

$(init);

function init() 
{
	$(".cartForm").submit(cart_put);
	$(".cartForm input[name=count]").each(function(i, item){
		$(item).focus(function(){
			if ($(this).attr("value")=="-") {
				$(this).attr("value", "")
			}
		});
		$(item).change(function(){
			$(this).parent().submit();
			return false;
		});
	});
	
	$(".del").click(function(){
		var href = $(this).attr('href');
		href = href+'&ajax=1';
		
		$.getJSON(href, function(result){
			$("#item_"+result.id).removeClass("active");
			$("#item_"+result.id+" input[name=count]").attr("value", "-");
			update_popup_cart();
			$("#sub_products_"+result.id).html("");
			add_sub_products_select(result.id);
		});
		return false;
	});
}

function add_sub_products_select(pid, selected, count, num)
{
	var product = "#sub_products_"+pid;
	if ( !num) num = $(".item", product).length+1;
	
	$.get("/netcat/modules/default/ajax.php?a=sub_products_select&pid="+pid+"&selected="+selected+"+&num="+num+"&count="+count, function(select){
		$(product).append(select);
		init();
	});
}