$(function(){
	// Механизм работы складок на карточке товара
	
	$('#item_tab tr.title td').click(function(){
		$('#item_tab tr.title td').each(function(){
			if ( $(this).hasClass( 'title_l_active' ) ) $(this).removeClass( 'title_l_active' ).addClass( 'title_l' );
			if ( $(this).hasClass( 'title_c_active' ) ) $(this).removeClass( 'title_c_active' ).addClass( 'title_c' );
			if ( $(this).hasClass( 'title_r_active' ) ) $(this).removeClass( 'title_r_active' ).addClass( 'title_r' );
			$('#' + $(this).attr('rel')).hide();
		});
		if ( $(this).hasClass( 'title_l' ) ) $(this).removeClass( 'title_l' ).addClass( 'title_l_active' );
		if ( $(this).hasClass( 'title_c' ) ) $(this).removeClass( 'title_c' ).addClass( 'title_c_active' );
		if ( $(this).hasClass( 'title_r' ) ) $(this).removeClass( 'title_r' ).addClass( 'title_r_active' );
		$('#' + $(this).attr('rel')).show();
	});
	$('#item_tab tr.title td:first').click();
	
	$('div.filtr_nav a.short').click(function(){
		$('div.filtr_left, div.filtr_left_update').toggleClass('filtr_expand');
		$(this).html($('div.filtr_left, div.filtr_left_update').hasClass('filtr_expand') ? 'Сокращенный подбор' : 'Расширенный подбор' );
		return false;
	});
	$('div.filtr_nav a.svernut').click(function(){
		$('div.filtr_right').toggleClass('filtr_expand');
		$(this).html($('div.filtr_right').hasClass('filtr_expand') ? 'Свернуть' : 'Развернуть' );
		return false;
	});
	
	$('div.big_search a').click(function(){
		$('div.big_search div.search_filter').toggle();
		$(this).html($(this).text() == 'Свернуть' ? 'Расширенный поиск' : 'Свернуть' );
		return false;
	});
	
	
	// Загрузка корзины
	$.post('/cart.php?cart_info', {}, function(data){
		$('#header_bin').html(data) }, 'html');
	
	// Загрузка сравнения
	$.post('/compare.php?compare_info', {}, function(data){
		$('#compare_box').html(data) }, 'html');
});

// Отправка товара в корзину
function in_cart( in_cart, input, redirect ) {
	$.post('/cart.php', {'in_cart':in_cart}, function(){
		$(input).replaceWith('<img src="/image/design/buy_noact.gif" />');
		$.post('/cart.php?cart_info', {}, function(data){
			$('#header_bin').html(data) }, 'html');
		$.post('/cart.php?cart_popup', {}, function(data){
			$('#popup_content').html(data);
			$('#popup').modal({
				opacity:30, overlayCss:{backgroundColor:'#000'}, overlayClose:true, onClose: function (dialog) {
					$.modal.close(); if (redirect) location.href = location.href; }});
			$('#popup_content a.contin').click(function(){
				$.modal.close(); return false;
			});
		}, 'html');
	});
	return false;
}

// Отправка товара на сравнение
function in_compare( in_compare, input ) {
	$.post('/compare.php', {'in_compare':in_compare}, function(){
		$(input).replaceWith('<p class="compare_noact">Товар добавлен к сравнению</p>');
		$.post('/compare.php?compare_info', {}, function(data){
			$('#compare_box').html(data) }, 'html');
		$.post('/compare.php?compare_popup', {}, function(data){
			$('#popup_content').html(data);
			$('#popup').modal({
				opacity:30, overlayCss:{backgroundColor:'#000'}, overlayClose:true
			});
			$('#popup_content a.add').click(function(){
				$.modal.close(); return false;
			});
		}, 'html');
	});
	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.num input',$product_row);
	$count_input.val( Math.max(parseInt($count_input.val()) + shift, 0) );
	$('td.num p',$product_row).text($count_input.val());
	
	update_cart( $cart_table );
	
	return false;
}

// Удаление товара из корзины
function remove_product( input ) {
	if ( confirm( 'Вы уверены, что хотите удалить товар из корзины?' ) ) {
		var $product_row = $(input).parents('tr:first');
		var $cart_table = $($product_row).parents('table:first');
		
		$product_row.next().remove();
		$product_row.remove();
		
		update_cart( $cart_table );
	}
	
	return false;
}

// Пересчет стоимости товаров и суммы заказа
function update_cart( table ) {
	var order_sum = 0;
	var cart_index = 0;
	
	$('tr.cell', table).each(function(){
		var product_cost = parseInt($('td.price input',$(this)).val()) *
			parseInt($('td.num input',$(this)).val());
		$('td.price p',$(this)).text(product_cost + ' pуб.');
		order_sum += product_cost;
	});
	
	$('td.result2 span',table).text(order_sum + ' pуб.');
	
	$('#cart_form').ajaxSubmit({
		success: function(){
			$.post('/cart.php?cart_info', {}, function(data){
				$('#header_bin').html(data) }, 'html');
			if ( $('tr.cell', table).length == 0 )
				location.href = '/cart.php';
		}
	});
}

// Просмотр оригинала картинки
function in_popup( src, title, price, product_id ) {
	$('#popup_zoom .main_cell img').attr('src', src);
	$('#popup_zoom .title_win').html(title);
	
	if (price)
		$('#info_item p.green_text').show().find('span').html(price);
	else
		$('#info_item p.green_text').hide();
	if (product_id)
		$('#info_item a.buy_button').show().attr('href', '/cart.php?in_cart=' + product_id);
	else
		$('#info_item a.buy_button').hide();
	
	$('#popup_zoom').modal({
		opacity:30, overlayCss:{backgroundColor:'#000'}, overlayClose:true
	});
	return false;
}

// Обратный звонок
function callback_window() {
	var ajax_callback_form = function(){
		$('#callback_form').ajaxForm({
			target: '#popup_content', success: ajax_callback_form, beforeSubmit: check_callback_form
		}); 
	};
	
	var check_callback_form = function(){
		return CheckForm.validate( document.getElementById('callback_form') );
	};
	
	$.post('/callback.php', {}, function(data){
		$('#popup_content').html(data); ajax_callback_form();
		$('#popup').modal({
			opacity:30, overlayCss:{backgroundColor:'#000'}, overlayClose:true
		});
	}, 'html');
	
	return false;
}

// Попап с текстом
function text_window( text_id ) {
	$('#popup_content').html($('#' + text_id).html());
	$('#popup').modal({
		opacity:30, overlayCss:{backgroundColor:'#000'}, overlayClose:true
	});
	
	return false;
}

// Функция, скрывающая пункты меню и покзывающая выделенный 
function toggle_menu( current_link )
{
	var was_closed = $(current_link).next('div').css( 'display' ) == 'none';
	
	$(current_link).parent().find('a').removeClass( 'link_active' );
	if( was_closed )
		$(current_link).addClass( 'link_active' );
	
	$(current_link).parent().find('div').hide();
	if( was_closed )
		$(current_link).next('div').show();
	
	return false;
}
