var Feed = function (url, domId, cnt) {
    this.domId = domId;
    this.cnt = cnt-0;
    this.showSpinner();
    this.loadFeed(url, this.onFeedLoad);
};
Feed.URL = '/cgi-bin/index.cgi';
Feed.COUNTER = 0;

Feed.prototype = {
    name: null,
    domId: null,
    url: null,
    spinner: null,
    cnt: 0,
    loadFeed: function (url, cb) {
        var scope = this;
        var name = 'handleRSS'+ (++Feed.COUNTER);
        window[name] = function(rss) {
            cb.call(scope, rss);
        };
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src =
            Feed.URL +
            "?url=" + url  +
            "&callback=" + name +
            "&t=" + (new Date()).getTime();
        document.getElementsByTagName("head")[0].appendChild(script);
    },
    onFeedLoad: function (rss) {
        var post, title, summary, end = (rss.entries.length<this.cnt) ? rss.entries.length : (this.cnt||rss.entries.length);
        var container = document.getElementById(this.domId);

        this.hideSpinner();

        for (var i=0; i<end; i++) {
            post = rss.entries[i];

            title = document.createElement('h3');
            title.innerHTML =  '<a class="title" target="_blank" href="'+ post.link + '">' + post.title + '</a>';
            container.appendChild(title);

            if (!!post.summary) {
                summary = document.createElement('div');
                summary.className = 'summary';
                summary.innerHTML = post.summary;
                container.appendChild(summary);
            }
        }
    },
    showSpinner: function() {
        if (!this.spinner) {
            this.spinner=document.createElement('div');
            this.spinner.className = 'spinner';
            this.spinner.innerHTML = '<img src="/images/loading.gif" /><span>Loading feed ...</span>';
            document.getElementById(this.domId).appendChild(this.spinner);
        }
        this.spinner.style.display='';
    },
    hideSpinner: function() {
        if (this.spinner) {
            this.spinner.style.display='none';
        }
    }
};

/* add background image to main navigation links */
if (typeof $ == 'function') {
    $(function() {
        $('#navigation li a').hover(function() {
            $(this).addClass('nav-link-hover');
        }, function() {
            $(this).removeClass('nav-link-hover');
        });
    });
}