(function (d) {

    function $(id) { return d.getElementById(id); }
    function nf(s) { return (s+"").length < 2 ? "0"+s : s; }
    function truncate(str, len) {
        var truncated = "";
        str = str.replace(/^\n+/g, "").split("");
        for (var i = 0, c = 0, l = str.length; i < l && c < len; i++, c++) {
            if (!str[i].match(/[\x00-\x7f]/)) c++; // 非asciiは全角幅として計算
            truncated += str[i];
        }
        if (len < str.length) truncated += "…";

        return truncated;
    }

    var gDate = function(d) {
        var date =  (typeof(d) == 'string')? new Date(d) : d;
        this.toJapaneseYMD = function() {
            var y = date.getFullYear();
            var m = nf(date.getMonth()+1);
            var d = nf(date.getDate());
            return y + "年" + m + "月" + d + "日";
        };
        this.toYMD = function(delimiter) {
            return [date.getFullYear(), nf(date.getMonth()+1), nf(date.getDate())].join(delimiter);
        };

        this.getWDay =  function() {
            if (!this.wday) {
                this.wday = (date.toString().match(/^\w{3}/))[0];
            }
            return this.wday;
        };
    };

    google.load("feeds", "1");
    google.setOnLoadCallback(function () {
        var feed = new google.feeds.Feed('http://shonandive.blog85.fc2.com/?xml');
        feed.setNumEntries(3);
        feed.load(function (result) {
            var entries = [];
            var blogProcSWF = getMovieName('externalShonanDive');
            for (var i = 0, entries_length = result.feed.entries.length; i < entries_length; i++) {
                var gEntry = result.feed.entries[i];
                var date = new gDate(gEntry.publishedDate);
				var image_src = "";
                var m = gEntry.content.match(/<img.*src="(.*?)"/);
                if (m && m[1]) {
                    var this_image = m[1];
                    if (!this_image.match(/^http/)) {
                        this_image = 'http://shonandive.blog85.fc2.com' + this_image;
                    }
                    image_src = this_image;
                }

                entries.push({
                    title : gEntry.title,
                    date  : date.toJapaneseYMD(),
                    desc  : truncate(gEntry.contentSnippet, 90),
					link  : gEntry.link,
					image :	image_src
                });
            }
            blogProcSWF.renderBlogSummary(entries);
        });
    });
})(document);

/* This utility function resolves the string movieName to a Flash object reference based on browser type. */
function getMovieName(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    }
    else {
        return document[movieName];
    }
}

