﻿function DmCampaignFeed(accountId, contactId, count, tag, domain, showDate, wrapperId, feedsId, lnk) {
    this._accountId = accountId;
    this._contactId = contactId;
    this._count = count;
    this._domain = domain;
    this._feedsId = feedsId;
    this._showDate = showDate;
    this._tag = tag;
    this._wrapperId = wrapperId;

    jQuery.getJSON(this.get_FeedUrl(), this.CreateFeedRows);
}

DmCampaignFeed.prototype = {
    get_AccountId: function () { return this._accountId },
    get_ContactId: function () { return this._contactId },
    get_Count: function () { return this._count },
    get_Domain: function () { return this._domain; },
    get_FeedsId: function () { return this._feedsId && this._feedsId != "" ? this._feedsId : "dmFeedContent"; },

    get_FeedUrl: function () {
        var url = "http://{0}/f.ashx?output=2&uid={1}&cid={2}&tagname={3}&count={4}&showdate={5}&wid={6}&fid={7}&callback=?";
        return url.replace("{0}", this.get_Domain())
                  .replace("{1}", this.get_AccountId())
                  .replace("{2}", this.get_ContactId())
                  .replace("{3}", this.get_Tag())
                  .replace("{4}", this.get_Count())
                  .replace("{5}", this.get_ShowDate())
                  .replace("{6}", this.get_WrapperId())
                  .replace("{7}", this.get_FeedsId());
    },

    get_ShowDate: function () { return this._showDate; },
    get_Tag: function () { return this._tag; },
    get_WrapperId: function () { return this._wrapperId && this._wrapperId != "" ? this._wrapperId : "dmCmpFeeds"; },

    CreateFeedRows: function (data) {
        var wrapperSelector = "#" + (data.wid ? data.wid : "dmCmpFeeds");
        var feedDiv = jQuery(wrapperSelector + " #" + (data.fid ? data.fid : "dmFeedContent"));
        feedDiv.html('');
        if (data.link == 'False'
            || jQuery(wrapperSelector + " a[href='http://dotmailer.co.uk']:not([style*=hidden]):visible").length > 0
            || jQuery(wrapperSelector + " a[href='http://www.dotmailer.co.uk']:not([style*=hidden]):visible").length > 0) {
            if (data.items.length > 0) {
                jQuery.each(data.items, function (i) {
                    feedDiv.append(jQuery('<div></div>')
                       .addClass(i > 0 && i % 2 != 0 ? 'dmDivAlt' : 'dmDiv')
                       .append(jQuery('<a></a>')
                       .addClass('dmCmpLink')
                       .attr({ target: '_blank', href: this.CantReadUrl })
                       .html(this.Name)));

                    if (data.showdate) {
                        var lastAdded = feedDiv.find('div:last');
                        lastAdded.html(this.DateSent + ' - ' + lastAdded.html());
                    }
                });
            }
            else
                feedDiv.append(jQuery('<div>There are no campaigns to show.</div>'));
        }
        else
            feedDiv.append(jQuery('<div>Feeds are currently unavailable.</div>'));
    }
}
