var OvertureIM = function(params) {

    this.QUERY_BASE = 'http://im.sonet.ov.yahoo.co.jp/js_flat/';

    this.params = params;
    if (this.params['ctxtId']) {
        var ids = this.params['ctxtId'].split(',');
        if (1 < ids.length) {
            this._ctxtIds = ids;
            this.params['ctxtId'] = ids[Math.floor(Math.random() * ids.length)];
        }
    }
 
    if (typeof this.params['ctxtUrl'] == 'undefined') {
        try { this.params['ctxtUrl'] = encodeURIComponent(window.top.document.location.href); } catch(e) {}
        if (!this.params['ctxtUrl'].match(/^https?/)) {
            this.params['ctxtUrl'] = undefined;
        }
    }
    if (typeof this.params['ref'] == 'undefined') {
        try { this.params['ref'] = encodeURIComponent(window.top.document.referrer); } catch(e) {}
        if (!this.params['ref'].match(/^https?/)) {
            this.params['ref'] = undefined;
        }
    }
}
OvertureIM.prototype = {

    scriptSource: function() {
        return this.QUERY_BASE + '?' + this.queryParams();
    },

    queryParams: function() {
        var pairs = [];
        for (key in this.params) {
            if (this.params[key]) {
                pairs.push(key + '=' + this.params[key])
            }
        }
        return pairs.join('&');
    },

    writeScript: function() {
        document.write('<scr' + 'ipt type="text/javascript" src="' + this.scriptSource() + '"></scr' + 'ipt>');
    },

    getHtml: function(offset, limit) {
        var html = '';
        if (typeof zSr != 'undefined') {
            var i = 6;
            if (offset) {
                i += 6 * offset;
            }
            if (i < zSr.length) {
                html += ('<div class="overture_im"><ul>');
                var end = zSr.length;
                if (limit) {
                    var limitEnd= 6 + 6 * (offset + limit);
                    if (limitEnd < zSr.length) { end = limitEnd; }
                }
                while (i < end) {
                    var desc = zSr[i++]; // 説明文
                    var unused1 = zSr[i++]; //
                    var clickURL = zSr[i++]; // クリックURL
                    var title = zSr[i++]; // タイトル
                    var sitehost = zSr[i++]; // サイトURL
                    var unused2 = zSr[i++]; //
        
                    html += '<li><a href="' + clickURL + '" target="_blank">';
                    html += '<span class="title">' + title + ' </span>';
                    html += '<span class="url">' + sitehost + '</span>';
                    html += '<span class="desc">' + desc + '</span>';
                    html += '</a></li>';
                }
                html += '</ul></div>';
                html += '<div class="overture_im_credit"><span class="title">インタレストマッチ - <a href="http://ov.yahoo.co.jp/service/int/index.html?o=IM0026" target="_blank">広告の掲載について</a></span></div>';
            }
        }
        return html;
    }
}

