﻿(function (app, $, undefined) {

    app.viewModel = app.viewModel || {};

    function player(index) {
        this.index = ko.observable(index);
    }

    app.viewModel.register = {
        values: ko.observableArray([]),
        remove: function () {
            app.viewModel.register.values.remove(this);
            return false;
        },
        add: function () {
            app.viewModel.register.values.push(new player(ko.observable(app.viewModel.register.values().length)));
            return false;
        },
        max: 12
    };

    app.viewModel.register.updatedValues = ko.computed(function () {
        if (this.values()) {
            for (var i = 0; i < this.values().length; i++) {
                if (this.values()[i].index)
                    this.values()[i].index(i);
                else
                    this.values()[i].index = ko.observable(i);
            }
        }

        return this.values;
    }, app.viewModel.register);

    app.parseResponse = function (data) {

        var json = data;
        if (typeof data === "String" || typeof data == "string") {
            json = (eval('(' + data + ')'));
        }
        return json;
    };

    app.init = function () {
        ko.applyBindings(app.viewModel);
    };

    app.displayProgress = function (show) {
        app.closeModal();

        if (show && !$('.loader').dialog('isOpen')) {
            $('.loader').dialog('open');
        }
        else {
            $('.loader').dialog('close');
        }
    };

    app.closeModal = function () {
        if ($('.loader').dialog('isOpen')) {
            $('.loader').dialog('close');
        }
    };

    app.register = function () {

        app.displayProgress(true);

        $.ajax({
            url: '/Register',
            data: $.toJSON($('form').serializeObject()),
            type: 'POST',
            dataType: '',
            contentType: "application/json",
            success: function (data) {

                app.displayProgress(false);

                try {
                    var json = app.parseResponse(data);

                    if ('object' === typeof json) {
                        if (json && json.length > 0) {
                            var message = 'Please correct the following errors\n\n';
                            $.each(json, function () {
                                message += ('\t-' + this + '\n');
                            });
                            alert(message);
                        } else {

                        }
                    } else {

                    }

                } catch (e) {
                    window.location = data;
                }
            },
            error: function (response) {
                app.displayProgress(false);
                alert('An unknown error has occurred.  It has been reported and we will fix it as soon as possible.  Please check back later.');
            }
        });
    };

    var options = {
        autoOpen: false,
        bgiframe: true,
        width: 600,
        height: 'auto',
        modal: true,
        dialogClass: 'loaderContainer',
        resizable: false,
        draggable: false,
        position: 'center',
        open: function (event, ui) {
            $('.ui-widget-overlay').show();

        },
        close: function (event, ui) {
            $(this).empty();
        }
    };

    $('.loader').dialog(options);

    $.fn.serializeObject = function () {
        var o = {};
        var a = this.find('input,select,textarea').filter(function () {


            if ($(this).attr('type') == 'hidden' && $(this).prev().attr('type') == 'checkbox') {
                var name = $(this).attr('name');
                return !($('form input[type=checkbox][name=' + name + ']').val() == 'true');
            } else if ($(this).attr('type') == 'checkbox') {
                return ($(this).val() == 'true');
            } else {
                return true;
            }
        })
        .serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };

})(window.app = window.app || {}, jQuery);
