var _pp = new popup();

$(document).ready(function(){
	//	Вкладки на странице товара
	$('div.pan-buttons a').click(function(){
		$('div.pan-buttons a').removeClass('sel');
		$(this).addClass('sel');
		$('div.panels > div').hide();
		$('div.panels > div#pan-'+$(this).attr('name')).show();
	});
	
	//	Просмотр картинок
	$("a.preview").fancybox({
		'overlayShow': true,
		'overlayOpacity': 0.8,
		'hideOnContentClick': true,
		'titleShow': false,
		'transitionIn': 'elastic',
		'transitionOut': 'elastic'
	});
	
	//	Фильтр
	$('.filter input').change(function(){
		$('.filter form').submit();
	});
	
	//	Сортировка
	$('div.sort > div').hover(
		function(){ $(this).addClass('hover'); },
		function(){ $(this).removeClass('hover'); }
	);
	
	//	Авторизация
	$('a.auth').click(function(){
		$('div.auth-form').slideToggle(300);
	});
	$('div.auth-form button').click(function(){
		var data = 'module=auth&action=login&'+$('div.auth-form form').serialize();
		$.post('/ajax.php', data, function(answer){
			if (answer && answer.result == 'ok')
			{
				$('div.auth-form form').submit();
			}
			else
			{
				if(answer && answer.message)
				{
					$('div.auth-form span').html(answer.message);
				}
			}
		}, 'json');
	});
	$('div.auth-block a.logout').click(function(){
		var data = 'module=auth&action=logout';
		$.post('/ajax.php', data, function(answer){
			if (answer && answer.result == 'ok')
			{
				document.location.reload();
			}
		}, 'json');
	});
	
	//	История заказов
	$('div.h-order a.more').click(function(){
		$(this).parent().parent().find('div.details').slideToggle(800);
	});
	
	//	Покупка
	$('.buy').live('click change keyup', function(e)
	{
		var _this = $(this);
		if(e.type == 'click' && $(this).is('input'))
		{
			return;
		}
		var p = $(this).attr('name').split('|');
		var count = 1;
		if (p[0] == 'set')
		{
			if (e.type == 'click')
			{
				return;
			}
			count = parseInt($(this).val());
			if (count <= 0)
			{
				return;
			}
		}
		$.post('/ajax.php', 'module=backet&action='+p[0]+'&id='+p[1]+'&count='+count, function(answer)
		{
			if (answer && answer.result == 'ok')
			{
				$('.i-c').html(answer.count);
				$('.i-p').html(answer.cost);
				if (answer.count > 0)
				{
					$('div.backet-block').fadeIn(300);
				}
				else
				{
					$('div.backet-block').fadeOut(300);
				}
				if (answer.cur_cost.length)
				{
					_this.parent('td').parent('tr').find('.cost').html(answer.cur_cost);
				}
				if (p[0] == 'del' && _this.hasClass('window') == false)
				{
					document.location.reload();
				}
				if (typeof(answer.html) != 'undefined' && answer.html.length)
				{
					if (p[0] == 'add' || p[0] == 'get' || (p[0] == 'del' && _this.hasClass('window') == true))
					{
						_pp.open({content:answer.html});
					}
				}
			}
		}, 'json');
	});
	
	//	Переключение типа пользователя в форме заказа
	$('.user-type-switch a').click(function(){
		$(this).siblings('a').removeClass('sel');
		$(this).addClass('sel');
		$('.user-type-forms > div').removeClass('sel');
		$('#ut-'+$(this).attr('name')).addClass('sel');
	});
	//	Валидация форм
	$('#ut-fiz-form').validate();
	$('#ut-ur-form').validate();
	$('#ut-auth-form').validate();
});

