var leto = {
    ajax: function(uri, cache)
    {
        if (typeof JsHttpRequest == 'undefined') {
            alert('Для продолжения работы требуется библиотека JsHttpRequest.js');
            return;
        }

        this.uri = uri || location.href;
        this.cache = cache || false;
        this.debug = true;

        this.onError = function(error)
        {
            alert(error);
            return false;
        }

        this.onSuccess = function(success)
        {
            return true;
        }

        this.onEmpty = function()
        {
            alert('ajax.js: Возвращен пустой результат.');
        }

        this.query = function(data)
        {
            var t = this;
            JsHttpRequest.query(
                this.uri,
                data || {},
                function(result, errors)
                {
                    if (errors && t.debug) {
                        t._debug(errors);
                        //return false;
                    }

                    if (result) {
                        if (result.error) {
                            return t.onError(result.error, result);
                        }
                        //if (result.success) {
                            return t.onSuccess(result.success, result);
                        //}
                    } else {
                        t.onEmpty();
                    }
                },
                !this.cache
            );
        }

        this._debug = function(errors)
        {
            if (errors) {
                $(document.body).prepend(
                    '<div id="debug" style="background: #333; color: #eee;' +
                    ' display: none; font: 12px courier new; padding: 1em;"' +
                    ' onclick="$(this).slideUp().remove()"></div>');

                $('#debug').empty().html('<pre>' + errors + '</pre>').slideDown();
            }
        }
    },


    messageBox: {
        id: '#leto-box',
        className: ['leto-error', 'leto-success'],
        html: '<div id="leto-box"><div></div></div>',

        timer: null,
        timeout: 3000,

        opacity: .7,
        duration: 300,

        set: function() {
            var $box = $(this.html).click(function() {
                $(this).fadeOut(leto.messageBox.duration);
            });
            $(document.body).append($box);
            return $box;
        },

        get: function() {
            var $box = $(this.id);

            if (!$box.length) {
                $box = this.set();
            }

            if (null !== this.timer) {
                //$box.stop();
                clearTimeout(this.timer);
                this.timer = null;
            }

            return $box;
        },

        box: function(className, message, timeout) {
            className += 0;
            this.get().children().html(message).end().removeClass().addClass(this.className[className]).css('opacity', .01).animate({ opacity: this.opacity, width: 'auto' }, this.duration);
            timeout = 'undefined' == typeof timeout ? this.timeout : timeout;
            if (timeout) {
                this.timer = setTimeout('$("' + this.id + '").fadeOut(' + this.duration + ');', timeout);
            }
        }
    },

    box: function(className, message, timeout) {
        this.messageBox.box(className, message, timeout);
    }
}