/************************************************************
 * @name	:	Multi AdSense For Contents
 * @author	:	Nifty Corporation (Daichi Morifuji <morifuji.daichi@nifty.co.jp>)
 * @version	:	0.1.5
 * @date	:	2007/11/20	create this format file
 *		:	2007/11/27	modify doms order
 *		:	2007/12/06	add switching logic of ad number in default dom
 *		:	2007/12/12	select ad type (text, badge) by class
 *		:	2007/12/12	and more packaging: service managers only modify
 *		:			class MAFC and add MAFC div into MAFC.doms
 *		:	2008/01/30	remove anchor tag from explaning text
 *		:	2008/02/08	modify class structure
 *		:	2008/03/04	treat with the trouble which the mafc object is not found in google_ad_request_done
 * @abstract	:	Use this script for insertion AdSense
 *			into multi DOM elements.
 ************************************************************/

/************************************************************
 * MODIFY ADSENSE PARAMTERES FOR YOUR SITE
 */
google_ad_client = 'ca-nifty_js';
google_ad_output = 'js';
google_max_num_ads = 3;
google_language = 'ja';
google_encoding = 'utf-8';
google_safe = 'medium';
google_ad_channel = 'podcast';
google_adtest = 'off';
/**
 *
 ************************************************************/

/************************************************************
 * MODIFY ONLY THIS AREA
 */
var MAFC = function(){this.initialize.apply(this, arguments)};
MAFC.prototype = {
    initialize : function(){
	this.google_max_num_ads = 0;
	this.ad_num_default = 3;
	this.ad_num_default_case_mafc = 3;
	this.ad_num_class_mafc = 2;
	this.ad_num_id_mafc = 2;
	this.logo = '<p class="ad_logo"><span>Ads by Google</span></p>';
	this.doms = new Array();
	
 	this.use_hints = false;
// 	this.use_hints = document.title;	//sample

	this.addClassMAFC();
	this.addIdMAFC();
	this.doms.push({dom:'default', num: (this.ad_num_default = (this.doms.length > 0 ? this.ad_num_default_case_mafc : this.ad_num_default))});
	
	for(var i in this.doms) this.google_max_num_ads += this.doms[i].num;
	google_max_num_ads = (this.google_max_num_ads < 20) ? this.google_max_num_ads: 20;

	if(this.use_hints) this.addHints(this.use_hints);

    },
    addClassMAFC : function(){
	var divs = document.getElementsByTagName('div');
	//MODIFY AD NUMBER IN EACH divs.MAFC
	for(var i = 0, l = divs.length; i < l; i++){
	    if(divs[i].className && divs[i].className.match(/MAFC/)){
		var num = (divs[i].className.match(/mafc_num(\d+)/)) ? divs[i].className.match(/mafc_num(\d+)/)[1] : this.ad_num_class_mafc;
		this.doms.push({dom:divs[i], num:num});
	    }
	}
    },
    addIdMAFC : function(){
	for(var i = 0; i < 10; i++){
	    var mafc = document.getElementById('MAFC'+i);
	    if(mafc){
		var num = (mafc.className.match(/mafc_num(\d+)/)) ? mafc.className.match(/mafc_num(\d+)/)[1] : this.ad_num_id_mafc;
		this.doms.push({dom:mafc, num:num});
	    }
	}
    },
    /************************************************************
     * customize hints setting
     */
    addHints : function(elm){
	var hints = elm;
	if(hints && hints.match(/:?(.+)$/))
	    hints = hints.match(/:?([^:]+)$/)[1];
 	if(window.google_hints)
 	    window.google_hints += hints;
 	else
 	    window.google_hints = hints;
    },
    /************************************************************
     * design this area in case badge type
     */
    createBadge : function(ads){
	var str = '<p>Ads by Google</p><ul>';
	for(var i = 0, l = ads.length; i < l; i++){
	    str += '<li><em><a target="_blank" class="ad_title" href="' + ads[i].url + '">' + ads[i].line1 + '</a> </em>'
		+ '<span><a target="_blank" class="ad_url" href="' + ads[i].url + '">' + ads[i].visible_url + '</a></span><br />'
		+ '<a target="_blank" class="ad_line" href="' + ads[i].url + '">' + ads[i].line2 + ads[i].line3 + '</a></li>';
	}
	return str + '</ul>';
    },
    /************************************************************
     * design this area in case listing type
     */
    createText : function(ads){
	var str = '<dl>';
	for(var i = 0, l = ads.length; i < l; i++){
	    str += '<dt><a target="_blank" class="ad_title" href="' + ads[i].url + '">' + ads[i].line1 + '</a> '
		+ '<span><a target="_blank" class="ad_url" href="' + ads[i].url+ '">' + ads[i].visible_url + '</a></span></dt>'
		+ '<dd><a class="ad_line" href="' + ads[i].url+ '">' + ads[i].line2 + ads[i].line3 + '</a></dd>';
	}
	return str+'</dl>';
    }
};
var mafc = new MAFC();
document.write('<script type="text/javascript" language="JavaScript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>');

/************************************************************
 * callback method from google adsense			*/
function google_ad_request_done() {
    if (google_ads.length > 0) {
	var t = 0;
	for(var i = 0, l = mafc.doms.length; i < l; i++){
	    var ads_div = document.createElement('div');
	    ads_div.className = 'ads';
	    var str = mafc.logo;
	    var ads = new Array();
	    
	    for(var j = 0; t < google_ads.length && j < mafc.doms[i].num; j++, t++){
		if(mafc.doms[i].dom != 'default' && t > (google_ads.length - mafc.ad_num_default - 1))	break;
		ads.push(google_ads[t]);
	    }
	    
	    str += ((mafc.doms[i].dom.className && mafc.doms[i].dom.className.match(/badge/)) ? mafc.createBadge(ads) : mafc.createText(ads));
	    
	    ads_div.innerHTML = str;
	    if(mafc.doms[i].dom == 'default'){
		document.write('<div class="ads default">'+str+'</div>');
	    }else if(ads.length > 0){
		mafc.doms[i].dom.insertBefore(ads_div, null);
	    }
	}
    }
}
