function google_ad_request_done(google_ads) {
var s = '';
var i;
if (google_ads.length == 0) {
	collapse_empty_divs();
	adsense_error_log('No Adsense ads returned');
	return;
}

if (google_ads[0].type == "flash") {
	curdiv = document.getElementById('ad10');
	
	curdiv.innerHTML = '<a href=\"' +
	google_info.feedback_url + '\" style="color:000000">Ads by Google</a><br>' +
	'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' +
	' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" WIDTH="' +
	google_ad.image_width + '" HEIGHT="' +
	google_ad.image_height + '"> <PARAM NAME="movie" VALUE="' +
	google_ad.image_url + '">' +
	'<PARAM NAME="quality" VALUE="high">' +
	'<PARAM NAME="AllowScriptAccess" VALUE="never">' +
	'<EMBED src="' +
	google_ad.image_url + '" WIDTH="' +
	google_ad.image_width + '" HEIGHT="' +
	google_ad.image_height +
	'" TYPE="application/x-shockwave-flash"' +
	' AllowScriptAccess="never" ' +
	' PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT>';

} else if (google_ads[0].type == "image") {
	curdiv = document.getElementById('ad10');
	
	curdiv.innerHTML = '<a href=\"' +
	google_info.feedback_url + '\" style="color:000000">Ads by Google</a><br> <a href="' +
	google_ads[0].url + '" target="_top" title="go to ' +
	google_ads[0].visible_url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' +
	google_ads[0].visible_url + '\';return true"><img border="0" src="' +
	google_ads[0].image_url + '"width="' +
	google_ads[0].image_width + '"height="' +
	google_ads[0].image_height + '"></a>';
		
} else if (google_ads[0].type == "html") {
	curdiv = document.getElementById('ad10');
	
	curdiv.innerHTML = '<a href=\"' +
	google_info.feedback_url + '\" style="color:000000">Ads by Google</a><br>' + google_ads[0].snippet;

} else {
	if (google_ads.length == 1) {
		for(ad_id in adsense_ads) {
			if(ad_id == 'ad13') {
				var ad_count=0;
				var i=0;
				adsense_ad=adsense_ads[ad_id];
				adhtml = '<p class="list_google_title" adsense_ads="'+adsense_ad.num_ads+'" google_ads_length="'+google_ads.length+'"><a href=\"' + google_info.feedback_url + '\" class=\"google_title\" style=\"padding-left:5px;\">Ads by Google</a></p>';
				for(; i < google_ads.length && ad_count < adsense_ad.num_ads; ++i,++ad_count) {
					adhtml += '<div class="list_ad'+i+'">' +
						'<div class="adsenseblock">' +
						'<p style="margin-bottom:5px;"><a class="title" href=\"' + 
						google_ads[i].url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' + 
						google_ads[i].visible_url + '\';return true"><b>' + 
						google_ads[i].line1 + '</b></a></p> <div class="text">' + 
						google_ads[i].line2 + ' ' + 
						google_ads[i].line3 + ' <a class="url" href="' + 
						google_ads[i].url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' + 
						google_ads[i].visible_url + '\';return true">' + 
						google_ads[i].visible_url + '</a>' +
						'</div></div>' +
						'<div class="clearLeft" style="height:1px;"></div>' +
						'</div>';
				}
				if(adhtml)
					$('div.'+ad_id).html(adhtml);
			}else{
				/*console.log(ad_id);*/
				adsense_ad=adsense_ads[ad_id];
				curdiv = document.getElementById(ad_id);
				if (!curdiv) {
					collapse_empty_divs();
					adsense_error_log('No div ad01 returned');
					return;
				}
				curdiv.innerHTML += '<a href=\"' +
					google_info.feedback_url + '\" class=\"google_title\">Ads by Google</a> <li><div><a class="one_adtitle" href=\"' +
					google_ads[0].url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' +
					google_ads[0].visible_url + '\';return true"><b>' +
					google_ads[0].line1 + '</b></a> <div class="one_adtext">' +
					google_ads[0].line2 + adsense_ad.line2br +
					google_ads[0].line3 + ' <a class="one_adurl" href="' +
					google_ads[0].url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' +
					google_ads[0].visible_url + '\';return true">' +
					google_ads[0].visible_url + '</a></div></div></li>';
			}
		}

	} else if (google_ads.length > 1) {
		function adsense_header (divid) {
			var div=document.getElementById(divid);
			if (!div) return null;
			div.innerHTML = '<a href=\"' + google_info.feedback_url + '\" class=\"google_title\">Ads by Google</a>'
			return div;
		}
		var curdiv;
		var i=0;
		for(ad_id in adsense_ads) {
			var ad_count=0;
			if(ad_id == 'ad13') {
				adsense_ad=adsense_ads[ad_id];
				adhtml = '<p class="list_google_title" adsense_ads="'+adsense_ad.num_ads+'" google_ads_length="'+google_ads.length+'"><a href=\"' + google_info.feedback_url + '\" class=\"google_title\" style=\"padding-left:5px;\">Ads by Google</a></p>';
				for(; i < google_ads.length && ad_count < adsense_ad.num_ads; ++i,++ad_count) {
					adhtml += '<div class="list_ad'+i+'">' +
						'<div class="adsenseblock">' +
						'<p style="margin-bottom:5px;"><a class="title" href=\"' + 
						google_ads[i].url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' + 
						google_ads[i].visible_url + '\';return true"><b>' + 
						google_ads[i].line1 + '</b></a></p> <div class="text">' + 
						google_ads[i].line2 + ' ' + 
						google_ads[i].line3 + ' <a class="url" href="' + 
						google_ads[i].url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' + 
						google_ads[i].visible_url + '\';return true">' + 
						google_ads[i].visible_url + '</a>' +
						'</div></div>' +
						'<div class="clearLeft" style="height:1px;"></div>' +
						'</div>';
				}
				if(adhtml)
					$('div.'+ad_id).html(adhtml);
			}else{
				adsense_ad=adsense_ads[ad_id];
				curdiv = adsense_header(ad_id);	
				if (!curdiv) {continue;}
				for(; i < google_ads.length && ad_count < adsense_ad.num_ads; ++i,++ad_count) {
					curdiv.innerHTML += '<li><div><a class="title" href=\"' +
						google_ads[i].url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' +
						google_ads[i].visible_url + '\';return true"><b>' +
						google_ads[i].line1 + '</b></a> <div class="text">' +
						google_ads[i].line2 + adsense_ad.line2br +
						google_ads[i].line3 + ' <a class="url" href="' +
						google_ads[i].url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' +
						google_ads[i].visible_url + '\';return true">' +
						google_ads[i].visible_url + '</a></div></div></li>';
				
					if(ad_count == adsense_ad.num_ads-1)
						curdiv.innerHTML += adsense_ad.close;
				}
			}
		}
	}
}	
	$(document).ready(function(){
		msg = collapse_empty_divs();
		if(msg) {
			adsense_error_log(msg);
		}
	});
}

function adsense_error_log(msg) {
	$(document).ready(function(){
		page = location.href;
		referrer = document.referrer;
		$.post('/error_logger.php',
			{ type: 'adsense', msg: msg, page: page, referrer: referrer });
	});
}

function collapse_empty_divs() {
	$(document).ready(function() {
		var msg = "";
		$('.adsenseblock ul').each(function (a) {
			html = $(this).html();
			if(html == "") {
				msg+= $(this).attr('id')+' has no ads, ';
				$(this).parent().parent().parent().hide();
			}
		});
		return msg;
	});
}
