window.onload = function() {
    $('#ajaxProgress').ajaxSend(function(a,b) { $(this).show()});
    $('#ajaxProgress').ajaxComplete(function(a,b) { $(this).hide();});    
    window.onbeforeunload = function() { $.event.trigger('ajaxSend') } 
    var doGuestFun = function(){
        $.post('/helpers/auth.php', {guest: 1}, function(data){
            if ($.trim(data).indexOf('pass') !== -1) {
              $('#logonScreen').css('display','none');
				window.location.href =  $('#logonScreen').attr('retPath');
               return;
            } else {
                alert('Не верный логин и/или пароль');
                var cnt = $.trim(data).match(/fail(\d)/);
                if (Number(cnt[1]) >= 3) {
                    window.location.reload();
                }
                document.getElementById('passwd').focus();
            }
        });        
    };
	$('#doGuest').click(doGuestFun);
	$()
    $('input#login').keydown(function(e) {
        if (e.keyCode == 13 && $('#login').val().length) {
            document.getElementById('passwd').focus();
        }
    })    
    $('input#passwd').keydown(function(e) {
        if (e.keyCode == 13 && $('#passwd').val().length) {
            $('#doLogon').click();
        }
    })        
    $('.close').click(function(){
        //$(this).parent().parent().parent().css('display','none');
		$('div#logonScreen').css('display', 'none');
    });
    $('.helpclose').click(function(){
        //$(this).parent().parent().parent().css('display','none');
		$('div#helpScreen').css('display', 'none');
    });	
    $('#logonScreen .help').click(function(){
        //$('#helpScreen').Grow(600);
        //$('#helpScreen').css('display','block');
			$(this).TransferTo({to: 'helpScreen',
                className: 'transferer1',
                duration: 500,
                complete: function() {
						$('#helpScreen').css('display','block');
					}
				});
    });
    $('a.login').click(function () {
        var retPath = $(this).attr('url');
        $('#logonScreen').attr('retPath',retPath);
        doGuestFun();
    });
    /*
    $('a.login').click(function(){
        var retPath = $(this).attr('url');
		var noEnter = $(this).attr('noEnter');
		if (noEnter == undefined) {
	        $('#logonScreen:visible').css('display','none');
	        $(this).blur().TransferTo({to: 'logonScreen',
	                className: 'transferer1',
	                duration: 400,
	                complete: function() {                
	                        $('#logonScreen').css('display','block').attr('retPath',retPath);
	                        document.getElementById('login').focus();                        
	                    }
	                });
			$('#logonScreen .help').click();
			return false;
		}
		return true;
    });*/
    $('#doLogon').click(function(){
		if (($('#login').val().length == 0) && ($('#passwd').val().length == 0)) {
			$('#doGuest').click();
		} else {
	        var data = {};
	        if ($('#login').val().length) {
	            data.login = $('#login').val();
	        } else {
	            $('#login').Pulsate(100,5);
	            return false;
	        }
	        if ($('#passwd').val().length) {
	            data.passwd = $('#passwd').val();
	        } else {
	            $('#passwd').Pulsate(100,5);
	            return false;
	        }
	        $.post('/helpers/auth.php', data, function(data){
	            if ($.trim(data).indexOf('pass') !== -1) {
	              window.location.href =  $('#logonScreen').attr('retPath');
	               return;
	            } else {
	                alert('Не верный логин и/или пароль');
	                var cnt = $.trim(data).match(/fail(\d)/);
	                if (Number(cnt[1]) >= 3) {
	                    window.location.reload();
	                }
	                document.getElementById('passwd').focus();
	            }
	        });
		}
    });
    if (typeof show_login != 'undefined') {
        login(0);
    }
	$('.login_guest').hover(function() {
		$(this).addClass('login_guest_hover');
	}, function() {
		$(this).removeClass('login_guest_hover')
	})
};
function login() {
$('#logonScreen:visible').css('display','none');
$('#logo').TransferTo({to: 'logonScreen',
                className: 'transferer1',
                duration: 1200,
                complete: function() {
						$('#logonScreen').css('display','block');
						if ($('#logonScreen').attr('retPath')==undefined) $('#logonScreen').attr('retPath',$('a.login:first').attr('url'));
                        document.getElementById('login').focus();
                    }
                });
}
window.onhelp = function() {
    $('#helpScreen').css('display','block');
    return false;
}
