var wheel_pos=0;
var mouse_pos=0;
var wait=false;
var ind=0;
var ind_plan=0;
var loaded=0;
var load_timer;

$(document).ready(function() {
	/* images & maps wrap */
	wrap_images();
	wrap_maps();
	/* /images & maps wrap */

	/* left & right column height */
	if(!wait) set_column_sizes();
	/* /left & right column height */
	
	/* left menu highlight */
	if ($('.menu_left a.sel').size() > 0) {
		menu_left.init();
		$('.wheel div').flash({
			swf: "/img/wheel.swf",
			width: 450,
			height: 460,
			wmode: 'transparent',
			play: true,
		});
	}
	/* /left menu highlight */

	/* bell */
	if ($('.bell').size() > 0 && $('.bell').css('display') == 'block') {
		$('.bell').flash({
			swf: "/img/bell.swf",
			width: 255,
			height: 490,
			wmode: 'transparent',
			play: true,
		});
	}
	
	if ($('.wheel').size() > 0) {
		
	}
	/* /bell */

	/* index images change */
	if ($('.index_images').size() > 0) {
		index_images.init();
	}
	/* /index images change */

	/* focus input */
	$('input[rel]').focus(function() {
		if($(this).val() == $(this).attr('rel')) {
			$(this).val('');
		}
	}).blur(function() {
		if($(this).val() == '') {
			$(this).val($(this).attr('rel'));
		}
	});
	/* /focus input */
	
	/* checkbox */
	if ($('.check').size() > 0) {
		$('.check input[type=checkbox]').each(function() {
			if ($(this).attr('checked')) {
				$(this).parents('.check').removeClass('check_off').addClass('check_on');
			}
			else {
				$(this).parents('.check').removeClass('check_on').addClass('check_off');
			}
		}).bind('change', function() {
			if ($(this).attr('checked')) {
				$(this).parents('.check').removeClass('check_off').addClass('check_on');
			}
			else {
				$(this).parents('.check').removeClass('check_on').addClass('check_off');
			}
		});
	}
	/* /checkbox */
	
	/* radio */
	if ($('.radio').size()) {
		$('.radio input[type=radio]').each(function() {
			if ($(this).attr('checked')) {
				$(this).parents('.radio').addClass('radio_on');
			}
			else {
				$(this).parents('.radio').addClass('radio_off');
			}
		});
		$('.radio input[type=radio]').bind('change', function() {
			$(this).parents('.radios').find('.radio').removeClass('radio_on').addClass('radio_off');
	
			if ($(this).attr('checked')) {
				$(this).parents('.radio').removeClass('radio_off').addClass('radio_on');
			}
		});
	}
	/* /radio */

	/* select */
	$('.select').each(function() {
		$(this).find('select').sSelect();
	});
	/* /select */

	/* infra map */
	if ($('.infra_map').size() > 0) {
		$('.infra_map .infra_map_link').hover(function() {
			var ind = $(this).attr('href').replace('#im', '');
			var img = $('.infra_map_img');
			img.find('img').attr('src', '/img/infra_img' + ind + '_s.jpg');
			img.css({'left': $(this).position().left - 58 + 'px', 'top': $(this).position().top - 20 + 'px'}).show();
		}, function() {
			var img = $('.infra_map_img');
			img.find('img').attr('src', '');
			img.hide();
		});
	}
	/* /infra map */
	
	/* infra plan */
	if ($('.infra_in_lnk').size() > 0) {
		infra.init();
	}
	/* /infra plan */

	/* table lined */
	if ($('.table_lined').size() > 0) {
		$('.table_lined').each(function(index, element) {
			create_table($(this));
		});
	}
	/* /table lined */
	
	/* calculator */
	if ($('.calc').size() > 0) {
		calc.init();
	}
	/* /calculator */
	
	/* plan */
	if ($('.plan_small').size() > 0) {
		var img=new Array();
		for(var i in info_arr){
			img[i] = new Image();
			img[i].src='/img/plan_small/p' + i + '_' + info_arr[i].status + '.gif';
			img[i].onload=function(){
				clearTimeout(window.load_timer);
				window.loaded++;
				$('#load_percent').text(Math.floor(window.loaded/422*100));
				if(window.loaded==422){
					clearTimeout(window.load_timer);
					plan.init();
					$("#loader").hide();
					$(".map.plan_small").show();
				};
				window.load_timer=setTimeout(function(){
					$("#loader").hide();
					$(".map.plan_small").show();
				}, 1000);
			}
		}
	}
	/* /plan */

	/* photos */
	if ($('.photo .zoom').size() > 0) {
		$('.photo .zoom').prev().hover(function(){
			$(this).css('cursor','pointer');
		});
		$('.photo .zoom').prev().click(function(){
			ind=$(this).parent('.photo').index();
			show_photo($(this).parent().find('img:eq(1)').attr('id'));
		});
		$('.photo .zoom').click(function(){
			ind=$(this).parent('.photo').index();
			show_photo($(this).parent().find('img:eq(1)').attr('id'));
		});
		$('.gal_popup .close').click(function(){
			$(".gal_popup").hide();
			$(".gal_popup_bg").hide();
		});
		
		if ($('.photo .zoom').size() > 1)
			$(".gal_popup").find('.prev, .next').show();
		else
			$(".gal_popup").find('.prev, .next').hide();
		
		$('.gal_popup .next').click(function(){
			ind++;
			show_next_photo(ind);
		});
		$('.gal_popup .prev').click(function(){
			ind--;
			show_prev_photo(ind);
		});	
	}
	
	if ($('.plan .zoom').size() > 0) {
		$('.plan .zoom').prev().hover(function(){
			$(this).css('cursor','pointer');
		});
		$('.plan .zoom').prev().click(function(){
			ind_plan=$(this).parent('.plan').index();
			show_plan($(this).parent().find('img:eq(1)').attr('id'));
		});
		$('.plan .zoom').click(function(){
			ind_plan=$(this).parent('.plan').index();
			show_plan($(this).parent().find('img:eq(1)').attr('id'));
		});
		$('.pr_plan_popup .close').click(function(){
			$(".pr_plan_popup").hide();
			$(".pr_plan_popup_bg").hide();
		});
		if ($('.plan .zoom').length > 1)
			$(".pr_plan_popup").find('.prev, .next').show();
		else
			$(".pr_plan_popup").find('.prev, .next').hide();
		
		$('.pr_plan_popup .next').click(function(){
			ind_plan++;
			show_next_plan(ind_plan);
		});
		$('.pr_plan_popup .prev').click(function(){
			ind_plan--;
			show_prev_plan(ind_plan);
		});
		
	}
	/* /photos */
	$('input[type=text]').each(function(){
		if($(this).val()=='') $(this).val($(this).attr('rel'));
	});
	$('input[type=text]').focus(function(){
		if($(this).val()==$(this).attr('rel')) $(this).val('');
	});
	$('input[type=text]').blur(function(){
		if($(this).val()=='') $(this).val($(this).attr('rel'));
	});
	if ($('.news_unsubcribe').size() > 0) {
        $('.news_unsubcribe').bind('click', function() {
        	email=$(document.forms['form_news']).find('input[name=EMAIL]').val();
        	location.href='?EMAIL='+email+'&action=unsubscribe';
        });
    }
	if ($('.news_subcribe').size() > 0) {
        $('.news_subcribe').bind('click', function() {
        	send_form(document.forms['form_news']);
        });
    }
});

function show_plan(src){
	$(".pr_plan_popup .photo_block img").attr("src", src);
	
		if($(".pr_plan_popup").is(':hidden')){
			$(".pr_plan_popup").show();
			$(".pr_plan_popup_bg").height($(document).height()).show();
			$(".pr_plan_popup .photo_block").show();
		}
	$(".pr_plan_popup .photo_block img").load(function(){
		var win_h = $(window).height();
		var popup_h = $(".pr_plan_popup").height();
		var popup_w = $(".pr_plan_popup").width();
		var top=(win_h - popup_h) / 2 + $(document).scrollTop();
		if (top<=10) top=10; 
		if((top+popup_h)>$(document).height()-10) top=$(document).height()-popup_h-10;
		$(".pr_plan_popup").css({
			'top': top + 'px',
			'marginLeft': - popup_w / 2 + 'px'
		});
	});	
};

function show_next_plan(){
	if (($('.plan').size()-1<window.ind_plan)) window.ind_plan=0;
	show_plan($('.plan:eq('+(window.ind_plan)+')').find('img:eq(1)').attr('id'));
};
function show_prev_plan(){
	if (window.ind_plan<0){
		window.ind_plan=$('.plan').size()-1;
	}
	show_plan($('.plan:eq('+(window.ind_plan)+')').find('img:eq(1)').attr('id'));
};

function show_photo(src){
	$(".gal_popup .photo_block img").attr("src", src);
	
		if($(".gal_popup").is(':hidden')){
			$(".gal_popup").show();
			$(".gal_popup_bg").height($(document).height()).show();
			$(".photo_block").show();
		}
	$(".gal_popup .photo_block img").load(function(){
		var win_h = $(window).height();
		var popup_h = $(".gal_popup").height();
		var popup_w = $(".gal_popup").width();
		var top=(win_h - popup_h) / 2 + $(document).scrollTop();
		if (top<=10) top=10; 
		if((top+popup_h)>$(document).height()-10) top=$(document).height()-popup_h-10;
		$(".gal_popup").css({
			'top': top + 'px',
			'marginLeft': - popup_w / 2 + 'px'
		});
	});	
};

function show_next_photo(){
	if (($('.photo').size()-1<window.ind)) window.ind=0;
	show_photo($('.photo:eq('+(window.ind)+')').find('img:eq(1)').attr('id'));
};
function show_prev_photo(){
	if (window.ind<0){
		window.ind=$('.photo').size()-1;
	}
	show_photo($('.photo:eq('+(window.ind)+')').find('img:eq(1)').attr('id'));
};

function show_tab(ind){
	$('.plan').hide();
	$('.plan_links.sel').removeClass('sel');
	$('.plan_links:eq('+ind+')').addClass('sel');
	if($('.plan:eq('+ind+')').is(":hidden")){
		$('.plan:eq('+ind+')').show();
	}
}

function detectIE6() {
	var browser = navigator.appName;
	if (browser == "Microsoft Internet Explorer") {
		var b_version = navigator.appVersion;
		var re = /\MSIE\s+(\d\.\d\b)/;
		var res = b_version.match(re);
		if (res[1] <= 6) {
			return true;
		}
	}
	return false;
}

function wrap_images() {
	var imgs = $('.page p img, .page_info p img, #YandexMap, #loader .loader').not('.no_wrap');
	imgs.each(function() {
		$(this).wrap('<span class="img_wrap"></span>');
		var wrap = $(this).parents('.img_wrap');
		var w = wrap.width();
		var h = wrap.height();
		var ext = (detectIE6()) ? 'gif' : 'png';
		if (w > 250 || h > 170) {
			var coef = w / 627;
			wrap.prepend('<img src="/img/img_bg_b.' + ext + '" width="' + (w + Math.ceil(32 * coef)) + '" height="' + (h + Math.ceil(32 * coef)) + '" class="img_bg" />');
			wrap.find('.img_bg').css({
				'left': - Math.ceil(20 * coef) + 'px',
				'top': - Math.ceil(12 * coef) + 'px'
			});
		}
		else if (w > 140 || h > 140) {
			var coef = w / 247;
			wrap.prepend('<img src="/img/img_bg_s.' + ext + '" width="' + (w + Math.ceil(30 * coef)) + '" height="' + (h + Math.ceil(30 * coef)) + '" class="img_bg" />');
			wrap.find('.img_bg').css({
				'left': - Math.ceil(18 * coef) + 'px',
				'top': - Math.ceil(12 * coef) + 'px'
			});
		}
		else if (w > 70 || h > 70) {
			var coef = w / 134;
			wrap.prepend('<img src="/img/img_bg_ss.' + ext + '" width="' + (w + Math.ceil(28 * coef)) + '" height="' + (h + Math.ceil(28 * coef)) + '" class="img_bg" />');
			wrap.find('.img_bg').css({
				'left': - Math.ceil(16 * coef) + 'px',
				'top': - Math.ceil(10 * coef) + 'px'
			});
		}
		else {
			var coef = w / 134;
			wrap.prepend('<img src="/img/img_bg_ss.' + ext + '" width="' + (w + Math.ceil(34 * coef)) + '" height="' + (h + Math.ceil(34 * coef)) + '" class="img_bg" />');
			wrap.find('.img_bg').css({
				'left': - Math.ceil(18 * coef) + 'px',
				'top': - Math.ceil(14 * coef) + 'px'
			});
		}
		if ($(this).attr('align') == 'left') {
			wrap.css({
				'float': 'left',
				'marginRight': Math.ceil(18 * coef) + 10 + 'px'
			});
			wrap.parents('p').css({'clear': 'both'});
		}
        if (wrap.parents('p').find('.zoom').size() > 0) {
            wrap.parents('p').find('.zoom').css({
                'top': h - 10 + 'px',
                'left': w - 23 + 'px'
            });
        }
  	});
}

function wrap_maps() {
	var maps = $('.map iframe');
	maps.each(function() {
		$(this).wrap('<span class="img_wrap"></span>');
		var wrap = $(this).parents('.img_wrap');
		var w = wrap.width();
		var h = wrap.height();
		var ext = (detectIE6()) ? 'gif' : 'png';
		var coef = w / 627;
		wrap.prepend('<img src="/img/img_bg_b.' + ext + '" width="' + (w + Math.ceil(32 * coef)) + '" height="' + (h + Math.ceil(32 * coef)) + '" class="img_bg" />');
		wrap.find('.img_bg').css({
			'left': - Math.ceil(20 * coef) + 'px',
			'top': - Math.ceil(12 * coef) + 'px'
		});
	});
}

function set_column_sizes() {
	$('.left_col').height($('.main').height() - $('.header').height() - $('.footer').height());
	$('.right_col').height($('.left_col').height());
}

function create_table(table) {
	table.find('th').eq(0).addClass('td_first');
	table.find('th').eq(table.find('th').size()-1).addClass('td_last');
	table.find('tr').eq(table.find('tr').size()-1).addClass('tr_last');
	table.find('tr').each(function(ind) {
		$(this).find('td').eq(0).addClass('td_first');
		$(this).find('td').eq($(this).find('td').size()-1).addClass('td_last');
		if (Math.ceil(ind/2) != Math.floor(ind/2)) {
			$(this).addClass('odd');
		}
	});
}

var menu_left = {
	index_cur: -1,
	init: function () {
		var ind = $('.menu_left a').index($('.menu_left a.sel'));
		menu_left.select_item(ind);
		
		/*
		$('.menu_left a').mouseover(function() {
			var ind = $('.menu_left a').index($(this));
			menu_left.move_wheel(ind);
			menu_left.select_item(ind);
		});
		$('.menu_left').mouseleave(function() {
			var ind = $('.menu_left a').index($('.menu_left a.sel'));
			menu_left.select_item(ind);
		});
		*/
		$('.menu_left a').mouseover(function(e) {
			var ind = $('.menu_left a').index($(this));
			
			menu_left.select_item(ind);
		});
		$('.menu_left').mouseover(function(e) {
			mouse_pos=e.pageY;
			menu_left.move_wheel(e.pageY);
		});
		
		$('.menu_left').mouseleave(function() {
			var ind = $('.menu_left a').index($('.menu_left a.sel'));
			menu_left.select_item(ind);
		});
	},
	select_item: function(ind) {
		var lnk = $('.menu_left a').eq(ind);
		$('.menu_left .arrow').css({'top': lnk.position().top + 'px'});
		menu_left.index_cur = ind;
	},
	move_wheel: function(ind) {
		/*
		var wheel_img = $('.wheel div');
		var wheel_pos = wheel_img.position().top;
		var new_pos = '';
		if (ind > menu_left.index_cur) {
			new_pos = (wheel_pos > -2325 ) ? (wheel_pos - 465) : 0;
		}
		else {
			new_pos = (wheel_pos < 0 ) ? (wheel_pos + 465) : -2325;
		}
		wheel_img.css({'top': new_pos + 'px'});
		*/
		if(wheel_pos<=(mouse_pos-290-3)||wheel_pos>=(mouse_pos-290+3)){
			if((mouse_pos-290)>wheel_pos) wheel_pos+=3; else wheel_pos-=3;
			$('.wheel div').flash(function() {
					this.SetVariable('/:mmouse', wheel_pos);
			});
			setTimeout(function(){menu_left.move_wheel(mouse_pos)}, 100);
		}
	}
};

/*
var bell = {
	is_anim: false,
	init: function () {
		$('.bell').mouseover(function() {
			bell.clapper_move();
		});
	},
	clapper_move: function() {
		if(!bell.is_anim) {
			bell.is_anim = true;
			var bell_block = $('.bell')
			bell_block.find('b').show();
			bell_block.find('i').hide();
			var timer = setTimeout(function() {
				bell_block.find('i').show();
				bell_block.find('b').hide();
				bell.is_anim = false;
			}, 100);
		}
	}
};
*/

var index_images = {
	number: 0,
	cur_index: 0,
	speed: 1000,
	imgs: '',
	init: function() {
		index_images.imgs = $('.index_images img');
		index_images.number = index_images.imgs.size();
		index_images.imgs.css({'opacity': 0}).show();
		index_images.imgs.eq(index_images.cur_index).css({'opacity': 1});
		
		$('.index_images .prev').bind('click', function() { index_images.anim_prev(); });
		$('.index_images .next').bind('click', function() { index_images.anim_next(); });
	},
	anim_prev: function() {
		var prev_index = (index_images.cur_index > 0) ? (index_images.cur_index - 1) : (index_images.number - 1);
		index_images.imgs.eq(index_images.cur_index).animate({'opacity': 0}, index_images.speed);
		index_images.imgs.eq(prev_index).animate({'opacity': 1}, index_images.speed, function() {
			index_images.cur_index = prev_index;
		});
	},
	anim_next: function() {
		var next_index = (index_images.cur_index < index_images.number - 1) ? (index_images.cur_index + 1) : 0;
		index_images.imgs.eq(index_images.cur_index).animate({'opacity': 0}, index_images.speed);
		index_images.imgs.eq(next_index).animate({'opacity': 1}, index_images.speed, function() {
			index_images.cur_index = next_index;
		});
	}
};

var calc = {
	init: function() {
		calc.init_slider();
	},
	init_slider: function() {
		$('.calc .slider').each (function (id, item) {
			if (!$(item).hasClass('inited')) {
				var min = parseInt($(item).attr('min'));
				var max = parseInt($(item).attr('max'));
				var step = parseInt($(item).attr('step'));
				var item_inp = $(item).attr('id') + '_inp';
				var order = ($(item).attr('order')=='1000')?' 000':(($(item).attr('order')=='1000000')?' 000 000':'');
				$("#" + item_inp).val(($("#" + item_inp).val()=='')?(min + order):$("#" + item_inp).val());
				$(item).slider({
					range: false,
					min: min,
					max: max,
					value: min,
					step: step,
					slide: function(event, ui) {
						$("#" + item_inp).val(ui.value + ((ui.value!=0)?order:''));
					}
				});
				
				var labels = $(item).parents('.slider_wrap').find('.slider_label');
				labels.each(function(ind) {
					var left = (280 / (labels.size() - 1)) * ind - $(this).width() / 2;
					$(this).css('left', left + 'px');
				});
				
				$(item).addClass('inited');
			}
		});
	}
};

var photos = {
	zoom: '',
	popup_bg: '',
	popup: '',
	photo: '',
	num: 0,
	cur: -1,
	init: function() {
		photos.zoom = $('.photo .zoom');
		photos.popup_bg = $('.gal_popup_bg');
		photos.popup = $('.gal_popup');
		photos.photo = photos.popup.find('.photo_block');
		photos.num = photos.zoom.size();
		
		photos.zoom.bind('click', function() {
			var ind = photos.zoom.index($(this));
			photos.show_popup_bg();
			photos.show_photo(ind);
			photos.show_popup();
			photos.wrap_photo(ind);
		});

		photos.popup.find('.prev').bind('click', function() {
			var ind = -1;
			
			if (photos.cur > 0) {
				ind = photos.cur - 1;
			}
			else {
				ind = photos.num - 1;
			}

			photos.hide_photo();
			photos.hide_popup();

			photos.show_photo(ind);
			photos.show_popup();
			
			photos.wrap_photo(ind);
		});

		photos.popup.find('.next').bind('click', function() {
			var ind = -1;
			
			if (photos.cur < photos.num - 1) {
				ind = photos.cur*1 + 1;
			}
			else {
				ind = 0;
			}

			photos.hide_photo();
			photos.hide_popup();

			photos.show_photo(ind);
			photos.show_popup();
			
			photos.wrap_photo(ind);
		});

		photos.popup_bg.add(photos.popup.find('.close')).bind('click', function() {
			photos.hide_photo();
			photos.hide_popup();
			photos.hide_popup_bg();
		});
	},
	hide_popup_bg: function() {
		photos.popup_bg.hide();
	},
	show_popup_bg: function() {
		var doc_h = $(document).height();
		photos.popup_bg.height(doc_h).show();
	},
	hide_popup: function() {
		photos.popup.hide();
	},
	show_popup: function() {
		var win_h = $(window).height();
		var popup_h = photos.popup.height();
		var popup_w = photos.popup.width();
		photos.popup.css({
			'top': (win_h - popup_h) / 2 + $(document).scrollTop() + 'px',
			'marginLeft': - popup_w / 2 + 'px'
		}).show();
		if (photos.num > 1)
			photos.popup.find('.prev, .next').show();
		else
			photos.popup.find('.prev, .next').hide();
	},
	hide_photo: function() {
		photos.popup.find('.photo_block, .no_photo_block').hide();
	},
	show_photo: function(ind) {
		photos.hide_photo();
		if (photos.photo.eq(ind).size() > 0)
			photos.photo.eq(ind).show();
		else
			photos.popup.find('.no_photo_block').show();
		photos.cur = ind;
	},
	wrap_photo: function(ind) {
		var img = (photos.photo.eq(ind).size()>0)?photos.photo.eq(ind).find('img'):photos.popup.find('.no_photo_block  	.no_photo');
		if (!img.parent().hasClass('img_wrap')) {
			img.wrap('<span class="img_wrap"></span>');
			var wrap = img.parents('.img_wrap');
			var w = wrap.width();
			var h = wrap.height();
			var ext = (detectIE6()) ? 'gif' : 'png';
			var coef = w / 627;
			wrap.prepend('<img src="/img/img_bg_b.' + ext + '" width="' + (w + Math.ceil(32 * coef)) + '" height="' + (h + Math.ceil(32 * coef)) + '" class="img_bg" />');
			wrap.find('.img_bg').css({
				'left': - Math.ceil(20 * coef) + 'px',
				'top': - Math.ceil(12 * coef) + 'px'
			});
		}
	}
};

var plan = {
	block_small: '',
	block_big: '',
	info_small: '',
	info_big: '',
	info_data: '',
	info_data_total: 421,
	point_link_small: '',
	point_link_big: '',
	legend_small: '',
	legend_big: '',
	init: function() {
		plan.block_small = $('.plan_small');
		plan.block_big = $('.plan_big');
		plan.info_small = plan.block_small.find('.plan_info');
		plan.info_big = plan.block_big.find('.plan_info');
		plan.info_data = info_arr;
		plan.legend_small = $('.plan_small_legend');
		plan.legend_big = $('.plan_big_legend');
		
		var num = 0;
		var status = '';
		for(var i in plan.info_data) {
			num++;
			status = plan.info_data[i].status;
			src = plan.info_data[i].src;
			if(status!=2&&src){
				plan.block_small.append('<div class="point p' + num + '" rel="' + status + '"><a href="'+src+'" target="_blank"><img id="point_p' + num + '" src="/img/plan_small/p' + num + '_' + status + '.gif" /></a></div>');
				plan.block_small.append('<div class="point_link p' + num + '" rel="' + i + '"><a href="'+src+'" target="_blank"><img id="point_p' + num + '" src="/img/plan_small/p' + num + '_' + status + '.gif" /></a></div>');
				plan.block_big.append('<div class="point p' + num + '" rel="' + status + '"><a href="'+src+'" target="_blank"><img id="point_p' + num + '" src="/img/plan_big/p' + num + '_' + status + '.gif" /></a></div>');
				plan.block_big.append('<div class="point_link p' + num + '" rel="' + i + '"><a href="'+src+'" target="_blank"><img id="point_p' + num + '" src="/img/plan_big/p' + num + '_' + status + '.gif" /></a></div>');
			}
			else {
				plan.block_small.append('<div class="point p' + num + '" rel="' + status + '"><img id="point_p' + num + '" src="/img/plan_small/p' + num + '_' + status + '.gif" /></div>');
				plan.block_small.append('<div class="point_link p' + num + '" rel="' + i + '"><img id="point_p' + num + '" src="/img/plan_small/p' + num + '_' + status + '.gif" /></div>');
				plan.block_big.append('<div class="point p' + num + '" rel="' + status + '"><img id="point_p' + num + '" src="/img/plan_big/p' + num + '_' + status + '.gif" /></div>');
				plan.block_big.append('<div class="point_link p' + num + '" rel="' + i + '"><img id="point_p' + num + '" src="/img/plan_big/p' + num + '_' + status + '.gif" /></div>');
			}
		}
		
		plan.point_link_small = plan.block_small.find('.point_link');
		plan.point_link_big = plan.block_big.find('.point_link');

		plan.point_link_small.live('mouseover', function() {
			plan.show_info_small($(this));
		});
		plan.point_link_small.live('mouseout', function() {
			plan.hide_info_small();
		});

		plan.point_link_big.live('mouseover', function() {
			plan.show_info_big($(this));
		});
		plan.point_link_big.live('mouseout', function() {
			plan.hide_info_big();
		});

		plan.legend_small.find('input[type=checkbox]').each(function() {
			plan.toggle_houses_small($(this));
			plan.toggle_houses_big($(this));
		});
		plan.legend_small.find('input[type=checkbox]').bind('change', function() {
			plan.toggle_houses_small($(this));
			plan.toggle_houses_big($(this));
		});

		plan.legend_big.find('input[type=checkbox]').each(function() {
			plan.toggle_houses_big($(this));
		});
		plan.legend_big.find('input[type=checkbox]').bind('change', function() {
			plan.toggle_houses_big($(this));
			plan.toggle_houses_small($(this));
		});

		plan.block_small.find('.plan_zoom').bind('click', function() {
			plan.show_plan_big();
		});
		plan.block_big.parents('.plan_popup').find('.close').bind('click', function() {
			plan.hide_plan_big();
		});
		$('.plan_popup_bg').bind('click', function() {
			plan.hide_plan_big();
		});
	},
	hide_info_small: function() {
		plan.info_small.html('').hide();
	},
	show_info_small: function(pnt) {
		var data = plan.info_data[pnt.attr('rel')];
		plan.info_small.append('<table cellpadding="0" cellspacing="0" width="100%"><tr></tr></table>');
		var t1 = plan.info_small.find('table').eq(0);
		t1.append('<caption style="line-height: 18px; font-size: 14px; padding-bottom: 5px;">'+data['name']+'</caption>');
		var t1_tr = t1.find('tr').eq(0);
		if (typeof(data.img)!='undefined')
			t1_tr.append('<td style="padding-top: 5px;"><img src="' + data.img + '" /></td><td width="5">&nbsp;</td><td><table cellspacing="0" cellpadding="0" width="100%"></table></td>');
		else
			t1_tr.append('<td style="padding-top: 5px;"><img src="/img/logo_plan.png" width="120"/></td><td width="5">&nbsp;</td><td><table cellspacing="0" cellpadding="0" width="100%"></table></td>');
		var t2 = t1_tr.find('table').eq(0);
		var units = '';
		for(var i in data) {
			if (i != 'status' && i != 'img' && i != 'name' && i !='src') {
				if (i.match('Площадь')) {
					units = 'м<sup>2</sup>';
				} else {
					units = '';
				}
				t2.append('<tr><td>' + i + ': <strong>' + data[i] + ((units != '')?('&nbsp;' + units):('')) + '</strong></td></tr>');
			}
		}
		if(data.status!=2)
			t1.append('<tr><td style="font-size: 9px; font-weight: normal; color: #122270; padding-top: 5px;" colspan=3 align="center">Для просмотра информации о проекте нажмите левую клавишу мышки</td></tr>')
		var point_left = pnt.position().left;
		var point_top = pnt.position().top;
		var left = Math.min(Math.max(point_left - 180, 0), plan.block_small.width() - plan.info_small.width() - 40);
		var top = (point_top - plan.info_small.height() - 40 >= 0) ? (point_top - plan.info_small.height() - 30) : (point_top + 30);
		plan.info_small.css({'left': left + 'px', 'top': top + 'px'}).show();
	},
	toggle_houses_small: function(check) {
		var status = check.attr('rel');
		var pnt = plan.block_small.find('.point[rel=' + status + ']');
		if (check.attr('checked')) {
			pnt.show();
			$('#pb_status'+status).parents('.check').removeClass('check_off').addClass('check_on');
			$('#pb_status'+status).attr('checked', 'checked');
			check.parents('.legend_item').addClass('sel');
		}
		else {
			pnt.hide();
			$('#pb_status'+status).parents('.check').removeClass('check_on').addClass('check_off');
			$('#pb_status'+status).removeAttr('checked');
			check.parents('.legend_item').removeClass('sel');
		}
	},
	hide_plan_big: function() {
		$('.plan_popup_bg').hide();
		plan.block_big.parents('.plan_popup').hide();
	},
	show_plan_big: function() {
		$('.plan_popup_bg').height($(document).height()).show();
		plan.block_big.parents('.plan_popup').css({
			'top': Math.max($(window).height() / 2 - 383 + $(document).scrollTop(), 0) + 'px'
		}).show();
	},
	hide_info_big: function() {
		plan.info_big.html('').hide();
	},
	show_info_big: function(pnt) {
		var data = plan.info_data[pnt.attr('rel')];
		plan.info_big.append('<table cellpadding="0" cellspacing="0" width="100%"><tr></tr></table>');
		var t1 = plan.info_big.find('table').eq(0);
		t1.append('<caption style="line-height: 18px; font-size: 14px; padding-bottom: 5px;">'+data['name']+'</caption>');
		var t1_tr = t1.find('tr').eq(0);
		if (typeof(data.img)!='undefined')
			t1_tr.append('<td style="padding-top: 5px;"><img src="' + data.img + '" /></td><td width="5">&nbsp;</td><td><table cellspacing="0" cellpadding="0" width="100%"></table></td>');
		else
			t1_tr.append('<td style="padding-top: 5px;"><img src="/img/logo_plan.png" width="120" /></td><td width="5">&nbsp;</td><td><table cellspacing="0" cellpadding="0" width="100%"></table></td>');
		var t2 = t1_tr.find('table').eq(0);
		var units = '';
		for(var i in data) {
			if (i != 'status' && i != 'img' && i != 'name' && i !='src') {
				if (i.match('площадь')) {
					units = 'м<sup>2</sup>';
				} else {
					units = '';
				}
				t2.append('<tr><td>' + i + ': <strong>' + data[i] + ((units != '')?('&nbsp;' + units):('')) + '</strong></td></tr>');
			}
		}
		if(data.status!=2)
			t1.append('<tr><td style="font-size: 9px; font-weight: normal; color: #122270; padding-top: 5px;" colspan=3 align="center">Для просмотра информации о проекте нажмите левую клавишу мышки</td></tr>')
		var point_left = pnt.position().left;
		var point_top = pnt.position().top;
		var left = Math.min(Math.max(point_left - 160, 0), plan.block_big.width() - plan.info_big.width() - 30);
		var top = (point_top - plan.info_big.height() - 40 >= 0) ? (point_top - plan.info_big.height() - 30) : (point_top + 30);
		plan.info_big.css({'left': left + 'px', 'top': top + 'px'}).show();
	},
	toggle_houses_big: function(check) {
		var status = check.attr('rel');
		var pnt = plan.block_big.find('.point[rel=' + status + ']');
		if (check.attr('checked')) {
			pnt.show();
			$('#ps_status'+status).parents('.check').removeClass('check_off').addClass('check_on');
			$('#ps_status'+status).attr('checked', 'checked');
			$('#ps_status'+status).parents('.legend_item').addClass('sel');
		}
		else {
			pnt.hide();
			$('#ps_status'+status).parents('.check').removeClass('check_on').addClass('check_off');
			$('#ps_status'+status).removeAttr('checked');
			$('#ps_status'+status).parents('.legend_item').removeClass('sel');
		}
	}
};

var infra = {
	init: function() {
		$('.infra_in_lnk').bind('click', function() { infra.show_plan(); });
		$('.infra_popup .close, .infra_popup_bg').bind('click', function() {
		infra.hide_plan();
	});

	for(var i in infra_arr) {
		$('.infra_popup .infra_plan').append('<div class="point" rel="' + i + '" num="' + infra_arr[i]['num'] + '" style="left: ' + infra_arr[i]['left'] + 'px; top: ' + infra_arr[i]['top'] + 'px;">' + infra_arr[i]['num'] + '</div>');
		if ($('.infra_popup .infra_plan_legend .legend_item a[num=' + infra_arr[i]['num'] + ']').size() == 0)
			$('.infra_popup .infra_plan_legend').append('<div class="legend_item"><span>' + infra_arr[i]['num'] + '</span><a href="javascript:void(0)" num="' + infra_arr[i]['num'] + '">' + infra_arr[i]['title'] + '</a></div>');
	}

	$('.infra_popup .infra_plan .point').live('mouseover', function() {
		infra.show_info($(this));
		var num = $(this).attr('num');
		$('.infra_popup .infra_plan .legend_item a[num=' + num + ']').addClass('sel');
	});
	$('.infra_popup .infra_plan .infra_plan_info').live('mouseout', function() {
		infra.hide_info();
		$('.infra_popup .infra_plan .legend_item a').removeClass('sel');
	});

	$('.infra_popup .infra_plan .legend_item a').live('mouseover', function() {
		var num = $(this).attr('num');
		$('.infra_popup .infra_plan .point[num=' + num + ']').css('fontWeight', 'bold');
	});
	$('.infra_popup .infra_plan .legend_item a').live('mouseout', function() {
		$('.infra_popup .infra_plan .point').css('fontWeight', 'normal');
	});
	},
	hide_plan: function() {
		$('.infra_popup_bg').hide();
		$('.infra_popup').hide();
	},
	show_plan: function() {
		$('.infra_popup_bg').height($(document).height()).show();
		$('.infra_popup').css({
			'top': Math.max($(window).height() / 2 - 383 + $(document).scrollTop(), 0) + 'px'
		}).show();
		$('.infra_popup .infra_plan_legend_bg').width($('.infra_popup .infra_plan_legend').outerWidth());
		$('.infra_popup .infra_plan_legend_bg').height($('.infra_popup .infra_plan_legend').outerHeight());
		$('.infra_popup .infra_plan_legend').css({
			'left': $('.infra_popup .infra_plan').width() - $('.infra_popup .infra_plan_legend').outerWidth() + 'px',
			'top':  $('.infra_popup .infra_plan').height() - $('.infra_popup .infra_plan_legend').outerHeight() + 'px'
		});
	},
	hide_info: function() {
		$('.infra_popup .infra_plan_info').html('').hide();
	},
	show_info: function(pnt) {
		$('.infra_popup .infra_plan_info').html('');
		var data = infra_arr[pnt.attr('rel')];
		$('.infra_popup .infra_plan_info').append('<table cellpadding="0" cellspacing="0" width="100%"><tr valign="top"><td><img src="' + data.img + '" /></td><td width="10">&nbsp;</td><td><strong>' + data.title + '</strong>' + data.desc + '</td></tr></table>');
		$('.infra_popup .infra_plan_info').css({
		'left': pnt.position().left + 'px',
		'top': pnt.position().top + 'px'
		}).show();
	}
};
