(function($) {
    "use strict";
    $.Login = function(settings) {
        var config = {
            url: '',
			surl: '',

        };
        if (settings) {
            $.extend(config, settings);
        }

    $("#backto").on('click', function() {
        $("#loginform").slideDown();
        $("#passform").slideUp();
    });
    $("#passreset").on('click', function() {
        $("#loginform").slideUp();
        $("#passform").slideDown();
    });
	
    $("button[name=doLogin]").on('click', function() {
        var $btn = $(this);
        $btn.addClass('loading');
        var username = $("input[name=username]").val();
        var password = $("input[name=password]").val();
        $.ajax({
            type: 'post',
            url: config.url + "/controller.php",
            data: {
                'action': 'userLogin',
                'username': username,
                'password': password
            },
            dataType: "json",
            success: function(json) {
				if(json.type === "error") {
					$.wNotice(decodeURIComponent(json.message), {
						autoclose: 6000,
						type: json.type,
						title: json.title
					});
				} else {
					window.location.href = config.surl + "/dashboard";
				}
                $btn.removeClass('loading');
            }
        });
    });
	
    $("button[name=doPass]").on('click', function() {
        var $btn = $(this);
        $btn.addClass('loading');
        var email = $("input[name=email]").val();
        var fname = $("input[name=fname]").val();
        $.ajax({
            type: 'post',
            url: config.url + "/controller.php",
            data: {
                'action': 'uResetPass',
                'email': email,
                'fname': fname,
				'front': true
            },
            dataType: "json",
            success: function(json) {
				$.wNotice(decodeURIComponent(json.message), {
					autoclose: 6000,
					type: json.type,
					title: json.title
				});
                if (json.type === "success") {
					$btn.prop("disabled", true);
				}
                $btn.removeClass('loading');
            }
        });
    });
    };
})(jQuery);