$(function(){
	// Подсказки для поисковых элементов
	$('#search_short_input, #search_input').focus(function(){
		$('label[for='+$(this).attr('id')+']').hide();
	}).blur(function(){
		if ($(this).val()=='')
			$('label[for='+$(this).attr('id')+']').show();
	}).each(function(){
		if ($(this).val()!='')
			$('label[for='+$(this).attr('id')+']').hide();
	});
	
	// Каталог
	$('div.catalogue_group_title a').click(function(){
		if( $(this).parent().next().size() > 0 ) {
			$('div.catalogue_group').removeClass('catalogue_show');
			$(this).parents('div.catalogue_group').addClass('catalogue_show');
			return false;
		} else {
			return true;
		}
	});
	
	// Загрузка содержимого корзины
	$.post('/cart.php?cart_info', {}, function(data){
		$('div.cart_info').html(data) }, 'html');
});

// Отправка товара в корзину
function in_cart( in_cart, link ) {
	$.post('/cart.php', {'in_cart':in_cart}, function(){
		$(link).addClass('disabled').html('Выбран').click(function(){return false});
		$.post('/cart.php?cart_info', {}, function(data){
			$('div.cart_info').html(data) }, 'html');
	});
	return false;
}

// Отправка товара на сравнение
function in_compare( in_compare, link ) {
	$.post('/compare.php', {'in_compare':in_compare}, function(){
		$(link).addClass('disabled').html('Выбран').click(function(){return false});
	});
	return false;
}

// Изменение количества товара в корзине
function change_product( input, shift ) {
	var $product_row = $(input).parents('tr:first');
	var $cart_table = $($product_row).parents('table:first');
	
	var $count_input = $('td.change input',$product_row);
	$count_input.val( Math.max(parseInt($count_input.val()) + shift, 0) );
	$('td.quantity',$product_row).text($count_input.val());
	
	update_cart( $cart_table );
	
	return false;
}

// Удаление товара из корзины
function remove_product( input, shift ) {
	if ( confirm( 'Вы уверены, что хотите удалить товар из корзины?' ) ) {
		var $product_row = $(input).parents('tr:first');
		var $cart_table = $($product_row).parents('table:first');
		
		$product_row.remove();
		
		update_cart( $cart_table );
	}
	
	return false;
}

// Пересчет стоимости товаров и суммы заказа
function update_cart( table ) {
	var order_sum = 0;
	var cart_index = 0;
	
	$('tr.product', table).each(function(){
		var product_cost = parseInt($('td.price',$(this)).text()) *
			parseInt( $('td.change input',$(this)).val() );
		$('td.counter',$(this)).text(++cart_index + '.');
		$('td.cost',$(this)).text(product_cost);
		order_sum += product_cost;
	});
	
	$('tr.footer td.total span.total_sum',table).text(order_sum + ' p.');
}

// Картинка товара
function picture_window( product_id, picture_id ) {
	iWidth = screen.width * 0.8, iHeight = screen.height * 0.6, iPosX = screen.width / 2 - iWidth / 2, iPosY = screen.height / 2 - iHeight / 2;
	oWin = window.open( '/picture.php?product_id=' + product_id + ( picture_id ? '&picture_id=' + picture_id : '' ), 'picture', 'left=' + iPosX + ', top=' + iPosY + ', width=' + iWidth + ', height=' + iHeight + ', toolbar=0, status=0, menubar=0, resizable=1, scrollbars=1' );
	oWin.focus();
	
	return false;
}

// Обратный звонок
function callback_window() {
	iWidth = 510, iHeight = 400, iPosX = screen.width / 2 - iWidth / 2, iPosY = screen.height / 2 - iHeight / 2;
	oWin = window.open( '/callback.php', 'callback', 'left=' + iPosX + ', top=' + iPosY + ', width=' + iWidth + ', height=' + iHeight + ', toolbar=0, status=0, menubar=0, resizable=1, scrollbars=1' );
	oWin.focus();
	
	return false;
}
