var AKForm = function(data){
    if ('undefined' != typeof(data.formId)){
        this.init(data.formId, data.onOkFunction);
    }
}

AKForm.prototype.formName;
AKForm.prototype.ajaxFormURL;
AKForm.prototype.onOkFunction;

AKForm.prototype.init = function(formId, onOkFunction){
    var t = this;
    t.onOkFunction = onOkFunction;
    switch (formId) {
        case 'register':
            t.formName = 'register';
            t.ajaxFormURL = '/profile/_AjaxGetRegisterForm';
            t.initRegisterForm();
            break;
        case 'login':
            t.formName = 'login';
            t.ajaxFormURL = '/profile/_AjaxGetLoginForm';
            t.initRegisterForm();
            break;
        case 'restore':
            t.formName = 'restore';
            t.ajaxFormURL = '/profile/_AjaxGetRestoreForm';
            t.initRegisterForm();
            break;
        case 'settings':
            t.formName = 'settings';
            t.ajaxFormURL = '/profile/_AjaxGetSettingsForm';
            t.initRegisterForm();
            break;
        default:
            break;
    }
    return false;
}

AKForm.prototype.initRegisterForm = function(){
    t = this;
    ClassLoader.ShowBigLoader(true);
    $.post(t.ajaxFormURL,
    {},
        function(data){
            ClassLoader.HideBigLoader();
            if ('undefined' != typeof(data.result) && 'undefined' != typeof(data.content)) {
                if (data.result =='1') {
                    $('#popup_full').remove();
                    var windowHeight = $(window).height();
                    popupContainer = $('<div id="popup_full"><div id="popup_bg"></div></div>');
                    popupContainer.append(data.content);
                    $('body').append(popupContainer);
                    popupContainer.children(':last').center();
                    t.initAjaxForm();
                    t.initFormEvents();
                } else if (data.result =='0') {
                    alert(data.content);
                }
            }
        }, 'json');
    return false;
}

AKForm.prototype.initAjaxForm = function(){
    t = this;
    $('#superAjaxForm').ajaxForm({
        url: t.ajaxFormURL,
        beforeSubmit: function () {
             ClassLoader.ShowBigLoader(true);
        },
        success: function(data){
            ClassLoader.HideBigLoader();
            if ('undefined' != typeof(data.result) && 'undefined' != typeof(data.content)) {
                switch (data.result) {
                    case '0':
                        switch_popup(t.formName);
                        alert(data.content);
                        break;
                    case '1':
                        popupContainer.children(':last').remove();
                        popupContainer.append(data.content);
                        popupContainer.children(':last').center();
                        t.initAjaxForm();
                        t.initFormEvents();
                        return;
                        break;
                    case '2':
                        switch_popup(t.formName);
                        if ('function' == typeof t.onOkFunction) {
                            t.onOkFunction();
                            return;
                        }
                        if (t.formName == 'register' || t.formName == 'login') {
                            document.location = '/playlist/';
                            return;
                        }
                        alert(data.content);
                        break;
                }
            }
        },
        dataType: 'json'
    });
}

AKForm.prototype.initFormEvents = function(){
    $("form input").keypress(function (e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $(this).parents('form:first').submit();
        } else {
            return true;
        }
    });
}

var AKFormInstance;

function initAKForm(formId, onOkFunction){
    AKFormInstance = new AKForm({
        formId: formId,
        onOkFunction: onOkFunction
    });
}