$(document).ready(function(){
var currentPosition = 0;
var slideWidth = 460;
var slides = $('.slide');
var numberOfSlides = slides.length;
$('#leftControl').addClass('pas');
$('#slideInner').css('width', slideWidth * numberOfSlides);
$('.control')
	.bind('click', function(){
		if($(this).attr('id')=='leftControl' && currentPosition==0 || $(this).attr('id')=='rightControl' && currentPosition==numberOfSlides-1){
			return false;
		}
		else {
			currentPosition = ($(this).attr('id')=='rightControl')
			? currentPosition+1 : currentPosition-1;

			if($(this).attr('id')=='rightControl') {
				$('.list_slides').find('.act').removeClass('act').next().addClass('act');
			}
			else{
				$('.list_slides').find('.act').removeClass('act').prev().addClass('act');
			};
			if(currentPosition==0){ $('#leftControl').addClass('pas'); }
			else{ $('#leftControl').removeClass('pas') }
			if(currentPosition==numberOfSlides-1){ $('#rightControl').addClass('pas'); }
			else{ $('#rightControl').removeClass('pas') }

			$('#slideInner').animate({
				'marginLeft' : slideWidth*(-currentPosition)
			});
		}
    });

	for(i=1; i<=numberOfSlides; i++){
		$('.list_slides ul').append('<li><a href="#">'+i+'</a></li>')
		if(i==1) {
			$('.list_slides ul li:first').addClass('act');
		};
	};
	$('.list_slides a').click(function(){
		currentPosition = $(this).html()-1;
		$('#slideInner').animate({
			'marginLeft' : slideWidth*(-currentPosition)
		});
		$('.list_slides li').removeClass('act');
		$(this).parents('li').addClass('act');
		if(currentPosition != numberOfSlides || currentPosition != 0) {
			$('#leftControl').removeClass('pas');$('#rightControl').removeClass('pas');
		};
		return false;
	});

	var w_list = $('.list_slides .over_ul_ls').width();
	w_list = w_list + 70;
	$('.list_slider').css('width', w_list);

	$('.list_slides a:first').click(function(){
		$('#leftControl').addClass('pas');$('#rightControl').removeClass('pas');
	});

	$('.list_slides a:last').click(function(){
		$('#leftControl').removeClass('pas');$('#rightControl').addClass('pas');
	});
});

$(function(){
	$(".head_where_city a").click(function(){
		if(!$(this).hasClass('act')) {
			$(".head_where_city a, .cnt_inf_hd").toggleClass('act');
		}
		return false;
	});
});

$(function(){
	$(".list_prt_clt a").click(function(){
		if(!$(this).hasClass('act')) {
			$(".list_prt_clt a, .table_clients").toggleClass('act');
		}
		return false;
	});
});

$(document).ready(function(){
	$('.online_zaiavka a').click(function(){
		$('.popup_nei').fadeIn(1000);
		$('.bg_popup').fadeIn(1000);
		return false;
	});

	$('.bg_popup, .popup_nei .esc_neibox').click(function(){
		$('.bg_popup').fadeOut(1000);
		$('.popup_nei').fadeOut(1000);
	});
});

$(document).ready(function(){
	$('.a_js_nei').click(function(){
		$('.popup_nei_otzivi').fadeIn(1000);
		$('.bg_popup').fadeIn(1000);
		return false;
	});

	$('.bg_popup, .popup_nei_otzivi .esc_neibox').click(function(){
		$('.popup_nei_otzivi').fadeOut(1000);
		$('.bg_popup').fadeOut(1000);
	});
});

$(document).ready(function(){
	$('.port_wo_js a').click(function(){
		$('.popup_nei_port_work').fadeIn(1000);
		$('.bg_popup').fadeIn(1000);
		return false;
	});

	$('.bg_popup, .popup_nei_port_work .esc_neibox').click(function(){
		$('.popup_nei_port_work').fadeOut(1000);
		$('.bg_popup').fadeOut(1000);
	});
});



$(document).ready(function(){
	$('.cent_part_ppw .some_block_ppw').click(function(){
		$(this).parents('.cent_part_ppw').find('.some_block_ppw').toggleClass('zind');
	});
});

$(document).ready(function(){
	$('.vac_js_ni .unic_a a').click(function(){
		$(this).parents('.vac_js_ni').find('.crey_block').slideToggle(function(){
			$(this).parents('.vac_js_ni').toggleClass('act');
		});
		return false;
	});
});

/*
$(document).ready(function(){
	$('.bl_txt_otzivi a').click(function(){
		$('.main_bl_otzivi .some_otziv').addClass('for_tup_js');
		$(this).parents('.some_otziv').removeClass('for_tup_js');
		var lng = $('.main_bl_otzivi .some_otziv').length;
		for(i=0; i<lng; i++) {
			$('.main_bl_otzivi .for_tup_js').eq(i).find('.bl_txt_otzivi').eq(0).addClass('act');
			$('.main_bl_otzivi .for_tup_js').eq(i).find('.bl_txt_otzivi').eq(1).removeClass('act');
		}
		$(this).parents('.some_otziv').find('.bl_txt_otzivi').toggleClass('act');
		return false;
	});
});*/


$(document).ready(function(){
	$('.addr_spc em').click(function(){

		if($(this).parents('.spc_fr_map').hasClass('act')){
			$(this).parents('.spc_fr_map').find('.map_contacts').slideUp(function(){
				$(this).parents('.spc_fr_map').removeClass('act');
			});

		}
		else {
			$('.spc_fr_map .map_contacts').slideUp();
			$('.spc_fr_map').removeClass('act');
			$(this).parents('.spc_fr_map').find('.map_contacts').slideToggle(function(){
				$(this).parents('.spc_fr_map').toggleClass('act');
			});
		};
		return false;
	});
	/*$('.addr_spc em').click(function(){
		$(this).parents('.spc_fr_map').find('.map_contacts').slideToggle(function(){
			$(this).parents('.spc_fr_map').toggleClass('act');
		});
		return false;
	});*/
});

$(function(){
	$(".js_inp_1").focus(function(){if(this.value=="представьтесь, пожалуйста") this.value="";});
	$(".js_inp_1").blur(function(){if(this.value=="") this.value="представьтесь, пожалуйста";});

	$(".js_inp_2").focus(function(){if(this.value=="предпочтительный способ связи (ICQ, телефон, скайп)") this.value="";});
	$(".js_inp_2").blur(function(){if(this.value=="") this.value="предпочтительный способ связи (ICQ, телефон, скайп)";});

	$(".popup_nei textarea").focus(function(){if(this.value=="текст заявки") this.value="";});
	$(".popup_nei textarea").blur(function(){if(this.value=="") this.value="текст заявки";});

	$(".js_inp_3").focus(function(){if(this.value=="представьтесь, пожалуйста") this.value="";});
	$(".js_inp_3").blur(function(){if(this.value=="") this.value="представьтесь, пожалуйста";});

	$(".js_inp_4").focus(function(){if(this.value=="название вашей организации") this.value="";});
	$(".js_inp_4").blur(function(){if(this.value=="") this.value="название вашей организации";});

	$(".popup_nei_otzivi textarea").focus(function(){if(this.value=="текст отзыва") this.value="";});
	$(".popup_nei_otzivi textarea").blur(function(){if(this.value=="") this.value="текст отзыва";});

	$(".js_inp_5").focus(function(){if(this.value=="представьтесь, пожалуйста") this.value="";});
	$(".js_inp_5").blur(function(){if(this.value=="") this.value="представьтесь, пожалуйста";});

	$(".js_inp_6").focus(function(){if(this.value=="название вашей организации") this.value="";});
	$(".js_inp_6").blur(function(){if(this.value=="") this.value="название вашей организации";});

	$(".popup_nei_port_work textarea").focus(function(){if(this.value=="текст заявки") this.value="";});
	$(".popup_nei_port_work textarea").blur(function(){if(this.value=="") this.value="текст заявки";});
	$('.line_mbc .some_mbc:last-child').css('margin-right','0');
});

$(document).ready(function(){
	$('#planet_jq_nei .spn').css('top','0').css({ opacity: 0 });
	$('.h_planet_jq_nei').css({ opacity: 0 });
	$('.h_planet_jq_nei').animate({opacity: 1}, 3000);
	var i = 0;
	setInterval(function(){
		if(i==2){
			$('#planet_jq_nei .spn').eq(i).animate({opacity: 1, top: "97px"}, 1000);
			i++
		}
		else{
			$('#planet_jq_nei .spn').eq(i).animate({opacity: 1, top: "140px"}, 1000);
			i++;
		};
	},300);
	/*$('#planet_jq_nei .spn').click(function(){
		if(!$(this).hasClass('big_plt')){
			var pos = $(this).position();
			$(this).animate({opacity: 0, left: "-200px"}, 1200,function(){
				$(this).css('top','97px').animate({opacity: 1, left: "473px"}, 1500);
				$(this).find('img').css('width','268px');
				$(this).toggleClass('big_plt');
			});
			$('.big_plt.spn').animate({opacity: 0, left: "1200px"}, 1200, function(){
				$(this).css('top','140px').animate({opacity: 1, left: pos.left}, 1500);
				$(this).find('img').css('width','170px');
				$(this).toggleClass('big_plt');
			});
		};
	});*/


// Index page portfolio scroller
	if ($('#planet_jq_nei').length > 0) {
		$(function() {

			var clickTime = new Date().getTime();

			var container = $('#planet_jq_nei').find('.planet_jq_nei__wrapper');
			var items = container.find('.spn');

			var itemTop = 140;
			var itemRight = parseInt(items.last().css('left'));
			var itemWidth = parseInt(items.first().outerWidth(1));

			var itemBig = items.eq(2);
			var itemBigLeft = parseInt(itemBig.css('left'));
			var itemBigTop = 97;
			var itemBigWidth = parseInt(itemBig.outerWidth(1));

			var itemPrevLeft = parseInt(itemBig.prev().css('left'));
			var itemNextLeft = parseInt(itemBig.next().css('left'));


			/* container.on('mouseenter', '.spn', function() {
				if ($(this).hasClass('big_plt')) return false;
				$(this).stop(true, true).animate({
						'padding-bottom': 30,
						top: itemBigTop
					}, 200);
			});
			container.on('mouseleave', '.spn', function() {
				if ($(this).hasClass('big_plt')) return false;
				$(this).stop(true, true).animate({
					'padding-bottom': 30,
					'top': itemTop
				}, 200);
			}); */

			container.on('click', '.spn', function() {

				// if ($(this).hasClass('big_plt')) return false;

				var scrollDuration = 300;

			// limit on click's interval
				var clickTime2 = new Date().getTime();
				if ( clickTime2 - clickTime < scrollDuration) return false;

			// refresh items
				items = container.find('.spn');

				var itemIndex = $(this).index();
				var firstItem = items.first();
				var lastItem = items.last();
				var itemMiddleIndex = 3;

				var offsetItem, offsetSign, itemToBig, imgBig, imgToBig;

			// left OR right scroll
				if (itemIndex < itemMiddleIndex) {
					offsetItem = lastItem;
					offsetSign = '+=';
					offsetItem.clone()
						.css({
							'left' :  -itemWidth,
							'top' : itemTop
						}).prependTo(container);
				} else {
					offsetItem = firstItem;
					offsetSign = '-=';
					offsetItem.clone()
						.css({
							'left' :  itemRight + itemWidth + 40,
							'top' : itemTop
						}).appendTo(container);
				}

			// refresh items
				items = container.find('.spn');
				itemBig = container.find('.big_plt');

			// scrol to right
				if (itemIndex < itemMiddleIndex) {
					itemToBig = itemBig.prev();
					itemToSmallLeft = itemNextLeft;
			// scroll to left
				} else {
					itemToBig = itemBig.next();
					itemToSmallLeft = itemPrevLeft;
				}

				imgBig = itemBig.find('img');
				imgToBig = itemToBig.find('img');
				itemBig.removeClass('big_plt');

			// scroll all items but not active item
				items.not(itemBig).stop(true, true).animate({
					left: offsetSign + (itemWidth + 40),
					top: itemTop
				}, scrollDuration + 50, function() {
					offsetItem.remove();
					clickTime = new Date().getTime();
				});

			// animate the big item to small
				itemBig.stop().animate({
					top: itemTop,
					left: itemToSmallLeft,
					width: itemWidth
				}, scrollDuration);
			// animate the big item's img to small
				imgBig.animate({
					width: itemWidth
				}, scrollDuration);

			// animate a regular item to big
				itemToBig.stop().animate({
					left: itemBigLeft,
					top: itemBigTop,
					width: itemBigWidth
				}, scrollDuration, function() {
					itemToBig.addClass('big_plt');
				});
			// animate a regular img to big
				imgToBig.animate({
					width: itemBigWidth
				}, scrollDuration);

			});

		});
	} // if ($('#planet_jq_nei').length > 0)

});


