var containerObj;
(function($) {
	// plugin definition
	$.fn.ezBgResize = function(cntr) {
		if (cntr) {
			center = 1;
		}
		// First position object
		containerObj = this;
		
		containerObj.css("visibility","hidden");
		
		$(window).load(function() {
			resizeImage();
		});
		
		$(window).bind("resize",function() {
			resizeImage();
		});
	};
	function resizeImage() {
		if ( ($(window).height() >= 520) && ($(window).width() >= 980)){
			$("body").css({
				"overflow":"hidden"
			});
		} else{
			$("body").css({
				"overflow":"auto"
			});
		}
		if ($(window).width() < 980){
			new_bg_width = 980;
		} else{
			new_bg_width = $('body').width();
		}
		if ($(window).height() < 520){
			new_bg_height = 520;
		} else {
			new_bg_height = $('body').height();
		}
		

		containerObj.css({
			"position":"relative",
			"top":"0px",
			"left":"0px",
			"z-index":"0",
			"overflow":"hidden",
			"width":new_bg_width,
			"height":new_bg_height
		});
		// Resize the img object to the proper ratio of the window.
		if ( containerObj.children('img').attr('width') && containerObj.children('img').attr('height') ){
			var iw = containerObj.children('img').attr('width');
			var ih = containerObj.children('img').attr('height');
		} else{
			var iw = containerObj.children('img').width();
			var ih = containerObj.children('img').height();
		}
		if (new_bg_width > new_bg_height) {
			if (iw > ih) {
				var fRatio = iw/ih;
				containerObj.children('img').attr("width",new_bg_width);
				containerObj.children('img').attr("height",Math.round(new_bg_width * (1/fRatio)));
				var newIh = Math.round(new_bg_width * (1/fRatio));
				if(newIh < new_bg_height) {
					var fRatio = ih/iw;
					containerObj.children('img').attr("height",new_bg_height);
					containerObj.children('img').attr("width",Math.round(new_bg_height * (1/fRatio)));
				}
			} else {
				var fRatio = ih/iw;
				containerObj.children('img').attr("height",new_bg_height);
				containerObj.children('img').attr("width",Math.round(new_bg_height * (1/fRatio)));
			}
		} else {
			var fRatio = ih/iw;
			containerObj.children('img').attr("height",new_bg_height);
			containerObj.children('img').attr("width",Math.round(new_bg_height * (1/fRatio)));
		}
		containerObj.css("visibility","visible");
		// Center BG Image
		containerObj.children('img').css("position","relative");
		var wDiff = - (containerObj.children('img').width() - containerObj.width()) / 2;
		containerObj.children('img').css("left", wDiff);
		var hDiff =  - (containerObj.children('img').height() - containerObj.height()) / 2;
		containerObj.children('img').css("top", hDiff);
		var w_sochi = (containerObj.children('img').width()*0.08+wDiff);
		var h_sochi = (containerObj.children('img').height()*0.7+hDiff);
		$('.sochi').css('left',w_sochi);
		$('.sochi').css('top',h_sochi);
		var w_spb = (containerObj.children('img').width()*0.22+wDiff);
		var h_spb = (containerObj.children('img').height()*0.37+hDiff);
		$('.spb').css('left',w_spb);
		$('.spb').css('top',h_spb);
		var w_moscow = (containerObj.children('img').width()*0.21+wDiff);
		var h_moscow = (containerObj.children('img').height()*0.52+hDiff);
		$('.moscow').css('left',w_moscow);
		$('.moscow').css('top',h_moscow);
		var w_transport_combain = (containerObj.children('img').width()*0.04+wDiff);
		var h_transport_combain = (containerObj.height()*0.23);
		$('.transport_combain').css('left',w_transport_combain);
		$('.transport_combain').css('top',h_transport_combain);
		$('.center').css('height',new_bg_height - 250);
		$('.center_experts').css('left',(new_bg_width - 788)/2);
		if(new_bg_width < 1050){
			$('.up_menu').css('right','0');
			$('.up_menu ul.menu li a').css('font-size', '18px');
		}else
		{
			$('.up_menu').css('right','26px');
			$('.up_menu ul.menu li a').css('font-size', '18px');
			}
		//portfolio
		$('.big_picture').css('height',new_bg_height - 350);
		$('.big_picture img').css('height',new_bg_height - 350);
		$('.center-portfolio').css('width',$('.big_picture img').width() + 375);
		$('.center-portfolio .forpscroll').css('height',$('.big_picture img').height() - $('.portfolio_title').height() - 25);
			if ( $('.scroll-pane_port').hasClass('hasScroll') ){
				$('.scroll-pane_port').jScrollPaneRemove();
				$('.scroll-pane_port').removeClass('hasScroll');
			}
			if ($('.forpscroll').height()){
				$('.forpscroll .scroll-pane_port').css('height', $('.forpscroll').height());
				$('.forpscroll .scroll-pane_port').addClass('hasScroll');
				$('.forpscroll .scroll-pane_port').jScrollPane({showArrows:true, scrollbarWidth:10, dragMaxHeight:127});
			}
		//contacts
		img_rate = 1.25;
		if ((new_bg_height - 410) > 246){
			new_img_height = 246;
		} else {
			new_img_height = new_bg_height - 410;
		}
		new_img_width = (new_bg_height - 410)*img_rate;
		if (new_img_width > 307){
			new_img_width = 307;
		}
		$('.centercontact').css('height',new_img_height + 170);
		$('.center-contacts .big_picture').css('height',new_img_height);
		$('.center-contacts .big_picture img').css('height',new_img_height);
		$('.center-contacts .big_picture img').css('width', new_img_width);
		$('.center-contacts .big_picture iframe').css('height',new_img_height);
                $('.center-contacts .big_picture').css('width',new_img_width);
                $('.center-contacts .img_textphoto').css('width',new_img_width + 10);
		var cch = $('.center-contacts').height();
		var marcch =  $('.big_picture').height() + 170;
		var ccimgt =  $('.big_picture').height() + 10;
		var contacts_margin = (cch - marcch) / 2;
		$('.contacts-content .img_text').css('height', new_img_height +10);
		var cph = $('.center-portfolio').height();
		var vcch =  $('.center-portfolio .big_picture').height() + 150;
		var center_margin = (cph - vcch) / 2;
		var cht = $('.center').height();
		var cc2h = $('.center-portfolio #port_big_image').height() + 80;
		var ctop_margin = (cht - cc2h) / 2;
		$('.center-portfolio').css('margin-top', ctop_margin);
		var mleft = (new_bg_width - $('.center-portfolio').width() - 212) / 2;
		$('.center-portfolio').css('margin-left', mleft);
		$('.experts').css('height',new_bg_height - 250);
		$('.experts_container').css('height',new_bg_height - 270);
		$('.maps_table').css('height',$('.contacts-content .img_text').height());
		$('.contacts-content .contacts').css('height',new_img_height - 60);
		$('.contacts-content .contacts .jScrollPaneContainer').css('height',new_img_height - 60);
		$('.centercontact').css('z-index',1);
		$('.center').css('z-index',1);
		$('.center-contacts .img_textcontent').css('width', 702 - new_img_width);
		$('.center-contacts .contacts').css('width', 687 - new_img_width);
		var hmaincenter = $('.center').height();
		var hmaincontent = $('.center .main-content').height();
		var hmaincont = $('.center .main-content .content').height();
		if(hmaincont > hmaincenter){
			$('.center .main-content').css('height', hmaincenter - 40);
		}
		 else {
			$('.center .main-content').css('height', hmaincont + 40);
		}
		var htexttcont = $('.main-content').height() + 30;
		$('.center .textcont').css('height', htexttcont);
		$('.center .openconentleft').css('height', htexttcont);
		$('.center .openconentleft').css('top', (hmaincenter - htexttcont)/2);		
		$('#center_transport').css('height',new_bg_height - 250);
		var hmaincenter2 = $('#center_transport').height();
		var hmaincontent2 = $('#center_transport .main-content').height();
		var hmaincont2 = $('#center_transport .main-content .content').height();
		if(hmaincont2 > hmaincenter2 - 90){
			$('#center_transport .main-content').css('height', hmaincenter2 - 155);
		}
		 else {
			$('#center_transport .main-content').css('height', hmaincont2 + 40);
		}
		var htexttcont2 = $('#center_transport .main-content').height() + 125;
		$('#center_transport .opencontentright').css('height', htexttcont2);
		$('#center_transport .opencontentright').css('top', (hmaincenter2 - htexttcont2)/2);		
		if ( $('.main-content').height() < $('.main-content .content').height() ){
			if ( $('.scroll-pane2').hasClass('hasScroll') ){
				$('.scroll-pane2').jScrollPaneRemove();
				$('.scroll-pane2').removeClass('hasScroll');
			}
			$('.scroll-pane2').css('height', $('.main-content').height() - 25);
			$('.scroll-pane2').addClass('hasScroll');
			$('.scroll-pane2').jScrollPane({showArrows:true, scrollbarWidth:10, dragMaxHeight:127});
		} else {
			if ( $('.scroll-pane2').hasClass('hasScroll') ){
				$('.scroll-pane2').jScrollPaneRemove();
				$('.scroll-pane2').removeClass('hasScroll');
			}
			$('.scroll-pane2').css('height', 'auto');
		}
		//experts
		var hcexp = (new_bg_height - 250);
		var hexp = $('.experts_container').height();
		var hexpcont = $('.experts .experts_container .content').height();
		if(hexpcont > hexp){
			$('.experts .experts_container').css('height', hmaincenter - 10);
		}
		 else {
			$('.experts .experts_container').css('height', hmaincont + 40);
		}
		$('.center_experts').css('height',hcexp);
		if ( $('.experts_container').height() < $('.experts_container .content').height() ){
			if ( $('.scroll-pane2').hasClass('hasScroll') ){
				$('.scroll-pane2').jScrollPaneRemove();
				$('.scroll-pane2').removeClass('hasScroll');
			}
			$('.scroll-pane2').css('height', $('.experts_container').height() - 25);
			$('.scroll-pane2').addClass('hasScroll');
			$('.scroll-pane2').jScrollPane({showArrows:true, scrollbarWidth:10, dragMaxHeight:127});
		}
		//our dest
		var hrc = (new_bg_height - 200)/2;
		var wrc = (new_bg_width - 855)/2;
		$('.right_center').css('top',hrc);
		$('.right_center').css('left',wrc);
		//experts resize overlay
		$('.cont_one>#overlay').css({
			'width': new_bg_width,
			'height': new_bg_height
		});
		$('.cont_2').css('height', new_bg_height);
		// resize Right Div
			var rh = new_bg_height - 118 - 59;
			var scrollh = new_bg_height - 118 - 59 - 100;
			$('#block-views-portfolio2-block_1').css('height',rh);
			$('#block-views-events-block_1').css('height',rh);
			$('#block-views-news-block_1').css('height',rh);
			$('#block-views-photogallery-block_1').css('height',rh);
			$('.scrollable').css('height',scrollh);
		//resize calendar
			if ( $('.center_calendar').height() < $('.calendar_container').height() ){
				$('.center_calendar').css('height', new_bg_height - 210);
			} 
			$('.center_calendar').css('margin-left', 0);
			var caltop = (new_bg_height - $('.calendar').height() + 59)/2;
			$('.center_calendar').css('top', caltop);
			if (new_bg_width > 1050){
				$('.right_cal').css('margin-left', 20);
			} else{
				$('.right_cal').css('margin-left', 0);
			}
			sat_marg = new_bg_width/2-390;
			$('.view-sations').css('margin-right', sat_marg);
			$('.month_nav').css('margin-right', sat_marg);
			pane_height = new_bg_height - 250;
			if (pane_height > 600){
				pane_height = 600;
			}
				hrate = (pane_height) / $('.right_cal').height();
				if ($('.right_cal').css('height')){
					fsize = Math.round(parseInt($('.right_cal').css('font-size').replace('px','')) * hrate);
					if (fsize < 8){
						fsize = 8;
					}
					if (fsize > 15){
						fsize = 15;
					}
					cursize = Math.round(parseInt($('.right_cal .curr_day').css('font-size').replace('px','')) * hrate);
					if (cursize < 10){
						cursize = 10;
					}
					if (cursize > 17){
						cursize = 17;
					}
					$('.right_cal').css('font-size', fsize);
					$('.right_cal .curr_day').css('font-size', cursize);
				}
			if ( $('.center_calendar').height() < $('.calendar_container').height() ){
				if ( $('.scroll-pane2').hasClass('hasScroll') ){
					$('.scroll-pane2').jScrollPaneRemove();
					$('.scroll-pane2').removeClass('hasScroll');
				}
				$('.scroll-pane2').css('height', $('.center_calendar').height() - 25);
				$('.scroll-pane2').addClass('hasScroll');
				$('.scroll-pane2').jScrollPane({showArrows:true, scrollbarWidth:10, dragMaxHeight:127});
				
			}
			for ( i = 1; i < $('.contact_panes').length + 1; i++ ){
				$('.pane_'+ i).css({
					'visibility' : 'hidden',
					'display'    : 'block'
				});
				$('.pane_'+ i +' .scroll-pane2').css('height', 'auto');
					$('.pane_'+ i +' .scroll-pane2').css('height', new_bg_height - 480);
					$('.pane_' + i + ' .scroll-pane2').css('width', $('.pane_' + i + ' .content').width() - 20);
					$('.pane_'+ i +' .scroll-pane2').addClass('hasScroll');
					$('.pane_'+ i +' .scroll-pane2').jScrollPane({showArrows:true, scrollbarWidth:10, dragMaxHeight:127});
					$('.pane_' + i + ' .jScrollPaneContainer').css('width', '100%');
					$('.pane_' + i + ' .scroll-pane2 p').css('margin','0');
				$('.pane_'+ i).css({
					'display'    : 'none',
					'visibility' : 'visible'
				});
				if ( i == 1 ){
					$('.pane_' + i).css('display', 'block');
				}
			}
		if ( ($(window).height() < 520) || ($(window).width() < 980) ){
			$("body").css("overflow", "auto");
			$('html').css('overflow', 'auto');
			if (new_bg_width > 980 && new_bg_width > $('body').width()){
				containerObj.css('width',$('body').width());
			}
			if (new_bg_height > 520 && new_bg_height > $('body').height()){
				containerObj.css('height',$('body').height());
			}
		} else{
			$('html').css('overflow', 'hidden');
			containerObj.css('width',$('body').width());
			containerObj.css('height',$('body').height());
			
		}
		
		
		
		$('#loading_div').animate(
		{
			opacity: 0
		}, 1000, function(){
			$(this).css('display', 'none');
			if ($('#motiongallery').height()){
				setTimeout('fillup();', 300);
			}
		});
	}
})(jQuery);
