RCRMAPI = Class.create();
RCRMAPI.prototype =
{
    initialize: function(site, lang, pageId, queryParams) {
        this.site = site;
        this.lang = lang;
        this.pageId = pageId;
        this.queryParams = queryParams;

        this.offerRequests = $A();
        this.index = 0;

        this.doneCallBack = null;
    },
    init: function(callBackWhenDone) {
        this.doneCallBack = callBackWhenDone;

        // Skicka initieringsrequest
        //        var params = $H({site: this.site,lang: this.lang,pageid: this.pageId});

        new Ajax.Request('/app/projects/common/templates/rcrm/api/init.php?__site=' + this.site + '&__lang=' + this.lang + "&__pageId=" + this.pageId + this.queryParams, { method: 'get', onSuccess: this._onInitDone.bind(this), onFailed: this._onInitDone.bind(this) });
        //
    },
    registerContainer: function(element, format, maxNum) {
        this.offerRequests[this.offerRequests.size()] = new OfferRequest(element, format, maxNum);
    },
    registerCallback: function(element, format, maxNum, callBack) {
        this.offerRequests[this.offerRequests.size()] = new OfferRequest(element, format, maxNum, callBack);
    },
    _loadNextOffer: function() {
        // Är vi klara ?
        if (this.offerRequests.size() == 0) {
            // Yep, gott
            this._finished();
        }
        else {
            // Nep, ta första ..
            var item = this.offerRequests.pop();
            new Ajax.Request('/app/projects/common/templates/rcrm/api/getoffers.php?__site=' + this.site + '&__lang=' + this.lang + "&__pageId=" + this.pageId + "&__format=" + item.format + "&__maxoffers=" + item.maxOffers + this.queryParams, { method: 'get', onSuccess: this._onOfferRequestDone.bind(this, item), onFailed: this._onOfferRequestDone.bind(this, item) });

        }
    },
    _onOfferRequestDone: function(originalRequest, transport) {
        try {
            res = eval('(' + transport.responseText + ')');
            if (res.result == 'OK') {
                // Any call bacl
                if (typeof (originalRequest.callBack) == 'undefined') {
                    // We use default
                    this._defaultOfferLoadedHandler(originalRequest, res);
                }
                else {
                    originalRequest.callBack(originalRequest, res);
                }
            }
        }
        catch (e) {
            alert(e.name + ":" + e.message);
        }

        // Nästa
        this._loadNextOffer();
    },
    _onInitDone: function(transport) {
        if (transport.responseText == 'OK') {
            // Starta laddning av erbjudanden
            this._loadNextOffer();
        }
        else {
            this._finished();
        }
    },
    _finished: function() {
        if (this.doneCallBack)
            this.doneCallBack();

        loaded('rcrm');
    },
    _defaultOfferLoadedHandler: function(request, offers) {
        if (offers.numOffers > 0) {
            if (!request.elementToUpdate)
                return;

            request.elementToUpdate.show();

            // Simply append html..
            for (var i = 0; i < offers.offers.length; i++) {
                var oHtml = offers.offers[i];                
                new Insertion.Bottom(request.elementToUpdate,oHtml);
            }
        }
    }
};
function OfferRequest(elementToUpdate,format,maxOffer,callBack)
{
    this.elementToUpdate = elementToUpdate;
    this.format = format;
    this.maxOffers = maxOffer;
    this.callBack = callBack;
}
