jQuery(document).ready(function(){

  
$("#auth_enter").fancybox({
	'scrolling'		: 'no',
	'titleShow'		: false,
	'hideOnOverlayClick' : false
  });

  
function makeAuth(){
		function showError(text){
			$("#auth_info").show().html(text).addClass("auth_error").fadeOut(1600, "swing");
		}

		var
    login=$("#auth_login").val(),
		pass=$("#auth_pass").val();
		remember=$("#auth_remember").attr("checked")?1:0;

    if(!login.length){
			showError("Логин не может быть пустым!");
		}

    else if(!pass.length){
			showError("Пароль не может быть пустым!");
		}

    else{
			$.ajax({
				type: "POST",
				url: "/login.php",
				data: "user_name="+login+"&user_password="+pass+"&auto_login="+remember,
				success: function(data){
					if(data=="WRONGLOGIN"){
						showError("Логин или пароль указаны неверно!");
						$("#auth_login").val("");
					  $("#auth_pass").val("");
					}
					else{
					  $("#auth_login").val("");
					  $("#auth_pass").val("");
            location.reload();
					}
				},
				error: function(){
					showError("Произошла ошибка, повторите попытку.");
				}
			});
		}
	}

    $("#auth_form").keypress(function(e){
  	if(e.keyCode==13)
  	  makeAuth();
  	});
  	
  	$("#auth_submit").click(function(){
  		makeAuth();
  	});

});
    
function focusOnNextSibling(elem){
var
next = elem.nextSibling;
if (next.nodeType != 1)
{
next = next.nextSibling;
while (next.nodeType != 1){next = next.nextSibling; }
}
next.focus();
}

function hideLabel(a) {
var p = a.previousSibling;
if (p.nodeType != 1){
var prev = p.previousSibling;
while (prev.nodeType != 1){
prev = prev.previousSibling; }
p = prev;
}
if (p) {
if (!p.patched){
if ("$" in window) {
$(a).blur(function() {
if (!this.value) {
p.style.top = ''; } }); } }
p.patched = true;
p.style.top = '-9999px'; } }

function cleanFields(){
var login = $em('auth_login');
var pass = $em('auth_pass');
setTimeout(function(){
if (login.value){ hideLabel(login); }
if (pass.value){ hideLabel(pass); } }, 100); }

