NCore.Ajax = function(url, params, settings) {
    var settings = new Hash(NCore.Ajax.Settings).merge(settings || {}).toObject();
    if (settings.showIndicator) {
        var oncomplete = Object.isFunction(params.onComplete) ? params.onComplete : function() {};
        params.onComplete = function(o) {
            NCore.Ajax.defaultHandlers.onComplete(o);
            oncomplete(o);
            if (settings.catchRedirect) {
                NCore.Ajax.defaultHandlers.afterComplete(o);
            }
        }

        params.onLoading = NCore.Ajax.defaultHandlers.onLoading;
    }
    params.requestHeaders = {
        accept: settings.accept
    };
    new Ajax.Request(url, params);
};

NCore.Ajax.Settings = {
    accept: 'text/javascript',
    showIndicator: true,
    catchRedirect: true,
    indicatorClass: 'ajax-loading',
    loadingSelector: null,
    overlayId: 'ajax-loading-overlay',
    overlayText: 'Betöltés...'
};

NCore.Ajax.defaultHandlers = {
    afterComplete: function(o) {
        var json = o.responseJSON;
        if ('redirect' in json) {
            window.location = json.redirect;
        }
    },
    onComplete: function() {
        $(NCore.Ajax.Settings.overlayId).remove();
    },
    onLoading: function() {
        var elem = new Element('p', { id: NCore.Ajax.Settings.overlayId }).update(NCore.Ajax.Settings.overlayText);
        elem.setStyle({ float: 'right', background: 'url("/static/js/Ajax/ajax-loader.gif") right no-repeat #000000', paddingRight: '25px', fontWeight: 'bold', fontSize: '12px', position: 'fixed', right: 0, top: 0, color: 'white' });
        document.body.appendChild(elem);
   }
};


