// GrokCore
var Grok = {};
Grok.Core = function(symbolName) {
    this.symbolName = symbolName;
    this.media             = 'jugem';
    this.codec             = 'utf8';
    this.srcCodec          = 'EUC-JP';
    this.grokHost          = 'grok.kizasi.jp/api';
    this.ovrHost           = 'im.gmo.ov.yahoo.co.jp'
    this.source            = 'gmo_jp_jugem_cat_ctxt_kz';
    this.config            = '2039279329';
    this.defaultCtxtId     = ['sensi001', 'sensi002', 'sensi003', 'sensi004'],
    this.defaultCat        = ['default_automotive', 'default_business', 'default_career',
                              'default_education','default_entertainment','default_fashion',
                              'default_finance', 'default_home', 'default_travel'];
    this.switchLimit       = 1.0;
    this.targetTextMax     = 200;
    this.tryLoadAdMaxCount = 200;
    this.grokKeywords      =undefined;
	this.grokKeywords= undefined;
	this.grocCtxtId= undefined;
	this.grokType= undefined;
	this.gmoPointId= undefined;
};
Grok.Core.prototype = {
    coreVersion: 1.0,
    zSr: undefined,
    grokSrc: undefined,
    ovrSrc: undefined,
    grokData: undefined,
    getType: function(){
        return 'kz';
    },
    getAdMaxCount: function() {
        return 2;
    },
    targetText: '',
    getTargetText: function() {
        return '';
    },
    trace: false,
    setStatus: function(status) {
        this.status = status;
        if (this.trace) {
            if (typeof (console) != 'undefined') {
                console.log(this.status);
            } else {
                alert(this.status);
            }
        }
    },
    status: 'init',
    invoke: function() {
        this.setStatus('invoke');
        this.switchLogic();
    },
    switchLogic: function() {
        this.setStatus('switchLogic');
        if (this.getAdMaxCount() < 1) {
            this.zSr = [];
            this.showAds(this.zSr);
        } else if (Math.random() < this.switchLimit) {
            this.setStatus('switchLogic grok');
			this.switchAd();
//            this.analyzeContents();
        }else{
            this.setStatus('switchLogic ovr');
            this.grokData = undefined;
            this.switchAd();
        }
    },
    analyzeContents: function() {
        this.setStatus('analyze');
        var targetText = '', grokQuery = [], script;
        targetText = this.getTargetText();
        this.targetText = targetText.substring(0, Math.min(this.targetTextMax, targetText.length));
        grokQuery.push('media=' + this.media);
        grokQuery.push('t=' + Grok.Util.kishEncode(this.targetText));
        grokQuery.push('cb=' + this.symbolName + '.setGrokData');
        grokQuery.push('c=' + this.codec);
        grokQuery.push('type=' + this.getType());
        this.grokSrc = 'http://' + this.grokHost + '/grokCMJSONP.py/?' + grokQuery.join('&');
        script = document.createElement('script');
        script.type = 'text/javascript';
        script.charset = this.srcCodec;
        script.src = this.grokSrc;
        document.body.appendChild(script);
    },
    setGrokData: function(grokData) {
        this.setStatus('setGrokData');
        this.grokData = grokData || undefined;
        this.switchAd();
    },
    switchAd: function() {
        this.setStatus('switchAd');
        var ovrQuery = [], keys = [], keyQuery = '';
        if (typeof(this.grokKeywords) == 'undefined' || this.grokKeywords=="") {
            this.setStatus('switchAd ovr default');
            ovrQuery.push('source=' + this.source);
            ovrQuery.push('ctxtId=' + Grok.Util.randomChoice(this.defaultCtxtId));
            ovrQuery.push('keywordCharEnc=utf8');
            ovrQuery.push('outputCharEnc=' + this.codec);
            ovrQuery.push('type=' + this.getType()+'-im');
            ovrQuery.push('maxCount=' + this.getAdMaxCount());
            ovrQuery.push('ctxtCat=' + Grok.Util.randomChoice(this.defaultCat));
            ovrQuery.push('ctxtUrl=' + encodeURIComponent(document.URL));
            this.ovrSrc = 'http://' + this.ovrHost + '/js_flat/?' + ovrQuery.join('&');
            this.loadAdData();
//        } else if (this.grokKeywords.ng.length > 0) {
//            this.setStatus('switchAd ng');
//            this.ovrSrc = '';
//            this.ngAd();
		} else {
            this.setStatus('switchAd grokkeywords');
            keyQuery ='ctxtKeywords=' + this.grokKeywords;
            ovrQuery.push('source=' + this.source);
            ovrQuery.push('ctxtId=' + this.grokCtxtId);
            if (keyQuery != '') {
                ovrQuery.push(keyQuery);
            }
            ovrQuery.push('keywordCharEnc=euc-jp');
            ovrQuery.push('outputCharEnc=' + this.codec);
            ovrQuery.push('type=' +  this.getType()+'-im');
            ovrQuery.push('maxCount=' + this.getAdMaxCount());
            ovrQuery.push('ctxtCat=' + Grok.Util.randomChoice(this.defaultCat));
            ovrQuery.push('ctxtUrl=' + encodeURIComponent(document.URL));
            this.ovrSrc = 'http://' + this.ovrHost + '/js_flat/?' + ovrQuery.join('&');
            this.loadAdData();
        }
    },
    loadAdData: function() {
        this.setStatus('loadAdData');
        var iframe, iframeDocument, iframeWindow, html;
        try {
            iframe = document.createElement('iframe');
            iframe.style.width = '0px';
            iframe.style.height = '0px';
            iframe.style.border = '0px';
            iframe.style.borderStyle = 'hidden';
        } catch (e) {}
        document.body.appendChild(iframe);
        iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
        html  = '';
        html += '<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">';
        html += '<head>';
        html += '<meta http-equiv="Content-Type" content="text/html; charset=' + this.srcCodec + '" />';
        html += '<script type="text/javascript" src="' + this.ovrSrc + '" charset="' + this.srcCodec + '" ></script>';
        html += '</head>';
        html += '<body></body>';
        iframeDocument.open();
        iframeDocument.write(html);
        iframeDocument.close();
        iframeWindow = iframe.contentWindow;
        (function(grok) {
            grok.setStatus('loadAdClosure');
            var timer = null, tryCount = 0;
            return function(){
                timer = setInterval(function() {
                    if (tryCount > grok.tryLoadAdMaxCount) {
                        grok.setStatus('loadAd timeOut tryCount:' + tryCount);
                        clearInterval(timer);
                        grok.loadAdDataTimeOut();
                    } else if (typeof(iframeWindow.zSr) == 'undefined') {
                        tryCount++;
                    } else if (iframeWindow.zSr.length > 0) {
                        grok.setStatus('adData loaded, tryCount:' + tryCount);
                        clearInterval(timer);
                        grok.zSr = [];
                        for (var i = 0, len = iframeWindow.zSr.length ;i < len;i++) {
                            grok.zSr[i] = iframeWindow.zSr[i];
                        }
                        grok.showAds(grok.zSr);
                    } else {
                        tryCount++;
                    }
                }, 100);
            };
        })(this)();
    },
    showAds: function(adData) {
        //
    },
    ngAd: function() {
        this.grokData = undefined;
        this.switchAd();
    },
    loadAdDataTimeOut: function() {
        //
    }
};
Grok.Util = {
    deligate: function(target, func) {
        return function() {
            func.apply(target, arguments);
        }
    },
    randomChoice: function(args) {
        return args[Math.floor(Math.random() * args.length)];
    },
    strip: function(str) {
        return String(str).replace(/^[ \s]*/gim, '').replace(/[ \s]*$/gim, '');
    },
    getText: function(elm) {
        elm = elm || document.body;
        var children = elm.childNodes, texts = [];
        for (var child, type, nodeName, i=0, len = children.length;i < len; i++) {
            child = children.item(i);
            type = child.nodeType;
            if (type == 1) {
                // Element Node
                nodeName = child.nodeName;
                if ((nodeName != 'SCRIPT')&&(nodeName != 'NOSCRIPT')) {
                    texts.push(arguments.callee(child));
                }
            } else if (type == 3) {
                // Text Node
                texts.push(child.data);
            } else if (type == 8) {
                // Comment Node
            }
        }
        return texts.join('');
    },
    getTextByMark: function(startMark, endMark) {
        var html, startIndex, endIndex, targetText;
        html = document.body.innerHTML;
        startIndex = startMark.length + html.indexOf(startMark);
        endIndex = html.indexOf(endMark);
        if (startIndex < endIndex) {
            targetText = this.strip(html.substring(startIndex, endIndex)).replace(/<\/?[^>]+>/gi, '');
        } else {
            targetText = this.getText(document.body);
        }
        return targetText;
    },
    getElementsByClassName: function(className) {
        var children = document.getElementsByTagName('*') || document.all;
        var elements = [];
        for (var i = 0, len1 = children.length; i < len1; i++) {
            var classNames = children[i].className.split(' ');
            for (var j = 0, len2 = classNames.length; j < len2; j++) {
                if (classNames[j] == className) {
                    elements.push(children[i]);
                    break;
                }
            }
        }
        return elements;
    },
    map: function(args, func) {
        var results = [];
        for (var i = 0, len = args.length; i < len; i++) {
            results.push(func(args[i]));
        }
        return results;
    },
    addListener: (function() {
        if ( window.addEventListener ) {
            return function(elm, type, func) {
                elm.addEventListener(type, func, false);
            }
        } else if ( window.attachEvent ) {
            return function(elm, type, func) {
                var wrapperFunc = function() {
                    func.call(elm, window.event);
                };
                elm.attachEvent('on'+type, wrapperFunc);
            };
        } else {
            return function(elm, type, func) {
                elm['on'+type] = func;
            }
        }
    })(),
    kishEncode: function(targetStr) {
        if (targetStr == '') {
            return '';
        }
        var encstr = 'a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|0|1|2|3|4|5|6|7|8|9|(|)'.split('|');
        var bitcnt = 0, rest = 0, result = '';
        for (var i = 0, len = targetStr.length; i < len; i++) {
            chrcode = targetStr.charCodeAt(i);
            rest <<= 16;
            rest |= chrcode;
            bitcnt += 16;
            while (bitcnt >= 6) {
                result += encstr[rest >> (bitcnt - 6)];
                rest &= (1 << (bitcnt - 6)) - 1;
                bitcnt -= 6;
            }
        }
        if (bitcnt) {
            result += encstr[rest << (6 - bitcnt)];
        }
        return result;
    }
};
// Grok.Custom
Grok.Custom = function(symbolName) {
    // call Grok.Core's constructor
    Grok.Core.call(this, symbolName);
    // override property
    this.media   = 'jugem'
    this.source  = 'gmo_jp_jugem_cat_ctxt_kz';
    this.config  = '1862730410';
    this.ovrHost = 'im.gmo.ov.yahoo.co.jp'
    // codec: 'utf8' || 'euc-jp' || 'shiftjis'
    this.codec    = 'euc-jp';
    // srcCodec: 'UTF-8' || 'EUC-JP' || 'Shift_JIS'
    this.srcCodec = 'EUC-JP';

    this.myAd='';

    // custom property: logicControllString
    // ex. 'active' | 'inactive' | 'image' | 'inactive-image' | 'foo' ,,,
    this.logicControllString = 'active';

    // custom property: adMx
    this.sideAdMax  = 2;
    this.bodyAdMax  = 3;
    this.imageAdMax = 0;
    this.entryAdMax = 3;
    this.border     = '000';
    this.bg         = 'FFF';
    this.title      = '1E90FF'
    this.text       = '000000';
    this.url        = '009900';
	this.grokKeywords= undefined;
	this.grocCtxtId= undefined;
	this.grokType= undefined;
	this.gmoPointId= undefined;


};
// inherit Grok.Core
Grok.Custom.prototype = new Grok.Core();
Grok.Custom.prototype.constructor = Grok.Custom;
// override methods
Grok.Custom.prototype.getType = function() {
    if (typeof(this.logicControllString) != 'undefined') {
        return this.logicControllString;
    } else {
        return 'none';
    }
};
Grok.Custom.prototype.getAdMaxCount = function() {
	var entryAdMax=this.entryAdMax;
	if(this.myAd){
		entryAdMax=0;
	}
    if (typeof(this.logicControllString) == 'undefined') {
        return 0;
    } else if (this.logicControllString == 'all' 
			|| this.logicControllString == 'all-search') {
        return this.sideAdMax + this.entryAdMax;


    } else if (this.logicControllString == 'all-inactive' 
			|| this.logicControllString == 'all-inactive-search'
			|| this.logicControllString == 'sidebar-inactive-search'
	) {
        return this.sideAdMax + this.entryAdMax+ this.bodyAdMax;  //2+3+3
    } else if (this.logicControllString == 'sidebar') {
        return this.sideAdMax;  //2
    } else if (this.logicControllString == 'sidebar-inactive') {
        return this.sideAdMax + this.bodyAdMax;	  //2+3
    } else if (this.logicControllString == 'sidebar-search') {
        return this.sideAdMax + this.entryAdMax;	  //2+3
    } else if (this.logicControllString == 'entry'
			|| this.logicControllString == 'entry-search'
	) {
        return this.entryAdMax;	  //3
    } else if (this.logicControllString == 'entry-inactive'
			|| this.logicControllString == 'entry-inactive-search'
	) {
        return this.entryAdMax+ this.bodyAdMax;	  //3+3
    } else if (this.logicControllString == 'image') {
        return this.imageAdMax;
    } else if (this.logicControllString == 'image-inactive') {
        return this.imageAdMax+this.bodyAdMax;
    } else if (this.logicControllString == 'image-search') {
        return this.imageAdMax+this.entryAdMax;
    } else if (this.logicControllString == 'image-inactive-search') {
        return this.imageAdMax+this.bodyAdMax+this.entryAdMax;
	} else {
		return 0;
	}
};
Grok.Custom.prototype.getTargetText= function() {
    var titles, bodies, entryTitle = '', entryBody = '';
    titles = Grok.Util.getElementsByClassName('jgm_entry_title_mark');
    bodies = Grok.Util.getElementsByClassName('jgm_entry_desc_mark');
    if (titles.length > 0) {
      entryTitle = Grok.Util.getText(titles[0].parentNode);
    }
    if (bodies.length > 0) {
      entryBody = Grok.Util.getText(bodies[0].parentNode);
    }
    return entryTitle + ' ' + entryBody;
};
Grok.Custom.prototype.showAds = function(adData) {
    var showAdsSideBar, showAdsBody, showAdsImage;
    var border=this.border;
    var bg=this.bg;
    var title=this.title;
    var text=this.text;
    var url=this.url;
	var pointurl="http://point.gmo.jp/consumer/PointRedirectServlet?";
	pointurl+="id="+this.gmoPointId+"&amp;url=";

    showAdsSideBar = function(adData, startIndex, endIndex,border,bg,title,text,url) {

    if(border=="")    border    = '000';
    if(bg=="")        bg        = 'FFF';
    if(title=="")     title     = '1E90FF';
    if(text=="")      text      = '000000';
    if(url=="")       url       = '009900';
	var css='';
    css+='<style type="text/css">' + "\n";
    css+='<!--' + "\n";
    css+="\n";
    css+='* {' + "\n";
    css+='    margin:0;' + "\n";
    css+='    padding:0;' + "\n";
    css+='}' + "\n";
    css+="\n";
    css+='body {' + "\n";
    css+='        font-family: "ＭＳ ゴシック", "Osaka‐等幅";' + "\n";
    css+='        font-size:11px;' + "\n";
    css+='        }' + "\n";
    css+="\n";
    css+='#jugem_feedme_ad {' + "\n";
    css+='        overflow:hidden;' + "\n";
    css+='        width:123px;' + "\n";
    css+='        height:227px;' + "\n";
    css+='        border:1px solid #' + border + ';/* 枠線 */' + "\n";
    css+='        background:#' + bg + ';/* 背景 */' + "\n";
    css+='    }' + "\n";
    css+='a,' + "\n";
    css+='a:link,' + "\n";
    css+='a:visited' + "\n";
    css+=' {' + "\n";
    css+='    color:#' + title + ';/* タイトル */' + "\n";
    css+='    text-decoration: none;' + "\n";
    css+='}' + "\n";
    css+="\n";
    css+='a:hover,' + "\n";
    css+='a:active {' + "\n";
    css+='    text-decoration:underline;' + "\n";
    css+='    }' + "\n";
    css+="\n";
    css+='.jugem_blog_ad_box {' + "\n";
    css+='        margin:3px 0 ;' + "\n";
    css+='        padding:2px 3px;' + "\n";
    css+='    }' + "\n";
    css+="\n";
    css+='.jugem_blog_ad_box dt { ' + "\n";
    css+='        font-size:12px;' + "\n";
    css+='        font-weight:bolder;' + "\n";
    css+='        line-height:140%;' + "\n";
    css+='}' + "\n";
    css+="\n";
    css+='.jugem_blog_ad_box dt.ad_title2 { ' + "\n";
    css+='        padding:5px 0 0 0;' + "\n";
    css+='}' + "\n";
    css+="\n";
    css+='.jugem_blog_ad_box dd { ' + "\n";
    css+='        text-align:left;' + "\n";
    css+='        margin:0 0 2px;' + "\n";
    css+='        font-size:10px;' + "\n";
    css+='        line-height:130%;' + "\n";
    css+='        color:#' + text + ';/* テキスト */' + "\n";
    css+='}' + "\n";
    css+='.jugem_blog_ad_box dd.url { ' + "\n";
    css+='        color:#' + url + ';/* URL */' + "\n";
    css+='        font-family:arial;' + "\n";
    css+='}' + "\n";
    css+="\n";
    css+='.jugem_blog_ad_by {' + "\n";
    css+='    margin-top:5px;' + "\n";
    css+='    font-size:8px;' + "\n";
    css+='    text-align:right;' + "\n";
    css+='    line-height:100%;' + "\n";
    css+='    font-family: verdana;' + "\n";
    css+="\n";
    css+='}' + "\n";
    css+="\n";
    css+='ul {list-style-type:circle;}' + "\n";
    css+="\n";
    css+='ul.jugem_blog_ad_2 {' + "\n";
    css+='    border-top:1px solid #' + border + ';/* 枠線 */' + "\n";
    css+='    margin:5px 0 0 0;' + "\n";
    css+='    padding:5px 0 0 0 ;' + "\n";
    css+='    font-size:10px;' + "\n";
    css+='    }' + "\n";
    css+="\n";
    css+='.jugem_blog_ad_2 li {' + "\n";
    css+='        margin:0 0 3px 0;' + "\n";
    css+='        padding:0;' + "\n";
    css+='        line-height:100%;' + "\n";
    css+='        }' + "\n";
    css+="\n";
    css+='.gmo_bnr {' + "\n";
    css+='            border-left:1px solid #' + border + ';/* 枠線 */' + "\n";
    css+='            border-right:1px solid #' + border + ';/* 枠線 */' + "\n";
    css+='            border-bottom:1px solid #' + border + ';/* 枠線 */' + "\n";
    css+='        }' + "\n";
    css+="\n";
    css+='-->' + "\n";
    css+='</style>' + "\n";


    var ads = '', i = startIndex, limit = Math.min(adData.length, endIndex);
    ads += '<div id="jugem_feedme_ad">';
    ads += '<div class="jugem_blog_ad_box">';

    while (i < limit) {
        var descr    = adData[i++];
        var unused1  = adData[i++];
        var clickURL = pointurl + escape(adData[i++]);
        var title    = adData[i++];
        var sitehost = adData[i++];
        var unused2  = adData[i++];
		ads += "<dl style='cursor:pointer;' onMouseOver=\"status='';return true;\" onMouseOut=\"status=\'';return true\" onclick=\"";
		ads += "window.open('" + clickURL + "');\"><dt><a href=\"#feedme1\" name=\"feedme1\" onclick=\"return false;\">";
		ads += title + "</a></dt><dd>";
		ads += descr + "</dd><dd class='url'>";
		ads += sitehost + "</dd></dl>";
    }
    ads += '<div class="jugem_blog_ad_by">インタレストマッチ<br /><a target="_blank" href="http://ov.yahoo.co.jp/service/int/index.html?o=IM0021">広告の掲載について</a></div></div></div>';

    var adArea = document.getElementById('grokSideAdContainer');
    if (adArea) {
        if (ads != '') {
		    var iframe = document.createElement("iframe");

		    //iframe setting
		    var att = document.createAttribute("width");
		    att.nodeValue="125";
		    iframe.setAttributeNode(att);

		    var att = document.createAttribute("height");
		    att.nodeValue="230";
		    iframe.setAttributeNode(att);

		    var att = document.createAttribute("scrolling");
		    att.nodeValue="no";
		    iframe.setAttributeNode(att);

		    var att = document.createAttribute("allowtransparency");
		    att.nodeValue="true";
		    iframe.setAttributeNode(att);

		    var att = document.createAttribute("frameborder");
		    att.nodeValue="0";
		    iframe.setAttributeNode(att);

		    var att = document.createAttribute("marginwidth");
		    att.nodeValue="0";
		    iframe.setAttributeNode(att);

		    var att = document.createAttribute("marginheight");
		    att.nodeValue="0";
		    iframe.setAttributeNode(att);

		    var att = document.createAttribute("vspace");
		    att.nodeValue="0";
		    iframe.setAttributeNode(att);

		    var att = document.createAttribute("hspace");
		    att.nodeValue="0";
		    iframe.setAttributeNode(att);

		    adArea.appendChild(iframe);
		    if (document.all) {
		        var iframedoc = iframe.contentWindow.document;
		    } else {
		        var iframedoc = iframe.contentDocument;
		    }
		    var html ='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
		    html+= '<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">';
		    html+= '<head>';
		    html+= '<meta http-equiv="Content-Type" content="text/html; charset=EUC-JP" />';
		    html+= '<meta http-equiv="Content-Style-Type" content="text/css" />';
		    html+= '<title>JUGEM AD</title>';
		    html+= '<body>'+ ads + css +'</body>';
		    iframedoc.open();
		    iframedoc.write(html);
		    iframedoc.close();
            adArea.style.display = 'block';
        } else {
            adArea.style.display = 'none';
        }
    }
};
    showAdsBody = function(adData, startIndex, endIndex,border,bg,title,text,url) {

	    if(border=="")    border    = '000';
	    if(bg=="")        bg        = 'FFF';
	    if(title=="")     title     = '1E90FF';
	    if(text=="")      text      = '000000';
	    if(url=="")       url       = '009900';
		var css='';
	    css+='<style type="text/css">' + "\n";
	    css+='<!--' + "\n";
	    css+='* {' + "\n";
	    css+='    margin:0;' + "\n";
	    css+='    padding:0;' + "\n";
	    css+='}' + "\n";
	    css+='' + "\n";
	    css+='body {' + "\n";
	    css+='        font-family: "ＭＳ ゴシック", "Osaka‐等幅";' + "\n";
	    css+='        font-size:11px;' + "\n";
	    css+='        }' + "\n";
	    css+='' + "\n";
	    css+='#jugem_feedme_ad {' + "\n";
	    css+='        overflow:hidden;' + "\n";
	    css+='        width:100%;' + "\n";
	    css+='        height:200px;' + "\n";
	    css+='        border:0px solid #' + border + ';/* 枠線 */' + "\n";
	    css+='        background:#' + bg + ';/* 背景 */' + "\n";
	    css+='    }' + "\n";
	    css+='a,' + "\n";
	    css+='a:link,' + "\n";
	    css+='a:visited' + "\n";
	    css+=' {' + "\n";
	    css+='    color:#' + title + ';/* タイトル */' + "\n";
	    css+='    text-decoration: none;' + "\n";
	    css+='}' + "\n";
	    css+='' + "\n";
	    css+='a:hover,' + "\n";
	    css+='a:active {' + "\n";
	    css+='    text-decoration:underline;' + "\n";
	    css+='    }' + "\n";
	    css+='' + "\n";
	    css+='.jugem_blog_ad_box {' + "\n";
	    css+='        margin:3px 0 ;' + "\n";
	    css+='        padding:2px 3px;' + "\n";
	    css+='    }' + "\n";
	    css+='' + "\n";
	    css+='.jugem_blog_ad_box dt { ' + "\n";
	    css+='        font-size:14px;' + "\n";
	    css+='        font-weight:bolder;' + "\n";
	    css+='        line-height:200%;' + "\n";
	    css+='}' + "\n";
	    css+='' + "\n";
	    css+='.jugem_blog_ad_box dt.ad_title2 { ' + "\n";
	    css+='        padding:5px 0 0 0;' + "\n";
	    css+='}' + "\n";
	    css+='' + "\n";
	    css+='.jugem_blog_ad_box dd { ' + "\n";
	    css+='        text-align:left;' + "\n";
	    css+='        margin:0 0 2px;' + "\n";
	    css+='        font-size:12px;' + "\n";
	    css+='        line-height:130%;' + "\n";
	    css+='        color:#' + text + ';/* テキスト */' + "\n";
	    css+='}' + "\n";
	    css+='.jugem_blog_ad_box dt span.url {' + "\n";
	    css+='        font-size: 11px;' + "\n";
	    css+='        margin:0 0 0 10px;' + "\n";
	    css+='        color:#' + url + ';/* URL */' + "\n";
	    css+='        font-family:arial;' + "\n";
	    css+='}' + "\n";
	    css+='' + "\n";
	    css+='.jugem_blog_ad_by {' + "\n";
	    css+='    margin-top:5px;' + "\n";
	    css+='    font-size:8px;' + "\n";
	    css+='    text-align:right;' + "\n";
	    css+='    line-height:100%;' + "\n";
	    css+='    font-family: verdana;' + "\n";
	    css+='' + "\n";
	    css+='}' + "\n";
	    css+='' + "\n";
	    css+='ul {list-style-type:circle;}' + "\n";
	    css+='' + "\n";
	    css+='ul.jugem_blog_ad_2 {' + "\n";
	    css+='    border-top:1px solid #' + border + ';/* 枠線 */' + "\n";
	    css+='    margin:5px 0 0 0;' + "\n";
	    css+='    padding:5px 0 0 0 ;' + "\n";
	    css+='    font-size:10px;' + "\n";
	    css+='    }' + "\n";
	    css+='' + "\n";
	    css+='.jugem_blog_ad_2 li {' + "\n";
	    css+='        margin:0 0 3px 0;' + "\n";
	    css+='        padding:0;' + "\n";
	    css+='        line-height:100%;' + "\n";
	    css+='        }' + "\n";
	    css+='' + "\n";
	    css+='.gmo_bnr {' + "\n";
	    css+='            border-left:1px solid #' + border + ';/* 枠線 */' + "\n";
	    css+='            border-right:1px solid #' + border + ';/* 枠線 */' + "\n";
	    css+='            border-bottom:1px solid #' + border + ';/* 枠線 */' + "\n";
	    css+='        }';
	    css+='-->' + "\n";
	    css+='</style>' + "\n";

        var ads = '', i = startIndex, limit = Math.min(adData.length, endIndex);
	    ads += '<div id="jugem_feedme_ad">';
    	ads += '<div class="jugem_blog_ad_box">';
        while (i < limit) {
            var descr    = adData[i++];
            var unused1  = adData[i++];
            var clickURL = pointurl + escape(adData[i++]);
            var title    = adData[i++];
            var sitehost = adData[i++];
            var unused2  = adData[i++];
			ads += "<dl onclick=\"window.open('" + clickURL + "');\" onmouseout=\"status='';return true\" onmouseover=\"status='';return true;\" style=\"cursor: pointer;\">";
			ads += "<dt>";
			ads += "<a href=\"#feedme1\" onclick=\"return false;\" name=\"feedme1\">" + title + "</a>";
			ads += "<span class=\"url\">"+ sitehost + "</span>";
			ads += "</dt>";
			ads += "<dd>"+ descr +"</dd>";
			ads += "</dl>";
        }
	    ads += '<div class="jugem_blog_ad_by">インタレストマッチ<br /><a target="_blank" href="http://ov.yahoo.co.jp/service/int/index.html?o=IM0021">広告の掲載について</a></div></div></div>';
        var adArea = document.getElementById('grokBodyAdContainer');
        if (adArea) {
            if (ads != '') {
			    var iframe = document.createElement("iframe");

			    //iframe setting
			    var att = document.createAttribute("width");
			    att.nodeValue="100%";
			    iframe.setAttributeNode(att);

			    var att = document.createAttribute("height");
			    att.nodeValue="250";
			    iframe.setAttributeNode(att);

			    var att = document.createAttribute("scrolling");
			    att.nodeValue="no";
			    iframe.setAttributeNode(att);

			    var att = document.createAttribute("allowtransparency");
			    att.nodeValue="true";
			    iframe.setAttributeNode(att);

			    var att = document.createAttribute("frameborder");
			    att.nodeValue="0";
			    iframe.setAttributeNode(att);

			    var att = document.createAttribute("marginwidth");
			    att.nodeValue="0";
			    iframe.setAttributeNode(att);

			    var att = document.createAttribute("marginheight");
			    att.nodeValue="0";
			    iframe.setAttributeNode(att);

			    var att = document.createAttribute("vspace");
			    att.nodeValue="0";
			    iframe.setAttributeNode(att);

			    var att = document.createAttribute("hspace");
			    att.nodeValue="0";
			    iframe.setAttributeNode(att);

			    adArea.appendChild(iframe);
			    if (document.all) {
			        var iframedoc = iframe.contentWindow.document;
			    } else {
			        var iframedoc = iframe.contentDocument;
			    }
//                adArea.innerHTML = ads;
			    var html ='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
			    html+= '<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">';
			    html+= '<head>';
			    html+= '<meta http-equiv="Content-Type" content="text/html; charset=EUC-JP" />';
			    html+= '<meta http-equiv="Content-Style-Type" content="text/css" />';
			    html+= '<title>JUGEM AD</title>';
			    html+= '<body>'+ ads + css +'</body>';
			    iframedoc.open();
			    iframedoc.write(html);
			    iframedoc.close();
                adArea.style.display = 'block';
            } else {
                adArea.style.display = 'none';
            }
        }
    };
    showAdsImage = function() {
	    var adArea = document.getElementById('grokSideAdContainer');
		var html='<iframe name="jugem_ad" src="http://imaging.jugem.jp/ad/ad_banner.html" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" frameborder="0" height="230" scrolling="no" width="125"></iframe>';
        if (adArea) {
			adArea.innerHTML=html;
            adArea.style.display = 'block';
        } else {
            adArea.style.display = 'none';
        }

    };
	showAdsEntry = function(adData, startIndex, endIndex) {
	    if(border=="")    border    = '000';
	    if(bg=="")        bg        = 'FFF';
	    if(title=="")     title     = '1E90FF';
	    if(text=="")      text      = '000000';
	    if(url=="")       url       = '009900';
		var css='';

        var ads = '', i = startIndex, limit = Math.min(adData.length, endIndex);
        while (i < limit) {
            var descr    = adData[i++];
            var unused1  = adData[i++];
            var clickURL = pointurl + escape(adData[i++]);
            var title    = adData[i++];
            var sitehost = adData[i++];
            var unused2  = adData[i++];
		ads +="<div style=\"margin: 2px 0pt 10px; cursor: pointer;\" onmouseover=\"bgcolor=this.style.backgroundColor;this.style.backgroundColor='#CDCDCD';status='';return true;\" onmouseout=\"this.style.backgroundColor=bgcolor;status='';return true\" onclick=\"window.open('" + clickURL + "');\">";
		ads +="	<div>";
		ads +="		<a href=\"#feedme1\" style=\"text-decoration: none;\" name=\"feedme1\" onclick=\"return false;\">";
		ads +="			<span style=\"font-size: 14px; font-weight: bold;line-height: 200%;\">" + title + "</span>";
		ads +="		</a>";
		ads +="		<span style=\"margin-left: 10px;font-size: 11px;color:#009900\">" + sitehost + "</span>";
		ads +="	</div>";
		ads +="	<span style=\"font-size: 12px; font-weight: normal;\">" + descr + "</span>";
		ads +="</div>";
        }
	    ads += '<div class="jugem_blog_ad_by" style="text-align:right">インタレストマッチ- <a target="_blank" href="http://ov.yahoo.co.jp/service/int/index.html?o=IM0021">広告の掲載について</a></div>';
        var adArea = document.getElementById('grokEntryAdContainer');
        if (adArea) {
            if (ads != '') {
                adArea.innerHTML = ads + css;
                adArea.style.display = 'block';
            } else {
                adArea.style.display = 'none';
            }
        }

    };
    showFoo = function() {
    };
//alert(this.logicControllString);
    if (typeof(this.logicControllString) == 'undefined') {
        return this.loadAdTimeOut();
    } else if (this.logicControllString == 'all' 
			|| this.logicControllString == 'all-search') {
		//entry
		var entryStartIndex = 6;
        var entryEndIndex = entryStartIndex + (6 * this.entryAdMax);
		showAdsEntry(this.myAd?this.myAd:adData, entryStartIndex, entryEndIndex);

		//sidebar
        var sideStartIndex = this.myAd?6:entryEndIndex;
        var sideEndIndex = sideStartIndex + (6 * this.sideAdMax);
        showAdsSideBar(adData, sideStartIndex, sideEndIndex,border,bg,title,text,url);


    } else if (this.logicControllString == 'all-inactive' 
			|| this.logicControllString == 'all-inactive-search'
			|| this.logicControllString == 'sidebar-inactive-search'
	) {
		//entry
		var entryStartIndex = 6;
        var entryEndIndex = entryStartIndex + (6 * this.entryAdMax);
		showAdsEntry(this.myAd?this.myAd:adData, entryStartIndex, entryEndIndex);

		//inactive
        var bodyStartIndex = this.myAd?6:entryEndIndex;
        var bodyEndIndex = bodyStartIndex + (6 * this.bodyAdMax);
        showAdsBody(adData, bodyStartIndex, bodyEndIndex,border,bg,title,text,url);

		//sidebar
        var sideStartIndex = bodyEndIndex;
        var sideEndIndex = sideStartIndex + (6 * this.sideAdMax);
        showAdsSideBar(adData, sideStartIndex, sideEndIndex,border,bg,title,text,url);


    } else if (this.logicControllString == 'sidebar') {
		//sidebar
        var sideStartIndex = 6;
        var sideEndIndex = sideStartIndex + (6 * this.sideAdMax);
        showAdsSideBar(adData, sideStartIndex, sideEndIndex,border,bg,title,text,url);


    } else if (this.logicControllString == 'sidebar-inactive') {
		//inactive
        var bodyStartIndex = 6;
        var bodyEndIndex = bodyStartIndex + (6 * this.bodyAdMax);
        showAdsBody(adData, bodyStartIndex, bodyEndIndex,border,bg,title,text,url);

		//sidebar
        var sideStartIndex = bodyEndIndex;
        var sideEndIndex = sideStartIndex + (6 * this.sideAdMax);
        showAdsSideBar(adData, sideStartIndex, sideEndIndex,border,bg,title,text,url);


    } else if (this.logicControllString == 'sidebar-search') {
		//entry
		var entryStartIndex = 6;
        var entryEndIndex = entryStartIndex + (6 * this.entryAdMax);
		showAdsEntry(this.myAd?this.myAd:adData, entryStartIndex, entryEndIndex);

		//sidebar
        var sideStartIndex = this.myAd?6:entryEndIndex;
        var sideEndIndex = sideStartIndex + (6 * this.sideAdMax);
        showAdsSideBar(adData, sideStartIndex, sideEndIndex,border,bg,title,text,url);

    } else if (this.logicControllString == 'entry'
			|| this.logicControllString == 'entry-search'
	) {
		//entry
		var entryStartIndex = 6;
        var entryEndIndex = entryStartIndex + (6 * this.entryAdMax);
		showAdsEntry(this.myAd?this.myAd:adData, entryStartIndex, entryEndIndex);


    } else if (this.logicControllString == 'entry-inactive'
			|| this.logicControllString == 'entry-inactive-search'
	) {
		//entry
		var entryStartIndex = 6;
        var entryEndIndex = entryStartIndex + (6 * this.entryAdMax);
		showAdsEntry(this.myAd?this.myAd:adData, entryStartIndex, entryEndIndex);

		//inactive
        var bodyStartIndex = this.myAd?6:entryEndIndex;
        var bodyEndIndex = bodyStartIndex + (6 * this.bodyAdMax);
        showAdsBody(adData, bodyStartIndex, bodyEndIndex,border,bg,title,text,url);


    } else if (this.logicControllString == 'image') {
		//image
        showAdsImage();

    } else if (this.logicControllString == 'image-inactive') {
		//inactive
        var bodyStartIndex = 6;
        var bodyEndIndex = bodyStartIndex + (6 * this.bodyAdMax);
        showAdsBody(adData, bodyStartIndex, bodyEndIndex,border,bg,title,text,url);

		//image
        showAdsImage();
    } else if (this.logicControllString == 'image-search') {
		//entry
		var entryStartIndex = 6;
        var entryEndIndex = entryStartIndex + (6 * this.entryAdMax);
		showAdsEntry(this.myAd?this.myAd:adData, entryStartIndex, entryEndIndex);

		//image
        showAdsImage();

    } else if (this.logicControllString == 'image-inactive-search') {
		//entry
		var entryStartIndex = 6;
        var entryEndIndex = entryStartIndex + (6 * this.entryAdMax);
		showAdsEntry(this.myAd?this.myAd:adData, entryStartIndex, entryEndIndex);

		//inactive
        var bodyStartIndex = this.myAd?6:entryEndIndex;
        var bodyEndIndex = bodyStartIndex + (6 * this.bodyAdMax);
        showAdsBody(adData, bodyStartIndex, bodyEndIndex,border,bg,title,text,url);

		//image
        showAdsImage();


	} else {
		return 0;
	}
};
Grok.Custom.prototype.ngAd = function() {
    this.setStatus('ngAd');
    this.grokData = undefined;
    this.switchAd();
};
Grok.Custom.prototype.loadAdDataTimeOut = function() {
    this.setStatus('loadAdDataTimeOut');
    var bodyAdArea = document.getElementById('grokBodyAdContainer');
    if (bodyAdArea) {
        bodyAdArea.style.display = 'none';
    }
    var sideAdArea = document.getElementById('grokSideAdContainer');
    if (sideAdArea) {
        sideAdArea.style.display = 'none';
    }
};
