if (typeof(LDWFetcher) == 'undefined') {
var is_dev = /\.dev\./.test( location.hostname );

var LDWFetcher = {};

LDWFetcher.Conf = {
    servers : 2
};

LDWFetcher.Utils = {
    queryString : function (vars) {
        var params = [];
        for (var key in vars)
            params.push(key + "=" + encodeURIComponent(vars[key]));
        return params.join("&");
    },
    createElement : function (p) {
        var n = document.createElement(p.tag);
        delete p.tag;
        for (var k in p) {
            var key = (this.ua() != "ie" && k == "className") ? "class" : k;
            n.setAttribute(key, p[k]);
        }
        return n;
    },
    createScript : function (url) {
        var s = this.createElement({ tag : "script", type : "text/javascript", src : url });
        document.getElementsByTagName("head")[0].appendChild(s);
    },
    // Thanks to ma.la and TAKESAKO
    createDummy : function (url) {
        var img = this.createElement({ tag : "img", width : 0, height : 0, src: url });
        img.onerror = function (e) {
            LDWFetcher.Utils.createScript(url);
        };
        return img;
    },
    formatDatetime : function (datetime) {
        datetime.match(/^(.*)T(.*)\+(.*)/);
        var d = RegExp.$1.split("-");
        var t = RegExp.$2;
        return d[0] + "年 " + d[1] + "月 " + d[2] + "日 " + t;
    },
    ua : function () {
        if (window.opera) return "opera";
        else if (document.all) return "ie";
        else return "other";
    },
    genServerId : function () {
        return "0" + Math.ceil(Math.random() * LDWFetcher.Conf.servers);
    },
    appendCSS : function (p) {
        var url = p.url.replace(/\_\#\_/, this.genServerId());
        var css = this.createElement({ tag : "link", rel : "stylesheet", href : url, type : "text/css", id : p.id });
        document.getElementsByTagName("head")[0].appendChild(css);
    }
};

LDWFetcher.Handler = {
    fetch : function (vars) {
        if (typeof(LDWFetcher.Item[vars.type]) == 'function') {
            new LDWFetcher.Item[vars.type](vars).fetch();
        }
    },
    callback : function (vars) {
        if (typeof(LDWFetcher.Item[vars.type]) == 'function' && vars.valid) {
            var item = new LDWFetcher.Item[vars.type](vars);
            item.addCSS();
            item.initBox();
            item.process();
        }
    }
};

LDWFetcher.Item = function () {};
LDWFetcher.Item.prototype = function () {
    var private = {};
    var public = {};
    public.fetch = function () {
        var url = this.getConf().apiURL + "?" + LDWFetcher.Utils.queryString(this.vars);
        LDWFetcher.Utils.createScript(url);
    };
    public.addCSS = function () {
        var conf = this.getConf();
        if (!document.getElementById(conf.cssID))
            LDWFetcher.Utils.appendCSS({ id : conf.cssID, url : conf.cssURL });
    };
    public.initBox = function () {};
    public.process = function () {};
    public.insNode = function (p) {
        var div = LDWFetcher.Utils.createElement({ tag : "div", className : p.classname });
        div.innerHTML = p.text;
        document.getElementById(this.vars.target_id).appendChild(div);
    };
    return public;
}();

LDWFetcher.Item.AWS = function (vars) {
    delete vars.type;
    this.vars = vars;
};
LDWFetcher.Item.AWS.prototype = function () {
    var private = {};
    private.conf = {
        apiURL : is_dev ? "http://wiki.dev.livedoor.jp/external_api/amazon/lookup" : "http://wiki.livedoor.jp/external_api/amazon/lookup",
        cssURL : "http://image_#_.wiki.livedoor.jp/css/aws_item.css",
        cssID  : "LDW_AWS_CSS"
    };
    var public = new LDWFetcher.Item();
    public.getConf = function () {
        return private.conf;
    };
    public.process = function () {
        var vars = this.vars;
        this.insNode({
            classname   : "aws_item_" + vars.size + " aws_item_" + vars.color,
            text        : this.buildHTML()
        });
    };
    public.buildHTML = function () {
        var vars = this.vars;
        var output = [];
        output.push('<div align="center"><a href="' + vars.url + '" target="_blank"><img style="border:0;" src="' + vars.image_url + '" alt="' + vars.title + '" title="' + vars.title + '" width="' + vars.image_width + '" height="' + vars.image_height + '"></a></div>');
        output.push('<div align="center"><a href="' + vars.url + '" target="_blank">' + vars.title + '</a></div>');
        return output.join("\n");
    };
    return public;
}();

LDWFetcher.Item.Nico = function (vars) {
    delete vars.type;
    this.vars = vars;
};
LDWFetcher.Item.Nico.prototype = function () {
    var private = {};
    private.conf = {
        apiURL : is_dev ? "http://wiki.dev.livedoor.jp/external_api/nicovideo/getinfo" : "http://wiki.livedoor.jp/external_api/nicovideo/getinfo",
        cssURL : "http://image_#_.wiki.livedoor.jp/css/nico_item.css",
        cssID  : "LDW_Nico_CSS"
    };
    var public = new LDWFetcher.Item();
    public.getConf = function () {
        return private.conf;
    };
    public.initBox = function () {
        var container = document.getElementById(this.vars.target_id);
        container.style.width = this.vars.width - ((LDWFetcher.Utils.ua() != "ie") ? 2 : 0) + "px";
        container.style.marginTop = 5 + "px";
    };
    public.process = function () {
        var vars = this.vars;
        this.insNode({
            classname   : "nico-title",
            text        : vars.title
        });
        this.insNode({
            classname   : "nico-text",
            text        : vars.description
        });
        this.insNode({
            classname   : "nico-date",
            text        : "公開日: " + LDWFetcher.Utils.formatDatetime(vars.first_retrieve)
        });
        this.addInfo({
            numComments : vars.comment_num,
            numViewed   : vars.view_counter
        });
        this.showBox();
    };
    public.addInfo = function (p) {
        var views = '<span class="count">再生回数</span><strong>' + p.numViewed + '</strong>';
        var comments = '<span class="comment">コメント数</span><strong>' + p.numComments + '</strong>';
        this.insNode({
            classname   : "nico-detail",
            text        : views + comments
        });
    };
    public.showBox = function () {
        document.getElementById(this.vars.target_id).style.display = "block";
    };
    return public;
}();

}

