function google_ad_request_done(google_ads) 
{
	/*
	* This function is required and is used to display
	* the ads that are returned from the JavaScript
	* request. You should modify the document.write
	* commands so that the HTML they write out fits
	* with your desired ad layout.
	*/
	var s = '';
	var i;
	/*
	* Verify that there are actually ads to display.
	*/
	if (google_ads.length == 0)
	return;
	/*
	* If an image or Flash ad is returned, display that ad.
	* If a rich media ad is returned, display that as "as is."
	* Otherwise, build a string containing all of the ads and
	* then use a document.write() command to print that string.
	*/
	if (google_ads[0].type == "image") {
		s += '<a href="' + google_ads[0].url +
		'" target="_blank" title="go to ' + google_ads[0].visible_url +
		'"><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 == "flash") {
		s += '<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 == "html") {
			s += google_ads[0].snippet;
	}
	else if (google_ads[0].type == "text") {
		// make sure there is a div so the width can be determined, and a link so the color can be determined
		document.write('<div id="_google_temp_div"><a id="_google_temp_a"></a></div>');
		var _temp	= $("#_google_temp_div");
		
		var link,
			containerClass	= '',
			columnClass		= '',
			link			= '',
			attrib			= 'Ads by Google',
			ad_width		= _temp.width(),
			width			= ad_width > 500 ? Math.floor(100 / google_ads.length) : 100,	/* use 500 because IE includes margins when calculating width */
			bgcolor			= $('#container').css('background-color'),
			textcolor		= $('#container').css('color'),
			linkcolor		= $('#_google_temp_a').css('color'),
			singleLine		= false,
			is_300x250		= false,
			adLink			= '';
			
		_temp.remove();	// remove temp tag
		
		if (ColorCheck.closelyMatches(bgcolor, textcolor)) {
			textcolor	= ColorCheck.invert(textcolor);
		} else {
			textcolor	= ColorCheck.hex(textcolor);
		}
		
		if (ColorCheck.closelyMatches(bgcolor, linkcolor)) {
			linkcolor	= ColorCheck.invert(linkcolor);
		} else {
			linkcolor	= ColorCheck.hex(linkcolor);
		}
		
		if (ad_width > 500) { 
			containerClass = 'wide'; 
		} else { 
			containerClass = 'narrow textAd300'; 
			is_300x250 = true; 
			singleLine = true;
		}
		
		if (google_ads.length === 1) { columnClass = 'single'; }
		
		adLink += '<p class="attribution ' + columnClass + '" style="color:#' + textcolor + '">';
		if (google_info && google_info.feedback_url) {
			adLink += '<a href="' + google_info.feedback_url + '" target="_blank"><font color="#' + linkcolor + '">' + attrib + '</font></a>';
		} else {
			adLink += attrib;
		}
		adLink += '</p>';
		
		s += '<div class="cfx ' + containerClass + '">';
		if (is_300x250) { s += adLink; }
		for(i=0; i < google_ads.length; ++i) {
			link	= 'style="color: #' + linkcolor + '" href="' + google_ads[i].url + '" ' + 'onmouseout="window.status=\'\'" ' + 'onmouseover="window.status=\'go to ' + google_ads[i].visible_url + '\';return true;" target="_blank"';

			s += '<div onclick="window.open(\'' + google_ads[i].url + '\', \'ad\'); return false;" style="width:' + width + '%;" class="column cfx ' + columnClass + '">' + 
				'<p style="color:#' + textcolor + '" class="adTitle"><strong><a ' + link + '><font color="#' + linkcolor + '">' + google_ads[i].line1 + '</font></a></strong></p>';
				if (singleLine) {
					s += '<p class="adText">' + google_ads[i].line2 + ' ' + google_ads[i].line3 + ' ' + '<a ' + link + '><font color="#' + linkcolor + '">' + google_ads[i].visible_url + '</font></a></p>';
				} else {
					s += '<p>' + google_ads[i].line2 + '<br>' + google_ads[i].line3 + '</p>';
					s += '<p class="adLink"><em><a ' + link + '><font color="#' + linkcolor + '">' + google_ads[i].visible_url + '</font></a></em></p>';
				}
			s += '</div>';
		}
		if (!is_300x250) { s += '<br>' + adLink; }
		s += '</div>';
	}
	document.write(s);
	return;
}

var ColorCheck = {
		
	closelyMatches : function(color1, color2)
	{
		color1	= this.cssToRgb(color1);
		color2	= this.cssToRgb(color2);
		
		if (!color1 || !color2) { return false; }
		
		if (color1.r == color2.r && color1.g == color2.g && color1.b == color2.b) { return true; }

		var rDiff	= Math.abs(color1.r - color2.r);
		var gDiff	= Math.abs(color1.g - color2.g);
		var bDiff	= Math.abs(color2.b - color2.b);
		
		var result = 100 - Math.round((rDiff + gDiff + bDiff) * 100 / (3*255));
		
		return result > 80;
	},
	
	
	cssToRgb : function(color)
	{
		if (!color) { return false; }

		var r, g, b, rgb	= {};
		
		if (color.indexOf('rgb') != -1) {
			arr	= color.replace(/rgb\(|\)| /g, "").split(",");
			rgb.r	= arr[0];
			rgb.g	= arr[1];
			rgb.b	= arr[2];
		} else {
			color	= color.replace(/#/, '');
			if (color.length == 3) {
				r	= color.charAt(0);
				g	= color.charAt(1);
				b	= color.charAt(2);
				
				rgb.r	= r + '' + r;
				rgb.g	= g + '' + g;
				rgb.b	= b + '' + b;
			} else {
				rgb.r	= color.substring(0, 2);
				rgb.g	= color.substring(2, 4);
				rgb.b	= color.substring(4, 6);
			}
			
			rgb.r	= parseInt(rgb.r, 16);
			rgb.g	= parseInt(rgb.g, 16);
			rgb.b	= parseInt(rgb.b, 16);
		}
		
		return rgb;
	},
	
	
	
	hex : function(color)
	{
		if (!color) { return false; }
		
		var rgb, r, g, b, hex;
		
		if (color.indexOf('rgb') != -1) {
			rgb	= color.replace(/rgb\(|\)| /g, "").split(",");
			
			r	= parseInt(rgb[0], 10).toString(16);
			g	= parseInt(rgb[1], 10).toString(16);
			b	= parseInt(rgb[2], 10).toString(16);

			if (r.length < 2) r	= '0' + r;
			if (g.length < 2) g = '0' + g;
			if (b.length < 2) b = '0' + b;
			
			hex	= r + '' + g + '' + b;
		} else {
			color	= color.replace(/#/, '');
			if (color.length == 3) {
				r	= color.charAt(0);
				g	= color.charAt(1);
				b	= color.charAt(2);
				
				hex	= r + '' + r + '' + g + '' + g + '' + b + '' + b;
			} else if (color.length == 6) {
				hex = color;
			} else {
				hex = false;
			}
		}
		
		return hex;
	},
	
	
	
	hexToDec : function(hex)
	{
		return hex.match(/[0-9a-fA-F]+/) ? parseInt(hex, 16) : -1;
	},
	
	
	
	getComponent : function(color, hex)
	{
		var component = '';
		
		switch (color) {
			case 'r' :
				component = hex.substring(0, 2);
				break;
			case 'g' :
				component = hex.substring(2, 4);
				break;
			case 'b' :
				component = hex.substring(4, 6);
				break;
		}
		
		return component;
	},
	
	
	invert : function(color)
	{
		var r, g, b;
		
		color	= this.cssToRgb(color);
		
		r	= parseInt(color.r, 10);
		g	= parseInt(color.g, 10);
		b	= parseInt(color.b, 10);
		
		if (r <= 127) r += 125; else r = r;
		if (g <= 127) g += 125; else g = 130 - (255 - g);
		if (b <= 127) b += 125; else b = 130 - (255 - b);
		
		return this.hex('rgb(' + r + ',' + g + ',' + b + ')');
	}
}
