(function() {
    var baseUrl = 'http://www.murablo.jp/';

    var jsonp = function(url, callback) {
        url += (-1 == url.indexOf('?') ? '?' : '&') + 'jsonp=' + callback;
        var script = document.createElement('script');
        script.type    = 'text/javascript';
        script.src     = url;
        script.charset = 'UTF-8';
        document.getElementsByTagName('head')[0].appendChild(script);
    }

    var script = (function(e) { if (e.nodeName.toLowerCase() == 'script') return e; return arguments.callee(e.lastChild) })(document);
    var callback = 'murabloJsonpCallback' + Math.floor(Math.random() * 10000);

    var query = [];
    if (window.murablo_number_of_items) {
        query.push('number_of_items=' + murablo_number_of_items);
    }
    if (window.murablo_genre) {
        murablo_genre = murablo_genre.join ? murablo_genre.join(',') : murablo_genre;
        query.push('m_blog_genre_ids=' + murablo_genre);
    }
    if (window.murablo_region) {
        murablo_region = murablo_region.join ? murablo_region.join(',') : murablo_region;
        query.push('m_region_genre_ids=' + murablo_region);
    }
    if (window.murablo_topic) {
        murablo_topic = murablo_topic.join ? murablo_topic.join(',') : murablo_topic;
        query.push('t_village_topic_ids=' + murablo_topic);
    }
    if (window.murablo_village) {
        query.push('m_village_alias=' + murablo_village);
    }
    if (window.murablo_blog_type) {
        query.push('blog_type=' + murablo_blog_type);
    }

    var url = baseUrl + 'feed/json';
    if (query.length) {
        url += '?' + query.join('&');
    }

    jsonp(url, callback);


    window[callback] = function(json) {
        var style = {
            title: {
                text:        '新着記事一覧',
                color:       '#000000',
                font_weight: 'normal',
                font_size:   '12px'
            },
            kiji: {
                indent:      '0',
                display:     'block',
                color:       '#0000ff',
                font_weight: 'bold',
                font_size:   '12px'
            },
            blog: {
                indent:      '1em',
                display:     'block',
                color:       '#000000',
                font_weight: 'bold',
                font_size:   '11px'
            },
            date: {
                indent:      '1em',
                display:     'block',
                color:       '#000000',
                font_weight: 'normal',
                font_size:   '10px'
            }
        };

        for (element in style) {
            if (!style.hasOwnProperty(element)) continue;
            for (attribute in style[element]) {
                if (!style[element].hasOwnProperty(attribute)) continue;
                var name = 'murablo_' + element + '_' + attribute;
                if (window[name]) {
                    style[element][attribute] = window[name];
                }
            }
        }

        // container
        var container = document.createElement('div');
        with (container.style) {
            margin         = '0';
            padding        = '0';
            overflow       = 'hidden';
            fontFamily     = '"ヒラギノ角ゴ Pro W3", "Hiragino Kaku Gothic Pro", "メイリオ", Meiryo, Verdana, "ＭＳ Ｐゴシック", sans-serif';
            fontStyle      = 'normal';
            fontWeight     = 'normal';
            textAlign      = 'left';
            textDecoration = 'none';
            whiteSpace     = 'normal';
            wordWrap       = 'break-word';

            width = window.murablo_style_width
                ? murablo_style_width : 'auto';
            height = window.murablo_style_height
                ? murablo_style_height : 'auto';
            border = window.murablo_style_border_color
                ? '1px solid ' + murablo_style_border_color : 'none';
            backgroundColor = window.murablo_style_background_color
                ? murablo_style_background_color : '#ffffff';
        }

        // header
        var img = document.createElement('img');
        img.src    = baseUrl + 'static/images/general/logo_murablo_small.gif';
        img.alt    = '村ぶろ';
        img.width  = 94;
        img.height = 23;
        with (img.style) {
            border = 'none';
        }

        var a = document.createElement('a');
        a.href   = baseUrl;
        a.target = '_blank';
        a.appendChild(img);
        with (a.style) {
            display = 'block';
        }

        container.appendChild(a);

        // free image
        if (/^[-\w]+\.(?:gif|jpg|png)$/i.test(window.murablo_free_image)) {
            img = document.createElement('img');
            img.src = baseUrl + 'static/images/feed_plugin/' + murablo_free_image;
            img.alt = '';
            container.appendChild(img);
            with (img.style) {
                border  = 'none';
                display = 'block';
            }
        }

        // register
        if (!window.murablo_village) {
            img = document.createElement('img');
            img.src    = baseUrl + 'static/images/feed_plugin/register.gif';
            img.alt    = '村ぶろ登録はこちらから';
            img.width  = 163;
            img.height = 34;
            with (img.style) {
                border = 'none';
            }

            a = document.createElement('a');
            a.href   = baseUrl.replace('www', 'member') + 'register/agreement';
            a.target = '_blank';
            a.appendChild(img);
            with (a.style) {
                display = 'block';
            }

            container.appendChild(a);
        }

        // html 1
        if (window.murablo_html_1) {
            var html_1 = document.createElement('div');
            html_1.innerHTML = murablo_html_1.join('\n');
            container.appendChild(html_1);
            with (html_1.style) {
                fontSize = '12px';
            }
        }

        // title
        var p = document.createElement('p');
        p.appendChild(document.createTextNode(style['title']['text']));
        with (p.style) {
            margin     = '0.3em 5px';
            padding    = '0';
            lineHeight = '1.2';
            color      = style['title']['color'];
            fontWeight = style['title']['font_weight'];
            fontSize   = style['title']['font_size'];
        }

        container.appendChild(p);

        // list
        var ul = document.createElement('ul');
        with (ul.style) {
            margin  = '0 5px';
            padding = '0';
        }


        var entry_order = window.murablo_entry_order ? murablo_entry_order : 'KBD';

        for (var i = 0, l = json.length; i < l; i++) {
            var entry = json[i];

            var li = document.createElement('li');
            with (li.style) {
                margin        = '0.3em 0';
                padding       = '0';
                lineHeight    = '1.2';
                listStyleType = 'none';
            }

            for (var j = 0; j < entry_order.length; j++) {
                switch (entry_order.slice(j, j + 1)) {
                case 'K':
                    var a = document.createElement('a');
                    a.href   = entry.url;
                    a.target = '_blank';
                    a.appendChild(document.createTextNode(entry.title_text));
                    with (a.style) {
                        margin         = '0';
                        padding        = '0 0 0 ' + style['kiji']['indent'];
                        display        = style['kiji']['display'];
                        lineHeight     = '1.2';
                        color          = style['kiji']['color'];
                        fontWeight     = style['kiji']['font_weight'];
                        fontSize       = style['kiji']['font_size'];
                        textDecoration = 'underline';
                    }
                    li.appendChild(a);
                    break;

                case 'B':
                    var span = document.createElement('span');
                    span.appendChild(document.createTextNode(entry.blog_title));
                    with (span.style) {
                        margin     = '0';
                        padding    = '0 0 0 ' + style['blog']['indent'];
                        display    = style['blog']['display'];
                        lineHeight = '1.2';
                        color      = style['blog']['color'];
                        fontWeight = style['blog']['font_weight'];
                        fontSize   = style['blog']['font_size'];
                    }
                    li.appendChild(span);
                    break;

                case 'D':
                    var span = document.createElement('span');
                    span.appendChild(document.createTextNode(entry.public_datetime));
                    with (span.style) {
                        margin     = '0';
                        padding    = '0 0 0 ' + style['date']['indent'];
                        display    = style['date']['display'];
                        lineHeight = '1.2';
                        color      = style['date']['color'];
                        fontWeight = style['date']['font_weight'];
                        fontSize   = style['date']['font_size'];
                    }
                    li.appendChild(span);
                    break;
                }
            }

            ul.appendChild(li);
        }

        container.appendChild(ul);

        // html 2
        if (window.murablo_html_2) {
            var html_2 = document.createElement('div');
            html_2.innerHTML = murablo_html_2.join('\n');
            container.appendChild(html_2);
            with (html_2.style) {
                fontSize = '12px';
            }
        }

        script.parentNode.replaceChild(container, script);
    }
})();
