// instantSettingDefault
GrokCM.InstantSettingDefault = {
    source      : 'kizasi_jp_kitt_cat_ctxt_test',
    config      : '2039279329',
    type        : 'grok_instant_test',
    codec       : 'euc-jp',
    extractType : 'instant',
    layout      : 'vertical',
    width       : '300',
    maxAdCount  : 2,
    titleColor  : 'gray',
    descColor   : 'silver',
    linkColor   : 'gray',
    borderColor : 'silver',
    bgColor     : 'black',
    fontSize    : 13
};

// GrokCM.Custom
GrokCM.Custom = function(symbolName) {
    // call GrokCM.Core's constructor
    GrokCM.Core.call(this, symbolName);

    // initiarize property
    var setting = GrokCM.InstantSetting || {};
    var defaultSetting = GrokCM.InstantSettingDefault;
    this.source       = setting.source              || defaultSetting.source;
    this.config       = setting.config              || defaultSetting.config;
    this.type         = setting.type                || defaultSetting.type;
    this.codec        = setting.codec               || defaultSetting.codec;
    this.extractType  = setting.extractType         || defaultSetting.extractType;
    this.layout       = setting.layout              || defaultSetting.layout;
    this.width        = setting.width               || defaultSetting.width;
    this.maxAdCount   = parseInt(setting.maxAdCount || defaultSetting.maxAdCount);
    this.titleColor   = setting.titleColor          || defaultSetting.titleColor;
    this.descColor    = setting.descColor           || defaultSetting.descColor;
    this.urlColor     = setting.urlColor            || defaultSetting.urlColor;
    this.sponsorColor = setting.sponsorColor        || defaultSetting.sponsorColor;
    this.borderColor  = setting.borderColor         || defaultSetting.borderColor;
    this.bgColor      = setting.bgColor             || defaultSetting.bgColor;
    this.fontSize     = setting.fontSize            || defaultSetting.fontSize;
    this.srcCodec     = {'utf8'      : 'UTF-8',
                         'euc-jp'    : 'EUC-JP',
                         'shiftjis'  : 'Shift_JIS'}[this.codec];
};
// inherit GrokCM.Core
GrokCM.Custom.prototype = new GrokCM.Core();
GrokCM.Custom.prototype.constructor = GrokCM.Custom;
// override methods
GrokCM.Custom.prototype.getType = function() {
    return this.type;
};
GrokCM.Custom.prototype.getAdMaxCount = function() {
    return this.maxAdCount;
};
GrokCM.Custom.prototype.getTargetText = function() {
    if (this.extractType == 'custom') {
         return this.getTargetTextCustom();
    } else {
         return this.getTargetTextInstant();
    }
};
GrokCM.Custom.prototype.getTargetTextInstant = function() {
    var texts = [];
    texts.push(GrokCM.Util.getTextByMark('<!-- grok target title start -->', '<!-- grok target title end -->'));
    texts.push(GrokCM.Util.getTextByMark('<!-- grok target body start -->', '<!-- grok target body end -->'));
    texts.push(GrokCM.Util.getTextByMark('<!-- grok target additional1 start -->', '<!-- grok target additional1 end -->'));
    texts.push(GrokCM.Util.getTextByMark('<!-- grok target additional2 start -->', '<!-- grok target additional2 end -->'));
    texts.push(GrokCM.Util.getTextByMark('<!-- grok target additional3 start -->', '<!-- grok target additional3 end -->'));
    return GrokCM.Util.strip(texts.join(' '));
};
GrokCM.Custom.prototype.showAds = function(adData) {
    if (this.layout == 'custom') {
        this.showAdsCustom(adData);
    } else {
        this.showAdsInstant(adData);
    }
};
GrokCM.Custom.prototype.showAdsInstant = function(adData) {
    var i = 6, endIndex = adData.length, ads = [];
    while (i < endIndex) {
        var descr = adData[i++], unused1 = adData[i++], clickURL = adData[i++], title = adData[i++], sitehost = GrokCM.Util.textWrap(adData[i++]), unused2  = adData[i++];

        var adItem   = '<div>';
        adItem      += '<p class="ad_title"><a href="' + clickURL + '" target="_blank">' + title + '</a></p>';
        adItem      += '<p class="ad_text"><a href="' + clickURL + '" target="_blank">' + descr + '</a></p>';
        adItem      += '<p class="ad_url"><a href="' + clickURL + '" target="_blank">' + sitehost + '</a></p>';
        adItem      += '</div>';
        ads.push(adItem);
    }
    if (ads.length > 0) {
        var adsHTML  = '<div id="ads">';
        adsHTML     += '<div id="ads_in">';
        adsHTML     += ads.join('');
        adsHTML     += '</div>';
        adsHTML     += '<div>';
        adsHTML     += '<div><p class="sponsor">';
        adsHTML     += '<a href="http://www.overture.co.jp/ja_JP/arp/contentmatch.php" target="_blank">';
        adsHTML     += 'ads by overture</a></p></div>';
        adsHTML     += '</div>';

        var container, iframe, iframeDocument, html;
        var setAtt = function(targetNode, attName, val) {
            var att = document.createAttribute(attName);
            att.nodeValue = val;
            targetNode.setAttributeNode(att);
        }
        container = document.getElementById('grokInstantContainer');
        iframe = document.createElement('iframe');
        setAtt(iframe, 'scrolling', 'no');
        setAtt(iframe, 'frameborder', '0');
        setAtt(iframe, 'allowtransparency', 'true');
        setAtt(iframe, 'width', this.width);
        setAtt(iframe, 'marginwidth', '0');
        setAtt(iframe, 'marginheight', '0');
        setAtt(iframe, 'vspace', '0');
        setAtt(iframe, 'hspace', '0');
        container.appendChild(iframe);
        iframeDocument = iframe.contentDocument || iframe.contentWindow.document;

        var adItemWidth;
        if (this.layout == 'horizontal') {
            adItemWidth = ((this.width - 2) / ads.length) - 8;
        } else {
            adItemWidth = (this.width - 2) - 8;
        }

        html  = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
        html += '<html lang="ja" xml:lang="ja" xmlns="http://www.w3.org/1999/xhtml">';
        html += '<head>';
        html += '<meta http-equiv="Content-Type" content="text/html; charset=' + this.srcCodec + '" />';
        html += '<meta http-equiv="content-script-type" content="text/javascript" />';
        html += '<meta content="text/css" http-equiv="Content-Style-Type"/>';
        html += '<style type="text/css">';
        html += 'body {margin: 0;padding: 0;}';
        html += 'p {margin: 0;padding: 0;line-height: 110%;}';
        html += 'a {text-decoration: none;}';
        html += '#ads {margin: 0;padding: 0;background-color: ' + this.bgColor + ';border: 1px solid ' + this.borderColor + ';width: ' + (this.width - 2) + 'px;overflow: hidden;position: relative;}';
        html += '#ads #ads_in div {display: brock;float: left;padding: 3px 3px 3px 5px;width: ' + adItemWidth + 'px;}';
        html += '.ad_title a {font-size: ' + this.fontSize + 'px;}';
        html += '.ad_title a:hover, .ad_title a:visited {text-decoration: underline;}';
        html += '.ad_title a, .sponsor a {color: ' + this.titleColor + '; font-weight: bold;}';
        html += '.ad_text a {color: ' + this.descColor + ';font-size: ' + (this.fontSize - 2) + 'px;}';
        html += '.ad_url a {color: ' + this.urlColor + ';font-size: ' + (this.fontSize - 2) + 'px;}';
        html += '.sponsor {clear: both;width: 100%; padding-top: 1px;padding-bottom: 1px;font-size: ' + (this.fontSize - 2) + 'px;text-align: right;}';
        html += '.sponsor a {padding-right: 3px;font-size: ' + (this.fontSize - 2) + 'px; color: ' + this.sponsorColor + ';}';
        html += '</style>'
        html += '</head>';
        html += '<body onload="loadStat = true;">';
        html += adsHTML;
        html += '</body>';

        iframeDocument.open();
        iframeDocument.write(html);
        iframeDocument.close();

        (function (){
            var timer = null;
            return function() {
                timer = setInterval(function() {
                    if (iframe.contentWindow.loadStat) {
                        clearInterval(timer);
                        timer = null;
                        var iframeHeight = iframeDocument.body.scrollHeight + 2;
                        setAtt(iframe, 'height', iframeHeight);
                        iframe.style.height = iframeHeight;
                        (function () {
                            var animationTimer = null, h = 1, delta = parseInt(iframeHeight / 10);
                            return function() {
                                container.style.height = h + 'px';
                                animationTimer = setInterval(function() {
                                    if (h >= (iframeHeight)) {
                                    clearInterval(animationTimer);
                                    animationTimer = null;
                                    h = iframeHeight + 2;
                                    container.style.height = h + 'px';
                                    } else {
                                        h += delta;
                                        container.style.height = h + 'px';
                                    };
                                }, 10);
                            }
                        })()();
                    }
                }, 10);
            };
        })()();
    }
};
GrokCM.Custom.prototype.ngAd = function() {
    this.grokData = undefined;
    this.switchAd();
};
GrokCM.Custom.prototype.loadAdDataTimeOut = function() {
    if (this.layout == 'custom') {
        this.loadAdDataTimeOutCustom();
    } else {
        document.getElementById('grokInstantContainer').style.display = 'none';
    }
};

// for customize
GrokCM.Custom.prototype.getTargetTextCustom = function() {
    // for customize
    var targetText = '';
    return targetText;
};
GrokCM.Custom.prototype.showAdsCustom = function(adData) {
    // for customize
    alert(adData.length);
};
GrokCM.Custom.prototype.loadAdDataTimeOutCustom = function() {
    // for customize
    alert('loadAdDataTimeOutCustom');
};

