$(document).ready(function(){
	$('.buy-now').click(function(){
		var product=$(this).attr('id');
		var count=$('#count select').val();
		var size=$('#size select').val();

		$.post("shoppingcard.php",{a:'addtocard',i: product,c:count,s:size},
			function(data){
				if(data!==''){
					var bag=$('#u_bag span.count').text();
					var product = jQuery.parseJSON(data);
					$('#bag-user .listing #holder').replaceWith('<a href="/shop/'+product.id+'"><img src="/media/products/mini/'+product.cover+'" /></a>');
					$('#bag-user').css('display','block');
				}
				
				var card=$('#bag span.count').text();
				total=(card*1)+(count*1);
				$('#bag span.count').text(total);
			}
		);
		$('#bag-info').css('height',$(document).height()).css('display','block');
		scrolltop=$('html').scrollTop();
		windowHeight=$(window).height();
		$('#info-table').css('top',scrolltop+(windowHeight/2)-50).css('display','block');
	});

	$('#bag-info').click(function(){
		$('#info-table div.close p').click();
        $('#remove-confirm div.n p').click();
	});

	$('#info-table div.close p').click(function(){
        $('#bag-info').css('display','none');
		$('#info-table').css('display','none');
	});

	$(document).keyup(function(event){
		if(event.keyCode==27){
			$('#info-table div.close p').click();
            $('#remove-confirm div.n p').click();
		}
	});



	$('.shoppingcard .remove').click(function(){
		var data=$(this).parent();
		var order=data.attr('id');
		$('#remove-confirm').removeAttr('alt');
		$('#remove-confirm').attr('alt',order);
		$('#bag-info').css('height',$(document).height()).css('display','block');
		scrolltop=$('html').scrollTop();
		windowHeight=$(window).height();
		$('#remove-confirm').css('top',scrolltop+(windowHeight/2)-50).css('display','block');
	});

	$('#remove-confirm div.y p').click(function(){
		var order=$('#remove-confirm').attr('alt');
		$('#bag-table .items').each(function(i,v){
			if($(v).attr('id')==order){
				$(v).remove();
			}
		});
		$.post("shoppingcard.php",{a:'removecard',order:order},
			function(stat){
				if(stat){
					//var order=data.remove();
					if($('#bag-table .items').size()){
						$.post("shoppingcard.php",{a:'initbug'},
							function(total){
								delivery=parseInt($('#delivery').text());
								$('#total').text((total*1)+(delivery*1)+' р');
							}
						);
					}else{
						$('#content').remove();
						location.reload();
					}
				}
			}
		);
		$('#remove-confirm div.n p').click();
	});
	$('#remove-confirm div.n p').click(function(){
		$('#remove-confirm').attr('alt','');
		$('#bag-info').css('display','none');
		$('#remove-confirm').css('display','none');
	});


	$('p.clear').click(function(){
		$.post('/clear',function(data){
			$('#selection-user').remove();
		});
	});
});
