/**
 * 2009/05 InterestMatch
 *
 * adBlock: 広告表示するHTML要素のID
 * xNum: 横に表示する広告の数
 * yNum: 縦に表示する広告の数
 * width: 広告の幅(オプション 0で未指定)
 * height: 広告の高さ(オプション 0で未指定)
 */
var FS_Ad_Overture_CM2 = function(adBlock, xNum, yNum, width, height) {
    this.adBlock = adBlock;
    this.xNum = xNum;
    this.yNum = yNum;
    if (width) this.width = width;
    if (height) this.height = height;
    this.setAdNum(xNum * yNum);
};

FS_Ad_Overture_CM2.REQUEST_TO = 'http://im.ov.yahoo.co.jp/js_flat/';
FS_Ad_Overture_CM2.ZSR_DESCRIPTION = 0;
FS_Ad_Overture_CM2.ZSR_UNUSED1 = 1;
FS_Ad_Overture_CM2.ZSR_URL = 2;
FS_Ad_Overture_CM2.ZSR_TITLE = 3;
FS_Ad_Overture_CM2.ZSR_HOST = 4;
FS_Ad_Overture_CM2.ZSR_UNUSED2 = 5;
FS_Ad_Overture_CM2.ZSR_CYCLE = 6;

FS_Ad_Overture_CM2.prototype = {

    adBlock: null,

    adNum: 1,
    xNum: 1,
    yNum: 1,
    width: 0,
    height: 0,

    tryNum: 0,

    queries:
    {
        "source"  : 'fineseed_jp_mpe_cat_ctxt',
        "ctxtId"  : '',
        "type"    : 'site1',
        "maxCount": 1,
        "outputCharEnc" : 'shiftjis'
    },

    setQuery: function(key, value)
    {
        this.queries[key] = value;
    },

    setAdNum: function(n) {
        this.adNum = n;
        this.setQuery('maxCount', n);
    },

    load: function()
    {
        var q = [];

        for (var key in this.queries) {
            q.push(key + '=' + this.queries[key]);
        }

        var s = document.createElement('SCRIPT');
        s.charset = 'Shift_JIS';
        s.type = 'text/javascript';
        s.src = FS_Ad_Overture_CM2.REQUEST_TO + '?' + q.join('&');

        document.body.appendChild(s);

        this.checkerId = setInterval((function(CM2){
            return function() {
                // timeout 5 sec
                if (CM2.tryNum > 50) {
                    clearInterval(CM2.checkerId);
                } else if (typeof zSr != 'undefined') {
                    clearInterval(CM2.checkerId);
                    CM2.show();
                } else {
                    CM2.tryNum++;
                }
            }
        })(this), 100);
    },

    fetch: function(n)
    {
        if (!zSr) return null;

        n *= FS_Ad_Overture_CM2.ZSR_CYCLE;

        return {
            "description" : zSr[FS_Ad_Overture_CM2.ZSR_DESCRIPTION + n],
            "url"         : zSr[FS_Ad_Overture_CM2.ZSR_URL + n],
            "title"       : zSr[FS_Ad_Overture_CM2.ZSR_TITLE + n],
            "host"        : zSr[FS_Ad_Overture_CM2.ZSR_HOST + n]
        }
    },

    show: function()
    {
        if (typeof this.adBlock == 'string') {
            this.adBlock = document.getElementById(this.adBlock);
        }

        var html = '';

        var i = 1;

        var table = document.createElement('TABLE');
        var tbody = document.createElement('TBODY');

        for (var y = 0; y < this.yNum; y++) {

            var tr = document.createElement('TR');

            for (var x = 0; x < this.xNum; x++) {

                var td = document.createElement('TD');
                td.style.verticalAlign = 'top';

                tr.appendChild(td);

                var ad = this.fetch(i++);

                if (!ad || typeof ad.url == 'undefined') continue;

                var div = document.createElement('DIV');
                if (this.width != 0) {
                    div.style.width = this.width + 'px';
                }
                if (this.height != 0) {
                    div.style.height = this.height + 'px';
                }

                div.className = 'ad';
                div.onclick = (function(ad) {
                    return function() {
                        window.open(ad.url);
                    }
                })(ad);

                div.innerHTML = [
                    '<p class="header">',
                    ' <span class="title">' + ad.title + '</span>',
                    ' <span class="host">' + ad.host + '</span>',
                    '</p>',
                    '<p class="description">' + ad.description + '</p>',
                ].join('');

                td.appendChild(div);
            }

            tbody.appendChild(tr);
        }

        var tr = document.createElement('TR');
        var td = document.createElement('TD');
        td.colSpan = this.xNum;
        td.className = 'ads_by';
        td.innerHTML = 'インタレストマッチ - <a href="http://ov.yahoo.co.jp/service/int/index.html" target="_blank">広告の掲載について</a>';
        tr.appendChild(td);
        tbody.appendChild(tr);

        table.appendChild(tbody);
        this.adBlock.appendChild(table);

    }

};
