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();
            }
        }
    }
}
