

(function(){ 



	/**
	 * Constructor
	 */	
	 
	Slider = function() {
		this.scope = this;
		
		// Containers
		this.sliderContainer = null;
		this.sliderItemContainer = null;
		this.sliderNavigationContainer = null;
		this.sliderNavigationItemContainer = null;
		this.containerItemClass = '';
		this.sliderNavigationControl = null;
		
	
		//Other
		
		this.sliderItemIndex = 0;
		this.prevSliderItemIndex = 0;
		this.sliderTimeout = null;
		this.sliderTimeoutInterval = 5000;
		this.isSlideShows = false;
		this.isPauseMode = false;
		this.isPauseCurrentMode = 'pause';
		this.nextPrevContainer = null;
		this.nextPrevContainerTop = null;
		this.nextContainer = null;
		this.prevContainer = null;
		this.nextPrevMargin = null;
		this.nextPrevSpace = null;
		this.dblClickMode = false;
		this.controlMode = 'pause';
		this.currentPicture = null;
		
		this.FADE_IN_TIMEOUT = 400;
		this.FADE_OUT_TIMEOUT = 400;	

		this.resizeInitInterval = null;
		
	};
	
	
	Slider.prototype = {
	
		init:function(container, navigation, _containerItemClass, isSlideShow, nextPrev, dbclick) {

			if(_containerItemClass!=undefined || _containerItemClass!=null) {
				this.containerItemClass = _containerItemClass;
			} 
			this.isSlideShows = isSlideShow;
			this.sliderContainer = $(container);
			this.sliderItemContainer = this.sliderContainer.find("LI"+_containerItemClass)
			this.sliderNavigationContainer = $(navigation);
			this.sliderNavigationItemContainer = this.sliderNavigationContainer.find("LI").not('.control');
			this.sliderNavigationControl = this.sliderNavigationContainer.find("LI.control");			

			if(dbclick){
				this.dblClickMode = true;
			}
			
			if(this.sliderNavigationItemContainer.parent().find('LI.active').index()>0)
			{
				this.sliderItemIndex = this.sliderNavigationItemContainer.parent().find('LI.active').index();
			}
			
			this.gotoToItem(this.sliderItemIndex);

			if(nextPrev!= undefined || nextPrev!=null) {
				this.nextPrevContainer = $(nextPrev);
				this.nextContainer = this.nextPrevContainer.find('.next-arr');
				this.prevContainer = this.nextPrevContainer.find('.prev-arr');
				this.nextPrevContainer.find("LI").click($.proxy(this.scope, "scroll"));
				this.getSliderNavigationWidth();
				this.checkingPosition();
							this.sliderNavigationContainer.parent().height(this.sliderNavigationItemContainer.find('img').height()+13)
				this.nextContainer.find('A').height(this.sliderNavigationItemContainer.find('img').height()+13)
				this.prevContainer.find('A').height(this.sliderNavigationItemContainer.find('img').height()+13)
				
			}
			
			this.sliderNavigationControl.click($.proxy(this.scope, "switchMode"));
			if(this.sliderNavigationItemContainer.length!=0) {
				this.sliderNavigationItemContainer.click($.proxy(this.scope, "currentIndexCounter"));
				if(this.dblClickMode ) {
					var _PSNPopupGalleryClass = new PSNPopupGallery();
					this.sliderNavigationItemContainer.dblclick($.proxy(_PSNPopupGalleryClass.scope, 'buildGalery'));						
				}
			} else if(location.pathname != '/') this.sliderContainer.parent().hide(); // news fix and gallery fix
			
	
		},
		
		switchMode:function(e) {
			var currentItem = $(e.currentTarget);
			if(currentItem.hasClass('pause')) {
				this.controlMode = 'play';
				this.isSlideShows = false;
				currentItem.removeClass('pause').addClass('play');
				clearInterval(this.sliderTimeout);
			} else {
				this.controlMode = 'pause'
				this.isSlideShows = true;
				currentItem.removeClass('play').addClass('pause');
				
				var _counter = this.getCurrentItemIndex()+1;

				if(_counter>this.sliderItemContainer.length-1) { 
					_counter = 0;
				}
				
				if(_counter<0) { 
					_counter = this.sliderItemContainer.length-1; 
				}

				this.setCurrentItemIndex(_counter);
				this.gotoToItem(this.getCurrentItemIndex());								
			}
			return false;
		},		
		
		scroll:function(e) {
	
			if($(e.currentTarget).attr('_disabled') != "true") {
				var left = '-='+(this.sliderNavigationItemContainer.width()-64);
				var _left = (this.sliderNavigationItemContainer.width()+(this.nextPrevMargin+this.nextPrevSpace)) 
				if(e, $(e.currentTarget).hasClass('prev-arr')) { left = '+='+_left; } 
				if(e, $(e.currentTarget).hasClass('next-arr')) { left = '-='+_left; } 
				this.sliderNavigationContainer.animate({left: left}, 700, $.proxy(this.scope, "checkingPosition"));	
				this.prevContainer.attr("_disabled", "true");
				this.nextContainer.attr("_disabled", "true");	
			}
			return false;
		},
		
		  
		checkingPosition:function(e) {
				var _maxItems = Math.floor(this.sliderNavigationContainer.parent().width()/this.sliderNavigationItemContainer.width());
				var _maxXPosition = (this.sliderNavigationItemContainer.width()+(this.nextPrevMargin+this.nextPrevSpace))*(this.sliderNavigationItemContainer.length-_maxItems);
				
				if(this.sliderNavigationContainer.css('left') == (-((_maxXPosition-this.nextPrevContainerTop)))+"px") {
					this.prevContainer.attr("_disabled", "false");
					this.nextContainer.attr("_disabled", "true");					
				} else {
					this.nextContainer.attr("_disabled", "false");					
				}
				
				if(this.sliderNavigationContainer.css('left') == "5px") {
					this.prevContainer.attr("_disabled", "true");
					this.nextContainer.attr("_disabled", "false");					
				} else {
					this.prevContainer.attr("_disabled", "false");				
				}		
				
				if(this.sliderNavigationItemContainer.length == _maxItems) {
					this.prevContainer.attr("_disabled", "true");
					this.nextContainer.attr("_disabled", "true");					
				}	
				
				if(this.sliderNavigationItemContainer.length < _maxItems) {
					this.prevContainer.hide();
					this.nextContainer.hide();	
					this.sliderContainer.parent().find('.gallery-slider-wrap').css('margin', 0);				
				}					

				return false;
		},	
				
		
		getSliderNavigationWidth:function() {
			this.sliderNavigationContainer.width(this.sliderNavigationItemContainer.length*(this.sliderNavigationItemContainer.width()+(this.nextPrevSpace+this.nextPrevMargin)));
		},

		gotoToItem:function(index, animate) {
		
			if(index == undefined) {
		
				index = this.getCurrentItemIndex();
			}
			
			
			if(this.prevSliderItemIndex == index) {
				this.sliderItemContainer.fadeOut(1);
				this.sliderContainer.find('LI'+this.containerItemClass+':eq('+index+')').fadeIn(1);
			} else {
				if(!this.sliderContainer.hasClass('news-description')) {
					this.sliderItemContainer.fadeOut(this.FADE_OUT_TIMEOUT);
					this.sliderContainer.find('LI'+this.containerItemClass+':eq('+index+')').fadeIn(this.FADE_IN_TIMEOUT, function() { $(this).css('opacity', '1'); } );
				} else {
					this.sliderContainer.find('LI'+this.containerItemClass).css('opacity', '0');
					this.sliderContainer.find('LI'+this.containerItemClass+':eq('+this.prevSliderItemIndex+')').css('opacity', '1');
					this.sliderContainer.find('LI'+this.containerItemClass+':eq('+this.prevSliderItemIndex+')').fadeOut(this.FADE_OUT_TIMEOUT, $.proxy(this.scope, 'onNewsAnimatedComplete'));
					
				}
			}

			
			this.sliderItemContainer.removeClass('active');
			this.sliderNavigationItemContainer.removeClass('active')
			this.sliderNavigationItemContainer.removeClass('play');
			this.sliderNavigationItemContainer.removeClass('pause');
			
			if(this.isSlideShows){
				clearInterval(this.sliderTimeout);
				this.sliderTimeout = setInterval($.proxy(this.scope, "slideshow"), this.sliderTimeoutInterval);
			}
			if(this.isPauseMode) {
				this.sliderNavigationItemContainer.removeClass(this.isPauseCurrentMode);
				this.sliderNavigationContainer.find('LI:eq('+index+')').addClass(this.isPauseCurrentMode);
			} 
			this.sliderNavigationContainer.find('LI:eq('+index+')').addClass('active');
			this.sliderContainer.find('LI:eq('+index+')').addClass('active');
			this.currentPicture = this.sliderContainer.find('LI:eq('+this.getCurrentItemIndex()+')').find('IMG');
			
			if(this.currentPicture.length != 0) {
			
				if(this.currentPicture.attr('_width')!=undefined) {
					this.currentPicture.attr('_width', this.currentPicture.attr('_width'))
					this.currentPicture.attr('_height', this.currentPicture.attr('_height'));				
				} else {
					this.currentPicture.attr('_width', this.currentPicture.attr('width'))
					this.currentPicture.attr('_height', this.currentPicture.attr('height'));
				}
				
				this.resize();
				$(window).resize($.proxy(this.scope, 'resize'));	
			}	
			
			this.resizeInitInterval = setInterval($.proxy(this.scope, 'resize'), 10);
			
			this.resize();
			$(window).resize($.proxy(this.scope, 'resize'));
			
	
			preInitInterval = setTimeout(preInit, 10);
			clearTimeout(preInitInterval);
			clearInterval(this.resizeInitInterval);
			

		},

		
		onNewsAnimatedComplete:function() {
			this.sliderContainer.find('LI'+this.containerItemClass+':eq('+this.getCurrentItemIndex()+')').css('opacity', 1);
			this.sliderContainer.find('LI'+this.containerItemClass+':eq('+this.getCurrentItemIndex()+')').hide().fadeIn(this.FADE_IN_TIMEOUT);
		},
		
		resize:function() {
		
			if(this.currentPicture.length!=0) {
			
				
			
				var _width = this.currentPicture.attr('_width');
				var _height = this.currentPicture.attr('_height');

				var _imageRatio = _width / _height;
				
				this.currentPicture.attr('width', "auto")
				this.currentPicture.attr('height', _height)				
				

				
				if(_width> this.sliderContainer.parent().width()) {
				
					_width = this.sliderContainer.parent().width();
					_height = _width / _imageRatio;	
			
					this.currentPicture.attr('width', _width)
					this.currentPicture.attr('height', _height)
					
				} else {
				
					this.currentPicture.attr('width', _width)
					this.currentPicture.attr('height', _height)				
			
				}

				this.sliderContainer.height(this.currentPicture.attr('height'))
				this.sliderItemContainer.height(this.currentPicture.attr('height'));

				this.currentPicture.attr('height', this.sliderContainer.height());
				this.currentPicture.attr('width', this.currentPicture.attr('height')*_imageRatio);

				this.sliderItemContainer.width(this.sliderContainer.parent().width())
				clearInterval(this.resizeInitInterval);
				
	
				//this.sliderContainer.parent().height(this.sliderContainer.height()+$('.gallery-slider-wrap)
	
				/*if((this.sliderContainer.offset().top+this.sliderContainer.height())>$('#main-content').height()) {
					this.sliderContainer.height(($('#main-content').height()-this.sliderContainer.offset().top)+this.sliderNavigationContainer.height()-8)
					this.sliderItemContainer.height(($('#main-content').height()-this.sliderContainer.offset().top)+this.sliderNavigationContainer.height()-8)
				} */
				
				//this.sliderContainer.height(this.currentPicture.attr('height'))
				//this.sliderItemContainer.height(this.currentPicture.attr('height'))		

					

			}	
			
		},
		
		slideshow:function() {
			var index = this.getCurrentItemIndex();
				index+=1;
				if(index>=this.sliderItemContainer.length) { index = 0; }	
				this.prevSliderItemIndex = this.getCurrentItemIndex();
				this.setCurrentItemIndex(index);
				this.gotoToItem(index);
		},
	
		currentIndexCounter:function(e) {
						
						
						
			var currentItem = $(e.currentTarget);
			
			e.preventDefault();
			
			if(this.isSlideShows){
				clearInterval(this.sliderTimeout);
			}
			
			var _counter = Number($(e.currentTarget).index());

			if(_counter>this.sliderItemContainer.length-1) { 
				_counter = 0;
			}
			
			if(_counter<0) { 
				_counter = this.sliderItemContainer.length-1; 
			}
			
			if(currentItem.hasClass("active")) {
				if(this.isPauseCurrentMode == 'pause') {
					this.isPauseCurrentMode = 'play';
					this.isSlideShows = false;
					clearInterval(this.sliderTimeout);
				} else {
					this.isPauseCurrentMode = 'pause';
					this.isSlideShows = true;
				}	
			}

			
			this.prevSliderItemIndex = this.getCurrentItemIndex();
			this.setCurrentItemIndex(_counter);

			
			if(e.type=='dblclick') {
				this.sliderItemContainer.stop(true, true);
				this.buildPopupGallery(); 
			} else {
				if(!$(e.currentTarget).hasClass('active')) {
				this.gotoToItem(this.getCurrentItemIndex());
				}				
			}

			
		},
		
		setCurrentItemIndex:function(index) {
			this.sliderItemIndex = index;
		},
		
		getCurrentItemIndex:function() {
			return this.sliderItemIndex;
		}		
		
	}
})();


(function(){ 

	/**
	 * Class PSNPopup
	 */		
	var PSNPopupClass = (PSNPopup=function() {
	
		this.scope = this;
		
		// util
		this._class = 'PSNPopup';
		this.isDebug = false;
		this.FADE_IN_TIMEOUT = 100;
		this.FADE_OUT_TIMEOUT = 400;
		
		// popup template
		this._window = $('body');
		this._template = 
				'<div id="popup">'
				+'<div class="popup-inner">'
					+ '<div class="right-decoration"></div>'		
					+ '<div class="close"><a href="#">×</a></div>'			
				+'</div>'				
				+'</div>'
				+'<div class="popup-overlay"></div>'
		
		// containers
		this.popupContainer = $(this._template);
		this.popupInnerContainer = null;
		this.popupOverlay = null;
		this.closeButton = null;
		this.isCreated = false;
		
		PSNPopup.prototype = {
		
			/**
			 * create
			 * 
			 * @return {Boolean|false}
			 */	
			init:function() {
				
				if(this.isDebug) console.log(this._getClassName());			
					this.popupContainer.appendTo(this._window).hide().fadeIn(this.FADE_IN_TIMEOUT);
					this.closeButton = $(this.popupContainer).find('.close');
					this.popupInnerContainer = $('.popup-inner');
					this.popupOverlay = $('.popup-overlay');
					this.closeButton.bind('click', $.proxy(this.scope, 'close'));
					this.popupOverlay.bind('click', $.proxy(this.scope, 'close'));
			},				

			/**
			 * close
			 * 
			 * @return {Boolean}
			 */				
			close:function() {
				this.popupContainer.fadeOut(this.FADE_OUT_TIMEOUT, $.proxy(this.scope, 'removePopupContainer'));
				return false;
			},
			
			/**
			 * removePopupContainer
			 * 
			 * @return {Boolean}
			 */				
			removePopupContainer:function() {
				this.popupContainer.remove();
				return false;
			},	

			/**
			 * destroy
			 * 
			 * @return {Boolean}
			 */				
			destroy:function() {
				this.closeButton.unbind();
				return false;
			},				
			
			/**
			 * _getClassName
			 * 
			 * @return {String}
			 */				
			_getClassName:function() {
				return this._class;
			},
			
			/**
			 * _setDebug
			 * 
			 * @return {String}
			 */				
			_setDebug:function(b) {
				this._isDebug = b;
			},
			
			/**
			 * _getDebug
			 * 
			 * @return {String}
			 */				
			_getDebug:function() {
				return this._isDebug;
			}				
			
		}
		
	})(PSNPopupClass);
	
	
	/**
	 * Class PSNPopupGallery
	 */		
	var PSNPopupGalleryClass = (PSNPopupGallery=function() {

		PSNPopupGallery.prototype = new PSNPopup();
		PSNPopupGallery.prototype._class = 'PSNPopupGalleryClass';
		PSNPopupGallery.prototype.galleryContainer = null;
		PSNPopupGallery.prototype.galleryContainerSelector = 'ul.pictures';
		PSNPopupGallery.prototype.galleryContainer = $(PSNPopupGallery.prototype.galleryContainerSelector);		
		PSNPopupGallery.prototype.galleryLength = null;
		PSNPopupGallery.prototype.galleryCurrentItem = null;
		PSNPopupGallery.prototype.galleryCurrentIndex = null;
		PSNPopupGallery.prototype.galleryNextButton = null;
		PSNPopupGallery.prototype.galleryPrevButton = null;
		PSNPopupGallery.prototype.galleryCurrentTitle = null;
		PSNPopupGallery.prototype.galleryNavigationTemplate = 
				'<ul class="gallery-navigation">'
					+'<li class="prev-arr">'
						+'<a href="#">Предыдущая</a>'	
					+'</li>'
					+'<li class="next-arr">'
						+'<a href="#">Следующая</a>'	
					+'</li>'
				+'</ul>';
		PSNPopupGallery.prototype.galleryNavigationContainer = 	null;
		PSNPopupGallery.prototype.galleryTitleTemplate = '<h3 class="title"></h3>';
		PSNPopupGallery.prototype.galleryTitleContainer = 	null;
		PSNPopupGallery.prototype.originalImageWigth = 	null;
		PSNPopupGallery.prototype.originalImageHeight = null;

		/**
		 * buildGalery
		 * 
		 * @return {Boolean}
		 */				
		PSNPopupGallery.prototype.buildGalery = function(index, type) {
		
			
			this.init();
			
			if(this.popupInnerContainer.length==0 && this.popupInnerContainer.length=='null') {
				this.popupInnerContainer = $('.popup-inner');
			}

			this.popupInnerContainer.find(this.galleryContainerSelector).remove();	
			this.popupInnerContainer.find('.gallery-navigation').remove();	
			this.popupInnerContainer.find('h3.title').remove();			
			
			
			
			this.galleryContainer = $(this.galleryContainerSelector).clone().prependTo(this.popupInnerContainer);	
			this.galleryContainer.find('A').css('cursor', 'default');
			this.galleryContainer.find('A').click(function() { return false; });

			this.galleryNavigationContainer = $(this.galleryNavigationTemplate).appendTo(this.popupInnerContainer).hide().fadeIn(this.FADE_IN_TIMEOUT);
			this.galleryTitleContainer = $(this.galleryTitleTemplate).prependTo(this.popupInnerContainer).hide().fadeIn(this.FADE_IN_TIMEOUT);
		
			this.galleryNextButton = this.popupInnerContainer.find('.next-arr');		
			this.galleryPrevButton = this.popupInnerContainer.find('.prev-arr');
			
			this.galleryCurrentItem = this.galleryContainer.find('LI.active');
			this.setCurrentItemIndex(Number(this.galleryContainer.find('LI.active').index()));		
			this.galleryLength = Number(this.galleryContainer.find('LI').length);
			
			this.closeButton.bind('click', $.proxy(this.scope, 'close'));
			this.galleryNextButton.bind('click', $.proxy(this.scope, 'currentIndexCounter'));
			this.galleryPrevButton.bind('click', $.proxy(this.scope, 'currentIndexCounter'));
			
			this.gotoToItem(this.getCurrentItemIndex());
			
			this.galleryLength = Number(this.galleryContainer.find('LI').length);
		
			this.popupInnerContainer.find('.right-decoration').height(this.galleryContainer.find('LI:eq('+Number(this.galleryContainer.find('LI.active').index())+')').find("IMG").height()+10);
			
			if(this.galleryLength <=1 ) {
				this.galleryNextButton.hide();
				this.galleryPrevButton.hide();
			}
			
			if(this.isDebug) console.log('galleryNextButton:', this.galleryNextButton);
			if(this.isDebug) console.log('galleryLength:', this.galleryLength);
			if(this.isDebug) console.log('getCurrentItemIndex:', this.getCurrentItemIndex());
			
			return false;
		},
		
		/**
		 * Gallery change counter
		 * 
		 * @return {Boolean}
		 */				
		PSNPopupGallery.prototype.currentIndexCounter = function(e) {
		
			var currentItem = this.galleryCurrentItem;				
			var _counter = this.getCurrentItemIndex();

			if($(e.currentTarget).hasClass('next-arr')) { _counter +=1; } 
			if($(e.currentTarget).hasClass('prev-arr')) { _counter -=1; } 

			if(_counter>this.galleryLength-1) { 
				_counter = 0;
			}
			
			if(_counter<0) { 
				_counter = this.galleryLength-1; 
			}

			this.prevSliderItemIndex = this.getCurrentItemIndex();
			this.setCurrentItemIndex(_counter);				
			this.gotoToItem(this.getCurrentItemIndex());
			
			return false;
		}
		
		/**
		 * Setter:setCurrentItemIndex
		 * 
		 * @return {Void}
		 */		
		PSNPopupGallery.prototype.setCurrentItemIndex=function(index) {
			this.sliderItemIndex = index;
		}
		
		/**
		 * Getter:getCurrentItemIndex
		 * 
		 * @return {Nubmer}
		 */			
		PSNPopupGallery.prototype.getCurrentItemIndex=function() {
			return Number(this.sliderItemIndex);
		}	
		
		/**
		 * gotoToItem
		 * 
		 * @return {Boolean}
		 */			
		PSNPopupGallery.prototype.gotoToItem=function(index) {
			
			var index = this.getCurrentItemIndex();
			
			this.galleryContainer.find('LI').fadeOut(0);
			this.galleryContainer.find('LI:eq('+index+')').fadeIn(this.FADE_IN_TIMEOUT, function() { $(this).css('opacity', '1'); });
			
			this.galleryContainer.find('LI').removeClass('active');
			this.galleryContainer.find('LI:eq('+index+')').addClass('active');
			
			
			var _w = this.galleryContainer.find('LI:eq('+index+')').find("IMG").attr('width');
			var _h = this.galleryContainer.find('LI:eq('+index+')').find("IMG").attr('height');

			if((_w == undefined && _w == null) || (_h == undefined && _h == null) ) {
				_w = this.galleryContainer.find('LI:eq('+index+')').find("IMG").width();
				_h = this.galleryContainer.find('LI:eq('+index+')').find("IMG").height();		
			}
			
			this.galleryContainer.find('LI:eq('+index+')').find("IMG").attr('width', _w)
			this.galleryContainer.find('LI:eq('+index+')').find("IMG").attr('height', _h);

			this.originalImageWigth = 	this.galleryContainer.find('LI:eq('+index+')').find("IMG").attr('_width');
			this.originalImageHeight =  this.galleryContainer.find('LI:eq('+index+')').find("IMG").attr('_height');
			
			if(this.originalImageWigth == undefined && this.originalImageHeight == undefined) {
				this.originalImageWigth = _w;
				this.originalImageHeight = _h;
			}

			this.galleryContainer.find('LI:eq('+index+')').find("IMG").attr('_width', this.originalImageWigth);
			this.galleryContainer.find('LI:eq('+index+')').find("IMG").attr('_height', this.originalImageHeight);
			
			this.galleryContainer.find('li').width('auto');

						
			
			$('#popup').css('left', '-10px');
			this.resize(index);
			$(window).resize($.proxy(this.scope, 'resize'));
			
			return false
		},

		PSNPopupGallery.prototype.resize=function() {

			var index = this.getCurrentItemIndex();
			
			var _width = this.galleryContainer.find('LI:eq('+index+')').find("IMG").attr('_width');
			var _height = this.galleryContainer.find('LI:eq('+index+')').find("IMG").attr('_height');
			
			var offsetHeight = $(window).height();
			var offsetWidth = $(window).width();
			
			var imageRatio = _width / _height;


			if(_width>offsetWidth-80) {
				_width = offsetWidth-80;
			} else {
				imageRatio = _width / _height;
			   _width = _width;
			   _height = _width / imageRatio;			
			}
			
			if(_height > offsetHeight-250) {
				   _height = offsetHeight-250;
				   _width = _height * imageRatio;
			}	
		   

			this.galleryContainer.find('LI:eq('+index+')').find("IMG").width(_width);	
			this.galleryContainer.find('LI:eq('+index+')').find("IMG").height(_height);	
			
			this.popupInnerContainer.width(_width);
			this.popupInnerContainer.height(_height);

			this.popupInnerContainer.find('.right-decoration').height(this.galleryContainer.find('LI:eq('+index+')').find("IMG").height()+10);
			var title = this.galleryContainer.find('LI:eq('+index+')').find("IMG").attr('alt');
			
			if(title!='') {
				this.galleryTitleContainer.fadeIn(this.FADE_IN_TIMEOUT);
				this.galleryTitleContainer.css('left', '0px');
				this.galleryTitleContainer.css('top', (this.popupInnerContainer.height()+10)+'px');
				this.galleryTitleContainer.css('width', this.popupInnerContainer.width());				
				this.galleryTitleContainer.html(this.galleryContainer.find('LI:eq('+index+')').find("IMG").attr('alt'));
				this.popupInnerContainer.css('padding-bottom', this.galleryTitleContainer.height()-5);
			} else {
				this.popupInnerContainer.css('padding-bottom', '10px');
				this.galleryTitleContainer.fadeOut(this.FADE_OUT_TIMEOUT);
			}	

			return false		
		}
		
		
	})(PSNPopupGalleryClass);
	
	/**
	 * Class PSNPopupManager
	 */		
	var PSNPopupManagerClass = (PSNPopupManager=function() {

		PSNPopupManager.prototype = new PSNPopup();
		PSNPopupManager.prototype.currentItem = null;
		PSNPopupManager.prototype.innerContent = null;
		PSNPopupManager.prototype.titleTemplate = 
				'<div class="widget info">'
					+'<h3 class="clearfix">'
						+'<span class="icon"><img alt="" src="/i/src/img22.png"></span>'	
						+'<span>'+$('#main-content').find('h1').html()+'</span>'	
					+'</h3>'
				+'</div>';
		PSNPopupGallery.prototype.titleContainer = 	null;	
		PSNPopupGallery.prototype.newAnimatedImage = null;


		/**
		 * buildGalery
		 * 
		 * @return {Boolean}
		 */				
		PSNPopupManager.prototype.createManagerPopup = function(e) {
			this.init();
			
			
				this.popupInnerContainer.find('.inner').remove();	
				this.popupInnerContainer.find('.widget').remove();	
				this.currentItem = $(e.currentTarget);
				this.currentItem.find('DIV[rel="psn-popup"]');
				this.popupInnerContainer.addClass('management');
				this.innerContent = this.currentItem.find('DIV[rel="psn-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)

				var thumbPicture = this.currentItem.find('.photo>a>img')
				var currentPicture = this.innerContent.find('img');

				if(!this.isCreated) {
					this.isCreated = true;
					this.newAnimatedImage = this.innerContent.find('img').clone().appendTo('body');
					this.newAnimatedImage.css('position', 'absolute');
					this.newAnimatedImage.css('opacity', 1);
					this.newAnimatedImage.css('z-index', 1000);
					this.newAnimatedImage.css('top', thumbPicture.offset().top);
					this.newAnimatedImage.css('width', thumbPicture.width());
					this.newAnimatedImage.css('height', thumbPicture.height());
					this.newAnimatedImage.css('left', thumbPicture.offset().left);
					this.newAnimatedImage.attr('_top', thumbPicture.offset().top);
					this.newAnimatedImage.attr('_width', thumbPicture.width());
					this.newAnimatedImage.attr('_height', thumbPicture.height());
					this.newAnimatedImage.attr('_left', thumbPicture.offset().left);
					this.newAnimatedImage.attr('clicked', 'newAnimatedImage');
					this.newAnimatedImage.animate({
							left: currentPicture.offset().left+1,
							top: currentPicture.offset().top+1,
							width: currentPicture.width(),
							height: currentPicture.height()}, 400, $.proxy(this.scope, 'newAnimatedImageComplete'))
				
				}
				this.popupInnerContainer.hide();
				$('.popup-overlay').hide();
			
			return false;
		}	
		
		PSNPopupManager.prototype.newAnimatedImageComplete = function(e) {
			this.popupInnerContainer.fadeIn(300, $.proxy(this.scope, 'newAnimatedImageCompleteX'));
			$('.popup-overlay').fadeIn(400);	
		}
		
		PSNPopupManager.prototype.newAnimatedImageCompleteX = function(e) {
			this.newAnimatedImage.fadeOut(300);
		}		

		PSNPopupManager.prototype.close = function() {
			this.isCreated = false;		
			this.popupContainer.fadeOut(400, $.proxy(this.scope, 'removePopupContainer'));
			this.newAnimatedImage.css('display', 'block');
			this.newAnimatedImage.animate({
					left:this.newAnimatedImage.attr('_left'),
					top: this.newAnimatedImage.attr('_top'),
					width: this.newAnimatedImage.attr('_width'),
					height: this.newAnimatedImage.attr('_height')}, 400,  function() { $(this).remove(); })				
			return false;
		}
		
		PSNPopupManager.prototype.onClose = function() {

			this.newAnimatedImage.animate({
					left:this.newAnimatedImage.attr('_left'),
					top: this.newAnimatedImage.attr('_top'),
					width: this.newAnimatedImage.attr('_width'),
					height: this.newAnimatedImage.attr('_height')}, 400,  function() { $(this).css('opacity', '0'); })
					
			console.log('class test');	
				
			return false;
		}			
	
	})(PSNPopupManagerClass);	
	
	
	/**
	 * Class PSNPopupManager
	 */		
	var PSNPopupPressClass = (PSNPopupPress=function() {

		PSNPopupPress.prototype = new PSNPopup();
		PSNPopupPress.prototype.currentItem = null;
		PSNPopupPress.prototype.innerContent = null;
		PSNPopupPress.prototype.titleTemplate = 
				'<div class="widget info">'
					+'<h3 class="clearfix">'
						+'<span class="icon"><img alt="" src="/i/src/img22.png"></span>'	
						+'<span>Связь с пресс-службой</span>'	
					+'</h3>'
				+'</div>';
		PSNPopupPress.prototype.titleContainer = 	null;		


		/**
		 * buildGalery
		 * 
		 * @return {Boolean}
		 */				
		PSNPopupPress.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="psn-popup"]');
			
			this.popupInnerContainer.addClass('press-office');
			this.innerContent = this.currentItem.parent().find('DIV[rel="psn-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);
			
			
			this.popupInnerContainer.parent().css('text-align', 'left');
			this.popupInnerContainer.parent().css('left', this.currentItem.width()+this.currentItem.offset().left+56);
			
			$(window).resize($.proxy(this.scope, 'resize'));
			
			return false;
		}
		
		PSNPopupPress.prototype.resize = function(e) {
			this.popupInnerContainer.parent().css('text-align', 'left');
			this.popupInnerContainer.parent().css('left', this.currentItem.width()+this.currentItem.offset().left+56);			
		}
	
	})(PSNPopupPressClass);	
	
	
	/**
	 * Class PSNPopupHandle
	 */		
	var PSNPopupHandleClass = (PSNPopupHandle=function() {

		PSNPopupHandle.prototype = new PSNPopup();
		PSNPopupHandle.prototype.currentItem = null;
		PSNPopupHandle.prototype.innerContent = null;
		PSNPopupHandle.prototype.titleContainer = 	null;	
		PSNPopupHandle.prototype.titleTemplate = '<h3 class="title"></h3>';		

		/**
		 * buildGalery
		 * 
		 * @return {Boolean}
		 */				
		PSNPopupHandle.prototype.createPopup = function(e) {
			
			this.init();

			var currentItem = $(e.currentTarget);
			var currentIndex = currentItem.attr('index');
			var currentTitle = currentItem.attr('title');
			this.popupInnerContainer.find('.popup-handle').remove();
			this.titleContainer = $(this.titleTemplate).prependTo(this.popupInnerContainer);
			
			$('div[index='+currentIndex+']').clone().appendTo(this.popupInnerContainer).removeClass('hidden');

			this.popupInnerContainer.find('.right-decoration').height(this.popupInnerContainer.height()+10);
			
			if(currentTitle!='') {
				this.titleContainer.css('left', '0px');
				this.titleContainer.css('top', (this.popupInnerContainer.height()+10)+'px');
				this.titleContainer.css('width', this.popupInnerContainer.width());				
				this.titleContainer.html(currentTitle);
				this.titleContainer.css('padding-bottom', this.titleContainer.height()-5);
			} else {
				this.popupInnerContainer.css('padding-bottom', '10px');
			}	
			
			if($('.information-area').length!=0) {
				this.popupInnerContainer.find('.information-area').remove();
				$('.information-area').clone().appendTo(this.popupInnerContainer);
				this.popupInnerContainer.find('.information-area').css('position', 'absolute');
				this.popupInnerContainer.find('.information-area').css('bottom', '30px');
				this.popupInnerContainer.find('.information-area').css('right', '20px');
			}

			return false;					
		}
	
	})(PSNPopupHandleClass);		
		
	/**
	 * Class PSNPopupRecommendGallery
	 */		
	var PSNPopupRecommendGalleryClass = (PSNPopupRecommendGallery=function() {

		PSNPopupRecommendGallery.prototype = new PSNPopup();
		PSNPopupRecommendGallery.prototype._class = 'PSNPopupRecommendGalleryClass';
		PSNPopupRecommendGallery.prototype.galleryContainer = null;
		PSNPopupRecommendGallery.prototype.galleryContainerSelector = 'ul.pictures';		
		PSNPopupRecommendGallery.prototype.galleryLength = null;
		PSNPopupRecommendGallery.prototype.galleryCurrentItem = null;
		PSNPopupRecommendGallery.prototype.galleryCurrentIndex = null;
		PSNPopupRecommendGallery.prototype.galleryNextButton = null;
		PSNPopupRecommendGallery.prototype.galleryPrevButton = null;
		PSNPopupRecommendGallery.prototype.galleryPicture = null;
		PSNPopupRecommendGallery.prototype.galleryNavigationTemplate = 
				'<ul class="gallery-navigation">'
					+'<li class="prev-arr">'
						+'<a href="#">Предыдущая</a>'	
					+'</li>'
					+'<li class="next-arr">'
						+'<a href="#">Следующая</a>'	
					+'</li>'
				+'</ul>';
		PSNPopupRecommendGallery.prototype.galleryNavigationContainer = 	null;
		PSNPopupRecommendGallery.prototype.newAnimatedImage = null;
		
		/**
		 * buildGalery
		 * 
		 * @return {Boolean}
		 */				
		PSNPopupRecommendGallery.prototype.buildGalery = function(e) {
			
			this.init();
			
			this.galleryCurrentItem =  $(e.currentTarget).parent().parent();			
			this.popupInnerContainer.find('.gallery-navigation').remove();
			this.setCurrentItemIndex(Number(this.galleryCurrentItem.index()));		
			this.galleryContainer = this.galleryCurrentItem.parent();
			this.galleryLength = Number(this.galleryContainer.find('LI').length);
			this.popupInnerContainer.parent().css('top', 10)
			this.popupInnerContainer.parent().css('position', 'absolute')
			
			this.galleryNavigationContainer = $(this.galleryNavigationTemplate).appendTo(this.popupInnerContainer).hide().fadeIn(this.FADE_IN_TIMEOUT);
			this.galleryNextButton = this.popupInnerContainer.find('.next-arr');		
			this.galleryPrevButton = this.popupInnerContainer.find('.prev-arr');
			
			this.galleryNextButton.bind('click', $.proxy(this.scope, 'currentIndexCounter'));
			this.galleryPrevButton.bind('click', $.proxy(this.scope, 'currentIndexCounter'));
			this.gotoToItem(this.getCurrentItemIndex());
			
			if(this.galleryLength <=1 ) {
				this.galleryNextButton.hide();
				this.galleryPrevButton.hide();
			}
			
			var thumbPicture = this.galleryCurrentItem.find('.photo>a>img')

			if(!this.isCreated) {
				this.isCreated = true;
				this.newAnimatedImage = this.galleryPicture.clone().appendTo('body');
				this.newAnimatedImage.css('position', 'absolute');
				this.newAnimatedImage.css('opacity', 1);
				this.newAnimatedImage.css('z-index', 1000);
				this.newAnimatedImage.css('top', thumbPicture.offset().top);
				this.newAnimatedImage.css('width', thumbPicture.width());
				this.newAnimatedImage.css('height', thumbPicture.height());
				this.newAnimatedImage.css('left', thumbPicture.offset().left);
				this.newAnimatedImage.attr('_top', thumbPicture.offset().top);
				this.newAnimatedImage.attr('_width', thumbPicture.width());
				this.newAnimatedImage.attr('_height', thumbPicture.height());
				this.newAnimatedImage.attr('_left', thumbPicture.offset().left);
				
				
				this.newAnimatedImage.animate({
						left: this.galleryPicture.offset().left,
						top: this.galleryPicture.offset().top,
						width: this.galleryPicture.width(),
						height: this.galleryPicture.height()}, 400,  $.proxy(this.scope, 'newAnimatedImageComplete') )
			}
			this.popupInnerContainer.hide();
			$('.popup-overlay').hide();
			
			return false;
		}
		
		PSNPopupRecommendGallery.prototype.newAnimatedImageComplete = function(e) {
			this.popupInnerContainer.fadeIn(300, $.proxy(this.scope, 'newAnimatedImageCompleteX'));
			$('.popup-overlay').fadeIn(400);	
		}
		
		PSNPopupRecommendGallery.prototype.newAnimatedImageCompleteX = function(e) {
			this.newAnimatedImage.fadeOut(300);
		}	
		
		
		/**
		 * Gallery change counter
		 * 
		 * @return {Boolean}
		 */				
		PSNPopupRecommendGallery.prototype.currentIndexCounter = function(e) {
		
			var currentItem = this.galleryCurrentItem;				
			var _counter = this.getCurrentItemIndex();

			if($(e.currentTarget).hasClass('next-arr')) { _counter +=1; } 
			if($(e.currentTarget).hasClass('prev-arr')) { _counter -=1; } 

			if(_counter>this.galleryLength-1) { 
				_counter = 0;
			}
			
			if(_counter<0) { 
				_counter = this.galleryLength-1; 
			}

			this.prevSliderItemIndex = this.getCurrentItemIndex();
			this.setCurrentItemIndex(_counter);				
			this.gotoToItem(this.getCurrentItemIndex());
			
			return false;
		}
		
		/**
		 * Setter:setCurrentItemIndex
		 * 
		 * @return {Void}
		 */		
		PSNPopupRecommendGallery.prototype.setCurrentItemIndex=function(index) {
			this.sliderItemIndex = index;
		}
		
		/**
		 * Getter:getCurrentItemIndex
		 * 
		 * @return {Nubmer}
		 */			
		PSNPopupRecommendGallery.prototype.getCurrentItemIndex=function() {
			return Number(this.sliderItemIndex);
		}	
		
		/**
		 * gotoToItem
		 * 
		 * @return {Boolean}
		 */			
		PSNPopupRecommendGallery.prototype.gotoToItem=function(index) {
				
			this.popupInnerContainer.find('IMG').remove();
			this.galleryPicture = this.galleryContainer.find('LI:eq('+index+')').find("IMG.recommend-picture").clone().appendTo(this.popupInnerContainer).removeClass('hidden').hide().fadeIn(this.FADE_IN_TIMEOUT);			
			
			var originalImageWigth = 	this.galleryPicture.width();
			var originalImageHeight =  this.galleryPicture.height();
			
			this.galleryPicture.attr('_width', originalImageWigth);
			this.galleryPicture.attr('_height', originalImageHeight);			
			
			this.resize()
			$(window).resize($.proxy(this.scope, 'resize'));
			
			return false
		},
		
		PSNPopupRecommendGallery.prototype.resize=function() {
		

			var index = this.getCurrentItemIndex();
			
			var _width = this.galleryPicture.attr('_width');
			var _height = this.galleryPicture.attr('_height');
			
			var offsetHeight = 750;
			var offsetWidth = $(window).width();
			
			var imageRatio = _width / _height;
			
			if(_width>offsetWidth && _width>_height) {
				_width = offsetWidth;
			} else {
				imageRatio = _width / _height;
			   _width = _width;
			   _height = _width / imageRatio;			
			}
			
		   if(_height > offsetHeight) {
				   _height = offsetHeight;
				   _width = _height * imageRatio;
		   }				
			
			this.galleryPicture.width(_width);	
			this.galleryPicture.height(_height);	
			
			this.popupInnerContainer.width(_width);
			this.popupInnerContainer.height(_height);

			this.popupInnerContainer.find('.right-decoration').height(this.galleryPicture.height()+10);
			
			return false		
		}		

		
		PSNPopupRecommendGallery.prototype.close = function() {
			this.isCreated = false;	
			this.popupContainer.fadeOut(400, $.proxy(this.scope, 'removePopupContainer'));
			this.newAnimatedImage.css('display', 'block');
			this.newAnimatedImage.animate({
					left:this.newAnimatedImage.attr('_left'),
					top: this.newAnimatedImage.attr('_top'),
					width: this.newAnimatedImage.attr('_width'),
					height: this.newAnimatedImage.attr('_height')}, 400,  function() { $(this).remove(); })				
			return false;
		}
		
		PSNPopupRecommendGallery.prototype.onClose = function() {

			this.newAnimatedImage.animate({
					left:this.newAnimatedImage.attr('_left'),
					top: this.newAnimatedImage.attr('_top'),
					width: this.newAnimatedImage.attr('_width'),
					height: this.newAnimatedImage.attr('_height')}, 400,  function() { $(this).css('opacity', '0'); })			
			return false;
		}		

		
	})(PSNPopupRecommendGalleryClass);	
	
		
	/**
	 * Class PSNPopupRecommendGallery
	 */		
	var PSNPopupMapClass = (PSNPopupMap=function() {

		PSNPopupMap.prototype = new PSNPopup();
		PSNPopupMap.prototype._class = 'PSNPopupMapClass';
		PSNPopupMap.prototype.titleTemplate = '<h3 class="title"></h3>';
		PSNPopupMap.prototype.titleContainer = 	null;		
		
		/**
		 * buildGalery
		 * 
		 * @return {Boolean}
		 */				
		PSNPopupMap.prototype.createMap = function(e) {
			
			this.init();

			
			var currentItem = $(e.currentTarget);
			var currentIndex = currentItem.attr('index');
			var currentTitle = currentItem.attr('alt');
			
			this.titleContainer = $(this.titleTemplate).prependTo(this.popupInnerContainer);
			
			

			this.popupInnerContainer.find('.map-inner').remove();
			var mapContainer = $('div[index='+currentIndex+']').clone().appendTo(this.popupInnerContainer).removeClass('hidden');

			mapContainer.html('<div id="current_map"></div>');
			$('#current_map').height(mapContainer.height());
			
			GoogleMap.create();	

			this.popupInnerContainer.find('.right-decoration').height(this.popupInnerContainer.height()+10);
			
			if(currentTitle!='') {
				this.titleContainer.css('left', '0px');
				this.titleContainer.css('top', (this.popupInnerContainer.height()+10)+'px');
				this.titleContainer.css('width', this.popupInnerContainer.width());				
				this.titleContainer.html(currentTitle);
				this.titleContainer.css('padding-bottom', this.titleContainer.height()-5);
			} else {
				this.popupInnerContainer.css('padding-bottom', '10px');
			}	

			return false;					
		}
		
	})(PSNPopupMapClass);

	/**
	 * Constructor
	 */	
	CreditCalc = function() {
		this.scope = this;
		
		// Containers
		this.calcContainer = null;
		this.currencyContainer = $('DL.currency');
		this.currencyItemContainer = $('DL.currency').find('DD');
		this.currencyMode = null;
		this.argDefault = 0;
		this.price = this.argDefault;
		this.fee = this.argDefault;
		this.term = this.argDefault;
		this.rate = this.argDefault;
	};	
	
	CreditCalc.prototype = {
		
		create:function(calc) {
			this.calcContainer = $(calc);
			this.calcContainer.find('input').bind('change', $.proxy(this.scope, 'update'));	
			this.calcContainer.find('input').bind('keyup', $.proxy(this.scope, 'update'));	
			$('#price').val(this.price)
			$('#fee').val(this.fee)
			$('#term').val(this.term)
			$('#rate').val(this.rate )	
			
			this.currencyContainer.find('DD[data=rur]').addClass('active');
			this.currencyItemContainer.bind('click', $.proxy(this.scope, 'updateCurrencyMode'));
			this.currencyMode = this.currencyContainer.find('.active').attr('data');
			this.calcContainer.find('span.currency').html("&nbsp;руб.");	
			this.calcContainer.find('.payments').find('.month').html("0");
			this.calcContainer.find('.payments').find('.year').html("0");
			

				
			this.update();
		},
		
		updateCurrencyMode:function(e) {
			this.currencyItemContainer.removeClass('active');
			$(e.currentTarget).addClass('active');
			this.currencyMode = this.currencyContainer.find('.active').attr('data');
			var sym;
			if(this.currencyMode == "rur") sym="&nbsp;руб.";
			if(this.currencyMode == "usd") sym="&nbsp;$";
			if(this.currencyMode == "euro") sym="&nbsp;&#x20AC;";
			
			this.calcContainer.find('span.currency').html(sym);	
			return false;
		},
		
		updateFinalResult:function() {

			var _all = Math.ceil((this.price-this.fee)/(this.term*12));
			var _rate = _all*(this.rate/100);
			var _month = _all+_rate;
			var _year = (_all+_rate)*12;
			
			if(isNaN(_month) || _month == Infinity) { _month = 0 }
			if(isNaN(_year) || _year == Infinity) { _year = 0 }
			if(this.rate != 0 ) {
				this.calcContainer.find('.payments').find('.month').html(Math.floor(_month));
				this.calcContainer.find('.payments').find('.year').html(Math.floor(_year));
			}
		},
		
		update:function() {
			
			this.price = Number($('#price').val());
			this.fee = Number($('#fee').val());
			this.term = Number($('#term').val());
			this.rate = Number($('#rate').val());
			
			if(isNaN(this.price)) this.price=this.argDefault;
			if(isNaN(this.fee)) this.fee=this.argDefault;
			if(isNaN(this.term))this.term=this.argDefault;
			if(isNaN(this.rate)) this.rate=this.argDefault
			
			$('#price').val(this.price)
			$('#fee').val(this.fee)
			$('#term').val(this.term)
			$('#rate').val(this.rate )
			this.updateFinalResult();
		}
	}
	
	
	/**
	 * ServicesSlider
	 */	
	ServicesSlider = function() {
		this.scope = this;
		this.commercialManagementNavigation = $('.commercial-management-navigation');
		this.commercialManagementPicture = $('.commercial-management').find('.picture').find('UL');
		this.commercialManagementDescription = $('.commercial-management-description');
	};	
	
	ServicesSlider.prototype = {
		create:function() {	
			
			var index = 0;
			this.commercialManagementNavigation.find('li:eq('+index+')').addClass('active').hide().fadeIn(300);
			this.commercialManagementPicture.find('li:eq('+index+')').addClass('active').hide().fadeIn(300);
			//this.commercialManagementDescription.find('li:eq('+index+')').addClass('active').hide().fadeIn(300);
			this.commercialManagementNavigation.find('li').click($.proxy(this.scope, 'change'));
		},
		
		change:function(e) {
			var index = ($(e.currentTarget).index());
			this.commercialManagementNavigation.find('li').removeClass('active')
			this.commercialManagementPicture.find('li').removeClass('active').hide();
			this.commercialManagementNavigation.find('li:eq('+index+')').addClass('active')
			this.commercialManagementPicture.find('li:eq('+index+')').addClass('active').hide().fadeIn(300);
			


			var pane = $('.scroll-pane');
				var api = pane.data('jsp');
				api.scrollTo(0, 0);
				api.scrollTo(0, $($(e.currentTarget).find('a').attr('href')).offset().top - pane.offset().top);
	
			e.preventDefault(); 

			
			//return false;
		}
	}

 

	/**
	 * Constructor
	 */	
	TableCellFix = function(el, isDebug) {
		this.scope = this;
		this.init(el);
		if(isDebug) {
		}
	};	
	
	TableCellFix.prototype = {
		init:function(columns) {
			var tallestcolumn = 0;
			columns.each(
				function() {
					currentHeight = $(this).height();
					if(currentHeight > tallestcolumn) { tallestcolumn  = currentHeight;}
					
				}
			);
			columns.height(tallestcolumn);		
		}
	}
	

	/**
	 * Constructor
	 */	
	LayoutFix = function() {
		this.scope = this;
	};	
	
	LayoutFix.prototype = {
	
		mainScrollPaneInit:function() {
			
			var _header = $('#header');
			var _content = $('#content');
			var _footer = $('#footer');
			var _window = $(window);;
			var _wapper = $('#wrapper-inner');
			var _body = $('body');				
			var mainContentScrollPane = $('#main-content').find('.scroll-pane');
			var sidebarContentScrollPane = $('#sidebar').find('.scroll-pane');
			
			var _specMargin = 0;
			var _footerMargin = 0;

			if(mainContentScrollPane.length!=0){
				var titleFix = 0;
				
				mainContentScrollPane.height(($('#main-content').height() - (mainContentScrollPane.offset().top-$('#main-content').offset().top))-(titleFix))
				if(mainContentScrollPane.parent().find('a.more').length!=0) {
					mainContentScrollPane.height(($('#main-content').height() - (mainContentScrollPane.offset().top-$('#main-content').offset().top))-mainContentScrollPane.parent().find('a.more').height())
					mainContentScrollPane.css('max-height', '320px');
				} 
				if(mainContentScrollPane.parent().find('.promo-description').length!=0) {
					var _min = 0;

					if($('.objects-wrap').find('li').length<=3) {
						_min = 143;
					} else {
						_min = 300;
					}
					mainContentScrollPane.height(($('#main-content').height() - (mainContentScrollPane.offset().top-$('#main-content').offset().top))-mainContentScrollPane.parent().find('.promo-description').height()-0)
					mainContentScrollPane.css('min-height', _min);
					mainContentScrollPane.css('max-height', 255);
					mainContentScrollPane.css('height', _min);
					mainContentScrollPane.css('margin-bottom', '10px');
					mainContentScrollPane.parent().find('.scroll-pane:last-child').css('min-height', '50px');
					mainContentScrollPane.parent().find('.scroll-pane:last-child').css('height', $('#wrapper-inner').height()-mainContentScrollPane.parent().find('.scroll-pane:last-child').offset().top-30);
					mainContentScrollPane.parent().find('.scroll-pane:last-child').css('margin-bottom', '0');
				}	

				if(mainContentScrollPane.parent().find('.gallery').length!=0) {

						
					if($('#content').hasClass('one-column-left')) {
						mainContentScrollPane.css('min-height', 60)
						mainContentScrollPane.css('max-height', 'auto')
						$('.pictures').css('max-height', (_content.height()-$('.gallery').offset().top)+$('.gallery-slider-wrap').height()-82)
						$('.pictures').css('height', $('.pictures').find('li').height()-82)
						
						var _x = $('#main-content').height() -($('.pictures').offset().top+ $('.pictures').height()+$('.gallery-slider-wrap').height()-10)
						if(_x<=0) { _x = 50 }
						mainContentScrollPane.height(_x)
						
					} else {
						mainContentScrollPane.css('min-height', 57)
						mainContentScrollPane.css('max-height', 'auto')
						$('.pictures').css('max-height', (_content.height()-$('.gallery').offset().top)+$('.gallery-slider-wrap').height()-82)
						$('.pictures').css('height', $('.pictures').find('li.active').height()-82)
						
						var _x = $('#main-content').height() -($('.pictures').offset().top+ $('.pictures').height()+$('.gallery-slider-wrap').height()-10)
						
						if( $('#main-content').height() -($('.pictures').offset().top+ $('.pictures').height()+$('.gallery-slider-wrap').height()-10)<50) {
							$('.pictures').css('max-height', (_content.height()-$('.gallery').offset().top)+$('.gallery-slider-wrap').height()-128)
							$('.pictures').css('height', $('.pictures').find('li.active').height()-128)						
						}

						if(_x<=0) { 					
							_x = 57
						}
						mainContentScrollPane.height('auto')	
						mainContentScrollPane.height(_x)
						

	
						
					}
					

				}					
			}		
		}, 
		
		mapResizeInit:function() {
			var _map = $('.map.rent');
			var _gnplan = $('.general-plan');
			if(_map.length!=0){
				$('#main-content').height(($('#wrapper-inner').height()));
				_map.height(($('#wrapper-inner').height() - _map.offset().top)-22);
				_map.width($('#main-content').width()-30)
				$('#arenda-map').height(_map.height()+50)
				if($('body').hasClass('rent')) {
					_map.width($('#main-content').width()-2)
					_map.height(($('#wrapper-inner').height() - _map.offset().top)-($('.partners').height()+45));
					if($('.partners').find('li').length == 0) {
						_map.height(($('#wrapper-inner').height() - _map.offset().top)-22);
					}
				}
				
				
				
				$('.map-navigation-item-left').height(_map.height()-40)
				$('.map-navigation-item-right').height(_map.height()-40)
			}
			
			if(_gnplan.length!=0) {
				$('#main-content').height(($('#wrapper-inner').height()));
				_gnplan.height(($('#wrapper-inner').height() - _gnplan.offset().top)-42);
				_gnplan.width($('#main-content').width()-30)	
				_gnplan.css('max-width', _gnplan.find('.photo').find('img').width());
				_gnplan.css('max-height', _gnplan.find('.photo').find('img').height())	
				
				$('.map-navigation-item-left').height(_gnplan.height()-40)
				$('.map-navigation-item-right').height(_gnplan.height()-40)				
			}
		},
		
		resizeTitles:function() {
			$('div.main-contnet-title').find('img').height($('div.main-contnet-title').find('img').height())
			$('div.main-contnet-title').find('img').width($('div.main-contnet-title').find('img').width())
			$('div.main-contnet-title').find('h1').css('padding-left', ($('div.main-contnet-title').find('img').width()+20))
			$('div.main-contnet-title').find('h2').css('padding-left', ($('div.main-contnet-title').find('img').width()+20))
			var _h1height = $('div.main-contnet-title').find('h1').height()
			var _h2height = $('div.main-contnet-title').find('h2').height()
			if($('div.main-contnet-title').find('h2').length!=0) {
				$('div.main-contnet-title').height(_h1height+_h2height)	
			} else { 
				_h2height = $('div.main-contnet-title').find('img').height()
				$('div.main-contnet-title').height(_h2height)	
			}	
		},		
		
		
		init:function() {
		
			
			var _header = $('#header');
			var _content = $('#content');
			var _footer = $('#footer');
			var _window = $(window);;
			var _wapper = $('#wrapper-inner');
			var _body = $('body');		

			_body.height(Math.floor(_window.height()))
			_content.height( Math.floor(_body.height()-(_header.height()+(_footer.height()+20))))
			_wapper.height( Math.floor(_body.height()-(_header.height())))
			
			

			var nav 
			var mainContent
			var sideBar

			$('#nav').height(_content.height())	
			$('#main-content').height(_content.height())	
			$('#sidebar').height(_content.height())	
			
			var _heightFix = new TableCellFix($('#nav, #main-content, #sidebar'));
			
			var mainContentScrollPane = $('#main-content').find('.scroll-pane');
			var sidebarContentScrollPane = $('#sidebar').find('.scroll-pane');
			
			if($('.pictures').length!=0) {
				$('.pictures').css('max-height', (_content.height()-$('.gallery').offset().top)+$('.gallery-slider-wrap').height()-71)
				$('.pictures').css('height', $('.pictures').find('li').height()-71)
			}

			if($('.map.rent').length!=0) {
				$('.map.rent').css('height', 'auto')
			}
			this.mainScrollPaneInit();
			
			
			if(sidebarContentScrollPane.length!=0){
				sidebarContentScrollPane = $('#sidebar').find('.scroll-pane');
				sidebarContentScrollPane.height($('#sidebar').height() - (sidebarContentScrollPane.offset().top-$('#sidebar').offset().top))
			}

			$('#nav, #main-content, #sidebar').removeAttr('style');
			
			var _heightFix = new TableCellFix($('#nav, #main-content, #sidebar'))
			_content.height(Math.floor($('#main-content').height()))

			var _specMargin = 0;
			var _footerMargin = 0;

			this.mainScrollPaneInit();
			
			
			if(sidebarContentScrollPane.length!=0){
				sidebarContentScrollPane = $('#sidebar').find('.scroll-pane');
				sidebarContentScrollPane.height($('#sidebar').height() - (sidebarContentScrollPane.offset().top-$('#sidebar').offset().top))
			}

			var _MainContentMarginLeft=20;
			var _MainContentMarginRight=20;
			var _marginFormNav = $('#nav').width()+20;
			
			if($('#nav').length != 0 && $('#sidebar').length !=0 ) {
				_MainContentMarginLeft= $('#nav').width()+20;
				_MainContentMarginRight=$('#sidebar').width()+20;
				_marginFormNav = $('#nav').width()+20;
			}	

			if($('#nav').length != 0 && $('#sidebar').length ==0 ) {
				_MainContentMarginLeft= $('#nav').width()+20;
				_MainContentMarginRight=0;
				_marginFormNav = $('#nav').width()+20;
			}				
			
			if($('#nav').length == 0 && $('#sidebar').length ==0 ) {
				_MainContentMarginLeft= 0;
				_MainContentMarginRight=9;
				_marginFormNav = 9;
				
				if($('#content').hasClass('search-page')) {
					_MainContentMarginRight = 0;
					_marginFormNav = 0;
				}
			}
			
			if($('#nav').length == 0 && $('#sidebar').length !=0 ) {
				_MainContentMarginLeft= $('#sidebar').width()+20;
				_MainContentMarginRight=0;
				_marginFormNav = 9;
			}				

			
			$('#main-content').css('left', _marginFormNav);
			$('#main-content').width($('#content').width()-(_MainContentMarginLeft+_MainContentMarginRight));
			
			
			_wapper.height(Math.floor(_content.height()+_header.height()))
			

			if(_body.height()< _wapper.height()+_footer.height()){
				_body.css('overflow', 'auto');

			} else {
				_body.css('overflow', 'hidden');
				_body.css('padding-top', '0');

				
			}
			
			if(_body.height()-( _wapper.height()+_footer.height())>30) {
				 _wapper.height(Math.floor(_wapper.height()+(_body.height()-( _wapper.height()+_footer.height()))-20))
			}

			$('.gallery').width($('#main-content').width()-2)
			_body.height(_window.height())
			
			if(_body.height()-(_wapper.height()+(_footer.height()))>0){
				_wapper.height(_wapper.height()+20)
				_footer.css('margin-top', 0)
			} else {
				_wapper.height(_wapper.height()-15)
			}


			this.mainScrollPaneInit();
			this.mapResizeInit();
			
			
		
			
		}
	}	

	
})();

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

$(document).ready(function() {



})

var preInitInterval = null;

	$('#wrapper').css('display', 'block');

	var _LayoutFix = new LayoutFix()
	
	
	if($.browser.mozilla == true) {
		$('html').addClass('firefox');
	}
	

	
	
	var countryProjectsInterval = setTimeout(preInit, 10);
	$(window).resize(function() { 
		preInit()
		countryProjectsFix() ;		
		countryProjectsInterval = setTimeout(countryProjectsFix, 100);
	});	
	
	

	function countryProjectsFix()  {
		if($('body').hasClass("country-projects")) {
			if($('.promo-wrap').lenght!=0) {
				if($('.jspVerticalBar').length == 0) {
					$('#footer').css('top', -10)
				} else {
					$('#footer').css('top', 0)
				}
			}	
		}	

		clearTimeout(countryProjectsInterval);
	}	
	
	
	
	function preInit() {
	    _LayoutFix.resizeTitles();
		_LayoutFix.init();
		_gallerySlider.resize();
		countryProjectsFix();	
		clearInterval(preInit)
	}
	
	var _promoSlider = new Slider();
	_promoSlider.init('.promo>.picture', '.promo>.switch', '', true, null, false);
	
	var _newsSlider = new Slider();
	_newsSlider.sliderTimeoutInterval=4700;
	_newsSlider.sliderTimeoutInterval=4700;
	_newsSlider.init('.news>UL', '', '', true, null);	
	

	var _PSNPopupGalleryClass = new PSNPopupGallery();
	_PSNPopupGalleryClass.galleryContainer.find('LI>A').bind('click', $.proxy(_PSNPopupGalleryClass.scope, 'buildGalery'));	

	var _PSNPopupRecommendGalleryClass = new PSNPopupRecommendGallery();
	$('LI.recommend-item').find('.photo>A').bind('click', $.proxy(_PSNPopupRecommendGalleryClass.scope, 'buildGalery'));
	$('LI.recommend-item').find('H2>A').bind('click', $.proxy(_PSNPopupRecommendGalleryClass.scope, 'buildGalery'));		
	
	var _PSNPopupManagerClass = new PSNPopupManager();
	$('LI[rel="manager-item"]').bind("click", $.proxy(_PSNPopupManagerClass.scope, 'createManagerPopup'));	
	
	var _PSNPopupPressClass = new PSNPopupPress();
	$('A[rel="press-item"]').bind("click", $.proxy(_PSNPopupPressClass.scope, 'createPopup'));	
	
	var _PSNPopupMapClass = new PSNPopupMap();
	$('A[rel="map"]').bind("click", $.proxy(_PSNPopupMapClass.scope, 'createMap'));	
	
	
	var _PSNPopupHandleClass = new PSNPopupHandle();

	$('A[rel="popup"]').bind("click", $.proxy(_PSNPopupHandleClass.scope, 'createPopup'));	

	var _creditCalcClass = new CreditCalc();
	_creditCalcClass.create('div.loan-calculator');
	
	$('.questions-answers').find('A').click(function(e) {
		var currentItem = $(e.currentTarget);
		
		if(!$(e.currentTarget).parent().find('.description').hasClass('expand')) {
			currentItem.parent().parent().find('.description').hide().removeClass('expand')
			$(e.currentTarget).parent().find('.description').fadeIn(1).addClass('expand');	
		} else {
			$(e.currentTarget).parent().find('.description').fadeOut(1).removeClass('expand');	
		}		

		return false;
	})
	
	var _ServicesSlider = new ServicesSlider() 
	_ServicesSlider.create();

	$('.map').find('span.icon').height($('.map').find('span.city').height());
	//$('#main-content>.place>LI').find('span.icon').height($('.map').find('span.city').height());
	
	//var placeContainerFix = $('#main-content').find('.place').find('LI').find('.picture');
	//placeContainerFix.height(placeContainerFix.parent().find('.description-village').height()-15)
	
	$('#search').val("Поисковый запрос");
	
	$('#search').focus(function(){
		if($(this).val() == "Поисковый запрос") { $(this).val("") } 
		$(this).addClass("focus");
	});
	
	$('#search').blur(function(){
		if($(this).val() == "" || $(this).val() == "Поисковый запрос") { $(this).val("Поисковый запрос") } 
		$(this).removeClass("focus");
	});
	_LayoutFix.resizeTitles();

	$('#content.one-column #main-content .objects:last-child').css('margin', 0);
	
	var _gallerySlider = new Slider();
	_gallerySlider.nextPrevMargin = 2;
	_gallerySlider.nextPrevSpace = 6;	
	_gallerySlider.nextPrevContainerTop = 5;	
	

	_LayoutFix.init();	
	

	_gallerySlider.init('.gallery>UL.pictures', 'UL.gallery-slider', '', false, '.gallery-navigation', true);	
	_gallerySlider.resize();	
	
	countryProjectsFix();

	preInit()
		
	
	preInitInterval = setTimeout(preInit, 10);
	clearTimeout(preInit);
	clearInterval(preInit);

$(function()
{
	$('.scroll-pane').each(
		function()
		{
			$(this).jScrollPane(
				{
					showArrows: true,
					autoReinitialise: true
				}
			);
			var api = $(this).data('jsp');
			var throttleTimeout;

			
			$(window).bind(
				'resize',
				function()
				{
					if ($.browser.msie) {
						// IE fires multiple resize events while you are dragging the browser window which
						// causes it to crash if you try to update the scrollpane on every one. So we need
						// to throttle it to fire a maximum of once every 50 milliseconds...
						if (!throttleTimeout) {
							throttleTimeout = setTimeout(
								function()
								{
									api.reinitialise();
									throttleTimeout = null;
								},
								50
							);
						}
					} else {
						api.reinitialise();
					}
				}
			);
		}
	)
	
	

});



