$(document).ready(function() {
	
	//Открыть и закрыть блок спецпредложений
	var isDiv=0;
	/*$("#block").click(function(){
		isDiv=1;
		$("#block").attr("class","curr");
	});
	
	$('body').click(function(){
		if(isDiv==0){
			$("#block").attr("class","def");
		}
		isDiv=0;
	});*/
	//Конец (Открыть и закрыть блок спецпредложений)
	
	
	//Наведение мышки на рабочих на главной
	$('area').mouseenter(function(){
		var w_id = $(this).attr('id').toString();
		var p_id = w_id.substr(1);
		$('#'+p_id).parent().attr('class', p_id);
	});
	
	$('#full_photo').mouseleave(function(event){
		var obj = $(event.relatedTarget);
		if(obj.hasClass('hz') == true || obj.parent().hasClass('hz') == true) {		
			event.preventDefault();
			event.stopImmediatePropagation();
		}
		else {
			$('#full_photo').attr('class', 'def');
		}
	});
	//Конец (Наведение мышки на рабочих на главной)
	
	
	//Листалка на главной
	var widthWindow = $(window).width();
	var widthFullPhoto = $('#full_photo').width();
	var residue = (widthWindow - widthFullPhoto) / 2;
	var widthMasterSlider = $('#wom').width();
	var residureMaster = (widthWindow - widthMasterSlider) / 2;
	var perem = 1;
	var perem2 = 1;
	var speed = 3000;
	
	$('#arrow').click(function() {
		if (perem == 1) {
			$('#arrow').css('background', 'url(/images/left.png) no-repeat');
			
			$('#full_photo, #select_note, #block2').animate({
				left: '-=' + (widthWindow - residue - 120) +'px'
			}, speed);
			
			$('#arrow').animate({
				right: '+=' + (widthWindow - 320) +'px'
			}, speed);
			
			$('#wom, #vmp, #ws, #wss').animate({
				right: '+=' + (widthWindow - residureMaster + 30) +'px'
			}, speed, function() {
				perem = 2;
				perem2 = 2;
			});
		}
		else {
			$('#arrow').css('background', 'url(/images/right.png) no-repeat');
			
			$('#full_photo, #select_note, #block2').animate({
				left: '+=' + (widthWindow - residue - 120) +'px'
			}, speed, function() {
				$('#full_photo, #select_note').css('left', '42%');
				if (widthWindow <= 1100) {
					$('#block2').css('right', '0px');
				}
				else if (widthWindow > 1100 && widthWindow <= 1300) {
					$('#block2').css('right', '100px');
				}
				else {
					$('#block2, #awards_small').css('left', '76%');
					$('#block2, #awards_small').css('right', 'auto');
				}
			});
			
			$('#arrow').animate({
				right: '-=' + (widthWindow - 320) +'px'
			}, speed, function() {
				$('#arrow').css('right', '100px');
			});
			
			$('#wom, #vmp, #ws, #wss').animate({
				right: '-=' + (widthWindow - residureMaster + 30) +'px'
			}, speed, function() {
				perem = 1;
				perem2 = 1;
			});
		}
	});
	//Конец (Листалка на главной)
	
	
	//Фигня всякая для растяжки на главной
	if ($.browser.msie) {
		if ($.browser.version == "9.0") {
			$('#middle').css('background', '#ffbf00');
		}
	}
	
	if (widthWindow <= 1200) {
		$('#table_bot_main').css('display', 'none');
		$('#table_bot_main2').css('display', 'block');
	}
	else {
		$('#table_bot_main2').css('display', 'none');
		$('#table_bot_main').css('display', 'block');
	}
	
	if (widthWindow <= 1100) {
		$('#block2').css('right', '0px');
		$('#awards_small').css('right', '56px');
		$('#block2, #awards_small').css('left', 'auto');
	}
	else if (widthWindow > 1100 && widthWindow <= 1300) {
		$('#block2').css('right', '100px');
		$('#awards_small').css('right', '156px');
		$('#block2, #awards_small').css('left', 'auto');
	}
	else {
		$('#block2, #awards_small').css('left', '76%');
		$('#block2, #awards_small').css('right', 'auto');
	}
	
	$(window).resize(function() {
		widthWindow = $(window).width();
		widthFullPhoto = $('#full_photo').width();
		residue = (widthWindow - widthFullPhoto) / 2;
		widthMasterSlider = $('#wom').width();
		residureMaster = (widthWindow - widthMasterSlider) / 2;
		
		if (widthWindow > 1200) $('#wss').css('display', 'block');
		else $('#wss').css('display', 'none');
		
		if (widthWindow <= 1200) {
			$('#table_bot_main').css('display', 'none');
			$('#table_bot_main2').css('display', 'block');
		}
		else {
			$('#table_bot_main2').css('display', 'none');
			$('#table_bot_main').css('display', 'block');
		}
		
		if (widthWindow <= 1100) {
			if (perem2 == 1) $('#block2').css('right', '0px');
			$('#awards_small').css('right', '56px');
			$('#block2, #awards_small').css('left', 'auto');
		}
		else if (widthWindow > 1100 && widthWindow <= 1300) {
			if (perem2 == 1) $('#block2').css('right', '100px');
			$('#awards_small').css('right', '156px');
			$('#block2, #awards_small').css('left', 'auto');
		}
		else {
			if (perem2 == 1) $('#block2, #awards_small').css('left', '76%');
			$('#block2, #awards_small').css('right', 'auto');
		}
	});
	//Конец (Фигня всякая для растяжки на главной)
	
	
	/*Фигня всякая*/
	$('#services_a').click(function() {
		$('#services_b').css('display', 'block');
		return false;
	});
	
	$('body').click(function(){
		$('#services_b').css('display', 'none');
	});
	/*Конец (Фигня всякая)*/
	
	
	//Попап
	var widthWindow = $(window).width();
	var heightWindow = $(window).height();
	var widthDocument = $(document).width() - 20;
	var heightDocument = $(document).height();
	var widthPopup;
	var heightPopup;
	var imgSrc;
	var leftMargin;
	var topMargin;
	var peremPopup = 0;
	var scrollTop;
	var alt;
	
	popupShow = function() {
		$('#gallery_popup a img').unbind('click');
		$('#popup_close').unbind('click');
		$('#popup_zagluchka').unbind('click');
		
		scrollTop = $(window).scrollTop();
		imgSrc = $(this).parent().attr('href');
		alt = $(this).attr('alt');
		$('body').append('<img id="imgSrc" src="' + imgSrc + '" alt="" />');
		
		$('#imgSrc').load(function() {
			widthPopup = $('#imgSrc').width();
			heightPopup = $('#imgSrc').height();
			leftMargin = (widthWindow - (widthPopup + 27)) / 2;
			topMargin = (heightWindow - (heightPopup + 43)) / 2;
			
			if (topMargin < 0) topMargin = 0;
			if (leftMargin < 0) leftMargin = 0;
			
			$('#imgSrc').remove();
			if ($.browser.version == "6.0" || $.browser.version == "7.0" || $.browser.version == "8.0") $('body').append('<div id="popup_zagluchka" style="width:' + widthDocument + 'px; height:' + heightDocument + 'px; position:absolute; top:-240px; left:-30px; z-index:100000;">');
			else $('body').append('<div id="popup_zagluchka" style="width:' + widthDocument + 'px; height:' + heightDocument + 'px; position:absolute; top:0px; left:0px; z-index:100000;">');
			
			$('body').append('<div id="popup" style="top:' + (topMargin + scrollTop) + 'px; left:' + leftMargin + 'px;"><div id="popup_position"><div id="popup_close"><a href="#">Закрыть</a></div><div id="popup_alt">' + alt + '</div><table style="width:' + (widthPopup + 54) + 'px;" cellpadding="0" cellspacing="0" border="0"><tr><td id="popup_top_left"></td><td id="popup_top"></td><td id="popup_top_right"></td></tr><tr><td id="popup_left"></td><td><img id="popup_img" src="' + imgSrc + '" alt="" /></td><td id="popup_right"></td></tr><tr><td id="popup_bottom_left"></td><td id="popup_bottom"></td><td id="popup_bottom_right"></td></tr></table></div></div>');
			
			$('#popup_close').bind('click', popupClose);
			$('#popup_zagluchka').bind('click', popupClose);
		});		
		return false;
	}
	
	$(window).resize(function() {
		$('#popup_zagluchka').css({'width': '0px', 'height': '0px'});
		scrollTop = $(window).scrollTop();
		widthWindow = $(window).width();
		heightWindow = $(window).height();
		widthDocument = $(document).width() - 20;
		heightDocument = $(document).height();
		leftMargin = (widthWindow - (widthPopup + 27)) / 2;
		topMargin = (heightWindow - (heightPopup + 43)) / 2;
		if (topMargin < 0) topMargin = 0;
		if (leftMargin < 0) leftMargin = 0;
		$('#popup').css({'top': (topMargin + scrollTop) + 'px', 'left': leftMargin + 'px'});
		$('#popup_zagluchka').css({'width': widthDocument + 'px', 'height': heightDocument + 'px'});
	});
	
	popupClose = function() {
		$('#popup_close').unbind('click');
		$('#popup_zagluchka').unbind('click');
		$('#popup_zagluchka').remove();
		$('#popup').remove();
		$('#gallery_popup a img').bind('click', popupShow);
		return false;
	}
	
	$('#gallery_popup a img').bind('click', popupShow);
	//Конец (Попап)
	
	
	$('#click_hide').click(function() {
		isDiv=1;
		$('#blsp').removeClass().addClass('def');
		$('#blsp').parent().parent().removeClass().addClass('def');
	});
	
	$('#blsp').click(function() {
		isDiv=0;
		$('#blsp').removeClass().addClass('curr');
		$('#blsp').parent().parent().removeClass().addClass('curr');
	});
});
