/**
 *
 * @author Andriy Oblivantsev <eslider@gmail.com>
 * @author Fiodor Gorobet <casusarts@gmail.com>
 */

// md5 script: https://github.com/emn178/js-md5/

$(function() {

    // create frontend controller to manipulate User inputs

    window.application = new function() {

        /**
         * Request api
         */
        this.query = function(act, request) {

            if(!request) {
                request = {};
            }

            request.act = act;

            return $.ajax({
                url:      "api.php",
                data:     request,
                dataType: "json"
            });
        };

        /**
         * Register user
         *
         * @param userName
         * @param password
         * @returns jQuery ajax token
         */
        //this.registration = function (userName, password, email) {
        //    return this.query("registration", {
        //        user: userName,
        //        pass: md5(md5(password)),
        //        email: email
        //    }).done(function (response) {
        //        if (response.email != null) { // <- check for existing email in db to notificate
        //            if (email) {
        //                email(response.email);
        //            }
        //            $.notify("This email is already exists!");
        //
        //        } else {
        //            $.notify("Registration successful!");
        //        }
        //    });
        //};

        this.registration = function (userName, password, email) {
            return this.query("registration", {
                user: userName,
                pass: md5(md5(password)),
                email: email
            });
        };

        /**
         * login
         *
         * @param userName
         * @param password
         * @param onLogin login callback
         */
        this.login = function(userName, password, onLogin) {
            this.query("login", {
                user: userName,
                pass: md5(md5(password))
            }).done(function(response) {
                if(response.user != null) {
                    if(onLogin) {
                        onLogin(response.user)
                    }
                    $.notify(response.user.name + " is logged on.", "info");
                } else {

                    $.notify("User or password is incorrect.");
                }
            });
        };

        this.logout = function(){
            this.query("logout");

        };

        this.getLoggedUser = function(){
            this.query("getLoggedUser").done(function(response){
                console.log(response.user);
                $.notify(response.user.name + " is logged on!", "info");
            });
        };

        /**
         * Load View
         */
        this.loadView = function( templateName) {
            return $.ajax({
                url: "Views/" + templateName + ".html"
            });
        };
    };

});
@@ -796,693 +796,8 @@
     
-//this.registration = function (userName, password, email) %7B%0A        //    return this.query(%22registration%22, %7B%0A        //        user: userName,%0A        //        pass: md5(md5(password)),%0A        //        email: email%0A        //    %7D).done(function (response) %7B%0A        //        if (response.email != null) %7B // %3C- check for existing email in db to notificate%0A        //            if (email) %7B%0A        //                email(response.email);%0A        //            %7D%0A        //            $.notify(%22This email is already exists!%22);%0A        //%0A        //        %7D else %7B%0A        //            $.notify(%22Registration successful!%22);%0A        //        %7D%0A        //    %7D);%0A        //%7D;%0A
 %0A   
@@ -2303,8 +2303,9 @@
  %7D;%0A%0A%7D);
+%0A

Update app.js