var Callback = new Object();

Callback.Storage = new Object();

Callback.ExecuteCallback = function(bDelayed, oButton, iType) {
    new Ajax.Request('/ajax/services/callback', {
        parameters : {
            Delayed : (bDelayed) ? '1' : '0',
            Type : iType
        },
        onLoading : function() {
            Callback.DisableButton(oButton);
        },
        onComplete : function(transport) {
            oResponse = transport.responseJSON;
            if (oResponse == null || Object.isUndefined(oResponse.Status)
                    || oResponse.Status != 1) {
                Callback.EnableButton(oButton);
                Callback.Storage.Busy = false;
                Callback.ShowError(Object.isUndefined(oResponse.Error) ? false
                        : oResponse.Error);
                return;
            }
            
            setTimeout(function() {
                Callback.EnableButton(oButton);
                Callback.Storage.Busy = false;
            }, bDelayed ? 300000 : 20000);
        }
    });
};

Callback.ShowError = function(sMessage) {
    sMessage = sMessage
            || 'There was a problem with your callback, please try again later.';
    sMessage = "<div class='popup_request'>" + sMessage + "</div>";
    AnimatedOverlay.ShowByContent(sMessage, 'callback_error');
};

Callback.EnableButton = function(oButton) {
    Element.removeClassName(oButton, 'color2');
    Element.addClassName(oButton, 'color1');
};

Callback.DisableButton = function(oButton) {
    // oButton.style.backgroundColor = 'red';
    Element.removeClassName(oButton, 'color1');
    Element.addClassName(oButton, 'color2');
};

Callback.StartCallback = function(oButton, bDelayed) {
    if (ThreesomeService.Storage.Busy)
        return;
    Callback.Storage.Busy = true;
    Callback.DisableButton(oButton);
    Callback.RequestCallback(bDelayed, oButton);
};

var ThreesomeService = {};
ThreesomeService.Callback = function(oButton, bDelayed) {
    return Callback.ExecuteCallback(bDelayed, oButton, 2);
};

var GroupService = {};
GroupService.Callback = function(oButton, bDelayed) {
    return Callback.ExecuteCallback(bDelayed, oButton, 4);
};
