//---------------------------------------------------------------------------------
$(document).ready(function(){//----------------------------------------------------
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
// открытие/закрытие пунктов меню
$('#left_menu div.left_menu').click(function()
{
	$(this).next('div.left_menu_inner').toggle('blind');
});
//---------------------------------------------------------------------------------
// поле поиска вверзу страницы
$('#search').focus(function()
{
	if ($(this).val() == 'Поиск по сайту')
		$(this).val('');
});
//---------------------------------------------------------------------------------
$('#search').blur(function()
{
	if ($(this).val() == '')
		$(this).val('Поиск по сайту');
});
//---------------------------------------------------------------------------------
$('#search').keyup(function(event)
{
	if (event.keyCode == 13)
	{
        window.location = '/ru/search/?s=' + encodeURIComponent($(this).val());
    }

});
//---------------------------------------------------------------------------------
// удаление товара из корзины
$('#main table.cart_goods a.del').click(function()
{
	obj = $(this);
	$.post('/ru/ajax.php', { a: 'del_for_cart', id: obj.parents('table.cart_goods').attr('good_id') }, function(json)
	{
		if (json.ok == 1)
		{
			obj.parents('table.cart_goods').next('div.cart_line').remove();
			obj.parents('table.cart_goods').remove();
			
			if (json.mes != '')
			{
				alert(json.mes);
			}
		}
		else if (json.mes != '')
		{
			alert(json.mes);
		}
		else
		{
			alert('Неизвестная ошибка.');
		}
	}, 'json');
});
//---------------------------------------------------------------------------------
// пересчитывание покупки в корзине
$('#main table.cart_info a.recount').click(function()
{
	obj = $(this);
	data = '';
	$('#main table.cart_goods td.count input').each(function()
	{
		data += '|' + $(this).parents('table.cart_goods').attr('good_id'); // id товара
		data += '!' + (parseInt($(this).val()) < 1 ? 1 : parseInt($(this).val())); // количество
	});
	$.post('/ru/ajax.php', { a: 'recount', data: data.substr(1) }, function(json)
	{
		if (json.ok == 1)
		{
			top_catr_update(json.data.count, json.data.cost);
			$('#cart_count').html(json.data.count);
			$('#cart_cost').html(json.data.cost + ' руб.');
			
			if (json.mes != '')
			{
				alert(json.mes);
			}
		}
		else if (json.mes != '')
		{
			alert(json.mes);
		}
		else
		{
			alert('Неизвестная ошибка.');
		}
	}, 'json');
});
//---------------------------------------------------------------------------------
// клиу на "оформление покупки" на странице оформления покупки
$('#form_order img.submit').click(function()
{
	if ($('#form_order input[name=delivery]:checked').val() == undefined)
	{
		alert('Выберите способ оплаты и доставки.');
		return;
	}
	
	if ($('#form_order input[name=fio]').val() == '')
	{
		alert('Введите ваше имя.');
		return;
	}
	
	var reg_email = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
	if (!$('#form_order input[name=email]').val().match(reg_email))
	{
		alert('Введите корректный e-mail.');
		return;
	}
	
	if ($('#form_order input[name=phone]').val() == '')
	{
		alert('Введите телефон.');
		return;
	}
	
	if ($('#form_order input[name=index]').val() == '')
	{
		alert('Введите почтовый индекс.');
		return;
	}
	
	if ($('#form_order input[name=address]').val() == '')
	{
		alert('Введите адрес.');
		return;
	}
	
	$('#form_order').submit();
});
//---------------------------------------------------------------------------------
// выбор способа оплаты и доставки
$('#form_order input[name=delivery]').change(function()
{
	var delivery_cost = $('#form_order input[name=delivery]:checked').attr('cost');
	$('#all_cost').html(parseInt($('#all_cost').attr('goods_cost')) + parseInt(delivery_cost));
});
//---------------------------------------------------------------------------------
});//------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
// добавление товара в корзину
function add_to_cart(good_id)
{
	$.post('/ru/ajax.php', { a: 'add_to_cart', id: good_id }, function(json)
	{
		if (json.ok == 1)
		{
			top_catr_update(json.data.count, json.data.cost);
			
			if (json.mes != '')
			{
				alert(json.mes);
			}
		}
		else if (json.mes != '')
		{
			alert(json.mes);
		}
		else
		{
			alert('Неизвестная ошибка.');
		}
	}, 'json');
}
//---------------------------------------------------------------------------------
// обновление данных в корзине в верхнем блоке
function top_catr_update(count, cost)
{
	$('#top_cart').html('<div id="cart" onClick="window.location=\'/ru/cart/\';"><b>Корзина</b><br>Товаров: ' + count + '<br>Сумма: ' + cost + ' руб.</div>');
}
