(function(){ 
		/**
	 * Class PSNPopupManager
	 */		
	var PSNPopupTermsClass = (PSNPopupTerms=function() {

		PSNPopupTerms.prototype = new PSNPopup();
		PSNPopupTerms.prototype.currentItem = null;
		PSNPopupTerms.prototype.innerContent = null;
		PSNPopupTerms.prototype.titleTemplate = 
				'<div class="widget info">'
					+'<h3 class="clearfix">'
						+'<span class="icon"><img alt="" src="/i/src/img67.png"></span>'	
						+'<span>Условия пользования сайтом</span>'	
					+'</h3>'
				+'</div>';
		PSNPopupTerms.prototype.titleContainer = 	null;		


		/**
		 * buildGalery
		 * 
		 * @return {Boolean}
		 */				
		PSNPopupTerms.prototype.createPopup = function(e) {
			this.init();
			this.popupInnerContainer.find('.inner').remove();	
			this.popupInnerContainer.find('.widget').remove();	
			this.currentItem = $(e.currentTarget);
			this.currentItem.parent().find('DIV[rel="terms-popup"]');
			
			this.popupInnerContainer.addClass('press-office');
			this.innerContent = this.currentItem.parent().find('DIV[rel="terms-popup"]').clone().prependTo(this.popupInnerContainer).removeClass("hidden");
			this.innerContent.find("H2").html(this.innerContent.find("H2>A").html())
			this.titleContainer = $(this.titleTemplate).prependTo(this.popupInnerContainer);
			
			return false;
		}	
	
	})(PSNPopupTermsClass);	
	
	
	
	
	
	
	var PSNGenplanClass = (PSNGenplan=function() {

		PSNGenplan.prototype = new PSNPopup();
		PSNGenplan.prototype.currentItem = null;
		PSNGenplan.prototype.innerContent = null;
		PSNGenplan.prototype.titleTemplate = //'';
				'<div class="widget info">'
					+'<h3 class="clearfix">'
						//+'<span class="icon"><img alt="" src="/i/src/img67.png"></span>'	
						+'<span>Генеральный план</span>'	
					+'</h3>'
				+'</div>';
		PSNGenplan.prototype.titleContainer = null;		
	
		PSNGenplan.prototype.createPopup = function(e) {
			this.init();
			
			this.popupInnerContainer.find('.inner').remove();	
			this.popupInnerContainer.find('.widget').remove();	
			this.currentItem = $(e.currentTarget);
			this.currentItem.parent().find('DIV[rel="terms-popup"]');
			
			this.popupInnerContainer.addClass('genplan-popup');
			//this.innerContent = 'test test test';
			this.innerContent = $(".genplan-content").prependTo(this.popupInnerContainer).removeClass("farfaraway");
			//this.innerContent = this.currentItem.parent().find('DIV[rel="terms-popup"]').clone().prependTo(this.popupInnerContainer).removeClass("hidden");
			//this.innerContent.find("H2").html(this.innerContent.find("H2>A").html())
			this.titleContainer = $(this.titleTemplate).prependTo(this.popupInnerContainer);
			
			$('.genplan-popup').parents('#popup').css('top', '15px');
			
			$("body.country-projects area").mousemove(function(e){

				var	areadiv = "areadiv" + $(this).attr("id");
		
				$("div#" + areadiv).removeClass('hidden').show();
				$("div#" + areadiv).css({'position':'fixed'});
				$("div#" + areadiv).css("left", (e.pageX - 280 - $(document).scrollLeft()) + "px");
				$("div#" + areadiv).css("top", (e.pageY + 40 - $(document).scrollTop()) + "px");
	
			});
		
			$("body.country-projects area").mouseout(function(){
				$("div.plot").hide();
			});
			
			return false;
			//e.preventDefault();
		}
		
		PSNGenplan.prototype.close = function(e) {

			$("body.country-projects area").unbind('mouseover');
			this.popupContainer.fadeOut(this.FADE_OUT_TIMEOUT, $.proxy(this.scope, 'removePopupContainer'));

			var timer = setInterval(function(){
				$('div.plot').fadeOut();
				clearInterval(timer);
			},
			500
			);
			
			return false;
			
		}
	
	})(PSNGenplanClass);

	
	
	
	
})();
function explode( delimiter, string ) {	// Split a string by string
	// 
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: kenneth
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)

	var emptyArray = { 0: '' };

	if ( arguments.length != 2
		|| typeof arguments[0] == 'undefined'
		|| typeof arguments[1] == 'undefined' )
	{
		return null;
	}

	if ( delimiter === ''
		|| delimiter === false
		|| delimiter === null )
	{
		return false;
	}

	if ( typeof delimiter == 'function'
		|| typeof delimiter == 'object'
		|| typeof string == 'function'
		|| typeof string == 'object' )
	{
		return emptyArray;
	}

	if ( delimiter === true ) {
		delimiter = '1';
	}

	return string.toString().split ( delimiter.toString() );
}
function implode( glue, pieces ) {	// Join array elements with a string
	// 
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: _argos

	return ( ( pieces instanceof Array ) ? pieces.join ( glue ) : pieces );
}




$(function(){
	var _PSNPopupTermsClass = new PSNPopupTerms();
	var _PSNGenplanClass = new PSNGenplan();
	
	var scrHeight = 1 * $(window).height();
	var genplanHeight = 1 * $('#gplan_png').height();
	var popupHeight = 0;
	
	if (scrHeight > genplanHeight + 75)
	{
		popupHeight = genplanHeight;
	}
	else
	{
		popupHeight = scrHeight - 75;
	}

	$('.genplan-popup').css('height', popupHeight + 'px');
	$('.genplan-content').css('height', popupHeight + 'px');
	
	//var popupHeight = 1 * $('#wrapper').height() - 50;
	//$('.genplan-content').css('height', popupHeight + 'px');
	//console.log(window.genplanHeight);
	
	// genplan scroll pane
	$(".genplan-content").jScrollPane(
		{
			showArrows: true,
			autoReinitialise: true
		}
	);
	
	$('A[rel="terms-item"]').bind("click", $.proxy(_PSNPopupTermsClass.scope, 'createPopup'));	
	
	$('a#genplan-click').bind("click", $.proxy(_PSNGenplanClass.scope, 'createPopup'));
	//$('a#genplan-click').bind("click", function(){ return false; });

	$('body.country-projects ul.map-navigation').live('click', function(e){

		if($(e.target).parent().hasClass('top')){
			var top = -1 * $('#gplan_png').css('top').replace('px', '');
				top = top - 50 < 0 ? 0 + 'px' : '+=50px';
			$('#gplan_png').animate({'top': top}, 'fast');
		}else if($(e.target).parent().hasClass('bottom')){
			var height = 1 * $('#gplan_png').height() - 1 * $('.general-plan').height(),
				top = -1 * $('#gplan_png').css('top').replace('px', '');
				top = top + 50 > height  ? -height + 'px' : '-=50px';
			$('#gplan_png').animate({'top': top}, 'fast');
		}else if($(e.target).parent().hasClass('left')){
			var left = -1 * $('#gplan_png').css('left').replace('px', '');
				left = left - 50 < 0 ? 0 + 'px' : '+=50px';
			$('#gplan_png').animate({'left': left}, 'fast');
		}else if($(e.target).parent().hasClass('right')){
			var width = 1 * $('#gplan_png').width() - 1 * $('.general-plan').width(),
				left = -1 * $('#gplan_png').css('left').replace('px', '');
				left = left + 50 > width  ? -width + 'px' : '-=50px';
			$('#gplan_png').animate({'left': left}, 'fast');
		}

		return false;
	});


	/*
$(window).resize(function(){ 

		if(!/\/projects\/genplan\//.test(location.pathname))
			return true;

		var width = 1 * $('#gplan_png').width() - 1 * $('.general-plan').width(),
			height = 1 * $('#gplan_png').height() - 1 * $('.general-plan').height(),
			top = 1 * $('#gplan_png').css('top').replace('px', ''),
			left = 1 * $('#gplan_png').css('left').replace('px', '');

		if(top < -height)
			$('#gplan_png').css({'top': top - (height + top)  + 'px'});

		if(left < -width)
			$('#gplan_png').css({'left': left - (width + left)  + 'px'});

	});
*/
	
	/*
$('#gplan_png').each(function(){
		//$('<img>').load(this.src, function(){
			$('#gplan_png').animate({'top': -($('#gplan_png').height()/2) + ($('.general-plan').height()/2)  + 'px', 'left': -($('#gplan_png').width()/2) + ($('.general-plan').width()/2)  + 'px'}, 1000);
		//});
	});
*/

	/*
$('body.country-projects div.general-plan, map').live('mousedown', function(e){

		if(e.target.tagName == 'A')
			return false;

		var x = e.pageX - 1 * $('#gplan_png').css('left').replace('px', ''),
			y = e.pageY - 1 * $('#gplan_png').css('top').replace('px', '');

		var width = 1 * $('#gplan_png').width() - 1 * $('.general-plan').width(),
			height = 1 * $('#gplan_png').height() - 1 * $('.general-plan').height(),
			top = 1 * $('#gplan_png').css('top').replace('px', ''),
			left = 1 * $('#gplan_png').css('left').replace('px', '');

		$('html').live('selectstart.disableTextSelect mousedown.disableTextSelect', function() {return false});
*/
		
/*
$('#wrapper').live('mousemove', function(e){

			var t = e.pageY - y,
				l = e.pageX - x;

			if(t < -height || t > 0 || l < -width || l > 0) {

				if(t < -height)
					t = -height;
				if(t > 0)
					t = 0;
				if(l < -width)
					l = -width;
				if(l > 0)
					l = 0;

			//	return false;
			}
			$('#gplan_png').css({'left': l + 'px', 'top': t  + 'px'});
		});

		
		return false;
	});
*/

	$('html').live('mouseup', function(e){
		$('html').die('mousedown.disableTextSelect selectstart.disableTextSelect mousemove.SelectPlugin');
		$('#wrapper').die('mousemove');
	});

	$('input[type=checkbox]').wrap('<span class="ui-checkbox"></span>').change(function(){

		var th = $(this).parent();

		if(th.attr('class') == 'ui-checkbox ui-checkbox-hover ui-checkbox-state-hover')
			th.attr('class', 'ui-checkbox ui-checkbox-state-checked-hover ui-checkbox-checked ui-checkbox-hover');
		else if(th.attr('class') == 'ui-checkbox')
			th.attr('class', 'ui-checkbox ui-checkbox-state-checked ui-checkbox-checked');
		else if(th.attr('class') == 'ui-checkbox ui-checkbox-state-checked ui-checkbox-checked')
			th.attr('class', 'ui-checkbox');
		else if(th.attr('class') == 'ui-checkbox ui-checkbox-state-checked-hover ui-checkbox-checked ui-checkbox-hover')
			th.attr('class', 'ui-checkbox ui-checkbox-hover ui-checkbox-state-hover');

	}).hover(function(){

		var th = $(this).parent();

		if(th.attr('class') == 'ui-checkbox')
			th.attr('class', 'ui-checkbox ui-checkbox-hover ui-checkbox-state-hover');
		else if(th.attr('class') == 'ui-checkbox ui-checkbox-hover ui-checkbox-state-hover')
			th.attr('class', 'ui-checkbox');
		else if(th.attr('class') == 'ui-checkbox ui-checkbox-state-checked ui-checkbox-checked')
			th.attr('class', 'ui-checkbox ui-checkbox-state-checked-hover ui-checkbox-checked ui-checkbox-hover');
		else if(th.attr('class') == 'ui-checkbox ui-checkbox-state-checked-hover ui-checkbox-checked ui-checkbox-hover')
			th.attr('class', 'ui-checkbox ui-checkbox-state-checked ui-checkbox-checked');

	});

	$('input[type=radio]').wrap('<span class="ui-radio"></span>').click(function(){

		var th = $(this).parent(),
			all = th.parents('dd'),
			Class = th.attr('class');

		all.find('.ui-radio').attr('class', 'ui-radio');

		if(Class == 'ui-radio')
			th.attr('class', 'ui-radio ui-radio-state-checked ui-radio-checked');
		else
			th.attr('class', 'ui-radio ui-radio-state-checked-hover ui-radio-checked ui-radio-hover');

	}).hover(function(){

		var th = $(this).parent();

		if(th.attr('class') == 'ui-radio')
			th.attr('class', 'ui-radio ui-radio-hover ui-radio-state-hover');
		else if(th.attr('class') == 'ui-radio ui-radio-hover ui-radio-state-hover')
			th.attr('class', 'ui-radio');
		else if(th.attr('class') == 'ui-radio ui-radio-state-checked ui-radio-checked')
			th.attr('class', 'ui-radio ui-radio-state-checked-hover ui-radio-checked ui-radio-hover');
		else if(th.attr('class') == 'ui-radio ui-radio-state-checked-hover ui-radio-checked ui-radio-hover')
			th.attr('class', 'ui-radio ui-radio-state-checked ui-radio-checked');

	});
	$("#contact-form").validate({
		rules: {
			'P_NAME' : "required",
			P_EMAIL: {
				required: true,
				email: true
			},
			'P_PHONE' : "required",
			'P_QUESTION' : "required"
		},

		messages: {
			'P_NAME' : '',//"Нужно указать свое имя!",
			'P_EMAIL' : {
				required: '',//"Нужно указать email адрес!",
				email: ''//"Некорректный email адрес!"
			},
			'P_PHONE' : '',//"Не указан телефон!",
			'P_QUESTION' : ''//"Это поле обязательно для заполнения!"					
		},
		errorPlacement: function(error, element) {
		     var _LayoutFix = new LayoutFix()
			 	_LayoutFix.init();
		}
	});
	$("#rent_form").validate({
		rules: {
			'fio' : "required",
			email: {
				required: true,
				email: true
			},
			'phone' : "required"
		},

		messages: {
			'fio' : '',//"Нужно указать свое имя!",
			'email' : {
				required: '',//"Нужно указать email адрес!",
				email: ''//"Некорректный email адрес!"
			},
			'phone' : ''//"Не указан телефон!"
		},
		errorPlacement: function(error, element) {
		     var _LayoutFix = new LayoutFix()
			 	_LayoutFix.init();
		}
	});

	$('#rent_on_line').validate({
		rules: {
			'fio' : "required",
			email: {
				required: true,
				email: true
			},
			'phone' : "required",
			'organ' : "required"

		},

		messages: {
			'fio' : '',//"Нужно указать свое имя!",
			'organ' : '',//"Не указана организация!",
			'email' : {
				required: '',//"Нужно указать email адрес!",
				email: ''//"Некорректный email адрес!"
			},
			'phone' : ''//"Не указан телефон!"
		},
		errorPlacement: function(error, element) {
		     var _LayoutFix = new LayoutFix()
			 	_LayoutFix.init();
		}
	});
	$('ul.section-navi').live('click', function(e){

		if(!/\/rent\/on-line\//.test(location.pathname))
				return true;

		if(e.target.nodeName !== 'A')
			return false;

		var th = $(e.target).parent('li');

		if(th.hasClass('active'))
			return false;

		th.parent('ul').find('li').removeClass('active');
		th.addClass('active');

		var dl_online_application = $('dl.online-application'),
			next = $('a.section-navi.next');

		dl_online_application.css({'display':'none'}).eq(th.index()).css({'display':'block'});

		if(dl_online_application.size() - 1 == th.index())
			next.hide();
		else if(dl_online_application.eq(th.index()).find('input:checked').size())
			next.show().animate({'opacity': 1}, 500).each(function(){this.change = true;});
		else
			next.show().animate({'opacity': .5}, 500).each(function(){this.change = false;});

		return false;

	});

	$('a.section-navi.prev, a.section-navi.next').animate({'opacity': .5}, 500).live('click', function(e){

		if(!/\/rent\/on-line\//.test(location.pathname))
				return true;

		var li = $('ul.section-navi li.active'),
			th = $(this);

		if(!this.change)
			return false;

		if(th.hasClass('next') && li.next().size())
			li = li.next();
		else if(th.hasClass('prev') && li.next().prev() )
			li = li.prev();
		else
			return false

		li.find('a').trigger('click');

		return false;

	});

	$('dl.online-application input').live('change', function(e){

		if(!/\/rent\/on-line\//.test(location.pathname))
			return true;

		var step = e.target.name.replace(/step\[(\d)\]\[?\]?/, '$1'),
			step = $('#step_' + step);

		step.html('');

		if($(this).parents('fieldset').parent('dd').find('input:checked').each(function(){

			step.append('<p>- ' + $(this).parents('fieldset').find('strong').text() + '</p>');

		}).size())
			$('a.section-navi.next').animate({'opacity': 1}, 500).each(function(){this.change = true;});
		else
			$('a.section-navi.next').animate({'opacity': .5}, 500).each(function(){this.change = false;});

	});
	if(window.special_ofer) {

		$('html').addClass('message');

		$('body').prepend('<div class="special-offer"><h2><strong>' + window.special_ofer + '</strong></h2><a class="link" href="#"><span class="corn left"></span><i>Спецпредложение</i><span class="corn right"></span></a></div>').parents('html').addClass('message');
		$('div.special-offer a.link').live('click', function(e){

			var th = $('html');

			if(th.hasClass('open_'))
				th.animate({'margin-top': 0});
			else
				th.animate({'margin-top': '40px'});

			$('html').toggleClass('open_');
			$(window).trigger('resize');
		

		});
	
	};

	$('#phone, #P_PHONE').mask('+7 (999) 999-99-99');
	
	$('ul.bread-crumbs').next().children('img').each(function(){
		
		var th = this;

		$('<img>').load(this.src, function(){

			$(th).css('width', $(th).width());
			$(th).css('height', $(th).height());
			
		});
		
	})

	if(window.initialize)
		window.initialize();
})
