﻿$.fn.rssReader = function (options) {
    this.options = $.extend({
        url: null,
        max: 5,
        zebra: false,
        defaultText: "Geen items gevonden"
    }, options);

    if (this.options.url != null) {
        var element = $(this);
        //Read the Items and append to the element

        try {
            $.get(this.options.url, function (items) {
                var i = 0;

                //find each 'item' in the file and parse it
                $(items).find('item').each(function () {

                    if (i < options.max) {
                        //name the current found item this for this particular loop run
                        var $item = $(this);
                        // grab the post title
                        var title = $item.find('title').text();
                        // grab the post's URL
                        var link = $item.find('link').text();
                        // next, the description
                        var description = $item.find('description').text();
                        //don't forget the pubdate
                        var pubDate = $item.find('pubDate').text().substring(5, 25);

                        // now create a var 'html' to store the markup we're using to output the feed to the browser window
                        var item = $("<div />").addClass("item");
                        var rssTitle = $("<div />").addClass("rssTitle");
                        var link = $("<a />").attr("href", link).attr("target", "_blank");
                        link.html(title);

                        var date = $("<div />").addClass("date");
                        date.html(pubDate);

                        rssTitle.append(link);
                        item.append(rssTitle);
                        item.append(date);

                        //put that feed content on the screen!
                        element.append(item);

                        i++;
                    }

                    //No Items found
                    if (i == 0) {
                        element.append("<div>" + options.defaultText + "</div>");
                    }
                });

                //Apply zebra if it is in the Options
                if (options.zebra) {
                    element.zebra();
                }
            });
        } catch (e) { }
    }
}
