﻿if (typeof (Vietgeek) == 'undefined')
    var Vietgeek = {};


Vietgeek.SimpleDialog = function (options) {
    var defaults = {
        dialog_id: 'simple-dialog',
        width: 500,
        position: 'fixed',
        waiting_message: '<p class="dialog_loading">Đang tải dữ liệu...</p>'
    };

    var opts = jQuery.extend({}, defaults, options);
    var ie6 = ($.browser.msie && $.browser.version < 7);
    var dialog;
    var dialog_content;

    var init = function () {
        dialog = $('#' + opts.dialog_id);
        if (dialog.length == 0) {
            var dialog_html = '<div id="' + opts.dialog_id + '" style="display:none;"> \
                                  <div class="popup"> \
                                    <table> \
                                      <tbody> \
                                        <tr> \
                                          <td class="tl"/><td class="b"/><td class="tr"/> \
                                        </tr> \
                                        <tr> \
                                          <td class="b"/> \
                                          <td class="body"> \
                                            <div class="dialog-content"> \
                                            </div> \
                                          </td> \
                                          <td class="b"/> \
                                        </tr> \
                                        <tr> \
                                          <td class="bl"/><td class="b"/><td class="br"/> \
                                        </tr> \
                                      </tbody> \
                                    </table> \
                                  </div> \
                                </div>';
            dialog = $(dialog_html);
            dialog_content = dialog.find('div.dialog-content');

            $(document.body).append(dialog);
        }
        else {
            dialog_content = dialog.find('div.dialog-content');
        }

        dialog_content.empty().html(opts.waiting_message);
        dialog.css({
            'width': opts.width + 'px',
            'marginLeft': '-' + opts.width / 2 + 'px'
            /*'position': opts.position*/
        });
    };

    init();

    return {
        show: function () {
            if (dialog == null)
                init();

            if (opts.position == 'fixed') {
                dialog.attr('class', 'fixed');
            } else {
                dialog.attr('class', '');
            }

            dialog.fadeIn(300);
        },

        hide: function () {
            dialog.fadeOut(300, function () {
                dialog_content.empty().html(opts.waiting_message);
            });
        },

        setContent: function (html) {
            dialog_content.empty().html(html);
        },

        resetContent: function () {
            dialog_content.empty().html(opts.waiting_message);
        },

        width: function (width) {
            dialog.css({ 'width': width + 'px', 'marginLeft': '-' + width / 2 + 'px' });
        }
    };
};



Vietgeek.LoginDialog = function () {
    var dialog = new Vietgeek.SimpleDialog({ width: 440, position: 'fixed' });
    var requestData = function () {
        $.ajax({
            url: '/Services/LoginForm',
            data: '',
            dataType: 'html',
            type: 'get',
            async: true,
            success: function (results) {
                dialog.setContent(results);

                if (typeof (dialogCallback) != 'undefined')
                    dialogCallback();
            },
            error: function (xhr) {
                if (xhr.status == 400) {
                    alert('Yêu cầu không hợp lệ!');
                }
                else {
                    alert('Xảy ra lỗi khi thực thi!');
                }
                dialog.hide();
            }
        });
    };

    var _show = function () {
        dialog.show();

        requestData();
    };

    var _hide = function () {
        dialog.hide();
    }

    var _setContent = function (html) {
        dialog.setContent(html);
    };

    return {
        init: function () {

        },

        show: function () {
            _show();
        },

        close: function () {
            _hide();
        },

        setContent: function (html) {
            _setContent(html);
        }
    }
};

var showLoginForm = function () {
    if (typeof (loginDialog) == 'undefined') {
        loginDialog = new Vietgeek.LoginDialog();
    }
    loginDialog.show();
    return false;
};
