jQuery.fn.count=function(){
	if($(this).hasClass('weight')){
		$(this).attr('title','товар весовой, введите вес в килограммах');
	}
	var up=function(el){
		if($(el).hasClass('weight')){
			el.value=(el.value-(-0.1)).toFixed(2);
		}else{
			el.value-=-1;
		}
	};
	var down=function(el){
		if($(el).hasClass('weight')){
			el.value=(el.value-0.1>0?(el.value-0.1).toFixed(2):'0.10');
		}else{
			el.value=(el.value-1?el.value-1:1);
		}
	};
	$(this).keyup(function(event){
		this.value=this.value.replace(new RegExp('[,]','g'),'.').replace(new RegExp('[^0-9\.]','g'),'');
		
		if(!this.value){
			this.value=1;
		}
		if(!$(this).hasClass('weight')){
			this.value=parseInt(this.value)
		}
		if(event.keyCode==38){
			up(this);
		}
		if(event.keyCode==40){
			down(this);
		}
	}).click(function(){$(this).select()});
	if($(this).mousewheel){
		$(this).mousewheel(function(res,e){
			if(e>0){
				up(this);
			}else{
				down(this);
			}
			return false
		});
	}
	$(this).before('<span class="down">-</span>');
	$(this).after('<span class="up">+</span>');
	$(this).prev().click(function(){down($(this).next()[0]);return false}).each(function(){
		this.onselectstart=function(){return false}
		this.onmousedown=function(){return false}
	});
	$(this).next().click(function(){up($(this).prev()[0]);return false}).each(function(){
		this.onselectstart=function(){return false}
		this.onmousedown=function(){return false}
	});
	return $(this);
}


var shop={
	/* добавление в корзину 
		gid - id товара
		count - количество
		prop - свойства {sale_unit:1}
		
	*/
	
	add:function(gid,count,prop){
		var data={'id':gid,'count':parseFloat(count)};
		if(prop){
			for(var i in prop){
				data[i]=prop[i];
			}
		}
		util.notice('Добавлено '+count+' товаров')
		$.post('/catalog/?act=add',data,shop._add2);
	},

	addSelected:function(data){
		util.notice('Выбранные товары добавлены в корзину')
		$.post('/catalog/?act=addSelected',data,shop._add2);
	},
	_add2:function(res){
		$('#basket-content').html(res);
		$('#basket-clear').toggle($('#basket-content .item').length>0);
	},
	
	_add:function(res){
		$('#item'+res.id).find('.to_basket_put').hide();
		
		$('#basket-content #good'+res.id).remove();
		
		$('<div id="good'+res.id+'" class="item">'
		+'<a class="del" href="?act=clear&id='+res.id+'"></a><a class="name" href="/catalog/goods/'+res.id+'/">'+res.name+'</a>'
		+'<strong>'+parseFloat(res.price).toFixed(2)+'</strong></div>').insertBefore('#basket-total').find('.del').click(function(){
				var el=$(this);
				var id=$(this).parent().attr('id').replace('good','');
				$.get(this.href,function(res){
					$('#basket-content #good'+id).remove();
					var sum=0;
					$('#basket-content .item strong').each(function(){sum+=parseFloat($(this).text())});
					$('#basket-total strong').text(sum.toFixed(2));
				});
				return false;
			});
		var summ=0;		
		$('#basket-content .item strong').each(function(){summ+=parseFloat($(this).html())});
		$('#basket-total strong').text(summ.toFixed(2));
		
		$('#basket-clear').show();	
	}
}



			
$(function(){
	$('#basket-clear a').live('click',function(){
		$.get(this.href,function(res){
			$('#basket-content .item').remove();
			$('#basket-clear').hide();
			$('#basket-total strong').text('0.00');
		});
		return false;
	});	
	/*01.11.2011 очистка корзины*/
	$('#clear-basket').live('click',function(){
		util.notice('Ваша корзина пуста')
		$.get(this.href,shop._add2);
		return false;
	});
	$('#basket-content .del').live('click',function(){
		var el=$(this);
		var id=$(this).parent().attr('id').replace('good','');
		$.get(this.href,function(res){
			$('#basket-content #good'+id).toggle('fast',function(){
				$(this).remove();
				var sum=0;
				$('#basket-content .item strong').each(function(){sum+=parseFloat($(this).text())});
				$('#basket-total strong').text(sum.toFixed(2));
				$('#basket-clear').toggle($('#basket-content .item').length>0);
			});
			
		});
		return false;
	});
				
	$('#basket-content .up').live('click',function(){
	//				var el=$(this);
		var id=$(this).parent().parent().attr('id').replace('good','');
		$.post('/catalog/?act=add',{'id':id,'count':'+1'},function(res){
				$('#basket-content').html(res);
				$('#basket-clear').toggle($('#basket-content .item').length>0);
			}
		);
		return false;
	});
	$('#basket-content .down').live('click',function(){
	//				var el=$(this);
		var id=$(this).parent().parent().attr('id').replace('good','');
		$.post('/catalog/?act=add',{'id':id,'count':'-1'},function(res){
				$('#basket-content').html(res);
				$('#basket-clear').toggle($('#basket-content .item').length>0);
			}
		);
		return false;
	});
				
	$('#basket-clear').toggle($('#basket-content .item').length>0);
				
});
