;(function($)
{
	$.LoginClass = function()
	{
		this.construct();
	}
	
	$.extend($.LoginClass.prototype,
	{
		construct: function()
		{
			$('#subscriber_login').unbind().click(function()
			{
				$.Login.try_login();
			});
			
			$('input[name*=subscriber_username]:first').unbind().keyup(function(e)
			{
				$.Login.check_keyboard_key(e);
			});
			
			$('input[name*=subscriber_password]:first').unbind().keyup(function(e)
			{
				$.Login.check_keyboard_key(e);
			});
		},
		
		check_keyboard_key: function(e)
		{
			var characterCode = 0;
			if( e && e.which )
			{
				e = e;
				characterCode = e.which;
			}
			
			else
			{
				e = typeof(event) != "undefined" ? event : null;
				if( e != null ) characterCode = e.keyCode;
			}
			
			if( characterCode == 13 )
			{
				$.Login.try_login();
			}
		},
		
		try_login: function()
		{
			var subscriber_username = $.trim( $('input[name*=subscriber_username]:first').val());
			var subscriber_password = $.trim( $('input[name*=subscriber_password]:first').val());
			
			if( !subscriber_username)
			{
				alert('Please provide username');
				$('input[name*=subscriber_username]:first').focus();
				return false;
			}
			
			if( !subscriber_password)
			{
				alert('Please provide password');
				$('input[name*=subscriber_password]:first').focus();
				return false;
			}
			
			$.post( '../reports/auth/login',
			{
				method: 'xhr',
				username: subscriber_username,
				password: subscriber_password
				}, $.Login.login_status, 'json'
			);
		},
		
		login_status: function(resp)
		{
			if(resp.error == '0')
			{
				location.href = '../' + resp.redirect;
			}
			
			else
			{
				alert(resp.msg);
				$('input[name*=subscriber_username]:first').val('');
				$('input[name*=subscriber_password]:first').val('');
				$('input[name*=subscriber_username]:first').focus();
			}
		}
	});
	
	$(document).ready(function()
	{
		if(typeof $.Login === 'undefined')
		{
			$.Login = new $.LoginClass();
		}
	});
	
})(jQuery)
