var afShow = 0; //"О компании" 0 - мелкая, 1 - полная, -1 скрыты обе
var animation = false;
var layer = 0;
var dTop = 0; //смещение, при добавлении нескольких пунктов lastmenu
var lastmenuarr = new Array("#0a0d12","#20466d","#4474a4","#0a0d12","#20466d","#4474a4","#0a0d12");
var ruLang = false;
var lnk = ''; //ссылка на текущую страницу
var pageHref = ''; //якорь текущей страницы (менеджмент)
var sdate = '0';
var sblock = '0';
var email1_valid_fl = false;
var email2_valid_fl = false;
var p2id = 0; //id пункта меню второго уровня
var lasthash;
var NotNavigate = false;

$(document).ready(function() {
	ruLang = !$('#logo_en').length;

	initialize();
	update_breadcrumbs();
	aboutInit();
	topmenuInit();
	managementInit();
	top2Init();
	accordInit();
	SearchInit();

	$(window).hashchange( function(){
		if (NotNavigate)
			NotNavigate = false;
		else
			Navigate(location.hash);
	});
	$(window).hashchange();

	$("div#content").mouseleave(function() {
		$(".links").hide();
	});

	$("div#logo, div#logo_en").click(function() {
		location.href = '/';
	});

	$(".menu-item span").click(function() {
		if (!$(this).attr('class')) {
			$(this).attr('class', 'selected');
			$(this).parent().siblings().children().attr('class', '');
			id = $(this).attr('id');
			$('.message_list').each(function() {
				if ($(this).attr('id') == id)
					$(this).show();
				else
					$(this).hide();
			});
		}
	});

	//================== Кряки =======================
	pos = $('.menuscroll').position();
	if (pos)
		$('.lastmenu').css('top', pos.top + 'px');

	MTMw();
	$(window).resize(function() {
		MTMw();
		BackPos();
	});

	BackPos();
	$(window).scroll(function () {
		BackPos();
	});

	$('.popup').click(function(e) {
		t = e.target||e.srcElement;
		if (t.className == 'popup_bg')
			$('.popup').fadeOut(300);
	});

	$("form#send_link").submit(function() {
		if (email1_valid_fl && email2_valid_fl) {
			if (ruLang)
				s = '';
			else
				s = '&en=1';
			s_from = '&from=' + $('#tohide #from').val();
			s_to = '&to=' + $('#tohide #to').val();
			$.ajax({
				url: "/sendlink.php",
				type: "POST",
				data: "link=" + lnk.slice(1) + s + s_from + s_to,
				dataType: 'html',
				success: function(html){
					$('#tohide').hide();
					$('#tochange').text('Спасибо, ссылка отправлена');
					setTimeout("$('.popup').fadeOut(300);", 3000);
				}
			});
		}
		return false;
	});

	$('#tohide input').keyup(function() {
		VerE(this);
	});
	$('#tohide input').click(function() {
		VerE(this);
	});

});

function SendInit()
{
	if (ruLang)
		s = ['Отправить ссылку на страницу', 'Отправитель', 'Получатель', 'Отправить'];
	else
		s = ['Send a link to the page', 'Sender', 'Recipient', 'Send'];
	$('#tohide').show();
	$('#send_link legend').text(s[0]);
	$('#tochange').text(s[1]);
	$('#tohide label').text(s[2]);
	$('#send_link .send_bar input').css({'color': '#666', 'cursor': 'default'}).val(s[3]);
	$('#tohide #from').css({'background-color': '#FFDDDD', 'color': '#CCC'}).val('E-mail');
	$('#tohide #to').css({'background-color': '#FFDDDD', 'color': '#CCC'}).val('E-mail');
	email1_valid_fl = false;
	email2_valid_fl = false;
}

function VerE(sender)
{
	val = $(sender).css({'color': '#000'}).val();
	if (val == 'E-mail') {
		val = '';
		$(sender).val(val);
	}
	fl = verf_email(val);
	if (fl)
		$(sender).css({'background-color': '#FFFFFF'});
	else
		$(sender).css({'background-color': '#FFDDDD'});
	if ($(sender).attr('id') == 'from')
		email1_valid_fl = fl;
	else
		email2_valid_fl = fl;
	VerConfirm();
}

function VerConfirm()
{
	if (email1_valid_fl && email2_valid_fl)
		$('#send_link .send_bar input').css({'color': '#FFF', 'cursor': 'pointer'});
	else
		$('#send_link .send_bar input').css({'color': '#666', 'cursor': 'default'});
}

function verf_email(email)
{
	var at = '@';
	var max_user_len = 64;
	var max_host_len = 255;
	if (email.indexOf(at) == -1 ) {
		return false;
	}
	var email_arr = email.split(at);
	var user = email_arr[0];
	var host = email_arr[1];
	if (user.length == 0) {
		return false;
	}
	if (user.length > max_user_len) {
		return false;
	}
	if (host.length == 0) {
		return false;
	}
	if (host.length > max_host_len) {
		return false;
	}
	var user_regexp = /^([a-zA-Z0-9_\.\-\+])+$/;
	var inet_host_regexp = /^(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{2,4})+$/;
	if (!user_regexp.test(user)) {
		return false;
	}
	if (!inet_host_regexp.test(host)) {
		return false;
	}
	return true;
}

window.onload = function() {
	MTMw();
};

function MTMw()
{
	var width = $("body").css('width').split('px');
	$(".MoreThanMax").css('width', width[0]*2 + 'px');
}

function BackPos()
{
	wh = $(window).height();
	botPos = wh + $(window).scrollTop();
	if (botPos < 1684)
		$('#content').css({'background-attachment': 'scroll', 'background-position': 'center top'});
	else {
		$('#content').css({'background-attachment': 'fixed', 'background-position': 'center ' + wh + 'px'});
		$('body').css('height', 'auto').css('height', $('body').css('height')).css('height', '100%');
	}
}

function initURL(sender)
{
	code = $(sender).attr('code');
	p2 = $(sender).attr('p2');
	if (p2)
		p2 = p2 + '_';
	else
		p2 = '';
	if (code)
		lnk = '#' + p2 + code;
	else {
		var id = $(sender).attr('id');
		if (id.substr(0, 2) == 'li')
			lnk = '#' + p2 + 'id_' + id.substr(2);
		else
			lnk = '#' + p2 + 'id_' + id.substr(3);
	}
	location.href = lnk;
}

function SetHash(hsh) {
	location.href = hsh;
}

function initialize()
{

	$(".center .leftlistitem").unbind('hover');
	$(".center .leftlistitem").hover( function () {
		$(this).parent().children(".leftlistitem").removeClass('select');
		$(this).addClass('select');
	});
	
	$(".leftlistitem").unbind('click');
	$(".leftlistitem").click(function () {
		if (!animation)
			SetHash($(this).attr('href'));
	});

	$(".lastmenu .h1").unbind('click');
	$(".lastmenu .h1").click(function() {
		if (!animation) {
			if ($(this).attr('href') == location.hash)
				lastmenuClick($(this));
			else if ($(".lastmenu .h1").length == 1) {
				NotNavigate = true;
				lastmenuClick($(this));
				SetHash($(this).attr('href'));
			}
			else		
				SetHash($(this).attr('href'));
		}
	});

	$(".new").unbind('click');
	$(".new").click(function(){
		if (!animation) {
			SetHash($(this).attr('href'));
		}
	});

	$("li.print").unbind('click');
	$("li.print").click(function() {
		id = $(this).attr('id');
		en = $(this).attr('en');
		if (id.substr(0, 2) == 'a_')
			id = id + $(this).parents('.content_menu').children('.accorditem').children('h1.active').attr('id');
		//location.href = '/print.php?id=' + id + '&en=' + en;
		window.open('/print.php?id=' + id + '&en=' + en);
	});

	$("li.mail").unbind('click');
	$("li.mail").click(function() {
		SendInit();
		$('.popup').fadeIn(300);
	});

	$("li.add a").unbind('click');
	$("li.add a").click(function() {
		bookmark(this);
	});

	if (typeof(ie7fix) == 'function')
		ie7fix();
}

function getBrowserInfo()
{
	//alert(navigator.appCodeName + '   ' + navigator.appName);
	var t,v = undefined;
	if (window.chrome)
		t = 'Chrome';
	else if (window.opera)
		t = 'Opera';
	else if (document.all) {
		t = 'IE';
		var nv = navigator.appVersion;
		var s = nv.indexOf('MSIE') + 5;
		v = nv.substring(s, s + 1);
	}
	else if (navigator.appName)
		t = 'Netscape';
	return {type: t, version: v};
}

function bookmark(a){
	var url = window.document.location;
	var title = window.document.title;
	var b = getBrowserInfo();
	if (b.type == 'IE' && 8 >= b.version && b.version >= 4)
		window.external.AddFavorite(url, title);
	else if (b.type == 'Opera') {
		a.href = url;
		a.rel = "sidebar";
		a.title = title;
		return true;
	}
	else if (b.type == "Netscape")
		window.sidebar.addPanel(title, url, "");
	else
		if (ruLang)
			alert("Нажмите CTRL-D, чтобы добавить страницу в закладки.");
		else
			alert("Press CTRL-D, to add bookmark");
	return false;
}

function GetMenuVars(sender)
{
	var MenuVars = {};
	if (sender) {
		MenuVars._mt = $(sender).attr('type');
		MenuVars._id = $(sender).attr('id').slice(2);
		if ($(sender).children('div').length)
			MenuVars._name = $(sender).children('div').text();
		else
			MenuVars._name = $(sender).attr('name');
		MenuVars._code = $(sender).attr('code');
	}
	MenuVars.lM = $('.left').last();
	MenuVars.lMname = MenuVars.lM.attr('name');
	MenuVars.lMid = MenuVars.lM.attr('id').slice(2);
	MenuVars.lMmt = MenuVars.lM.attr('type');
	MenuVars.lMcode = MenuVars.lM.attr('code');
	MenuVars.cM = $('.center');
	MenuVars.cMname = MenuVars.cM.attr('name');
	MenuVars.cMid = MenuVars.cM.attr('id').slice(2);
	MenuVars.cMmt = MenuVars.cM.attr('type');
	MenuVars.cMcode = MenuVars.cM.attr('code');
	MenuVars.cMvis = (MenuVars.cM.get(0).style.left != '16%');
	MenuVars.nMinit = function() {
		var nM = $('.center'); //Новое меню
		if (this._id) {
			nM.attr('id', 'lm' + this._id).attr('type', this._mt);
			nM.attr('name', this._name);
			if (this._code)
				nM.attr('code', this._code);
		}
		else {
			this._id = nM.attr('id');
			this._mt = nM.attr('type');
			this._name = nM.attr('name');
			this._code = nM.attr('code');
		}
		this.nM = nM;
	}
	return MenuVars;
}

function AddToLast(newid, newname, code)
{
	if (code)
		var hsh = code;
	else
		var hsh = newid;
	$('.lastmenu').append('<div class="h1" id="h_' + newid + '" href="#' + hsh + '" style="background-color:' + lastmenuarr[layer++] + '">' +
							'<span class="darr" >&darr;</span><div class="article">' + newname + '</div></div>');
	dTop = dTop + 40;
}

function AnimationStart()
{
	animation = true;
	$('.leftlistitem').css('cursor', 'progress');
	$('.lastmenu').css('cursor', 'progress');
	$('.submit').css('cursor', 'progress');
	$('.accorditem h1').css('cursor', 'progress');
	$('body').css('cursor', 'progress');
}

function AnimationStop()
{
	animation = false;
	$('.leftlistitem').css('cursor', 'pointer');
	$('.lastmenu').css('cursor', 'pointer');
	$('.submit').css('cursor', 'pointer');
	$('.accorditem h1').css('cursor', 'pointer');
	$('body').css('cursor', 'default');
}

function leftlistitemClick(sender)
{
	AnimationStart();
	MV = GetMenuVars(sender);
	if (MV._mt == 'e' && $(sender).attr('code') == 'empl') {
		location.href = '/management/';
		return;
	};
	if (MV._mt == 'e' && $(sender).attr('code') == 'empl_en') {
		location.href = '/en/management/';
		return;
	};

	$(sender).parent().children(".leftlistitem").removeClass('select');
	$(sender).addClass('select');

	var cCM = $("#cm" + MV._id + ":first");
	if (cCM.length) { //если меню уже загружено
		cCM.parent().children(".content_menu").hide();
		cCM.show();
		MV.nMinit();
		animateAfterLIC(MV);
	}
	else
		$.ajax({
			url: "menuItems.php",
			type: "GET",
			data: "id=" + MV._id + "&mt=" + MV._mt + "&code=" + MV._code,
			dataType: 'html',
			success : function(html){
				$(".MoreThanMax div.center").remove();
				$(".MoreThanMax").append(html);
				if (MV._mt == 'a')
					accordInit();
				MV.nMinit();
				MV.cM = $('.center');
				animateAfterLIC(MV);
			},
			error: function() {
				AnimationStop();
			}
		});
}

function animateAfterLIC(MV)
{
	if (MV._mt == 'm')
		MV.cM.css({"width": "16%", "z-index": "1"}).show();
	else
		MV.cM.css({"width": "32%", "z-index": "1"}).show();
	if (MV._mt == 'm' && MV.cMvis)
		Finish();
	else {
		if (MV._mt == 'm')
			tLeft = '0';
		else {
			tLeft = '-16%';
			MV.lM.children('.padding').children(".shadow").show();
		}
		MV.cM.animate({"left": tLeft}, 500, function() {
			if (MV._mt != 'm') {
				MV.lM.css({'left': '0', 'marginRight': '-16%'});
				MV.cM.css({'left': '0', 'marginRight': '0'});
				MV.lM.animate({"height": "1px", 'opacity': '0'}, 500);
				AddToLast(MV.lMid, MV.lMname, MV.lMcode);
				dTop = 40;
				LastMenuAnimateUp();
				MV.lM.animate({"height": "1px", 'opacity': '0'}, 500, function() {
					Finish();
				});
			}
			else
				Finish();
		});
	}

	function Finish() {
		initialize();
		update_breadcrumbs();
		AnimationStop();
	}
}

function centeritemClick(sender)
{
	AnimationStart();
	MV = GetMenuVars(sender);
	if (MV._mt == 'e' && $(sender).attr('remurl')) {
		window.open($(sender).attr('remurl'));
		AnimationStop();
		return;
	};
	$.ajax({
		url: "menuItems.php",
		type: "GET",
		data: "id=" + MV._id + "&mt=" + MV._mt + "&code=" + MV._code,
		dataType:'html',
		success : function(html){
			MV.cM.addClass('left').removeClass('center');
			$(".MoreThanMax").append(html);
			if (MV._mt == 'a')
				accordInit();
			MV.nMinit();
			dTop = 0;
			if (MV.cMmt == 'm')
				AddToLast(MV.lMid, MV.lMname, MV.lMcode);
			if (MV._mt != 'm')
				AddToLast(MV.cMid, MV.cMname, MV.cMcode);
			animateAfterCIC(MV);
		},
		error: function(){
			AnimationStop();
		}
	});
}

function animateAfterCIC(MV, pdf)
{
	if (MV.cMmt == 'm')
		MV.lM.children('.padding').children(".shadow").show();
	if (MV._mt != 'm')
		MV.cM.children('.padding').children(".shadow").show();
	MV.cM.css({"z-index": "1"});
	if (MV._mt != 'm') {
		MV.nM.css({"z-index": "2", "width": "32%"}).show();
		if (MV.cMmt == 'm')
			MV.cM.animate({"margin-right": "-16%"}, 500);
		else
			MV.cM.animate({"margin-right": "-32%"}, 500);
	}
	MV.lM.animate({"margin-right": "-16%"}, 500, function() {
		if (MV._mt != 'm')
			MV.cM.animate({"height": "1px", 'opacity': '0'}, 500);
		LastMenuAnimateUp();
		MV.lM.animate({"height": "1px", 'opacity': '0'}, 500, function() {
			update_breadcrumbs();
			initialize();
			AnimationStop();
			if (pdf)
				location.href = pdf;
		});
	});
}

function LastMenuAnimateUp()
{
	aboutHide();
	h = $('.lastmenu').height();
	pos = $('.menuscroll').position();
	new_top = pos.top - h;
	$('.lastmenu').animate({'top': new_top + 'px'}, 500);
}

function lastmenuClick(sender, topMenuItem, t2mId, searchfl, html)
{
	AnimationStart();

	var clicked = $(sender).index();
	var last = $(".lastmenu .h1").last().index();
	var del = last + 1 - clicked;
	layer -= del;
	var h = $(sender).height() + 26;
	$('.lastmenu .h1:gt(' + clicked + ')').each(function() {
		h = h + $(this).height() + 26;
	});
	$('.lastmenu').animate({"top": "+=" + h + "px"}, 300, function() {
		$('.lastmenu .h1:gt(' + clicked + ')').remove();
		$(sender).remove();
	});

	var M1 = $('.listmenu:eq(' + clicked + ')');
	var M2 = $('.listmenu:eq(' + (clicked+1) + ')');
	var cM = $(".center");
	var M1mt = M1.attr('type');
	var M2mt = M2.attr('type');
	var M2vis = (M2.get(0).style.height != '1px');
	var cMmt = cM.attr('type');
	var M2anim = (M1mt == 'm' && M2mt != 'm');

	M1.css({'opacity': '1'}).children('.padding').children('.shadow').hide();
	M2.css({'opacity': '1'}).children('.padding').children('.shadow').hide();
	if (M2vis || M2anim) {
		var l2 = 0;
		M2.css({'height': 'auto'});
	}
	else {
		if (M2mt == 'm')
			var l2 = 16;
		else
			var l2 = 32;
		M2.css({'left': '-' + l2 + '%', 'height': 'auto'});
	}
	if (M1mt == 'm')
		var l1 = 16;
	else
		var l1 = 32;
	var lt = l1 + l2;
	M1.css({'left': '-' + lt + '%', 'height': 'auto'});
	if (M2anim)
		M2.animate({'left': '+=16%', 'margin-right': '+=16%'}, 500, function() {
			M2.hide();
		});
	M1.animate({'left': '+=' + lt + '%', 'margin-right': '+=' + lt + '%'}, 600, function() {
		M1.css({'left': '0', 'margin-right': '0'});
		if (M2anim)
			M2.css({'left': '16%', 'margin-right': '0'});
		else
			M2.css({'left': '0', 'margin-right': '0'});
		if (M1mt == 'm') {
			M2.addClass('center').removeClass('left');
			$('.listmenu:gt(' + (clicked+1) + ')').remove();
		} else {
			M1.addClass('center').removeClass('left');
			$('.listmenu:gt(' + clicked + ')').remove();
		}
		// -------------  Дополнительные параметры ------------
		if (topMenuItem) {
			Left1Change(topMenuItem);
			if (t2mId)
				lli = $('#li'+t2mId);
			else
				lli = $("#cm" + $("a", topMenuItem).attr('id').replace('t1m', '')).children(".select");
			if (clicked == 0 && lli.attr('type') == 'm')
				aboutShow();
			leftlistitemClick(lli);
			if (html)
				AddAndAnimateAfterAjax(html);
		} else if (searchfl)
			Search()
		else {
			initialize();
			update_breadcrumbs();
			AnimationStop();
			if (clicked == 0)
				aboutShow();
		}
	});
}

function breadcrumbs_click(elem)
{
	$('div.lastmenu > div.h1:eq(' + $(elem).index() + ')').click();
}

function update_breadcrumbs()
{
	if ($('div.lastmenu').length) {
		$('#breadcrumbs').empty();
		$('div.lastmenu > div.h1').each(function() {
			$('#breadcrumbs').append('<a href="javascript:void(0)" onclick="breadcrumbs_click(this);">'+$(this).find('div.article').text()+'</a> / ');
		});
		ll = $('.MoreThanMax > .left:last');
		id = ll.attr('id').slice(2);
		if ($('div.lastmenu > div.h1#h_' + id).length)
			first_title = $('.center').attr('name');
		else
			first_title = ll.attr('name');
		$('#breadcrumbs').append('<span>'+ first_title +'</span>');
	}
}

function SearchInit()
{
	$(".sr").unbind('click');
	$(".sr").click(function() {
		if (!animation) {
			var ibc = $(this).attr('ibc');
			var id = $(this).attr('id');
			if (ibc.substr(0, 10) == 'management')
				location.href = 'management/index.php#empl' + id.slice(3);
			else
				SetHash($(this).attr('href'));
		}
	});

	$('.textfield').focus(function() {
		if ($(this).val() == 'Введите строку поиска' || $(this).val() == 'Enter search string')
			$(this).val('');
	});

	$('.textfield').keyup(function() {
		var v = $(this).val();
		if (!$(this).parent().is('.search_input'))
			$('.search_input > .textfield').val(v);
		else
			$('#search > form > div > .textfield').val(v);
	});

	$("form#quest, form#quest1").submit(function() {
		if (!animation && $('.textfield').val()) {
			var lastmenuFirstItem = $(".lastmenu .h1:eq(0)");
			var cMmt = $('.center').attr('type');
			if ($(lastmenuFirstItem).length && cMmt != 's')
				lastmenuClick(lastmenuFirstItem, false, false, true);
			else
				Search();
			//alert(1);
		}
		return false;
	});

	$("#search_pages .prev").click(function(){
		var ul = $("#search_pages .items ul");
		var sli = ul.children('.selected');
		var selectedIndex = sli.index();
		if (selectedIndex > 0)
		{
			selectedIndex--;
			sli.removeClass('selected');
			ul.children("li:eq("+selectedIndex+")").addClass('selected');
			SearchPage(selectedIndex);
		}
	});

	$("#search_pages .next").click(function(){
		var ul = $("#search_pages .items ul");
		var cnt = ul.children('li').length;
		var sli = ul.children('.selected');
		var selectedIndex = sli.index();
		if (selectedIndex < cnt - 1)
		{
			selectedIndex++;
			sli.removeClass('selected');
			ul.children("li:eq("+selectedIndex+")").addClass('selected');
			SearchPage(selectedIndex);
		}
	});

	$("#search_pages .items > ul > li").click(function(){
		if (!$(this).hasClass('selected')) {
			$(this).siblings('li').removeClass('selected');
			$(this).addClass('selected');
			SearchPage($(this).index());
		}
	});
}

function SearchPage(si)
{
	$("#search_results > div").hide();
	$("#search_results > div:eq("+si+")").show();
}

/*function escapeEx(str)
{
	var ret = '';
	for (i = 0; i < str.length; i++) {
		var n = str.charCodeAt(i);
		if (n >= 0x410 && n <= 0x44F)
			n -= 0x350;
		else if (n == 0x451)
			n = 0xB8;
		else if (n == 0x401)
			n = 0xA8;
		if ((n < 65 || n > 90) && (n < 97 || n > 122) && n < 256) {
			if (n < 16)
				ret += '%0' + n.toString(16);
			else ret += '%' + n.toString(16);
		}
		else
			ret += String.fromCharCode(n);
	}
	return ret;
}*/

function Search()
{
	AnimationStart();
	str = $('.textfield').val();
	datastr = "str=" + str;
	s1 = $('#quest #sdate').val();
	if (!s1)
		s1 = sdate;
	s2 = $('#quest #sblock').val();
	if (!s2)
		s2 = sblock;
	location.href = '#srch' + s1 + s2 + '_' + str;
	datastr = datastr + '&date=' + s1 + '&block=' + s2;
	sblock = '0';
	sdate = '0';
	$.ajax({
		url: "search.php",
		type: "POST",
		data: datastr,
		dataType:'html',
		success : function(html){
			MV = GetMenuVars();
			if (MV.cMmt == 's') {
				MV.cM.remove();
				$(".MoreThanMax").append(html);
				$('#quest #sdate').val(s1);
				$('#quest #sblock').val(s2);
				$(".center").css({"z-index": "2", "width": "32%"});
				AnimationStop();
			}
			else {
				MV._mt = 's';
				MV.cM.addClass('left').removeClass('center');
				$(".MoreThanMax").append(html);
				$('#quest #sdate').val(s1);
				$('#quest #sblock').val(s2);
				if (!MV.cMvis) {
					MV.lM.css({'margin-right': '16%'});
					MV.cM.css({'left': '0', 'margin-right': '-32%', 'height': '1px'});
				}
				MV.nMinit();
				dTop = 0;
				AddToLast(MV.lMid, MV.lMname, MV.lMcode);
				animateAfterCIC(MV);
			}
			SearchInit();
		},
		error: function() {
			AnimationStop();
		}
	});
}

function top2Init()
{
	$("#awardsTop a").click(function() {
		return !animation;
	});

	$(".message_list a").click(function() {
		return !animation;
	});

	$(".bottom_column a").click(function() {
		return !animation;
	});

}

function aboutInit()
{
	$("#about_caption").click(function()
	{
		$("#about_caption").fadeOut(1000, function()
		{
			$("#about_full").animate({opacity: ".45", left: "+=800"}, 500);
			$("#about_full").animate({opacity: ".90"}, 500);
		});
		afShow = 1;
		return false;
	});

	$('body').click(function(e) {
		t = e.target||e.srcElement;

		if ( ($(t).parents(".topmenu").length == 0) &&
					($(t).parents(".links").length == 0) &&
					(t.className != "topmenu") &&
					(t.className != "links") )
			$(".links").hide();
		if (afShow == 1) {
			var af = $("#about_full");
			var afc = $("#about_full a").get(0);
			if ( t == afc || (t != af.get(0) && !af.has(t).length) ) {
				afShow = 0;
				$("#about_full").animate({opacity:0, left: "-=800"}, 250, function() {
					//$("#about_caption").css({opacity:0}).show().animate({opacity:1}, 250);
					$("#about_caption").fadeIn(250);
				});
			}
		}
	});

}

function aboutHide()
{
	if (afShow == 1) {
		afShow = -1;
		$("#about_full").animate({opacity:0, left: "-=800"}, 250);
	}
	else if (afShow == 0) {
		afShow = -1;
		$("#about_caption").fadeOut(1000);
	}
}

function aboutShow()
{
	if (afShow == -1) {
		afShow = 0;
		$("#about_caption").fadeIn(500);
	}
}

var accordAnimate = 0;
function accordInit()
{
	$(".accorditem").each(function() {
		if ($(this).attr('code') == 'a') {
			$(this).children("h1").addClass("active").children('.award_arr').addClass("close").removeClass("open");
			$(this).children(".year").show();
			accordHideAllCaption();
		}
		else {
			h1 = $(this).children("h1:first");
			h1.addClass("active").children('.award_arr').addClass("close").removeClass("open");
			h1.siblings("h1").children('.award_arr').addClass("open").removeClass("close");
			$(this).children(".year").hide();
			$(this).children(".year:first").show();
			accordShowAllCaption();
		}
	});

	$(".accordTop span").click(function() {
		if (accordAnimate == 0) {
			accordAnimate = 1;
			ai = $(this).parent().siblings('.accorditem');
			if ($(this).attr('id') == 'acShowAll') {
				dy = ai.children('div.year:not(:visible)');
				dy.each(function() {
					h = $(this).css({'height': 'auto'}).height();
					$(this).css({'height': '0'}).show();
					$(this).animate({'height': h + 'px'}, 500, function() {
						if (accordAnimate == 1) {
							accordAnimate = 0;
							accordHideAllCaption();
							ai.children('h1').addClass("active");
							ai.children('h1').children('.award_arr').addClass("close").removeClass("open");
						}
					});
				});
			}
			else {
				dy = ai.children('div.year:visible');
				dy.animate({'height': '0'}, 500, function() {
					dy.hide();
					accordAnimate = 0;
					accordShowAllCaption();
					ai.children('h1').removeClass("active");
					ai.children('h1').children('.award_arr').addClass("open").removeClass("close");
				});
			}
		}
	});

	$(".accorditem h1").click(function(){
		if (accordAnimate == 0) {
			var h1 = $(this);
			if ( !(h1.hasClass("active")) ) {
				accordAnimate = 1;
                var newItem = h1.next("div.year");
				var visItems = newItem.siblings("div.year:visible");
                vIl = visItems.length;
                h = newItem.css({'height': 'auto'}).height() + 'px';

				var b = getBrowserInfo();
				//================= для IE7 анимация поочередная ===================
				if (b.type == 'IE' && b.version == 7) {

						function step2() {
							newItem.css({'height': '0'}).show();
							newItem.animate({'height': h}, 500, function() {
								accordAnimate = 0;
							});
						}

					if (vIl)
						visItems.animate({'height': '0'}, 500, function() {
							visItems.hide();
							step2();
						});
					else {
						step2();
					}
				}
				//================= для остальных анимация одновременная =========
                else {
					if (vIl) {
						visItems.animate({'height': '0'}, 500);
						newItem.css({'height': '0'}).show().animate2({'height': visItems.height() + 'px'}, 500, function() {
							if (vIl) {
								newItem.animate({'height': h}, 500, function() {
									accordAnimate = 0;
								});
								visItems.hide();
							}
							else
								accordAnimate = 0;
						});
					}
					else {
						newItem.css({'height': '0'}).show().animate({'height': h}, 500, function() {
							if (vIl) {
								newItem.animate({'height': h}, 500, function() {
									accordAnimate = 0;
								});
								visItems.hide();
							}
							else
								accordAnimate = 0;
						});
					}
				}
				//================================================================
				h1.addClass("active");
				h1.siblings("h1").removeClass("active");
				h1.children('.award_arr').addClass("close").removeClass("open");
				h1.siblings("h1").children('.award_arr').addClass("open").removeClass("close");
			}
			else if ($('.accordTop span').attr('id') == 'acHideAll') {
				accordAnimate = 1;
				thisItem = h1.next("div.year");
				otherItems = thisItem.siblings("div.year");
				otherItems.animate({'height': '0'}, 500, function() {
					otherItems.hide();
					accordAnimate = 0;
					accordShowAllCaption();
					h1.siblings("h1").removeClass("active");
					h1.siblings("h1").children('.award_arr').addClass("open").removeClass("close");
				});
			}
			else {
				accordAnimate = 1;
				thisItem = h1.next("div.year");
				thisItem.animate({'height': '0'}, 500, function() {
					thisItem.hide();
					accordAnimate = 0;
				});
				h1.removeClass("active");
				h1.children('.award_arr').addClass("open").removeClass("close");
			}
		}
	});
	
	$(".awards_wrap_show span").click(function() {
		$(this).parent().parent().css('height', 'auto');
		$(this).parent().hide().siblings('.awards_wrap').show();
	});
}

function accordShowAllCaption()
{
	if (ruLang)
		$('.accordTop span').text('показать все');
	else
		$('.accordTop span').text('show all');
	$('.accordTop span').attr('id', 'acShowAll');
}

function accordHideAllCaption()
{
	if (ruLang)
		$('.accordTop span').text('скрыть все');
	else
		$('.accordTop span').text('hide all');
	$('.accordTop span').attr('id', 'acHideAll');
}

function topmenuInit()
{

	$('.topmenu li').click(function () {
		if ($(this).children('a').attr('id') != 'mm') {
			if (!animation) {
				if ($(this).children('a').attr('code') == 'groupAbout') {
					window.open('http://www.vtb.ru/we/today/');
					return false;
				};
				if ($(this).children('a').attr('code') == 'groupAbout_en') {
					window.open('http://www.vtb.com/we/today/');
					return false;
				};
				if ($(this).children('a').attr('code') == 'russia_calling') {
					window.open('/russia_calling/');
					return false;
				};
				if ($(this).children('a').attr('code') == 'russia_calling_en') {
					window.open('/russia_calling/');
					return false;
				};

				SetHash($(this).children('a').attr('href'));
				$(".links").show();
			}
		}
		else
			$(".links").show();
		return false;
	});

	$(".top2menu").click(function(){
		$(".links").hide();
		return !animation;
	});
}

//============================== Страница пользователей ============================
function managementInit()
{
	$(".profile_nav > ul > li").click(function(){
		thisId = $(this).attr('id').slice(2);
		location.href = '#empl' + thisId;
		pvId = $(".profile_view").attr('id').slice(2);
		if (thisId == pvId)
			Show_profile_view();
		else
	        $.ajax({
				url: "EmplDetails.php",
				type: "GET",
				data: "id=" + thisId,
				dataType: 'html',
				success : function(html){
					$(".profile_view > div").remove();
					$(".profile_view").append(html);
					$(".profile_view").attr('id', 'pv' + thisId);
					Show_profile_view();
					initialize();
				}
			});
		return false;
	});

	$(".profile_view > h2").click(function(){
		location.href = '#' + pageHref;
		Show_profile_nav();
		return false;
	});

	$(".profile_pagination .prev").click(function(){
		var ul = $(".profile_pagination .items ul");
		var sli = ul.children('.selected');
		var selectedIndex = sli.index();
		if (selectedIndex > 0)
		{
			selectedIndex--;
			sli.removeClass('selected');
			ul.children("li:eq("+selectedIndex+")").addClass('selected');
			ShowPage(selectedIndex);
		}
		return false;
	});

	$(".profile_pagination .next").click(function(){
		var ul = $(".profile_pagination .items ul");
		var cnt = ul.children('li').length;
		var sli = ul.children('.selected');
		var selectedIndex = sli.index();
		if (selectedIndex < cnt - 1)
		{
			selectedIndex++;
			sli.removeClass('selected');
			ul.children("li:eq("+selectedIndex+")").addClass('selected');
			ShowPage(selectedIndex);
		}
		return false;
	});

	$(".profile_pagination .items > ul > li").click(function(){
		if (!$(this).hasClass('selected')) {
			$(this).siblings('li').removeClass('selected');
			$(this).addClass('selected');
			ShowPage($(this).index());
		}
		return false;
	});
}

function Show_profile_view()
{
	$(".profile_nav > h2").css({'top': '0'});
	$(".profile_nav > h2").animate({'top': '-39px'}, 400, function() {
		$(".profile_nav").hide();
		$(".profile_view > h2").css({'top': '-39px', zIndex: 0});
		$(".profile_view").show();
	});
	var url = $(".profile_view input").attr('value');
	var ib = $("img#back:first");
	var ibd = $("img#back_default:first");
	ib.attr("src", url);
	if (ib.attr('src') != ibd.attr('src'))
		ibd.animate({opacity:0}, 1000, function() {
			ibd.hide();
			ib.show();
			if (ib.attr('src') == "")
				ib.css({opacity:"1"});
			else
				ib.animate({opacity:"1"}, 1000);
		});
}

function Show_profile_nav()
{
	$(".profile_view > h2").css({'top': '-=39px', zIndex: "-1"});
	$(".profile_view > h2").animate({'top': '0'}, 400, function() {
		$(".profile_view").hide();
		$(".profile_nav > h2").css({'top': '0'});
		$(".profile_nav").show();
	});
	var ib = $("img#back:first");
	var ibd = $("img#back_default:first");
	if (ib.attr('src') != ibd.attr('src'))
		/*if (ib.attr('src') == "") {
			ib.hide();
			ibd.fadeIn();
		} else
			ib.fadeOut(function() {
				ibd.fadeOut();
			});*/
		if (ib.attr('src') == "") {
			ib.css({opacity:0});
			ib.hide();
			ibd.show();
			ibd.animate({opacity:"1"}, 1000);
		} else
			ib.animate({opacity:0}, 1000, function() {
				ib.hide();
				ibd.show();
				ibd.animate({opacity:"1"}, 1000);
			});
}

function ShowPage(si)
{
	pageHref = 'page' + si;
	location.href = '#' + pageHref;
	$(".profile_nav > ul").hide();
	$(".profile_nav > ul:eq("+si+")").show();
}
// ================================== Навигация ================================
function topMenuClick(sender)
{
	//alert(1);
	AnimationStart();
	var lastmenuFirstItem = $(".lastmenu .h1:eq(0)");
	var pId = $(sender).children('a').attr('id').substr(3);
	if ($(lastmenuFirstItem).length) {
		lastmenuClick(lastmenuFirstItem, sender);
	} else {
		Left1Change(sender);
		leftlistitemClick( $("#cm" + pId).children(".select") ); //клик на выбранном пункте левого меню
	}
}

function top2menuClick(sender, html)
{
	//alert(2);
	var t2mId = $(sender).attr('id').slice(2);
	var centId = $(".center").attr('id');
	if (centId)
		centId = centId.slice(2);
	var l = parseInt($(".center").css('left'));
	var vis = (l == '0');
	if ( t2mId != centId || !vis) {
		AnimationStart();
		var li_index = $(sender).parent().parent().parent().index();
		var topMenuItem = $("ul.topmenu li:eq("+li_index+")");
		var lastmenuFirstItem = $(".lastmenu .h1:eq(0)");
		if ($(lastmenuFirstItem).length) {
			lastmenuClick(lastmenuFirstItem, topMenuItem, t2mId, false, html);
		} else {
			Left1Change(topMenuItem);
			leftlistitemClick($('#li'+t2mId));
			if (html)
				AddAndAnimateAfterAjax(html);
		}
	} else
		if (html)
			AddAndAnimateAfterAjax(html);
}

function Left1Change(sender)
{
	$(sender).addClass('current').siblings().removeClass('current');
	var pId = $("a", sender).attr('id').substr(3);
	var code = $("a", sender).attr('code');
	var contmenu = $("#cm" + pId);
	$('.left:first').attr('id', 'lm' + pId);
	$('.left:first').attr('name', $(sender).text());
	$('.left:first').attr('code', code);
	$(".menucaption h1").text( $(sender).text() );
	contmenu.siblings(".content_menu").hide();
	contmenu.show();
}

function Navigate(hsh)
{
	//alert('nav: ' + hsh);
	if (hsh == '#' || !hsh)
		return;
	if (hsh.substr(1, 4) == 'srch') {
		$('.textfield').attr('value', hsh.substr(8));
		sdate = hsh.substr(5, 1);
		sblock = hsh.substr(6, 1);
		$("form#quest").submit();
	}
	else {
		var tm = $(".topmenu a[href='" + hsh + "']");
		var t2m = $(".top2menu[href='" + hsh + "']");
		if (tm.length)
			topMenuClick(tm.parent());
		else if (t2m.length)
			top2menuClick(t2m);
		else {
			var last = $(".lastmenu .h1[href='" + hsh + "']");
			if (last.length)
				lastmenuClick(last);
			else {
				var shref = "[href='" + hsh + "']";
				var item = $(".leftlistitem" + shref + ", .new" + shref);
				if (item.length) {
					var cm = item.parent();
					var lm = cm.parent().parent();
					if (cm.is('.year') && cm.parent().parent().parent().parent().css('margin-right') >= 0)
						centeritemClick(item);
					else if (cm.css('display') != 'none' && lm.is('.left') && lm.css('opacity') != 0)
						leftlistitemClick(item);
					else if (cm.css('display') != 'none' && lm.is('.center') && lm.css('opacity') != 0)
						centeritemClick(item);
					else
						NavigateAjax(hsh);
				}
				else
					NavigateAjax(hsh);
			}
		}
	}
}

function NavigateAjax(hsh)
{
	//alert('ajax');
	var lang = '';
	if (!ruLang)
		lang = '&lang=en';
	//alert("hsh=" + hsh.slice(1) + lang);
	$.ajax({
		url: "hashNavigate.php",
		type: "GET",
		data: "hsh=" + hsh.slice(1) + lang,
		dataType: 'html',
		success: function(html){
			var p = html.indexOf('|');
			if (p > 0) {
				p2Id = html.substr(0, p);
				var t2m = $(".top2menu#tm" + p2Id);
				if (t2m.length == 0)
					var t2m = $(".top2menu[href='#" + p2Id + "']");
				if (t2m.length) {	
					lasthash = hsh.slice(1);		
					top2menuClick(t2m, html.substr(p + 1));
				}
				else
					AnimationStop();
			}
			else
				AnimationStop();
		},
		error: function(){
			AnimationStop();
		}
	});
}

function AddAndAnimateAfterAjax(html) {
	if (html) {
		MV = GetMenuVars();
		MV.cM.addClass('left').removeClass('center');
		var iLast = $('.listmenu').length - 1;
		$(".MoreThanMax").append(html);
		if ($('input#remurl').length)
			window.open($('input#remurl').val());
		accordInit();
		dTop = 0;
		var cnt = $('.listmenu').length - 1;
		var pmf = true;
		$('.listmenu').each(function(index) {
			var mt = $(this).attr('type');
			var id = $(this).attr('id').slice(2);
			var nm = $(this).attr('name');
			var cd = $(this).attr('code');
			if (index > iLast) {
				//$('.listmenu:eq(' + index + ')').children().children('.content_menu');
				if (mt != 'm')
					$(this).css({'width': '32%'});
				if (index < cnt) {
					$(this).addClass('left');
					if (mt != 'm')
						$(this).css({'margin-right': '-32%', 'height': '1px', 'z-index': '1'});
					else
						$(this).css({'margin-right': '-16%', 'height': '1px', 'z-index': '1'});
				}
				else
					$(this).addClass('center');
			}
			var lmc = $('.lastmenu').children().length;
			if (index != cnt)
				if ( !(index == 0 && lmc > 0) )
					if ( !(index == cnt - 1 && $('.listmenu:last').attr('type') == 'm') )
						AddToLast(id, nm, cd);
		});
		MV.nMinit();
		pdf = false;
		if (lasthash.substr(0, 8) == 'reports_') {
			a = $('.center .anal[href="#' + lasthash + '"] a');
			if (a.length)
				pdf = a.attr('href');
		}
		animateAfterCIC(MV, pdf);
	}
	else
		AnimationStop();
}

