var myTInt;
var uid;


addMask = function(){
	var mask = $('<div></div>');
	mask.css({'width': $(document).width(),'height':$(document).height()});  
	mask.addClass('mask');
	$('body').append(mask);
	$('.mask').show();
}

delMask = function() {
  	$('.mask').remove();
	
	$('#authForm').animate({opacity:'hide'},'fast');
	$('#loader').css('display','none');
}
	
authForm = function(){
	$('#authForm').css({ left: ($(window).width()- $('#authForm').width())/2 } );
	$('#authForm').css({ top:  ( $('body').scrollTop() + ($(window).height() - $('#authForm').height())/2)-40 } );
	$('#authForm').animate({opacity:'show'},'fast');
}

showErr = function(msg) {
	$('#authErr').html(msg);
	clearTimeout(myTInt);
	myInt = setTimeout( hideErr, 2000);
}

hideErr = function() {
	$('#authErr').html('');
}


$(function() {


	$('#myauth').click(function() {
		if(uid) return true;
		
		addMask();
		authForm() ;
		
		return false;
	});

	
	$('#closeAuth').click(function() {
		delMask();
	});
	
	
	$('.authin').click(function() {
		hideErr();
	});
	
	
	$('#do_auth_btn').click(function() {
	
		var ulogin = jQuery.trim(  $('#ulogin').val() );
		var upwd = jQuery.trim(  $('#upwd').val() );
		
		if( ulogin =="") return showErr('Пожалуйста, укажите ваш логин!');
		if( upwd =="") return showErr('Пожалуйста, укажите ваш пароль!');
		
		$('#loader').css('display','inline');
		
		
		$.ajax({
    		url:'/doauth.php?a=doauth', type: 'POST', cache: true, timeout: 2000, dataType: "json", data: 'l='+ulogin+'&p='+upwd,

			beforeSend: function() {
                $('#do_auth_btn').attr('disabled', 'disabled');
            },
    		error: function() {
				$('#authErr').html('Ошибка авторизации!');
				$('#do_auth_btn').attr('disabled', '');
				$('#loader').css('display','none');
    		},
    		success: function(res) {
				$('#loader').css('display','none');
				$('#do_auth_btn').attr('disabled', '');
				if(res[0]>0) return showErr(res[1]);
				
				$('#authErr').html('Пожалуйста, пожождите...');
				location.href = '/journalhome';
    		}
		});

	});
	
	

	
	
	/*article rating*/
	var didVoted;
	$("#rhold  input:radio").click(function() {
		if(didVoted) return;
		$('.do_rate').attr('disabled', '');
		$('#res').html('');
	});
	
	
	$('.do_rate').click(function() 
	{
		var el = $("#rhold  input:radio:checked");
		var id = this.id;
		if(el.length <1) return $('#res').html('Пожалуйста, укажите вашу оценку статье!');
		
		
		$.ajax({
    		url:'/doauth.php?a=dorate', type: 'POST', cache: true, timeout: 2000, dataType: "json", data: 'r='+el.val()+'&id='+id,

			beforeSend: function() {
                $('.do_rate').attr('disabled', 'disabled');
				$('#rldr').css('display','inline');
				$('#res').html('Подождите...');
            },
    		error: function() {
                $('.do_rate').attr('disabled', '');
				$('#rldr').css('display','none');
				$('#res').html('Ошибка!');
    		},
    		success: function() {
				didVoted = true;
				$('#rldr').css('display','none');
				$('#res').html('Ваша оценка учтена!');

    		}
			

		});

		
		
		
		

	});
	
	

});
