var sto;

$(document).ready(function() {
	$('#loginform .slider label').each(function()
	{
		var labelColor = '#999';
		var restingPosition = '5px';

		// стилизация и позиционирование подписей
		$(this).css(
		{
			'color'		: labelColor,
		 	'position'	: 'absolute',
	 		'top'		: '5px',
			'left'		: restingPosition,
			'display'	: 'inline',
			'z-index'	: '99'
		});

		// получение введенного в поле, значения
		var inputval = $(this).next('input').val();

		// получение длины подписи, и добавление к ней 5px
		var labelwidth = $(this).width();
		var labelmove = labelwidth + 5;

		//проверяем, если поле заполнено, то сдвигаем подпись за пределы поля
		if(inputval !== '')
		{
			$(this).stop().animate({ 'left':'-'+labelmove }, 1);
		}    	

		// если значение поля пустое при фокусе, сдвигаем подпись влево
		// если оно осталось пустым, при потере фокуса, возвращаем их обратно
		$('input').focus(function()
		{
			var label = $(this).prev('label');
			var width = $(label).width();
			var adjust = width + 15;
			var value = $(this).val();

			if(value == '')
			{
				label.stop().animate({ 'left':'-'+adjust }, 'fast');
			} 
			else 
			{
				label.css({ 'left':'-'+adjust });
			}
		}).blur(function()
		{
			var label = $(this).prev('label');
			var value = $(this).val();
			if(value == '')
			{
				label.stop().animate({ 'left':restingPosition }, 'fast');
			}
		});
	});
	$('#login').trigger('focus');
	$('div.error').hide();
	$('#login_form').submit( function()
	{
		$.fancybox.showActivity();
		if (($('#login').val() == '') || ($('#password').val() == ''))
		{
			clearTimeout(sto);
			$('div.error').text('Заполните поля и нажмите "Войти"');
//			$('div.error').stop();			
			$('div.error').slideDown('fast', function ()
			{	// После того как ошибка показана, ждем немного и гасим сообщение
				sto = setTimeout(function ()
				{
//					$('div.error').stop();
					$('div.error').slideUp('slow', function ()
					{
						$('div.error').text('');
					});
				}, 3000); // 3 sec;
			});
		}
		else
		{
			$.ajax({
				'url': '/login.php',
				'type': 'POST',
				'data': 'login=' + $('#login').val() + '&password=' + $('#password').val(),
				'cache': false,
				'async': false,
				'success': function(responce)
				{
					if (responce == 'OK')
					{
						parent.location = parent.location;
					}
					else
					{	
						clearTimeout(sto);
//						alert(responce);
//						$('div.error').text(responce);
						$('div.error').text('Неверный логин или пароль');
//						$('div.error').stop();			
						$('div.error').slideDown('fast', function ()
						{	// После того как ошибка показана, ждем немного и гасим сообщение
							sto = setTimeout(function ()
							{
//							$('div.error').stop();
								if ($('div.error') != null)
								{
									$('div.error').slideUp('slow', function ()
									{
										$('div.error').text('');
									});
								}
							}, 2500); // 2,5 sec;
						});
					}
				}
			});
		}
		$.fancybox.hideActivity();
		return false;
	});
});
