// 
// This will get all of the ads from google
// and then populate our ad fields with the data.
// 
// Each block will look like this
// 
// <div class="ad_block">
//    <div class="ads_by">Ads by Google</div>
//    <div class="ad">
//       <div class="line1"></div>
//       <div class="line2"></div>
//       <div class="line3"></div>
//       <div class="url"></div>
//    </div>
//    <div class="ad">
//       <div class="line1"></div>
//       <div class="line2"></div>
//       <div class="line3"></div>
//       <div class="url"></div>
//    </div>
//    <div class="ad">
//       <div class="line1"></div>
//       <div class="line2"></div>
//       <div class="line3"></div>
//       <div class="url"></div>
//    </div>
// </div>
//

var block_index = 0;
var google_adnum = 0;

function google_radlink_request_done(radlinks) {
	var current_index = block_index;
	block_index++;

	// Special code if the side bar isn't being displayed
	// and this is the last ad block, then forcefully show the category
	// 
	// This will prevent the categories from not being show
	// if there are no ads returned by google. 

	if(block_index == ad_arr.length) {
		var side_bar = document.getElementById("side_bar_content");
		if(side_bar) { 
			side_bar.style.visibility = "visible";
		}
	}

	if(radlinks.length <= 0) { 
		return; 
	}
	
	var ad_block = document.getElementById("googlead_" + (ad_arr[current_index].unique_id));

	// Add in the "Related Ads"
	var ads_by = document.createElement("span");
	ads_by.className = "ads_by";
	ads_by.innerHTML = "Related Ads";
	ad_block.appendChild(ads_by);

	var radlink_results = radlinks[0].line1;
	for(i=1; i<radlinks.length; i++) {
		radlink_results += ","+radlinks[i].line1;
	}

	// keep track of the index
	var ad_index = 0;
	for(i=0; i<radlinks.length; i++) {
		var ad = document.createElement("span");

		var line1 = radlinks[ad_index].line1;
		var term = radlinks[ad_index].term;
		var url_escaped_term = radlinks[ad_index].url_escaped_term;

		ad.innerHTML += '<a href="/pageads?term='+url_escaped_term+'&channel='+ad_arr[current_index].channel+'&category='+ad_arr[current_index].category_title+'&links='+radlink_results+'" class="rad" onmouseover="window.status=\''+term+'\'; return true;" onmouseout="window.status=\'\'; return true;">'+line1+'</a>';

		// Add the current set of ads to the current AD Block
		ad_block.appendChild(ad);
		ad_index++;
	}

	// Once the radlinks are displayed, then displayed the category results below.
	// this is to prevent that wierd bouncing effect
	// when the google ads pop in after the page has been drawn out.
	var side_bar = document.getElementById("side_bar_content");
	if(side_bar) { 
		side_bar.style.visibility = "visible";
	}

	return;
}

function google_ad_request_done(google_ads) {
	// Verify that there are actually ads to display.
    if (google_ads.length == 0) { return; }

	var ad_block = document.getElementById("googlead_" + (ad_arr[block_index].unique_id));

	// keep track of the index
	var ad_index = 0;

	if (google_ads[0].type == "image") {
		ad_block.className = ad_block.className.trim() + "_image";
		ad_block.style.width = google_ads[0].image_width + "px";

		var ad = document.createElement("div");
		ad.className = "ad";

		ad.innerHTML += '<a href="'+google_ads[0].url+
						'" title="go to '+google_ads[0].visible_url+
						'" target="_blank" >'+
						'<img src="'+google_ads[0].image_url+
						'" width="'+google_ads[0].image_width+
						'" height="'+google_ads[0].image_height+
						'" border="0" /></a>';

		// Add the current set of ads to the current AD Block
		ad_block.appendChild(ad);
		
	} else if (google_ads[0].type == "flash") {
		ad_block.className = ad_block.className.trim() + "_flash";
		ad_block.style.width = google_ads[0].image_width + "px";

		var ad = document.createElement("div");
		ad.className = "ad";

		ad.innerHTML += '<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_ads[0].image_width +
						'" HEIGHT="' + google_ads[0].image_height + '">' +
						'<PARAM NAME="movie" VALUE="' + google_ads[0].image_url + '">' +
						'<PARAM NAME="quality" VALUE="high">' +
						'<PARAM NAME="AllowScriptAccess" VALUE="never">' +
						'<EMBED src="' + google_ads[0].image_url +
						'" WIDTH="' + google_ads[0].image_width +
						'" HEIGHT="' + google_ads[0].image_height +
						'" TYPE="application/x-shockwave-flash"' +
						' AllowScriptAccess="never" ' +
						' PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT>';

		// Add the current set of ads to the current AD Block
		ad_block.appendChild(ad);
		
	} else if (google_ads[0].type == "text") {
		
		ad_block.className = ad_block.className.trim() + " " + ad_block.className.trim()+"_"+google_ads.length;

		// Add in the "ads by google"
		var ads_by = document.createElement("div");
		ads_by.className = "ads_by";
		ads_by.innerHTML = "Ads by Google";
		ad_block.appendChild(ads_by);

		for(i=0; i<google_ads.length; i++) {
			var ad = document.createElement("div");
			ad.className = "ad";
			if(ad_block.className.trim().split(" ")[1] == "question_list_ad") { 
				ad.innerHTML += add_line(google_ads[ad_index].line1, google_ads[ad_index].url, google_ads[ad_index].visible_url, "line1");
				ad.innerHTML += add_line(google_ads[ad_index].visible_url, google_ads[ad_index].url, google_ads[ad_index].visible_url, "url");		
				ad.innerHTML += add_line(google_ads[ad_index].line2 + " " + google_ads[ad_index].line3, google_ads[ad_index].url, google_ads[ad_index].visible_url, "line2");
			} else {
				ad.innerHTML += add_line(google_ads[ad_index].line1, google_ads[ad_index].url, google_ads[ad_index].visible_url, "line1");
				ad.innerHTML += add_line(google_ads[ad_index].line2 + " " + google_ads[ad_index].line3, google_ads[ad_index].url, google_ads[ad_index].visible_url, "line2");
				ad.innerHTML += add_line(google_ads[ad_index].visible_url, google_ads[ad_index].url, google_ads[ad_index].visible_url, "url");		
			}

			// Add the current set of ads to the current AD Block
			ad_block.appendChild(ad);
			ad_index++;
		}
		google_adnum = google_adnum + google_ads.length;
    }

	return;
}

function add_line(line, url, visible_url, class_name) {
	var str = "";
	str += '<div class="'+class_name+'">' +
		      '<a href="'+url+'" onmouseover="window.status=\''+visible_url+'\'; return true;" onmouseout="window.status=\'\'; return true;" target="_blank" >' +
			  line + 
		      '</a>' + 
		   '</div>';
	return(str);
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };