var plan_entrance, plan_floor, open_gal, open_con, open_dist = 'all';

$(document).ready(function () {
/* Colorbox */
	$("a[rel='gallery']").colorbox();
	$("a[rel='gal']").colorbox();
/* Реактив */
	$('#rktv a, #rktv #reaktive_block').hover(function() {
		ShowReaktiveStart();
	}, function() {
		HideReaktiveStart();
	});
/*  */
	$('#tel').live('click', function() {
		if ($(this).attr('name') == "phone") {
			$(this).keypress(function(e) {
				if (!e) {e = window.event; e.currentTarget = e.srcElement;} //IE
				var ch = e.keyCode == 0 ? e.charCode : e.keyCode;
				if (!(ch >= 48 && ch <= 57 || ch < 32 || ch >= 37 && ch <= 40)) return false;
			});
		}
	});
/* Биг форм */
	$('#f_1_9 input').change(function() {
		if ($(this).attr('checked')) {
			$('#f_1_9_1').val($('#f_1_8_1').val());
		} else {
			$('#f_1_9_1').val('');
		}
	});
/* Баннеры на главной */
	$('#banners .banner').hover(function() {
		$(this).find('div.padd p').css('background-position', 'left top');
		$(this).find('div.padd .left').css('background-position', 'left top');
		$(this).find('div.padd .right').css('background-position', 'right top');
	}, function() {
		$(this).find('div.padd p').css('background-position', '0 -44px');
		$(this).find('div.padd .left').css('background-position', '-7px top');
		$(this).find('div.padd .right').css('background-position', '-14px top');
	});
/* Главная навигация */
	$('#nav .button').hover(function() {
		if (!$(this).hasClass('active')) {
			/*var arr = [];
			arr = $(this).css('background-position').split(' ');
			$(this).css('background', 'url(/pics/bg/nav_button.png) no-repeat ' + arr[0] + ' bottom');*/
			$(this).css('background', 'url(/pics/bg/nav_button_grey.png) repeat left top');
		}
	}, function() {
		if (!$(this).hasClass('active')) {
			/*var arr = [];
			arr = $(this).css('background-position').split(' ');
			$(this).css('background', 'none no-repeat ' + arr[0] + ' bottom');*/
			$(this).css('background', 'none');
		}
	});
/* Принт и мэйл */
	$('#to_print .pressable, #mail p .pressable').hover(function() {
		$(this).parent().css('background-position', 'left -20px');
	}, function() {
		$(this).parent().css('background-position', 'left top');
	});
	$('#to_print .pressable').click(function() {
		window.print();
	});
/* Фамбы */
	$('.thumb .info .img').hover(function() {
		$(this).find('p').css('background-color', '#4D9CEA');
	}, function() {
		$(this).find('p').css('background-color', '#455A7B');
	});
/* Проценты */
	$('.percent_line').each(function() {
		$(this).find('.yellow').width($(this).attr('value')+"%");
	});
/* Кнопка */
	$('.y_button').live("mouseover mouseout mousedown mouseup", function(event) {
		if (event.type == "mouseover") {
			$(this).find('.part.left').css('background-position', 'left -49px');
			$(this).find('.part.right').css('background-position', 'right -49px');
		} else if (event.type == "mouseout") {
			$(this).find('.part.left').css('background-position', 'left top');
			$(this).find('.part.right').css('background-position', 'right top');
		} else if (event.type == "mousedown") {
			$(this).find('.part.left').css('background-position', 'left -98px');
			$(this).find('.part.right').css('background-position', 'right -98px');
			if ($(this).attr('id') == 'build') {
				$(this).css('padding', '8px 7px 10px');
			} else {
				$(this).css('padding', '11px 7px 7px');
			}
		} else {
			$(this).find('.part.left').css('background-position', 'left -49px');
			$(this).find('.part.right').css('background-position', 'right -49px');
			if ($(this).attr('id') == 'build') {
				$(this).css('padding', '7px 7px 11px');
			} else {
				$(this).css('padding', '10px 7px 8px');
			}
		}
	});
/* Закрыть */
	$('.close_popup').live('click', function() {
		$('.popup').animate({left: '-100%'}, 750, function() {
			$('#layer_devider').hide();
		});
	});
	$('#layer_devider').click(function() {
		$('.popup').animate({left: '-100%'}, 750, function() {
			$('#layer_devider').hide();
		});
	});
/* Кнопка вэб-камеры */
	$('#span_cam, #img_cam').click(function() {
		$('#layer_devider').css('top', -(9000-$('#wrapper').height())).show();
		$('.popup.s').attr('id', 'webcam');
		$('#webcam').css('top', '200px');
		$('#webcam').load('/forms/webcam.php', function(success) {
			$('#webcam').animate({left: '50%'}, 750);
		});
	});
/* Кнопка написать письмо */
	$('#mail .pressable').click(function() {
		$('#layer_devider').css('top', -(9000-$('#wrapper').height())).show();
		$('.popup.s').attr('id', 'mailto');
		$('#mailto').css('top', '200px');
		$('#mailto').load('/forms/mailto.php', function(success) {
			$('#mailto').animate({left: '50%'}, 750);
		});
	});
/* Кнопка перезвноните мне */
	$('#call .pressable').click(function() {
		$('#layer_devider').css('top', -(9000-$('#wrapper').height())).show();
		$('.popup.s').attr('id', 'callback');
		$('#callback').css('top', '200px');
		$('#callback').load('/forms/callback.php', function(success) {
			$('#callback').animate({left: '50%'}, 750);
		});
	});
/* Кнопка заявки */
	$('#request').click(function() {
		var top = $(this).offset().top/2;
		$('#layer_devider').css('top', -(9000-$('#wrapper').height())).show();
		$('.popup.s').attr('id', 'ask');
		$('#ask').css('top', top);
		var object = $(this).find('div.in').attr('id');
		$('#ask').load('/forms/ask.php?object='+object, function(success) {
			$('#ask').animate({left: '50%'}, 750);
		});
	});
/* Кнопка задать вопрос */
	$('#ask_q').click(function() {
		var top = $(this).offset().top - 189;
		$('#layer_devider').css('top', -(9000-$('#wrapper').height())).show();
		$('.popup.s').attr('id', 'question');
		$('#question').css('top', top);
		$('#question').load('/forms/question.php', function(success) {
			$('#question').animate({left: '50%'}, 750);
		});
	});
/* Кнопка проектной документации */
	$('#prodocs').click(function() {
		var id = $(this).parent().attr('id');
		$('.popup.s').attr('id', 'projectdocs');
		$('#projectdocs').css('top', '340px');
		$('#projectdocs').load('/forms/projectdocs.php?id='+id, function(success) {
			$('#projectdocs').animate({left: '50%'}, 750);
		});
	});
/* Кнопка заказать строительство коттеджа */
	$('#build').click(function() {
		var id = $(this).parent().attr('id');
		var top = $(this).offset().top - 250;
		$('#layer_devider').css('top', -(9000-$('#wrapper').height())).show();
		$('.popup.s').attr('id', 'building');
		$('#building').css('top', top);
		$('#building').load('/forms/building.php?id='+id, function(success) {
			$('#building').animate({left: '50%'}, 750);
		});
	});
/* Кнопка заказать коттедж в поселке */
	$('#vernisage').click(function() {
		var id = $(this).parent().attr('id');
		var top = $(this).offset().top - 189;
		$('#layer_devider').css('top', -(9000-$('#wrapper').height())).show();
		$('.popup.s').attr('id', 'settlement');
		$('#settlement').css('top', top);
		$('#settlement').load('/forms/settlement.php?id='+id, function(success) {
			$('#settlement').animate({left: '50%'}, 750);
		});
	});
/* Кнопка заказать проект */
	$('#house').click(function() {
		var id = $(this).parent().attr('id');
		var top = $(this).offset().top - 189;
		$('#layer_devider').css('top', -(9000-$('#wrapper').height())).show();
		$('.popup.s').attr('id', 'houses');
		$('#houses').css('top', top);
		$('#houses').load('/forms/houses.php?id='+id, function(success) {
			$('#houses').animate({left: '50%'}, 750);
		});
	});
/* Кнопка программа 1-2-3 */
	$('#program1-2-3').click(function() {
		window.location.href = "/realty/flats/program123/order/";
	});
/* Кнопка подписаться на рассылку */
	$('#subs').click(function() {
		var top = $(this).offset().top - 189;
		$('#layer_devider').css('top', -(9000-$('#wrapper').height())).show();
		$('.popup.s').attr('id', 'subscribe');
		$('#subscribe').css('top', top);
		$('#subscribe').load('/forms/subscribe.php', function(success) {
			$('#subscribe').animate({left: '50%'}, 750);
		});
	});
/* Кнопка купить землю */
	$('#bland').click(function() {
		var top = $(this).offset().top - 189;
		$('#layer_devider').css('top', -(9000-$('#wrapper').height())).show();
		$('.popup.s').attr('id', 'buyland');
		$('#buyland').css('top', top);
		$('#buyland').load('/forms/buyland.php', function(success) {
			$('#buyland').animate({left: '50%'}, 750);
		});
	});
/* Планировки */
	// Линки
	$('.pressable.plan').click(function() {
		$('#layer_devider').css('top', -(9000-$('#wrapper').height())).show();
		$('.popup.b').attr('id', 'plan');
		$('#plan').css({'position': 'fixed', 'top': '50px'});
		$('#plan').load('/forms/plan.php?id='+$(this).attr('id'), function(success) {
			if ($('body').height() < ($('#plan').offset().top + $('#plan').height())) {
				var top = $(window).height() - $('#plan').height() - 20;
				$('#plan').css('top', top+'px');
			}
			$('#plan').animate({left: '50%'}, 750, function() {
				var top = $(this).offset().top;
				$('#plan').css({'position': 'absolute', 'top': top+'px'});
			});
		});
	});
	// Изображения
	$('img.plan, .pressable.pl').click(function() {
		$('#layer_devider').css('top', -(9000-$('#wrapper').height())).show();
		$('.popup.b').attr('id', 'plan');
		$('#plan').css({'position': 'fixed', 'top': '50px'});
		var object = $(this).parent().find('span.object').attr('id');
		var entrance = $(this).parent().find('span.entrance').attr('id');
		var floor = $(this).parent().find('span.floor').attr('id');
		$('#plan').load('/forms/plan.php?object='+object+'&entrance='+entrance+'&floor='+floor, function(success) {
			if ($('body').height() < ($('#plan').offset().top + $('#plan').height())) {
				var top = $(window).height() - $('#plan').height() - 20;
				$('#plan').css('top', top+'px');
			}
			$('#plan').animate({left: '50%'}, 750, function() {
				var top = $(this).offset().top;
				$('#plan').css({'position': 'absolute', 'top': top+'px'});
			});
		});
		$('#plan').animate({left: '50%'}, 750);
	});
	// Этажи и подъезды
	$('p .press').live('click', function() {
		if (!$(this).hasClass('active')) {
			$(this).parent().children().removeClass('active');
			$(this).addClass('active');
			var object = $('p.object').find('span').attr('id');
			var entrance = $('p.entrance').find('span.active').attr('id');
			var floor = $('p.floor').find('span.active').attr('id');
			$('#plan').load('/forms/plan.php?object='+object+'&entrance='+entrance+'&floor='+floor, function(success) {
				if ($('body').height() < ($('#plan').offset().top + $('#plan').height())) {
					var top = $(window).height() - $('#plan').height() - 20;
					$('#plan').css('top', top+'px');
				}
			});
		}
	});
	// Звёзды на карте
	$('#plan .star').live('click', function() {
		if (!$(this).hasClass('selected')) {
			$(this).parent().children().removeClass('selected');
			$(this).addClass('selected');
			$('#plan').load('/forms/plan.php?id='+$(this).attr('id'), function(success) {
				if ($('body').height() < ($('#plan').offset().top + $('#plan').height())) {
					var top = $(window).height() - $('#plan').height() - 20;
					$('#plan').css('top', top+'px');
				}
			});
		}
	});
/* Очистка строки поиска */
	$('#search input').focus(function() {
		var sr;
		if ($(this).val() == "Поиск по сайту" || $(this).val() == "Search") {
			sr = $(this).val();
			$(this).val("");
		}
		$(this).blur(function() {
			if ($(this).val() == "") $(this).val(sr);
		});
	});
/* Списки */
	$('h3 .pressable').click(function() {
		var arr = new Array();
		arr = $(this).attr('id').split('_');
		//console.log($(this));
		if ($('#selector').length) {
			if ($('.ingroup#s_'+arr[1]+'_'+arr[2]).css('display') == 'block') {
				$(this).removeClass('active');
				$('.ingroup#s_'+arr[1]).slideUp();
			} else {
				$('.ingroup#s_'+arr[1]).slideDown();
				$(this).addClass('active');
			}
		} else {
			if ($('.ingroup#s_'+arr[1]+'_'+arr[2]).css('display') == 'block') {
				$(this).removeClass('active');
				$('.ingroup#s_'+arr[1]+'_'+arr[2]).slideUp();
			} else {
				$('.ingroup#s_'+arr[1]+'_'+arr[2]).slideDown();
				$(this).addClass('active');
			}
		}
	});
/* Навигация слева */
	$('#left_menu .mb .pressable').click(function() {
		$('#right_column').css('min-height', 0);
		if (!$(this).parent().hasClass('active')) {
			$(this).parent().addClass('active');
			$(this).parent().parent().find('.cont').stop(true, true).slideDown(function() {
				if ($('#left_column').height() >= $('#right_column').height()) {
					$('#right_column').css('min-height', $('#left_column').height()-30);
				}
			});
		} else {
			$(this).parent().removeClass('active');
			$(this).parent().parent().find('.cont').stop(true, true).slideUp(function() {
				if ($('#left_column').height() >= $('#right_column').height()) {
					$('#right_column').css('min-height', $('#left_column').height()-30);
				}
			});
		}
	});
/* Сортировка таблицы */
	$('.sel').click(function() {
		if ($(this).attr('id') == 's_all') {
			$('table.sortable tbody tr').show();
			$('.sel').removeClass('active');
			$(this).addClass('active');
		} else {
			$('.sel').removeClass('active');
			$(this).addClass('active');
			arr = [];
			arr = $(this).attr('id').split('_');
			$('table.sortable tbody tr').hide();
			$('table.sortable tbody tr.rooms_'+arr[1]).show();
		}
	});
/* Галерея */
	if ($('.gallery').length) {
		check_scroll();
		$('.gallery').each(function(){
			// Клик на заголовок
			$(this).find('.header .pressable').click(function() {
				var arr = [];
				arr = $(this).attr('id').split('_');
				if (!$(this).parent('p').hasClass('active')) {
					if (open_gal == undefined) {
						open_gallery(arr[1]);
					} else {
						close_gallery(open_gal);
						open_gallery(arr[1]);
					}
				} else {
					close_gallery(arr[1]);
					open_gal = undefined;
				}
			});
			// Клик на изоюражение
			$(this).find('.images .img').click(function() {
				var arr = [];
				arr = $(this).attr('id').split('_');
				if (open_gal == undefined) {
					open_gallery(arr[1], arr[2]);
				} else if (open_gal != 'all' && arr[1] != open_gal) {
					close_gallery(open_gal);
					open_gallery(arr[1], arr[2]);
				} else if (open_gal == 'all') {
					clear_act();
					act_img(arr[2], arr[1]);
				} else {
					clear_act();
					act_img(arr[2]);
				}
				var left = parseInt(arr[2]*145-145) + $(this).width()/2 - $(this).parent('.images').width()/2;
				$(this).parent('.images').scrollTo({'left': left, 'top': 0}, "normal");
			});
			// Клик на стрелки
			$(this).find('.arrow').click(function() {
				if ($(this).hasClass('left')) {
					change_img('left');
				} else {
					change_img('right');
				}
			});
		});
	}
/* Консультации */
	if ($('.selectable').length && $('#selector').length) {
		// Старт
			open_con = 0;
			$('#selector .select:first').addClass('active');
			$('#selector .select:first p').addClass('white');
			$('#selector .select:first p span').removeClass('pressable');
			$('.selectable').show();
		// Выбор категории
		$('#selector .select').click(function() {
			var arr = [];
			arr = $(this).attr('id').split('_');
			$('#selector .select#p_'+open_con).removeClass('active');
			$('#selector .select#p_'+open_con+' p').removeClass('white');
			$('#selector .select#p_'+open_con+' p span').addClass('pressable');
			if (open_con == 0) {
				$('.selectable').hide();
			} else {
				$('.selectable#c_'+open_con).hide();
			}
			open_con = arr[1];
			$('#selector .select#p_'+open_con).addClass('active');
			$('#selector .select#p_'+open_con+' p').addClass('white');
			$('#selector .select#p_'+open_con+' p span').removeClass('pressable');
			if (open_con == 0) {
				$('.selectable').show();
			} else {
				$('.selectable#c_'+open_con).show();
			}
		});
		// выбр вопроса
		$('.selectable .pressable').click(function() {
			var arr = [];
			arr = $(this).attr('id').split('_');
			$('#s_'+arr[1]+'_'+arr[2]).slideToggle();
		});
	} else if ($('#selector').length) {
/* Фильтры районов */
		// Старт
		$('#selector .select#dist_'+open_dist).addClass('active');
		$('#selector .select#dist_'+open_dist+' p').addClass('white');
		$('#selector .select#dist_'+open_dist+' p span').removeClass('pressable');
		$('#selector .select span').click(function() {
			if ($(this).parent().parent().attr('id').substr(0, 4) == 'dist') {
				$('#selector .select#dist_'+open_dist).removeClass('active');
				$('#selector .select#dist_'+open_dist+' p').removeClass('white');
				$('#selector .select#dist_'+open_dist+' p span').addClass('pressable');
				var arr = [];
				arr = $(this).parent().parent().attr('id').split('_');
				open_dist = arr[1];
				$('#selector .select#dist_'+open_dist).addClass('active');
				$('#selector .select#dist_'+open_dist+' p').addClass('white');
				$('#selector .select#dist_'+open_dist+' p span').removeClass('pressable');
				if (arr[1] != 'all') {
					if ($('#index').length) {
						$('.thumbs .thumb').each(function() {
							if ($(this).parent().parent().parent().parent().attr('id') != 'estates') {
								$(this).removeClass('visible');
								$('.thumbs .thumb.distr_'+open_dist).addClass('visible');
							}
						});
					} else {
						$('.thumbs .thumb').hide();
						$('.thumbs .thumb.distr_'+open_dist).show();
					}
				} else {
					if ($('#index').length) {
						$('.thumbs .thumb').addClass('visible');
					} else {
						$('.thumbs .thumb').show();
					}
				}
				if ($('#left_column').height() >= $('#right_column').height()) {
					$('#right_column').css('min-height', $('#left_column').height()-30);
				}
			} else {
				$('#selector .select').removeClass('active');
				$('#selector .select p').removeClass('white');
				$('#selector .select p span').addClass('pressable');
				$(this).parent().parent().addClass('active');
				$(this).parent().addClass('white');
				$(this).removeClass('pressable');
			}
		});
	}
/* Ченджер */
	//
	$('.changer .button').hover(function() {
		if (!$(this).hasClass('active')) {
			if ($(this).find('p').hasClass('lenta')) {
				$(this).find('p').css('background-position', '0 -23px');
			} else {
				$(this).find('p').css('background-position', '-134px -26px');
			}
		}
	}, function() {
		if (!$(this).hasClass('active')) {
			if ($(this).find('p').hasClass('lenta')) {
				$(this).find('p').css('background-position', '0 7px');
			} else {
				$(this).find('p').css('background-position', '-134px 4px');
			}
		}
	});
	if ($('#grey .changer').length) {
		// Старт
		$('.changer .button.fleft').addClass('active');
		$('.changer .button.fleft p').addClass('white');
		$('.changer .button.fleft span').removeClass('pressable');
		// События
		$('.changer .button').click(function() {
			if (!$(this).hasClass('active')) {
				if ($(this).hasClass('fright')) {
					$(this).parent().find('.button.fleft').removeClass('active');
					$(this).parent().find('.button.fleft p').removeClass('white');
					$(this).parent().find('.button.fleft p').css('background-position', '0 7px');
					$(this).parent().find('.button.fleft span').addClass('pressable');
					$(this).parent().find('.button.fright').addClass('active');
					$(this).parent().find('.button.fright p').addClass('white');
					$(this).parent().find('.button.fright span').removeClass('pressable');
				} else {
					$(this).parent().find('.button.fright').removeClass('active');
					$(this).parent().find('.button.fright p').removeClass('white');
					$(this).parent().find('.button.fright p').css('background-position', '-134px 4px');
					$(this).parent().find('.button.fright span').addClass('pressable');
					$(this).parent().find('.button.fleft').addClass('active');
					$(this).parent().find('.button.fleft p').addClass('white');
					$(this).parent().find('.button.fleft span').removeClass('pressable');
				}
			}
		});
		$('#estates .changer .button').click(function() {
			if ($(this).hasClass('active')) {
				if ($(this).hasClass('fright')) {
					$('#estates .slider_container').css('left', '0');
					$('#estates .thumbs').removeClass('nobr');
					$('#estates .m_slider_controls').hide();
				} else {
					$('#estates .thumbs').addClass('nobr');
					$('#estates .m_slider_controls').show();
				}
			}
		});
		$('#grey .changer .button').click(function() {
			if ($(this).hasClass('active')) {
				if ($(this).hasClass('fright')) {
					$('#index .slider_container').css('left', '0');
					$('#index .thumbs').removeClass('nobr');
					$('#index .m_slider_controls').hide();
				} else {
					$('#index .thumbs').addClass('nobr');
					$('#index .m_slider_controls').show();
				}
			}
		});
	}
/* Двухколончатость */
	$('.columns').each(function() {
		if ($(this).find('.column').height()/2%20 != 0) {
			var co = parseInt($(this).find('.column').height()/40) + 1;
			$(this).height(co*20);
			$(this).find('.column.right').css('top', -$(this).height());
		} else {
			$(this).height($(this).find('.column').height()/2);
			$(this).find('.column.right').css('top', -$(this).height());
		}
	});
/* Материалы в корзину */
	$('.y_but').click(function() {
		var id = $(this).attr('id');
		var quan = $(this).parent().parent().find('input').val();
		if (quan>0) {
			$(this).hide();
			$(this).parent().find('.y_loading').show();
			if ($(this).hasClass('pressed')) {
				$(this).load('/inc/materials.php?id='+id+'&del=1', function(success) { // удаление из корзины
					$(this).removeClass('pressed').attr('title', 'Добавить в заказ');
					$('#p_totalsum').html(success);
					var s = success.substr(18, success.length-1);
					$('#totalSum_span').html(s);
					$(this).parent().parent().find('input').val('0');
					$(this).parent().parent().removeClass('yellowBg');
					$(this).html('');
				});
				if ($('span.basket').length) {
					$('span.basket strong').html(parseInt($('span.basket strong').html())-1)
					/*$(this).load('/inc/materials.php?id='+id+'&add='+quan, function(success) {
						var s = parseInt(success) - 1;
						var decCache = [],
							decCases = [2, 0, 1, 1, 1, 2];
						function decOfNum(number, titles)
						{
							if(!decCache[number]) decCache[number] = number % 100 > 4 && number % 100 < 20 ? 2 : decCases[Math.min(number % 10, 5)];
							return titles[decCache[number]];
						}
						var goodsString = decOfNum(parseInt(success,10), ['товар', 'товара', 'товаров']);
						$('span.basket').html('<strong>'+s+'</strong>&nbsp;'+goodsString);
						$(this).html('');
					});*/
				}
			} else {
				$(this).load('/inc/materials.php?id='+id+'&add='+quan, function(success) { // добавление из каталога материалов
					var decCache = [],
					    decCases = [2, 0, 1, 1, 1, 2];
					function decOfNum(number, titles)
					{
					    if(!decCache[number]) decCache[number] = number % 100 > 4 && number % 100 < 20 ? 2 : decCases[Math.min(number % 10, 5)];
					    return titles[decCache[number]];
					}
					var goodsString = decOfNum(parseInt(success,10), ['товар', 'товара', 'товаров']);
					$('span.basket').html('<strong>'+success+'</strong>&nbsp;'+goodsString);
					$(this).html('');
				});
			}
			$(this).parent().find('.y_loading').hide();
			$(this).addClass('pressed').show().attr('title', 'Убрать из заказа');
			$('#basket_p').show();
			$('#basket_div').css('visibility','visible');
			$(this).parent().parent().addClass('yellowBg');
			/*$(this).parent().find('.y_galochka').show();
			var galochka = $(this).parent().find('.y_galochka');
			setTimeout(function () {
				galochka.hide();
			}, 5000);*/
		}
	});
	$('.basket_input').keyup(function() {
		var id = $(this).attr('id');
		var quan = $(this).val();
		$(this).load('/inc/materials.php?id='+id+'&quan='+quan, function(success) { // изменение в корзине
			$('#p_totalsum').html(success);
		});
	});
	
/* Баги */
	if ($.browser.opera) {
		operabugs();
	}
	if ($.browser.webkit) {
		webkitbugs();
	}
	if (($.browser.msie && $.browser.version >= 9) || $.browser.mozilla) {
		$('span.rur').each(function() {
			if ($(this).parent('p').hasClass('bigest')) {
				$(this).css('background-position', '0 13px');
			} else if ($(this).parent()[0].tagName == 'TD') {
				$(this).css('background-position', '-16px 6px');
			}
		});
	}
});

$(window).load(function() {
/* Баги */
	if ($('#estates').width() > 184*$('#estates .thumb').length) {
		$('#estates .changer').hide();
	}
	if ($('#left_column').height() >= $('#right_column').height()) {
		$('#right_column').css('min-height', $('#left_column').height()-30);
	}
	$('body').css('background', 'url(/pics/bg/footer.png) repeat-x center bottom');
	$('#footer').show();
});

$(window).resize(function() {
/* Двухколончатость */
	if ($('#estates').width() > 184*$('#estates .thumb').length) {
		$('#estates .changer').hide();
	} else {
		$('#estates .changer').show();
	}
	$('.columns').each(function() {
		if ($(this).find('.column').height()/2%20 != 0) {
			var co = parseInt($(this).find('.column').height()/40) + 1;
			$(this).height(co*20);
			$(this).find('.column.right').css('top', -$(this).height());
		} else {
			$(this).height($(this).find('.column').height()/2);
			$(this).find('.column.right').css('top', -$(this).height());
		}
	});
/* Галерея */
	if ($('.gallery').length) {
		check_scroll();
	}
/* Баги */
	if ($('#left_column').height() >= $('#right_column').height()) {
		$('#right_column').css('min-height', $('#left_column').height()-30);
	}
	if ($.browser.opera) {
		operabugs();
	}
	if ($.browser.webkit) {
		webkitbugs();
	}
});

/* Реактив */
var ShowReaktiveFlag = false;
function ShowReaktiveStart() {
	ShowReaktiveFlag = true; setTimeout("ShowReaktive()",50);
}
function ShowReaktive() {
	if ((ShowReaktiveFlag) && ($("#reaktive_block").css('display')=='none')) {
		$("#reaktive_block").css({display: "block"});
		$("#reaktive_block img").css({width: 0, height: 0, left: 60, top: 0});
		$("#reaktive_block img").animate({width: 274, height: 131, left: 0, top: -125}, 200);
	}
}
function HideReaktiveStart() {
	ShowReaktiveFlag = false; setTimeout("HideReaktive()",50);
}
function HideReaktive() {
	if ((!ShowReaktiveFlag) && ($("#reaktive_block").css('display')!='none')) {
		if ($.browser.msie) $("#reaktive_block").css('display','none');
		else $("#reaktive_block").fadeOut(300);
	}
}

/* Галерея */
// Скроллинг
function check_scroll() {
	$('.gallery').each(function() {
		if ($(this).find('.images').width() < $(this).find('.images .img').length * 140) {
			$(this).find('.images').css({'height': '128px', 'overflow-x': 'scroll'});
		} else {
			$(this).find('.images').css({'height': '112px', 'overflow-x': 'hidden'});
		}
	});
}
// Открыть галлерею
function open_gallery(gal, pict) {
	open_gal = gal;
	$('.gallery#g_'+open_gal+' .header p').addClass('active');
	if (pict == undefined) {
		pict = 1;
	}
	$('.gallery#g_'+open_gal+' .image').slideToggle();
	act_img(pict);
	if ($('#left_column').height() > $('#right_column').height()) {
		$('#right_column').height($('#left_column').height());
	} else {
		$('#right_column').height('auto');
	}
}
// Закрыть галлерею
function close_gallery(gal) {
	$('.gallery .images .img').removeClass('active');
	$('.gallery#g_'+gal+' .header p').removeClass('active');
	$('.gallery#g_'+gal+' .image').slideToggle();
}
// Активное изображение
function act_img(img, gal) {
	if (gal != undefined) {
		$('div#i_'+gal+'_'+img).addClass('active');
		$('.gallery#g_'+gal+' .image img').attr('src', $('div#i_'+gal+'_'+img).attr('value'));
	} else {
		$('div#i_'+open_gal+'_'+img).addClass('active');
		$('.gallery#g_'+open_gal+' .image img').attr('src', $('div#i_'+open_gal+'_'+img).attr('value'));
	}
}
// Очистка
function clear_act() {
	$('.gallery .images .img').removeClass('active');
}
// Листать
function change_img(direct){
	if (open_gal == 'all') {
		var quan = $('.gallery#g_2 .images .img').length;
		var arr = [];
		var next_img = 0;
		$('.gallery#g_2 .images .img').each(function() {
			if ($(this).hasClass('active')){ arr = $(this).attr('id').split('_')};
		});
	} else {
		var quan = $('.gallery#g_'+open_gal+' .images .img').length
		var arr = [];
		var next_img = 0;
		$('.gallery#g_'+open_gal+' .images .img').each(function() {
			if ($(this).hasClass('active')) arr = $(this).attr('id').split('_');
		});
	}
	if (direct == 'left') {
		next_img = parseInt(arr[2], 10) - 1;
		if (next_img < 1) next_img += quan;
	} else {
		next_img = parseInt(arr[2],10) + 1;
		if (next_img > quan) next_img -= quan;
	}
	clear_act();
	act_img(next_img, arr[1]);
}

/* Баги */
function operabugs() {
	$('#nav').width('100%');
	$('#nav .part.l').width('1px');
	if ($('#nav').width()%2 != 0) {
		if ($('#nav').width() == ($('#nav .part.l').width() + $('#nav .part.r').width())) {} else {
			var wi = $('#nav').width() - $('#nav .part.l').width();
			$('#nav .part.r').width(wi);
		}
	}
	$('.y_button').each(function() {
		$(this).width('auto');
		if ($(this).width()%2 != 0) {
			$(this).width($(this).width() - 1);
		}
	});
}

function webkitbugs() {
	$('#nav').width('100%');
	$('#nav .part.l').width('75%');
	if ($('#nav').width()%2 != 0) {
		if ($('#nav').width() == ($('#nav .part.l').width() + $('#nav .part.r').width())) {} else {
			var wi = $('#nav').width() - $('#nav .part.l').width();
			$('#nav .part.r').width(wi);
		}
	}
	$('.y_button').each(function() {
		$(this).width('auto');
		if ($(this).width()%2 != 0) {
			$(this).width($(this).width() - 1);
		}
	});
}

function removeThisFuckingPopup(){
	$('#popupp').animate({top:-10}, 'slow');
	$('#popupp').hover(function(){
		$(this).animate({top:-70}, 'slow');
	});
	
	$('#popupp').oneTime(5000, function(){
		$(this).animate({top:-70}, 'slow');
	});
}

