//Don't forget to include jQuery library.

//Feed URL
var feed_url = 'Blog/syndication.axd?format=rss';

//Number of articles to fetch
var total_articles = 1;

//Maximum length of a title
var title_length = 35;

var c = 0, xmlCheck = 0;
$(document).ready(function() {

    // Do ajax to load xml feed
    $.ajax({
        type: "GET",
        url: feed_url,
        dataType: "xml",
        error: function() {
            if (xmlCheck == 0) {
                title_length = 25;
                var xml = ($.ajax({ url: feed_url, async: false }).responseText);
                var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async = "false";
                xmlDoc.loadXML(xml);
                $(xmlDoc).find('item').each(function() {
                    if (total_articles > c) {
                        var link = $(this).find('link').text();
                        var title = $(this).find('title').text();
                        var description = $(this).find('description').text();

                        if (description.length > 130) {
                            description = description.substring(0, 120) + '...';
                        }
                        if (title.length > title_length) {
                            title = title.substring(0, title_length) + '... ';
                        }

                        var pub_date = $(this).find('pubDate').text().substring(0, 16);
                        $('#view_more').before('<b><a class="date" style="font-size: 12px; display: block; padding-bottom: 4px;" class="article" href="' + link + '"><span>' + pub_date + ' - </span>' + title + '</a></b><span style="color: #777777;">' + description + '</span>');
                        c++;
                    }
                });
            }

        },
        success: function(xml) {
            $(xml).find('item').each(function() {
                if (total_articles > c) {
                    var link = $(this).find('link').text();
                    var title = $(this).find('title').text();
                    var description = $(this).find('description').text();
                   
                    if (description.length > 150) {
                        description = description.substring(0, 120) + '...';
                    }
                    if (title.length > title_length) {
                        title = title.substring(0, title_length) + '... ';
                    }
                    var pub_date = $(this).find('pubDate').text().substring(0, 16);
                    $('#view_more').before('<b><a class="date" style="font-size: 12px; display: block; padding-bottom: 4px;" class="article" href="' + link + '"><span>' + pub_date + ' - </span>' + title + '</a></b><span style="color: #777777;">' + description + '</span>');
                    c++;
                }
            });
        }
    }); //close $.ajax(

});

function dirname( url ) {
	return url.substring(0, url.lastIndexOf('/'));
}
