$(document).ready(function(){
	
	// Скругления
	DD_roundies.addRule('.top_menu .left .data', '7px 0px 0px 0px', true);
	DD_roundies.addRule('.top_menu .right .data', '0px 7px 0px 0px', true);
	DD_roundies.addRule('.bottom_line', '0px 0px 7px 7px', true);
	DD_roundies.addRule('#basket_box, .price_box, .left_menu .article.active', '5px', true);
	DD_roundies.addRule('.top_menu .sub', '0px 0px 4px 4px', true);
	DD_roundies.addRule('.siterobot, .shadow_left, .shadow_right');	
	
	
	// Всплывающее меню
	$('.top_menu td').each(function(n,v){
		
		sub = $('.sub',v);
		
		if (sub.size())
		{
			$(v).hover(
				function(){
					dat = this;
					timer = setTimeout(function(){$('.sub',dat).show()},200);
				},
				function(){
					$('.sub',this).hide();
					clearTimeout(timer);
				}
			);

			$(v).find('.data').click(function(){
			
				if( $(this).css('display')=='block' ){
					//$(this).parent().find('.sub').hide();
					return false;
				}
					
			});

			//$('.data a',v).click(function(){ return false; });
			
		}

	});
	
	
	// Изменение количества для каталога и корзины
	$('#catalog_list form, #basket_list .edit form').each(function(n,v){
		
		$(v).submit(function(){
			
			form = $(this);
			
			$.ajax({
				data: form.serializeArray(),
			    url: form.attr('action'),
			    dataType: 'json',
			    type: 'POST',
			    success: function(data) 
			    {
			    	$('input[type=submit]',v).fadeOut();
			    	
			    	if (form.parent().attr('class')=='edit')
			    	{
						$('.price_res',form.parent().parent()).html(data.price);
			    		$('.price_all').html(data.price_all);
			    		$('input[type=text]',v).attr('value',data.count);
			    	}
			    	else
			    	{
			    		$('input[type=text]',v).attr('value','');
			    	}
			    	
			    	$('#basket_box').html(data.basket);
			    }
			});			
			
			return false;
			
		});
		
		$('input[type=text]',v).keypress(function(e){

			if(e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))
			{
				return false;
			}
		});
		
		$('input[type=text]',v).keyup(function(e){	
			button = $('input[type=submit]',$(this).parent());
			
			if ($(this).attr('value')>0) 
				button.fadeIn();
			else
				button.fadeOut();
		});
		
	});
	
	
	// Удаление из корзины
	$('#basket_list .delete form').submit(function(from){ 
		
		form = $(this);
		
		$.ajax({
			data: form.serializeArray(),
		    url: form.attr('action'),
		    dataType: 'json',
		    type: 'POST',
		    success: function(data) 
		    {
		    	$('#basket_box').html(data.basket);
		    	$('.price_all').html(data.price_all);
		    	
		    	form.parent().parent().remove();
		    	
		    	if (!$('#basket_list .delete form').size()) 
		    		$('#basket_list').html('Ваша корзина пуста');
		    }
		});			
		
		return false;
		
	});
	
	
	$('.price_box, #basket_box').click(function(){
		
		window.location = $(this).find('a').attr('href');
		return false;
		
	});
	
});
