function buildMainPage_(){
	$('body.main_page').each(function(){
		var block_width = 200;
		var block_height = 250;
		var columns = getColumnsMax();
		var columns_max = columns;
		var overlay_width = $('body').width();
		var min_overlay_width = 900; //меньше 900 не даём вообще странице сжиматься
		if (overlay_width < min_overlay_width) {
			overlay_width = min_overlay_width;
		}
		var bg_width = $('#works_overlay_line').attr('width');
		var bg_height = $('#works_overlay_line').attr('height');
		var line_height = 350;
		var tg = (bg_height-line_height)/bg_width;
		var overlay_height = Math.ceil(line_height+overlay_width*tg);
		
		var map_coords = bg_width+',0,0,'+Math.ceil(bg_height-line_height)+',0,'+bg_height+','+bg_width+','+line_height;
		$('#works_overlay area').attr('coords',map_coords)
	
		if(overlay_width >= 900){
			$('#works_overlay').width('100%');
		} else {
			$('#works_overlay').width(900);
		}
		$('#works_overlay')
			.height(overlay_height)
			.children('.bl').hide();
		
		var i = 1;
		while (Math.ceil(overlay_width/i) > block_width && i <= columns_max) { 
			columns = i;
			i++;	
		}
		var block_column_width = Math.ceil((overlay_width-16)/columns);
		$('.bl').addClass('bl_hidden');
		for(var block_id = Math.ceil(columns_max-columns+1); block_id < columns_max+1; block_id++) {
			var pos_left = Math.ceil((block_id-1-(columns_max-columns))*block_column_width+block_column_width/2-block_width/2);
			$('#block_'+block_id)
				.css({'position':'absolute','rightMargin':'0','left':pos_left,'top':Math.ceil(0+(line_height-block_height-block_width*tg)/2+(overlay_width-pos_left)*tg),'width':block_width,'height':block_height})
				.show().removeClass('bl_hidden');
		}
	
		var mainBgWidth = $('#bg_main img').attr('orig_width');
		var mainBgHeight = $('#bg_main img').attr('orig_height');
		
		if(mainBgWidth > 0 && mainBgHeight > 0){
			var screenFactor = $('body').width()/$('body').height();
			var mainBgFactor = mainBgWidth/mainBgHeight;
			if(screenFactor >= mainBgFactor){
				mainBgWidth = $('body').width();
				mainBgHeight = Math.ceil(mainBgWidth/mainBgFactor);
			} else {
				mainBgHeight = $('body').height();
				mainBgWidth = Math.ceil(mainBgHeight*mainBgFactor);
			}
			$('#bg_main img')
				.width(mainBgWidth)
				.height(mainBgHeight)
		}
		
		$('#bl_news_main').css({'top':'55px'})
		$('#bl_routine_main').css({'top':'auto', 'bottom':'-15px'})
		
		$('#last_new').width('100%')
		$('#pre_last_new').width('100%')
		if (overlay_width <= min_overlay_width) {
			$('#last_new').width('70%')
			$('#pre_last_new').width('85%')
		} else if (overlay_width <= min_overlay_width+100) {
			$('#last_new').width('70%')
		} else if (overlay_width <= min_overlay_width+200) {
			$('#last_new').width('80%')
		}
		
		var pos_top = Math.ceil(0.4*overlay_width*tg-67);
		$('#works_overlay_title')
			.show()
			.css({'top':pos_top})
	})
}

function buildMainPage(){
	$('body.main_page').each(function(){
		var mainBgWidth = $('#bg_main img').attr('orig_width');
		var mainBgHeight = $('#bg_main img').attr('orig_height');
		
		if(mainBgWidth > 0 && mainBgHeight > 0){
			var screenFactor = $('body').width()/$('body').height();
			var mainBgFactor = mainBgWidth/mainBgHeight;
			if(screenFactor >= mainBgFactor){
				mainBgWidth = $('body').width();
				mainBgHeight = Math.ceil(mainBgWidth/mainBgFactor);
			} else {
				mainBgHeight = $('body').height();
				mainBgWidth = Math.ceil(mainBgHeight*mainBgFactor);
			}
			$('#bg_main img')
				.width(mainBgWidth)
				.height(mainBgHeight)
		}
		
		var headerHeight = $('#header').height();
		var footerHeight = $('#footer').height();
		var bodyHeight = $('body').innerHeight();
		
		var contentHeight = Math.round((bodyHeight-headerHeight-footerHeight));

		if(contentHeight > 360){
			$('.content').height(contentHeight);
		} else {
			$('.content').height(360);
		};
	})
}

$(document).ready(function(){
	buildMainPage();
	$('HTML').addClass('JS');
})

$(window).resize(function() {
	buildMainPage();
});

$(function(){
	$('#works_overlay_title')
		.hover(
		  function () {
			if($(this).hasClass('works_overlay_title_0')){
				$(this).addClass('works_overlay_title_0_hover');
			} 
			if($(this).hasClass('works_overlay_title_1')){
				$(this).addClass('works_overlay_title_1_hover');
			}
		  }, 
		  function () {
		    $(this)
		    	.removeClass('works_overlay_title_0_hover works_overlay_title_1_hover');
		  }
		)
		.click(function(){
				if($('#works_overlay').hasClass('works_overlay_hidden')) {
					$('#works_overlay')
						.removeClass('works_overlay_hidden')
						.find('#works_overlay_line')
						.stop()
						.animate({opacity: 1}, 300, function() {}).end()
						.find('.bl')
						.stop()
						.css('display','block')
						.animate({opacity: 1}, 300, function() {})
						.end()
						.find('.bl_hidden').hide();
				} else {
					$('#works_overlay')
						.addClass('works_overlay_hidden')
						.find('#works_overlay_line')
						.stop()
						.animate({opacity: 0}, 300, function() {})
						.end()
						.find('.bl')
						.stop()
						.animate({opacity: 0}, 300, function() {})
						.css('display','none')
						.end()
						.find('.bl_hidden').hide();
				}
		});
})

$(function(){
	$('ul.con_contacts .path h5').click(function(){
		$(this).next().toggle();
		if($('#by_car').is(':visible') || $('#by_foot').is(':visible')) {
			$('#scheme').show();
		}
		else {
		    $('#scheme').hide();
		}
		
	});
})

$(function(){
	$('div.con_work_image_gallery_list a').click(function(){
		$(this).siblings().removeClass('selected').end().addClass('selected');
		placeFullGalleryImage();
		return false;
	});
})

$(function(){
	$('div.con_work_image_gallery_arrows span.next').click(function(){
		if(!$(this).hasClass('disabled')){
			$('div.con_work_image_gallery_arrows span.prev').removeClass('disabled').removeAttr('disabled');
			var place = $(this).parent().attr('place');
			$('div.con_work_image_gallery_list[place='+ place +'] a.selected')
				.removeClass('selected')
				.next().addClass('selected').click();
		}
	});
	
	$('div.con_work_image_gallery_arrows span.prev').click(function(){
		if(!$(this).hasClass('disabled')){
			$('div.con_work_image_gallery_arrows span.next').removeClass('disabled').removeAttr('disabled');
			var place = $(this).parent().attr('place');
			$('div.con_work_image_gallery_list[place='+ place +'] a.selected')
				.removeClass('selected')
				.prev().addClass('selected').click();
		}
	});
})

function placeFullGalleryImage(){
	$('div.con_work_image_gallery_list a.selected').each(function() {
		var place = $(this).attr('place');
		var picture_path = $(this).attr('href');
		$('div#work_gallery_place_'+place).html('<img src="'+picture_path+'" />');
		
		var imgIndex = parseInt($(this).siblings().andSelf().index(this))+1;
		var imgTotal = parseInt($(this).siblings().andSelf().length);
		if(imgIndex == 1){
			$('div.con_work_image_gallery_arrows[place='+ place +'] .prev').addClass('disabled').attr('disabled','disabled');
		} else {
			$('div.con_work_image_gallery_arrows[place='+ place +'] .prev').removeClass('disabled').removeAttr('disabled');
		}
		if(imgIndex == imgTotal){
			$('div.con_work_image_gallery_arrows[place='+ place +'] .next').addClass('disabled').attr('disabled','disabled');
		} else {
			$('div.con_work_image_gallery_arrows[place='+ place +'] .next').removeClass('disabled').removeAttr('disabled');
		}
		
	});
}


$(function(){
	$('.str_logo').hover(function(){
		$(this).addClass('str_logo_over');
	}, function(){
		$(this).removeClass('str_logo_over');	
	});
})
