/*
    Document   : window
    Created on : 23.10.2010, 13:18:28
    Author     : С. Одинцов
    Description:
        Обработчик всплывающих окон.
*/

var Window = {

    //ID окна
    window: null,
    //Время появления окна
    ShowTime: 200,
    title: null,
    w: null,

    init: function (url, obj, title, w, callback) {
        //Биндим ссылку на клик
        $(obj).click(function () {

            Window.Create();
            Window.LoadData(url, title, w, callback);

            return false;
        });

    },

    Create: function () {

        //Если окно уже создано, выходим
        if (this.window) this.Close();

        //Объект окна
        this.window = $('<div class="window" id="Window"></div>');

        this.window.css({
            'top': 0,
            'left': 0
        }).html("<div id='Header'></div><div id='Body'></div></div><div id='Close' onclick='Window.Close(this);'></div>");

        if (this.w) this.window.css('width', this.w);

        //Добавляем окно в Body
        $('body').append(this.window);

        this.window.draggable({
            cursor: 'move',
            scroll: false,
            handle: '#Header'
        });

        this.window.find('#Body').html("<div class=\"load\"></div>");
        this.window.animate({
            'opacity': 1
        }, this.ShowTime);

        this.Pos();

        return false;
    },

    Close: function () {
        this.window.remove();
        this.window = null;
    },

    Pos: function () {
        this.window.css({
            "left": $(window).width() / 2 - this.window.width() / 2,
            "top": $(window).height() / 2 - this.window.height() / 2
        });
        if (this.window.height() > $(window).height()) {
            this.window.find('#Body').css({
                'height': $(window).height() - 100,
                'overflow-y': 'auto'
            });
            this.window.css({
                'top': ($(window).height() / 2 - this.window.height() / 2) - 10
            });
        }
    },

    LoadData: function (url, title, w, callback) {

        Window.window.find('#Body').html("<div class=\"load\"></div>");
        //Если ссылка не указана, выходим
        if (!url || url == '') {
            Window.error({ status: '404', responseText: 'Не указана ссылка!' });
            return;
        }

        $.ajax({
            url: url,
            dataType: 'script',
            success: function (msg) {
                //Если не соответствует формату, выводим ошибку.
                if (!msg.match(/^json/)) {
                    Window.window.css({
                        width: w ? w : 'auto'
                    });
                    Window.window.find('#Header').html(title || title == '' ? title : "Ошибка запроса");
                    if (title == '') {
                        Window.window.find('#Header').remove();
                    } else {
                        Window.window.addClass('with-header');
                    }
                    Window.window.find('#Body').html(msg);
                    Window.Pos();
                } else {
                    //Устанавливаем полученную ширину
                    Window.window.css({
                        width: json.w ? json.w : 300
                    });
                    //Вставляем текст
                    Window.window.find('#Header').html(json.title);
                    Window.window.find('#Body').html(json.text);
                    Window.Pos();
                }

                if (callback) callback(msg);
            },
            error: function (msg, st, er) {
                Window.error(msg);
            }
        });
    },

    error: function (msg) {
        this.window.find('#Header').html("Ошибка запроса: " + msg.status);
        this.window.find('#Body').html(msg.responseText);
        this.window.css({
            width: 500
        });
        this.Pos();
    }
}
