﻿function DTBannerContainer(id, numBanners, type) {
    this._id = id;
    this._numBanners = numBanners;
    this._type = type;
    this._startPage = false;
}

DTBannerContainer.prototype._id;
DTBannerContainer.prototype._numBanners;
DTBannerContainer.prototype._type;
DTBannerContainer.prototype._currentBanners;
DTBannerContainer.prototype._startPage;

function DTBanners() {
    this._bannerContainers = new Array();
    this._updateFreq = 75000;
}

DTBanners.prototype._bannerContainers;
DTBanners.prototype._updateFreq;
DTBanners.prototype._updateTimer;

DTBanners.prototype.start = function() {
    this.load();
}

DTBanners.prototype.registerContainer = function(container) {
    if (this._bannerContainers[container._type] == null)
        this._bannerContainers[container._type] = new Array();
    this._bannerContainers[container._type][container._id] = container;
}

DTBanners.prototype._loadBannerContainer = function(bannerContainers, type, num) {
    DTAjax.BannerService_GetBanners(type, num, function(banners) {
        var index = 0;
        var first = true;
        for (key in banners) 
        {
            if (first) 
            {
                first = false;
                $("#" + bannerContainers[index]._id).html("");
            }
            $("#" + bannerContainers[index]._id).html($("#" + bannerContainers[index]._id).html() + banners[key].html);
            eval(banners[key].script);
            index++;
        }
    });
}

DTBanners.prototype.load = function() {
    var tempBannerContainerIndexArray = new Array();
    for (key in this._bannerContainers) {
        var numBanners = 0;
        var containerIndex = 0;
        tempBannerContainerIndexArray[key] = new Array();
        var bannerTypeContainer = this._bannerContainers[key]
        for (bKey in bannerTypeContainer) {
            numBanners += bannerTypeContainer[bKey]._numBanners;
            for (var i = 0; i < bannerTypeContainer[bKey]._numBanners; i++) {
                tempBannerContainerIndexArray[key][containerIndex] = bannerTypeContainer[bKey];
                containerIndex++;
            }
        }

        // TODO: Get banners by type and number
        this._loadBannerContainer(tempBannerContainerIndexArray[key], key, numBanners)
    }
    if (this._updateTimer != null)
        clearTimeout(this._updateTimer);
    this._updateTimer = setTimeout("banners.load();", this._updateFreq);
}