function numberEnding(number, ending0, ending1, ending2) {
	// $ending0 - родительный падеж, множ. число (10 организаций)
	// $ending1 - именительный падеж, ед. число (1 организация)
	// $ending2 - винительный падеж, множ. число (3 организации)
	var num100 = number % 100;
	var num10 = number % 10;
	if (num100 >= 5 && num100 <= 20) {
		return ending0;
	} else if (num10 == 0) {
		return ending0;
	} else if (num10 == 1) {
		return ending1;
	} else if (num10 >= 2 && num10 <= 4) {
		return ending2;
	} else if (num10 >= 5 && num10 <= 9) {
		return ending0;
	} else {
		return ending2;
	}
}

function countRemainingChars(){
	$textarea = $(this).find('textarea');
	var max = parseInt($textarea.attr('maxlength'));
	if($textarea.val().length > max){
		$textarea.val($textarea.val().substr(0, max));
	}
	var remaining = max - $textarea.val().length;
	if (txtLang == 'ru') {
		$(this).find('.counter').html(numberEnding(remaining, 'осталось', 'остался', 'осталось') + ' <em>' + remaining + '</em> ' + numberEnding(remaining, 'символов', 'символ', 'символа'));
	}
	else if (txtLang == 'en') {
		$(this).find('.counter').html(' <em>' + remaining + '</em> characters left');
	}
}

$(document).ready(function() {
	$('#main #main-menu').mouseenter(function() {
		$('#main #main-menu .item-icon').hide();
		$('#main #main-menu .hover').show();
	});
	$('#main #main-menu').mouseleave(function() {
		$('#main #main-menu .item-icon').hide();
		$('#main #main-menu .unactive').show();
	});
	$('#main #main-menu .hover').mouseenter(function() {
		$currentLine = $(this).parent().parent().parent();
		if (!$.browser.msie) {
			$currentLine.find('.line-bg').css('backgroundColor', '#666666');
			$currentLine.find('.line-bg').css('opacity', '0');
			$currentLine.find('.line-bg').stop().animate({
				border: '1px solid #000',
				backgroundColor: '#666666',
				opacity: 1
			});
		}
		$currentLine.addClass('line-hover');
		// Change name and description
		$currentLine.find('.name').hide();
		$currentLine.find('.subheader').hide();
		$(this).parent().find('.name').show();
		$(this).parent().find('.subheader').show();
		// Change icon
		$(this).hide();
		$(this).parent().find('.active').show();
	});
	$('#main #main-menu .active').mouseleave(function() {
		$currentLine = $(this).parent().parent().parent();
		if (!$.browser.msie) {
			$currentLine.find('.line-bg').stop().animate({
				border: '1px solid transparent',
				backgroundColor: 'transparent',
				opacity: 0
			}, 500);
		}
		$currentLine.removeClass('line-hover');
		// Change name and description
		$(this).parent().find('.name').hide();
		$(this).parent().find('.subheader').hide();
		$currentLine.find('> .name').show();
		$currentLine.find('> .subheader').show();
		// Change icon
		$(this).hide();
		$(this).parent().find('.hover').show();
	});
	
	$(".photoalbum").each(function() {
		$(this).find(".carousel").jCarouselLite({
			btnNext: $(this).find(".next"),
			btnPrev: $(this).find(".prev"),
			circular: false
		});
	});

	$("#photoalbum a").lightbox();
	$.Lightbox.construct({
		"speed": 500,
		"show_linkback": true,
		"keys": {
			close:	"q",
			prev:	"z",
			next:	"x"
		},
		"opacity": 0.3,
		text: {
			image: txtImage,
			of: txtOf,
			close: txtClose,
			closeInfo: txtCloseInfo,
			download: txtDownload,
			help: {
				close: txtClose,
				interact:	""
			},
			about: {
				text: 	"",
				title:	"",
				link:	""
			}
		}
	});
	
	$(".countable").each(countRemainingChars);
	$(".countable").keyup(countRemainingChars);

	$("form.validate").validate({
		errorElement: 'div',
		errorClass: 'error',
		errorPlacement: function(error, element) {
			if ($(element).parent().find(".tipsbox_error_text").size() == 0) {
				$(element).parent().append("<table class='tipsbox' cellspacing='0' cellpadding='0' border='0'>" +
						"<tbody>" +
						"<tr>" +
							"<td class='tipsbox_top_left'></td>" +
							"<td class='tipsbox_top'></td>" +
							"<td class='tipsbox_top_right'></td>" +
						"</tr>" +
						"<tr>" +
							"<td class='tipsbox_left'></td>" +
							"<td class='tipsbox_inner'>" +
								"<div class='tipsbox_error'></div>" +
								"<p class='tipsbox_error_text'></p>" +
							"</td>" +
							"<td class='tipsbox_right'></td>" +
						"</tr>" +
						"<tr>" +
							"<td class='tipsbox_bottom_left'></td>" +
							"<td class='tipsbox_mark'></td>" +
							"<td class='tipsbox_bottom_right'></td>" +
						"</tr>" +
						"</tbody>" +
						"</table>");
			}
			error.appendTo($(element).parent().find(".tipsbox_error_text"));
			$(element).parent().find(".tipsbox").show();
		},
		highlight: function(element, errorClass, validClass) {
			$(element).addClass(errorClass).removeClass(validClass);
			$(element).parent().find(".tipsbox").show();
		},
		unhighlight: function(element, errorClass, validClass) {
			$(element).removeClass(errorClass).addClass(validClass);
			$(element).parent().find(".tipsbox").hide();
		  }
	});

});
