(function () {

    var app = angular.module('store', []);

    app.controller('loginController', function ($scope, $http) {
        angular.element(document).ready(function () {
           
            $("#loading").fadeOut();
        });

        $scope.login = function ($event) {
            $($event.target).attr("disabled", true);

            username = $("#username").val();
            password = $("#password").val();

            if (username === '' || password === '') {
                alert('Please fill out all fields.');

                $($event.target).attr("disabled", false);

                return false;
            }

            var data = $.param({ username: username, password: password });

            var config = {
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
                    }
            }

            $http.post('/user/login/', data, config).success(function (data, status) {
                if (data.status === 0) {
                    window.location.href = 'http://mall.vaiox.net/';

                    return false;
                }

                alert(data.message);
            }).error(function (data, status, headers, config) {
                alert(data.message);
            });

            $($event.target).attr("disabled", false);
        };

        $scope.forum = function () {
            window.location.href = 'http://mall.vaiox.net/';
        };

        $scope.logged = function ($event) {
            $($event.target).attr("disabled", true);

            var data = $.param({});

            $http.post('/user/auto', data).success(function (data) {
                if (data.status === 0) {
                    window.location.href = 'http://mall.vaiox.net/';

                    return false;
                }
            }).error(function (data, status, headers, config) {
                alert(data.message);
            });

            $($event.target).attr("disabled", false);
        };
    });
})();