var numTimesCalled = 0;
var first_ad_unit = '', second_ad_unit = '', third_ad_unit = '', extra_ad_unit = '';
var pageads = new Array();
var pageads_max = 2; // num ads per section
var pageads_index = 0; // index of ad section
var pageads_count = 0; // num ads in the current section

function google_ad_request_done(google_ads) {

	if (google_ads.length > 0) {
		// set up variables
		
		
		// increment
		numTimesCalled++;
		if (location.search.indexOf('testing') >= 0) {
			alert("numTimesCalled="+numTimesCalled+" and num googleads="+google_ads.length);		
		}
		// for within-content area ads
		if (numTimesCalled == 1 && page_ad_ids != undefined) {
		
			// remove #2, 3, 5, 7, 9, must be done in reverse
			page_ad_ids.splice(8, 1);
			page_ad_ids.splice(6, 1);
			page_ad_ids.splice(4, 1);
			page_ad_ids.splice(3, 1);
			page_ad_ids.splice(2, 1);
			page_ad_ids.splice(0, 1);
			
			for (var k = 0; k < page_ad_ids.length; k++) {
				pageads[k] = '';
			}
			
			for (var i=0; i<6; i++) {
				if (google_ads[i] != undefined) {
					// page ads
					if (pageads_count == pageads_max) {
						if (pageads_count > 0) {
							pageads[pageads_index] += '</div>';
							pageads_index++;
							pageads_count = 0;
						}
					}
					if (pageads_count == 0) {
						pageads[pageads_index] += '<div class="followdotted" style="padding-bottom: 16px;"><p style="margin-top: 0; margin-bottom: 5px;"><a class="googleadlink" href=\"' + google_info.feedback_url + '\">Ads by Google</a></p>';
					}
					pageads_count++;
					pageads[pageads_index] += '<p class="googlepageads"><a class="header" href="' + google_ads[i].url + '">' + google_ads[i].line1 + '</a><br />' + google_ads[i].line2 + ' ' + google_ads[i].line3 + '<br /><a href="' + google_ads[i].url + '">' + google_ads[i].visible_url + '</a></p>';
				}
			}
			if (pageads != undefined) {
				for (var j = 0; j < pageads.length; j++) {
					var thisAd = document.getElementById(page_ad_ids[j]);
					if (thisAd != undefined && pageads[j] != undefined) {
						thisAd.innerHTML += pageads[j];
					}
				}
			}
		}
		
		// for text links ad unit: (text links under {ARCHIVES})
		/*if (numTimesCalled == 2) {
			third_ad_unit += '<div class="sectiontitle" style="margin-top:0;">';
			third_ad_unit += '{Ads By Google}';
			third_ad_unit += '</div>';
			for (var i=6; i<=10; i++) {
				third_ad_unit += '<a href="' + google_ads[i].url + '">' + google_ads[i].line1 + '</a><br />';
			}
			document.getElementById("third_ad_unit").innerHTML += third_ad_unit;
		}*/
		
		// for first column ad unit
		if (numTimesCalled == 2) {
			for (var i=6; i<12; i++) {
				if (google_ads[i] != undefined) {
					first_ad_unit += '<p class="googleads"><a class="header" href="' + google_ads[i].url + '">' + google_ads[i].line1 + '</a><br />' + google_ads[i].line2 + ' ' + google_ads[i].line3 + '<br /><a href="' + google_ads[i].url + '">';
					if (google_ads[i].visible_url.length > 25) {
						first_ad_unit += google_ads[i].visible_url.substring(0,28) + '<br />' + google_ads[i].visible_url.substring(28);
					} else {
						first_ad_unit += google_ads[i].visible_url;
					}
					first_ad_unit += '</a></p>';
				}
			}
			document.getElementById("first_ad_unit").innerHTML += first_ad_unit;
		}
		
		// for third ad unit 
		if (numTimesCalled == 3) {
			for (var i=12; i<18; i++) {
				if (google_ads[i] != undefined) {
					second_ad_unit += '<p class="googleads"><a class="header" href="' + google_ads[i].url + '">';
					if (google_ads[i].line1.length > 19) {
						second_ad_unit += google_ads[i].line1.substring(0,19) + '<br />' + google_ads[i].line1.substring(19);
					} else {
						second_ad_unit += google_ads[i].line1;
					}
					second_ad_unit += '</a><br />' + google_ads[i].line2 + ' ' + google_ads[i].line3;
					second_ad_unit += '<br /><a href="' + google_ads[i].url + '">';
					if (google_ads[i].visible_url.length > 19) {
						second_ad_unit += google_ads[i].visible_url.substring(0,19) + '<br />' + google_ads[i].visible_url.substring(19);
					} else {
						second_ad_unit += google_ads[i].visible_url;
					}
					second_ad_unit += '</a></p>';
				}
			}
			document.getElementById("second_ad_unit").innerHTML += second_ad_unit;
		}
		
		// in case of extra ad unit (on pages without glam ads)
		if (numTimesCalled == 5 && document.getElementById("extra_ad_unit") != undefined) {
			for (var i=20; i<google_ads.length; i++) {
				extra_ad_unit += '<p class="googleads"><a class="header" href="' + google_ads[i].url + '">' + google_ads[i].line1 + '</a><br />' + google_ads[i].line2 + ' ' + google_ads[i].line3 + '<br /><a href="' + google_ads[i].url + '">' + google_ads[i].visible_url + '</a></p>';
			}
			document.getElementById("extra_ad_unit").innerHTML += extra_ad_unit;
		}
					
		
		/*
		for(var i = 0; i < google_ads.length; ++i) {
			if (i==0) {
				
				if (google_info.feedback_url) {
					first_ad_unit += '<br /><a class="googleadlink" href=\"' + google_info.feedback_url + '\">Ads by Google</a><br />';
				} else {
					first_ad_unit += 'Ads By Google<br />';
				}
				
			}
			
			if (i == 4) {
				if (google_info.feedback_url) {
					second_ad_unit += '<br /><a class="googleadlink" href=\"' + google_info.feedback_url + '\">Ads by Google</a><br />';
				} else {
					second_ad_unit += 'Ads By Google<br />';
				}
			}
			if (i == 9) {
				third_ad_unit += '<div class="sectiontitle" style="margin-top: 0;">';
				third_ad_unit += '{Ads By Google}';
				third_ad_unit += '</div>';
			}
			
			if (i < 4) {
				first_ad_unit += '<p class="googleads"><a class="header" href="' + google_ads[i].url + '">' + google_ads[i].line1 + '</a><br />' + google_ads[i].line2 + ' ' + google_ads[i].line3 + '<br /><a href="' + google_ads[i].url + '">' + google_ads[i].visible_url + '</a></p>';
			} else {
				if (i < 9) {
					second_ad_unit += '<p class="googleads"><a class="header" href="' + google_ads[i].url + '">' + google_ads[i].line1 + '</a><br />' + google_ads[i].line2 + ' ' + google_ads[i].line3 + '<br /><a href="' + google_ads[i].url + '">' + google_ads[i].visible_url + '</a></p>';
				} else {
					if (i < 14) {
						third_ad_unit += '<a href="' + google_ads[i].url + '">' + google_ads[i].line1 + '</a><br />';
					} else {
						if (document.getElementById("extra_ad_unit") != undefined && i < 19) {
							extra_ad_unit += '<p class="googleads"><a class="header" href="' + google_ads[i].url + '">' + google_ads[i].line1 + '</a><br />' + google_ads[i].line2 + ' ' + google_ads[i].line3 + '<br /><a href="' + google_ads[i].url + '">' + google_ads[i].visible_url + '</a></p>';
							
						} else {
							// page ads
							if (pageads_count == pageads_max) {
								if (pageads_count > 0) {
									pageads[pageads_index] += '</div>';
									pageads_index++;
									pageads_count = 0;
								}
							}
							if (pageads_count == 0) {
								pageads[pageads_index] += '<div class="followdotted" style="padding-bottom: 16px;"><p style="margin-top: 0; margin-bottom: 5px;"><a class="googleadlink" href=\"' + google_info.feedback_url + '\">Ads by Google</a></p>';
							}
							pageads_count++;
							pageads[pageads_index] += '<p class="googlepageads"><a class="header" href="' + google_ads[i].url + '">' + google_ads[i].line1 + '</a><br />' + google_ads[i].line2 + ' ' + google_ads[i].line3 + '<br /><a href="' + google_ads[i].url + '">' + google_ads[i].visible_url + '</a></p>';
						}
					}					
				}
			}
		}
		*/
	}
}