/* Copyright (c) 2008 Astronim* (astronim.com) */

var currentOpened = '2';

$(document).ready(function(){
	
	// ifixpng для всех картинок с класом png
	if ( $('#content').length ) {
		$.ifixpng( blankSrc );
		$('#content img').ifixpng(); 
	}

	initMenuVars();		
	
	menuvOtstypy(); /////////////////////

	// расскраска таблицы
	//$("#content table.tbl tr > td:last-child").addClass("last");
	$("#content tr:nth-child(even)").addClass("chet");

	$('#menuFirst').hover(
		function(){
			$(this).addClass( "hover" );
		},
		function(){
			$(this).removeClass( "hover" );
			closeAllmenuFirst();
		}
	);

	$('#menuSecond').hover(
		function(){
			$(this).addClass( "hover" );
		},
		function(){
			$(this).removeClass( "hover" );
			closeAllmenuSecond();
		}
	);


	$('#menuFirst .item').hover(
		function(){
			var my_height = $('ul', this).height()+25;
			var index = $('#menuFirst .item').index(this);
			closeAllmenuFirst();
			$('#menuFirst .item').each(function(){
				if (!$(this).is('.hover')) {
					$(this).removeClass( "hover" );
					var my_height = $('ul', this).height();
						
					$('#menuFirst .inner table').css('height', menuFirstHeight); 
					$('#menuFirst').css('margin-bottom', 0); 

					//$(this).css('display', 'inline'); 

					$('ul', this).hide(); 
				}
			});

			if ($('ul', this).length){
				// // alert(my_height);
				$('#menuFirst .inner table').css('height', menuFirstHeight+my_height); 
				$('#menuFirst').css('margin-bottom', -my_height);

				if (index > menuFirstLengthHalf){ 
					$('ul', this).css('left', $(this).width()-$('ul', this).width()-32);
					$('ul', this).css('text-align', 'right');
					
				} else {
					$('ul', this).css('left', 0);
				}
				
			//	$(this).css('height', menuFirstHeight); 
				
				$(this).addClass( "hover" );
				$('ul', this).show(); 
			}
		},
		function(){


			if (!$('#menuFirst').is('.hover')) {

				$(this).removeClass( "hover" );

				var my_height = $('ul', this).height();
						
				$('#menuFirst .inner table').css('height', menuFirstHeight); 
				$('#menuFirst').css('margin-bottom', 0); 
				
				$('ul', this).hide(); 

			//	$(this).css('height', 'auto'); 

			}
		}
	);


	
	
	$('#menuSecond .item').hover(
		function(){
			
			var my_height = $('ul', this).height()+25;
			var index = $('#menuSecond .item').index(this);
			closeAllmenuSecond();

			if ($('ul', this).length){
				$('#menuSecond .inner table').css('height', menuSecondHeight+my_height); 
				$('#menuSecond').css('margin-bottom', -my_height);


				if (index > menuSecondLengthHalf){ 
					$('ul', this).css('left', $(this).width()-$('ul', this).width()-32);
					$('ul', this).css('text-align', 'right');
				} else {
					$('ul', this).css('left', 0);
				}
				
			//	$(this).css('height', menuSecondHeight); 
				
				$(this).addClass( "hover" );
				$('ul', this).show(); 

				$('#menuSecond table').addClass( "hover" );

			}
		},
		function(){

			if (!$('#menuSecond').is('.hover')) {
				var my_height = $('ul', this).height();
						
				$('#menuSecond .inner table').css('height', menuSecondHeight); 
				$('#menuSecond').css('margin-bottom', 0); 

				$(this).removeClass( "hover" );
				$('ul', this).hide(); 

				$('#menuSecond table').removeClass( "hover" );
			};
		}
	);
	
	$('#sloganFirst a').not(".iBank").click(
		function(){
			if ( currentOpened == 0) {
				return false;
			}
			OpenFirstMenuAnimated();
			currentOpened = 0;
			return false;
	});
	
	$('#sloganSecond a').not(".iBank").click(
		function(){
			if ( currentOpened == 1) {
				return false;
			}
			OpenSecondMenuAnimated();
			currentOpened = 1;
			return false;
	});

	$('#poll .inner1 .header').click(
		function(){
			if (!$('#poll .inner2').is(':visible')){
				$('#poll').addClass( 'opened' );
			} else {
				$('#poll').removeClass( 'opened' );
			}
			if ($('#search .inner2').is(':visible')){
				$('#search').removeClass( 'opened' );
			}
		return false;
	});
	
	$('#search .inner1 .header').click(
		function(){
			if (!$('#search .inner2').is(':visible')){
				$('#search').addClass( 'opened' );
			} else {
				$('#search').removeClass( 'opened' );
			}
			
			if ($('#poll .inner2').is(':visible')){
				$('#poll').removeClass( 'opened' );
			}
			
		return false;
	});
						   
	if ($('#menuFirst').is('.open')) {
		OpenFirstMenu();
	}	
});

/*
$(window).resize(function(){
	initMenuVars();		
});
*/

function closeAllmenuFirst(){

	$('#menuFirst .item').each(function(){
		if ($(this).is('.hover')) {

			$(this).removeClass( "hover" );
			var my_height = $('ul', this).height();
				
			$('#menuFirst .inner table').css('height', menuFirstHeight); 
			$('#menuFirst').css('margin-bottom', 0);
			//$(this).css('display', 'inline'); 

			$('ul', this).hide();
		}
	});	
};
function closeAllmenuSecond(){
	
	$('#menuSecond .item').each(function(){
		if ($(this).is('.hover')) {

			$(this).removeClass( "hover" );
			var my_height = $('ul', this).height();
				
			$('#menuSecond .inner table').css('height', menuFirstHeight); 
			$('#menuSecond').css('margin-bottom', 0);
			//$(this).css('display', 'inline'); 

			$('ul', this).hide();
			$('#menuSecond table').removeClass( "hover" );
		}
	});	
};



function initMenuVars(){
	menuFirstHeight = $('#menuFirst').height(); // запоминаем высоту менюшки    !обновлять после ресайза окна
	menuSecondHeight = $('#menuSecond').height(); // запоминаем высоту менюшки    !обновлять после ресайза окна
	menuFirstLengthHalf = parseInt($('#menuFirst td').length/2); // вычислять  кол-во пунктов в меню, делить на 2 и округлять
	menuSecondLengthHalf = parseInt($('#menuSecond td').length/2); // вычислять  кол-во пунктов в меню, делить на 2 и округлять
	
	$('#menuFirst .item ul').css('top', menuFirstHeight-35); // позиционируем подменюшки.    !обновлять после ресайза окна
	$('#menuSecond .item ul').css('top', menuFirstHeight-35); // позиционируем подменюшки.    !обновлять после ресайза окна

	$("#menuFirst .item>a").each(function(){
		$(this).css('width', $(this).width());
	});
	$("#menuSecond .item>a").each(function(){
		$(this).css('width', $(this).width());
	});
}

function menuvOtstypy(){

	if ($("#menuFirst table").width != 0){

		var widthT = $("#menuFirst table").width(); // ширина таблицы
		var widthA = 0; 
		var count = $("#menuFirst .item>a").length;// кол-во ячеек

		$("#menuFirst .item>a").each(function(){  // ширина всех ссылок вместе взятых
			widthA += $(this).width();
		});

		var paddingTd = (widthT-widthA)/count;  // отступ, одинаковый lzk всех ссылок
		
		$("#menuFirst td").each(function(){  // устанавливаем ширину ячейки равнйо  ширине ссылки+ отступ
			cc = $('.item>a', this).width();
			$(this).width( $('.item>a', this).width()+paddingTd);
			$('.item>a', this).width(cc);
		});
	}
		
	if ($("#menuSecond table").width != 0){
		
		var widthT = $("#menuSecond table").width();
		var widthA = 0;
		var count = $("#menuSecond .item>a").length;

		$("#menuSecond .item>a").each(function(){
			widthA += $(this).width();
		});

		var paddingTd = (widthT-widthA)/count;

		$("#menuSecond td").each(function(){
			cc = $('.item>a', this).width();
			$(this).width( $('.item>a', this).width()+paddingTd);
			$('.item>a', this).width(cc);
		});
	}
	
}

function OpenFirstMenuAnimated (  ) {

	if (!$('#menuFirst').is(':visible')){
			
		$('#sloganFirst').animate({left :0}, 500, 'swing');
		$('#sloganSecond').animate({right :-60}, 250, 'swing');
		
		$('#sloganFirst').addClass( "current" );
		$('#sloganSecond').removeClass( "current" );
		
		$('#menuFirst').show();
		$('#menuSecond').hide();
		
		$('#visCenter').hide();
	//	menuvOtstypy();

	} else {
		$('#sloganFirst').animate({left :-260}, 500, 'swing');
		$('#sloganFirst').removeClass( "current" );
		$('#menuFirst').hide();
		
		$('#visCenter').show();
	}
}

function OpenFirstMenu (  ) {

	currentOpened = 0;

	$('#sloganFirst').animate({left :0}, 0);
	$('#sloganFirst').addClass( "current" );
	$('#sloganSecond').removeClass( "current" );
	
	$('#menuFirst').show();
	$('#menuSecond').hide();
	
	$('#visCenter').hide();
//	menuvOtstypy();
}

function OpenSecondMenuAnimated (  ) {

	if (!$('#menuSecond').is(':visible')){

		$('#sloganFirst').animate({left :-260}, 500, 'swing');
		$('#sloganSecond').animate({right :0}, 250, 'swing');
		
		$('#sloganFirst').removeClass( "current" );
		$('#sloganSecond').addClass( "current" );
		
		$('#menuFirst').hide();
		$('#menuSecond').show();
		
		$('#visCenter').hide();
	//	menuvOtstypy();
	} else {
		$('#sloganSecond').animate({right :-60}, 250, 'swing');
		$('#sloganSecond').removeClass( "current" );
		$('#menuSecond').hide();
		$('#visCenter').show();
		
	}
}

function OpenSecondMenu (  ) {

	currentOpened = 1;

	$('#sloganSecond').animate({right :0}, 0);
	$('#sloganFirst').removeClass( "current" );
	$('#sloganSecond').addClass( "current" );
	
	$('#menuFirst').hide();
	$('#menuSecond').show();
	
	$('#visCenter').hide();
//	menuvOtstypy();
}

function wo(link,ww,hh,title) {
	w=window.open(link,'',(ww ? 'width='+ww+',' : '')+(hh ? 'height='+hh+',' : '')+'toolbar=0,scrollbars=0,resizable=yes');
	if (link.indexOf('.html')==-1) {
		if (document.layers) title='';
		w.document.open();
		w.document.write('<html><head><title>'+title+'</title><meta http-equiv=Content-Type content="text/html; charset=windows-1251"></head>');
		w.document.write('<body bgcolor=white marginwidth=0 marginheight=0 topmargin=0 leftmargin=0><table width=100% height=100% border=0 cellspacing=0 cellpadding=0><tr><td align=center><img src='+link+' width='+ww+' height='+hh+' border=0 alt=""></td></tr></table></body></html>');
		w.document.close();
	}
	w.focus();
}

function trim( s ) {
	return s.replace(/\s/g, '');
}

function ConvertCourse( from, to, sum, date ) {

	$.post(_root+"include/convertcourse.php", { convertFrom: from, convertTo: to, convertSum: sum, date_course: date }, function(data){
		$('#resultSum').html( data );
		$('#resultBlock').css("visibility","visible");
	});

}

function FormOnlineCheck() {

	//$('form[name=form3]').submit(function() {
	var _err = '';
	if ( $("input[name=age]").val() < 21 ){
		_err = 'Ваш возраст должен быть не менее 21 года';

	}

	if ( $("input[name=work_years]").val() < 2 ){
		
		if ( _err != '' ) {
			_err = _err + '\n' + 'Ваш общий трудовой стаж должен быть не менее 2 лет';
		}
		else {
			_err = 'Ваш общий трудовой стаж должен быть не менее 2 лет';
		}

	}

	if ( _err != '' ) {
		alert( 'К сожалению Вы не можете претендовать на получение кредита.\n\n' + _err );
		return false;
	}
	else {
	
		return true;
	
	}

}
