function simple_nav(button_id){
	$('#'+button_id).trigger('click');
	return false;
}

/* ротация баннеров на главной */
function main_banners_cicle(){
	$('#main_teasers td').each(function(){
		$(this).children('a.teaser:first').delay(5000).fadeOut('slow').delay(5000).fadeIn('slow');
	})
}
/* /ротация баннеров на главной */

/* прелоадер */ 
function preloadImages() { //v3.0
  var d=document; 
  if(d.images)
  { 
	if(!d.MM_p) 
		d.MM_p=new Array();	
	var i,j=d.MM_p.length,a=preloadImages.arguments; 
	for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0){ 
			d.MM_p[j]=new Image; 
			d.MM_p[j++].src=a[i];
		}
  }		
}
/* /прелоадер */


/* получение названия вакансии при сабмите формы отправки резюме */
function getPositionName(event) {
	var target;
	if (!event) var event = window.event;
	if (event.target) target = event.target;
	else if (event.srcElement) target = event.srcElement;
	if (target.nodeType == 3)
		target = target.parentNode;	
	
	var position_name = $(target).closest('tr').prev().find('span.post').text();
	var recipient_name = $(target).prev().find('span.recipient_name').text();
	$(target).children('input[name="position_name"]').val(position_name);
	$(target).children('input[name="recipient_name"]').val(recipient_name);	
}
/* /получение названия вакансии при сабмите формы отправки резюме */

$(document).ready(function() {
	/* прелоад картинок на главной */ 
	preloadImages(
		'/img/active_ring.png',
		'/img/main_page_face_01_bw.png',
		'/img/main_page_face_02_bw.png',
		'/img/main_page_face_03_bw.png',
		'/img/main_page_face_04_bw.png',
		'/img/main_page_face_05_bw.png',
		'/img/main_page_face_00_bw.png'		
	);
	/* /прелоад картинок на главной */ 


	$('#consultants div:last').css({'margin-bottom':'0px','border-bottom':'0px','padding-bottom':'0px'});

	$('.hidden_block').hide();
	$('.pseudo_link').click(function(){
		if($(this).parent()[0].nodeName == 'TD') {
			$(this).parent().parent().next().show().find('.hidden_block').slideToggle('fast');			
		} else {
			$(this).parent().next('.hidden_block').slideToggle('fast');			
		}
	})

	$('.hidden_block .close_button').click(function(){
		$(this).parent('.hidden_block').slideToggle('fast');
	})




	/* навигация на главной странице */
	var arrow = '<img src="/img/main_page_faces_arrow.png" id="main_page_faces_arrow" title="" alt="" />';
	
	function showDefault() {
		if ($('#main_page_faces_arrow').length) return; 		
		$('#main_page_people a img').each(function(){
			var $this = $(this),
				new_src = $this.attr('src').replace('_bw.png','.png');
			$this.attr('src',new_src).removeClass('grayscale');
		});
		$('#main_page_intro_00').stop().fadeTo('fast', 1).show();
		$('#main_page_face_00').append(arrow).fadeTo('fast', 1).show();		
	}

	function HoverOver() {
		var id = $(this).attr('id').slice(-2);
		$('#main_page_people a img').not('#main_page_faces_arrow').not('.grayscale').each(function(){
			var $this = $(this), 
				new_src = $this.attr('src').replace('.png','_bw.png');
			$this.attr('src',new_src).addClass('grayscale');
		});
		$('#main_page_face_'+id+' img').attr('src','/img/main_page_face_'+id+'.png').removeClass('grayscale');

		$('#main_page_intros .intro:visible').stop().fadeTo('fast', 0).hide();
		$('#main_page_intro_'+id).stop().fadeTo('fast', 1).show();

		$('#main_page_faces_arrow').fadeTo('fast', 0).remove();
		$('#main_page_face_'+id).append(arrow).fadeTo('fast', 1).show();

		$('#main_page_ring_'+id).children().stop().clearQueue();
		$('#main_page_ring_'+id).children('.ring').queue(function(){
			var $this = $(this);
			if ($this.parent().hasClass('active_ring')) {				
				$this.animate({
					width: '216px'
				}, 100, function(){
					$this.siblings().not('.ring,').css('opacity','1');
				});
			} else {
				$this.siblings().not('.ring').css('opacity','0');
				$this.animate({
					width: '0px'
				}, 100, function(){
					$this.parent().addClass('active_ring').css('z-index','100');
					$this.attr('src','/img/active_ring.png').animate({
						width: '216px'
					}, 100, function(){
						$this.siblings().not('.ring, .external_link').css('opacity','1');
					})
				});
			}
			$this.dequeue();
		});
	}
		
	function HoverOut() {
		var id = $(this).attr('id').slice(-2);
		var new_src = $('#main_page_face_'+id+' img').attr('src').replace('_bw.png','.png');
		$('#main_page_face_'+id+' img').attr('src', new_src).removeClass('grayscale');
		
		$('#main_page_intros .intro:visible').stop().fadeTo('fast', 0).hide();
		$('#main_page_faces_arrow').fadeTo('fast', 0).remove();
		
		setTimeout(showDefault, 200);
		
		$('#main_page_ring_'+id).children().stop().clearQueue();
		$('#main_page_ring_'+id).children('.ring').queue(function(){
			if ($(this).parent().hasClass('active_ring')) {
				$(this).siblings().not('.ring, .external_link').css('opacity','0');
				$(this).animate({
					width: '0px'
				}, 100, function(){
					$(this).parent().removeClass('active_ring').css('z-index','');
					$(this).attr('src','/img/normal_ring.png').animate({
						width: '216px'
					}, 100, function(){
						$(this).siblings().not('.ring').css('opacity','1');
					})
				});
			} else {
				$(this).animate({
					width: '216px'
				}, 100, function(){
					$(this).siblings().not('.ring').css('opacity','1');
				});
			}
			$(this).dequeue();
		});
	}
	
	var config = {
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 0, // number = milliseconds for onMouseOver polling interval    
		 over: HoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 0, // number = milliseconds delay before onMouseOut    
		 out: HoverOut // function = onMouseOut callback (REQUIRED)    
	};
	
	$("#main_page_people a, #main_page_rings a").hoverIntent(config);
	
	/* кольца на разводных страницах */
	$('#services_page_rings a').hover(
		function () {
			$(this).children().stop().clearQueue();
			$(this).children('.ring').queue(function(){
				if ($(this).parent().hasClass('active_ring')) {
					$(this).animate({
						width: '216px'
					}, 100, function(){
						$(this).siblings().not('.ring,').css('opacity','1');
					});
					$(this).dequeue();
				} else {
					$(this).siblings().not('.ring').css('opacity','0');
					$(this).animate({
						width: '0px'
					}, 100, function(){
						$(this).parent().addClass('active_ring').css('z-index','100');
						$(this).attr('src','/img/active_ring.png').animate({
							width: '216px'
						}, 100, function(){
							$(this).siblings().not('.ring, .external_link').css('opacity','1');
						})
					});
					$(this).dequeue();
				}
			})
		}, function(){
			$(this).children().stop().clearQueue();
			$(this).children('.ring').queue(function(){
				if ($(this).parent().hasClass('active_ring')) {
					$(this).siblings().not('.ring, .external_link').css('opacity','0');
					$(this).animate({
						width: '0px'
					}, 100, function(){
						$(this).parent().removeClass('active_ring').css('z-index','');
						$(this).attr('src','/img/normal_ring.png').animate({
							width: '216px'
						}, 100, function(){
							$(this).siblings().not('.ring').css('opacity','1');
						})
					});
					$(this).dequeue();
				} else {
					$(this).animate({
						width: '216px'
					}, 100, function(){
						$(this).siblings().not('.ring, .external_link').css('opacity','1');
					});
					$(this).dequeue();
				}
			})
		}
	);
	/* /кольца на разводных страницах */


	/* видеоплеер доя страницы спонсорство и поддержка - фехтование */
	$('#video_block').jqm({modal: true});
	$('.video_gallery img.left').live('click',function(){
		var index = $('.video_gallery img.left').index(this) + 1;
		$('#video_block a').attr('href','/video/f'+index+'.flv');
		$('#video_block').jqmShow();
		flowplayer("player", "/video/flowplayer-3.2.5.swf",  {
			clip: {
				autoPlay: true,
				autoBuffering: true
				}
			});
	});
	/* /видеоплеер доя страницы спонсорство и поддержка - фехтование */

	/* ПЕРЕКЛЮЧЕНИЕ БЛОКОВ В ПРЕЗЕНТАЦИИ */
	$('#presentation_header span.pseudo_link').click(function presentation_nav(){
		$('#presentation_header span, #presentation_footer span').unbind('click');
		var id = $(this).attr('id').split('_')[1];
		var new_height = $('#presentation_'+id).height();
		$('#presentation_header span, #presentation_footer span').bind('click', presentation_nav).removeClass('active_page').addClass('pseudo_link').css('cursor','');
		$('#top-link_'+id +', #footer-link_'+id +', #digit_'+id).unbind('click').addClass('active_page').removeClass('pseudo_link');
		$('#digit_'+id).css('cursor','default');
		$('.presentation').css('opacity','0');
		$('.presentation:visible').css('opacity','1').animate({
			opacity: 0,
			height: new_height
		}, 200, function() {
			$(this).hide().css('height','');
			$('#presentation_'+id).show().animate({
				opacity: 1
			}, 200, function(){
				if('\v'=='v') { //http://habrahabr.ru/blogs/javascript/100544/
					this.style.removeAttribute("filter");
				}
			})
		}, $.scrollTo('#presentation_header', 800));
	})

	$('#presentation_footer span').click(function(){
		var id = $(this).attr('id').split('_')[1];
		$('#top-link_'+id).click();
	})

	$('#presentation_print div').click(function(){
		window.print();
	})
	/* /ПЕРЕКЛЮЧЕНИЕ БЛОКОВ В ПРЕЗЕНТАЦИИ */


	/* кастомные файл-инпуты */
	var W3CDOM = (document.createElement && document.getElementsByTagName);

	function initFileUploads() {
		if (!W3CDOM) return;
		var fakeFileUpload = document.createElement('div');
		fakeFileUpload.className = 'fakefile';
		fakeFileUpload.appendChild(document.createElement('input'));
		var image = document.createElement('img');
		image.src='/img/browse_button_bg.gif';
		image.className = 'pseudo_button';

		fakeFileUpload.appendChild(image);
		var x = document.getElementsByTagName('input');
		for (var i=0;i<x.length;i++) {
			if (x[i].type != 'file') continue;
			if (x[i].parentNode.className != 'fileinputs') continue;
			x[i].className = 'upload_file_field hidden';
			var clone = fakeFileUpload.cloneNode(true);
			x[i].parentNode.appendChild(clone);
			x[i].relatedElement = clone.getElementsByTagName('input')[0];
			x[i].onchange = x[i].onmouseout = function () {
				this.relatedElement.value = this.value;
			}
		}
	}
	initFileUploads();
	/* /кастомные файл-инпуты */

	/* форма "задать вопрос" */
	$('#question_block').jqm({modal: true, trigger: '.question_form_link'});

	$('#question_form_send').click(function () {
		if ($('#question_form').validate().form()) {
			$request = $("#question_form").serialize();
			$.ajax({
				type: "POST",
				url: "/mail/question_form.php",
				data: $request,
				success: function(msg){
					$('#question_message').html(msg);
				}
			});
		}
	});
	/* /форма "задать вопрос" */

	/* форма "контроль качества" */
	$('#qos_form_send').click(function () {
		$request = $("#qos_form_right").serialize();
		$.ajax({
			type: "POST",
			url: "/mail/qos_form.php",
			data: $request,
			success: function(msg){
				$('#qos_message').html(msg);
			}
		});
	});
	/* /форма "контроль качества" */


	/* тултипы на карте */
	$('map *').each(function(){
		var title = $(this).attr('title');
		var alt = $(this).attr('alt');
		var middle = -60;
		var top = -60;
		middle = Math.round(alt.length/2)*(-6.6);
		if ( alt.length <= title.length )  middle = Math.round(title.length/2)*(-8.1);
		//alert (title.length);
		var icon = '<div id="icon"><div class="td_center"><div class="td_left"></div><span>'+title+'</span>';
		if (alt.length > 2) {icon = icon+'<span class="blue">'+alt+'</span>';} else middle = -60;
		icon = icon+'<div class="td_right">&nbsp;</div></div></div>';

		if($(this).hasClass('big')){
			icon = '<div class="big_icon">'+title+alt+'</div>';
			middle = -78;
			top = -140;
		}

		$(this).tooltip({
			delay: 50,
			showURL: false,
			fixPNG: true,
			top: top,
			left: middle,
			track: true,
			fade: 200,
			bodyHandler: function() {
				return icon;
			}
		});
	});

	$('map *').click(function(){ return false; });
	$('map *').css('cursor', 'hover');
	/* /тултипы на карте */

	
	/* аккордеон на странице "Цены на услуги по налоговому праву" */	
	$('.accordeon_pan').not('.active').hide();
	$('.accordeon_trigger span').click(function(){
		var tab_to_open = $(this).parent().next();
		if (tab_to_open.is(':visible')) {
			$(this).parent().removeClass('active');
			tab_to_open.slideUp('fast');
		} else {
			$(this).parent().next().slideDown('fast').siblings('.accordeon_pan').slideUp('fast');
			$(this).parent().addClass('active').siblings('h5').removeClass('active');
			/* $.scrollTo('.page_title', 400); */
		}
	})
	/* /аккордеон на странице "Цены на услуги по налоговому праву" */
	
	/* личный менеджер */
	$('#personal_manager_block').jqm({modal: true, trigger: '.personal_manager .write'});

	$('.personal_manager .write').click(function(){
		var personal_manager_email = $(this).siblings('.email').children('a').text();
		$('#personal_manager_email').val(personal_manager_email);
	})

	$('#personal_manager_form_send').click(function () {
		$request = $("#personal_manager_form").serialize();
		$.ajax({
			type: "POST",
			url: "/mail/personal_manager_form.php",
			data: $request,
			success: function(msg){
				$('#personal_manager_message').html(msg);
			}
		});
	});
	/* /личный менеджер */
	
	/* переключение блоков */
	$('.block_switch div div').click(function(){
		$(this).parent().siblings().children('div').removeClass('active');
		var id = $(this).addClass('active').parent().attr('id').slice('-2');
		$('#switchable_tab_' + id).show().removeClass('hidden_block').siblings('.switchable_tab').hide();
	})
	/* /переключение блоков */
	
	$('a.fancybox').fancybox();
	
});
