// Create the mw.ads.google package (if needed).
mw				= typeof(mw) != "undefined" ? mw : {};
mw.ads			= typeof(mw.ads) != "undefined" ? mw.ads : {};
mw.ads.google	= typeof(mw.ads.google) != "undefined" ? mw.ads.google : {};

// Defining the mw.ads.google.afc package for the first time.
if ( typeof(mw.ads.google.afc) == "undefined" )
	{	// Fill in the details.
		mw.ads.google.afc =
			{	// An array of listeners.
				afc_listeners: new Array(),
			
				// Constructor for the default AFCAdRenderer object class.
				AFCAdRenderer: function ()
					{	// This method renders an ad.
						this.renderAd = function (afc_ad, index, total)
							{	// Create a leaderboard.
								if ( afc_ad.google_ad_unit == "leaderboard" )
									{
									} // if ( afc_ad.google_ad_unit == "leaderboard" )

								// Return an image ad.
								else if ( afc_ad.type == "image" ) 
									{	// Get the ad values.
										var visible_url		= afc_ad.visible_url;
										var ad_url			= afc_ad.url;
										var image_url		= afc_ad.image_url;
										var image_width		= Math.max(0, afc_ad.image_width);
										var feedback_url	= google_info.feedback_url;

										// Create the ad content.
										var content = 
											'<div class="creative image google">' + 
												'<div>' + 
													'<a href="' + ad_url + '" target="_top" title="go to ' + visible_url + '" onmouseover="javascript:window.status=\'Go to ' + visible_url + '\'; return true;" onmouseout="javascript:window.status=\'\'; return true;">' + 
														'<img border="0" src="' + image_url + '"  />' +
													'</a>' + 
												'</div>' + 
												'<table class="ads_by_google" border="0" cellpadding="2" cellspacing="0" style="width: ' + image_width + 'px;">' +
													'<tr>' +
														'<td style="text-align: left;"><a href="' + ad_url + '" onmouseover="javascript:window.status=\'Go to ' + visible_url + '\'; return true;" onmouseout="javascript:window.status=\'\'; return true;">Go to ' + visible_url + '</a></td>' +
														'<td style="text-align: right;"><a href="' + feedback_url + '">Feedback - Ads by Google</a></td>' + 
													'</tr>' +
												'</table>' 
											'</div>';

										return content;
									} // else if ( afc_ad.type == "image" ) 

								// Return a Flash video ad.
								else if ( afc_ad.type == "flash" ) 
									{	// Construct the ad content.
										var content =
											'<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="' + afc_ad.image_url + '" />' + 
												'<param name="quality" value="high" />' + 
												'<param name="AllowScriptAccess" value="never" />' + 
												'<param name="google_hints" value="' + escape(token) + '" />' + 
												'<embed ' +
													'src="' + afc_ad.image_url + '" ' + 
													'width="' + afc_ad.image_width + '" ' +
													'height="' + afc_ad.image_height + '" ' + 
													'type="application/x-shockwave-flash" ' + 
													'AllowScriptAccess="never" ' + 
													'pluginspace="http://www.macromedia.com/go/getflashplayer">' + 
												'</embed>' + 
											'</object>';

										// Add the feedback link since this ad isn't using the google player.
										try 
											{	if ( afc_ad.image_url.indexOf('googleadplayer.swf') == -1 )
													{	// Get the ad values.
														var visible_url		= afc_ad.visible_url;
														var ad_url			= afc_ad.url;
														var image_url		= afc_ad.image_url;
														var image_width		= Math.max(0, afc_ad.image_width);
														var feedback_url	= google_info.feedback_url;

														// Construct the ad content.
														content += 
															'<table class="ads_by_google" border="0" cellpadding="2" cellspacing="0" style="width: ' + image_width + 'px;">' +
																'<tr>' +
																	'<td style="text-align: left;">' +
																		'<a ' + 
																			'href="' + ad_url + '" ' + 
																			'onmouseover="javascript:window.status=\'Go to ' + visible_url + '\'; return true;" ' + 
																			'onmouseout="javascript:window.status=\'\'; return true;">' +
																			'Go to ' + visible_url + 
																		'</a>' + 
																	'</td>' +
																	'<td style="text-align: right;"><a href="' + feedback_url + '">Feedback - Ads by Google</a></td>' + 
																'</tr>' +
															'</table>';
													}; // if ( afc_ad.image_url.indexOf('googleadplayer.swf') == -1 )
											} // try

										catch (e)
											{
											}; // catch (e)

										return content;
									} // else if ( afc_ad.type == "flash" ) 

								else
									{	// The ad content.
										var content = index > 0 ? '' :  '<div class="header" style="background: #FFFFFF; padding: 3px 5px 0 0; color: #999999; float: right;">Ads by Google</div>';

										// Append the text link.
										content += 
											'<div class=\"googleAFCTextAd' + ( index == (total - 1) ? " googleAFCTextAdLast" : "" ) + '\">' + 
												'<a href="' + afc_ad.url + '" ' + 
													'onmouseout="javascript:window.status=\'\';return true;" ' +
													'onmouseover="javascript:window.status=\'' + afc_ad.visible_url + '\';return true;" target="_blank" ' +
													'>' +
													'<span class="line1"><strong>' + afc_ad.line1 + '</strong><br /></span>' +
													'<span class="line2">' + afc_ad.line2 + '&nbsp;' + afc_ad.line3 + '<br /></span>' + 
													'<span class="visible_url">' + afc_ad.visible_url + '</span>' +
												'</a>' + 
											'</div>';

										return content;
									}; // else
							}; // function renderAd (afc_ad, index, total)

						return this;
					}, // class AFCAdRenderer

				// This method adds an AFC listener.
				addListener: function (listener)
					{	this.afc_listeners[this.afc_listeners.length] = listener;
					}, // function addListener (listener)

				// This method handles all AFC load events.
				onAFCAdsLoaded: function (afc_ads, afc_ad_id)
					{	// Add the ad units parameter.
						for ( var index = 0; index < afc_ads.length; index++ )
							{	afc_ads[index].google_ad_unit = google_ad_unit;
							}; // for ( var index = 0; index < afc_ads.length; index++ )

						// Notify all the listeners.
						for ( var index = 0; index < this.afc_listeners.length; index++ ) 
							{	this.afc_listeners[index].onAFCAdsLoaded(afc_ads, afc_ad_id);
							}; // for ( var index = 0; index < this.afc_listeners.length; index++ ) 

					} // function onAFCAdsLoaded (afs_ads, afs_ad_id)		

			}; // mw.ads.google.afs
	}; // if ( typeof(mw.ads.google.afc) == "undefined" )

// Defining the mw.ads.google.afs package for the first time.
if ( typeof(mw.ads.google.afs) == "undefined" )
	{	// Fill in the details.
		mw.ads.google.afs =
			{	// An array of listeners.
				afs_listeners: new Array(),
			
				// Constructor for the default AFSAdRenderer object class.
				AFSAdRenderer: function ()
					{	// This method renders an ad.
						this.renderAd = function (afs_ad, index, total)
							{	// Create a text ad.
								if ( afs_ad.type && (afs_ad.type == "text/wide" || afs_ad.type == "text/narrow") )
									{	return this.renderTextAd(afs_ad, index, total);
									} // if ( afs_ad.type && (afs_ad.type == "text/wide" || afs_ad.type == "text/narrow") )

								// Invalid ad type.
								else
									{	return "";
									}; // else
							}; // function renderAd (afs_ad, index, total)

						// This method returns a text ad.
						this.renderTextAd = function (afs_ad, index, total)
							{	// Render the content.
								var content =
									'<div class="googleAFSTextAd">' +
										'<div class="afs_' + ( afs_ad.type == "text/wide" ? "wide" : "narrow" ) + '">' + 
											'<div class="afs_headline">' +
												'<a onmouseover="javascript:window.status=\'' + 
													afs_ad.visible_url + '\';return true;" ' + 
													'onmouseout="javascript:window.status=\'\';return true;" ' +
													'href="' + afs_ad.url + '" target="_blank">' + afs_ad.line1 + 
												'</a>' +
											'</div>' + 
											'<div class="afs_text">' + 
												'<a onmouseover="javascript:window.status=\'' + 
													afs_ad.visible_url + '\';return true;" ' +
													'onmouseout="javascript:window.status=\'\';return true;" ' +
													'href="' + afs_ad.url + '" target="_blank">' + 
													afs_ad.line2 + 
												'</a>' + 
											'</div>' + 
											'<div class="afs_url">' + 
												'<a onmouseover="javascript:window.status=\'' + 
													afs_ad.visible_url + '\';return true;" ' +
													'onmouseout="javascript:window.status=\'\';return true;" ' +
													'href="' + afs_ad.url + '" target="_blank">' +
													afs_ad.visible_url + 
												'</a>' + 
											'</div>' +
										'</div>' +
									'</div>';

								return content;
							}; // function renderTextAd (afs_ad, index, total)

						return this;
					}, // class AFSAdRenderer

				// This method adds an AFS listener.
				addListener: function (listener)
					{	this.afs_listeners[this.afs_listeners.length] = listener;
					}, // function addListener (listener)

				// This method handles all AFS load events.
				onAFSAdsLoaded: function (afs_ads, afs_ad_id)
					{	// Notify all the listeners.
						for ( var index = 0; index < this.afs_listeners.length; index++ ) 
							{	this.afs_listeners[index].onAFSAdsLoaded(afs_ads, afs_ad_id);
							}; // for ( var index = 0; index < this.afs_listeners.length; index++ ) 

					} // function onAFSAdsLoaded (afs_ads, afs_ad_id)		

			}; // mw.ads.google.afs
	}; // if ( typeof(mw.ads.google.afs) == "undefined" )

// Register an AFC listener.
mw.ads.google.afc.addListener
	(	{	onAFCAdsLoaded: function (ads, ad_id) 
				{	// Instantiate the renderer.
					var renderer = new mw.ads.google.afc.AFCAdRenderer();

					// The rendered ad content.
					var ad_content = new Array();

					// Render the ads.
					for ( var index = 0; index < ads.length; index++ )
						{	ad_content[index]	= renderer.renderAd(ads[index], index, ads.length);
						}; // for ( var index = 0; index < ads.length; index++ )	

					// Get the ad container.
					var ad_container = document.getElementById(ad_id);

					// Place the ads.
					if ( ad_container )
						{	for ( var index = 0; index < ad_content.length; index++ )
								{	ad_container.innerHTML += ad_content[index];
								}; // for ( var index = 0; index < ad_content.length; index++ )
						}; // if ( ad_container )
				} // function onAFCAdsLoaded (ads)
		}
	);

// Register an AFS listener.
mw.ads.google.afs.addListener
	(	{	onAFSAdsLoaded: function (ads, ad_id) 
				{	// Load the AFC text ads.
					if ( ads.length < 1 )
						{	// Set up the AFC text ads to display.
							google_ads				= new Array();
							google_ad				= new Object();
							google_info				= new Object();			
							google_ad_client		= 'ca-merriam-webster-site_js';		
							google_safe				= 'high';
							google_feedback			= "on";
							google_image_size		= '300x250';
							google_max_num_ads		= '5';
							google_ad_unit			= 'rect';
							google_ad_output		= 'js';
							google_ad_type			= "text";
							google_ads[0]			= google_ad;   
							google_ad_id			= "marketing_links";
							google_hints			= token;
							google_targeting		= 'site';

							// Add it to the document.
							document.write('<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>');
						} // if ( ads.length < 1 )

					// Display the predefined marketing ads.
					else if ( ads.length >= 1 )
						{	// Get the element to place the links in.
							var marketing_links = document.getElementById("marketing_links");

							// Initialize it.
							marketing_links.innerHTML = 
							
							'<div id="mrkting_rt" onclick="document.location=\'/word/subscribe.htm\'; return false;" style="cursor: pointer;">' +
							'<h1>Feed Your Head</h1>' +
							'<p>Sign up for <strong>Word of the Day</strong> and receive nourishing words and tasty language facts delivered free directly to your e-mail.</p>' +
							'<div align="right"><a href="/word/subscribe.htm">Subscribe today</a></div>' +
							'</div>'+
							'<div id="mrkting_rt" onclick="document.location=\'http://visual.merriam-webster.com\'; return false;" style="cursor: pointer;">' +
							'<h1>See the Difference</h1>' +
							'<p>Explore your world in exciting new ways with Merriam-Webster\'s <strong>Visual Dictionary Online</strong>.</p>' +
							'</div>';
							
						}; // else if ( ads.length >= 1 )

					// Nothing to do.
					if ( ads.length <= 0 ) 
						{	return;
						}; // if ( ads.length <= 0 ) 

					// Instantiate the renderer.
					var renderer = new mw.ads.google.afs.AFSAdRenderer();

					// The ad content.
					var ad_content = new Array();

					// Generate all the ad content.
					for ( var index = 0; index < ads.length; index++ )
						{	// Render the ad content.
							ad_content[index]	= renderer.renderAd(ads[index], index, ads.length);
						}; // for ( var index = 0; index < ads.length; index++ )

					// Get the results ad div.
					var results_box_ad = ad_id == "google_creative_4" ? document.getElementById("results_ad") : false;

					// Create the results ad.
					if ( results_box_ad )
						{	results_box_ad.innerHTML = 
								'<div class="googleAFSAds">' + 
									'<div class="ad_header">Ads by Google</div>' + 
									ad_content[0] + 
								'</div>' + 
								results_box_ad.innerHTML;
						}; // if ( results_box_ad )

					// Get the place holder for the entry ads.
					var ad_container = document.getElementById(ad_id);

					if ( ad_container )
						{	// The remaining ad content.
							var content = "";

							// Create the remaining ad content.
							for ( var index = ad_id == "google_creative_4" ? 1 : 0; index < ad_content.length; index++ )
								{	content += ad_content[index];
								}; // for ( var index = ad_id == "google_creative_4" ? 1 : 0; index < ad_content.length; index++ )

							// Set the ad content.
							ad_container.innerHTML	= 
									( content != "" ?
										'<div class="googleAFSAds">' + 
											'<div class="ad_header">Ads by Google</div>' + 
											content +
										'</div>'
										:
										'' 
									) + 
									ad_container.innerHTML;
						}; // if ( ad_container )
				} // function onAFSAdsLoaded (ads)
		}
	);

// This function handles all AFC load events.
function google_ad_request_done (afc_ads) 
	{	// Forward off to our ASF package.
		mw.ads.google.afc.onAFCAdsLoaded(afc_ads, google_ad_id);
	}; // function google_ad_request_done (afc_ads) 

// This function handles all AFS load events.
function google_afs_request_done (afs_ads) 
	{	// Forward off to our ASF package.
		mw.ads.google.afs.onAFSAdsLoaded(afs_ads, google_ad_id);
	}; // function google_afs_request_done (afs_ads) 


var google_ad_unit		= ""; //Note: The name of an ad unit ("rect" or "leaderboard")
var sRect				= ""; //Content of rect unit 
var sLead				= ""; //Content of leaderboard unit 
var creative_1_loaded	= false;
var creative_2_loaded	= false;
var creative_3_loaded	= false;
var creative_4_loaded	= false;


function google_write_ad (google_ads)
	{	// Get the number of ads returned.
		var google_num_ads = google_ads.length;

		// No ads returned.
	    if ( google_num_ads <= 0 ) 
			return "";

		// Return an image advertisement.
		if ( google_ads[0].type == "image" ) 
			{	// Get the ad values.
				var visible_url		= google_ads[0].visible_url;
				var ad_url			= google_ads[0].url;
				var image_url		= google_ads[0].image_url;
				var image_width		= Math.max(0, google_ads[0].image_width);
				var feedback_url	= google_info.feedback_url;

				// Construct the ad.
				var xhtml = 
					'<div class="creative image google">' + 
						'<div>' + 
							'<a href="' + ad_url + '" target="_top" title="go to ' + visible_url + '" onmouseover="javascript:window.status=\'Go to ' + visible_url + '\'; return true;" onmouseout="javascript:window.status=\'\'; return true;">' + 
								'<img border="0" src="' + image_url + '"  />' +
							'</a>' + 
						'</div>' + 
						'<table class="ads_by_google" border="0" cellpadding="2" cellspacing="0" style="width: ' + image_width + 'px;">' +
							'<tr>' +
								'<td style="text-align: left;"><a href="' + ad_url + '" onmouseover="javascript:window.status=\'Go to ' + visible_url + '\'; return true;" onmouseout="javascript:window.status=\'\'; return true;">Go to ' + visible_url + '</a></td>' +
								'<td style="text-align: right;"><a href="' + feedback_url + '">Feedback - Ads by Google</a></td>' + 
							'</tr>' +
						'</table>' 
					'</div>';

				return xhtml;
			} // if ( google_ads[0].type == "image" ) 

		// Return a Flash video advertisement.
		else if ( google_ads[0].type == "flash" ) 
			{  var xhtml =
					'<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" />' + 
						'<param name="google_hints" value="' + escape(token) + '" />' + 
						'<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" pluginspace="http://www.macromedia.com/go/getflashplayer"></embed>' + 
					'</object>';

				try 
					{	if ( google_ad.image_url.indexOf('googleadplayer.swf') == -1 )
							{	var visible_url		= google_ads[0].visible_url;
								var ad_url			= google_ads[0].url;
								var image_url		= google_ads[0].image_url;
								var image_width		= Math.max(0, google_ads[0].image_width);
								var feedback_url	= google_info.feedback_url;
								xhtml += 
									'<table class="ads_by_google" border="0" cellpadding="2" cellspacing="0" style="width: ' + image_width + 'px;">' +
										'<tr>' +
											'<td style="text-align: left;"><a href="' + ad_url + '" onmouseover="javascript:window.status=\'Go to ' + visible_url + '\'; return true;" onmouseout="javascript:window.status=\'\'; return true;">Go to ' + visible_url + '</a></td>' +
											'<td style="text-align: right;"><a href="' + feedback_url + '">Feedback - Ads by Google</a></td>' + 
										'</tr>' +
									'</table>';
									/**
									'<table class="ads_by_google" border="0" cellpadding="2" cellspacing="0" style="width: ' + google_ads[0].image_width + 'px;">' + 
										'<tr>' +
											'<td style="text-align: left;"><a href="' + google_ads[0].url + '" onmouseover="javascript: window.status=\'Go to ' + google_ads[0].visible_url + '\'; return true;" onmouseout="javascript:window.status=\'\'; return true;">Go to ' + google_ads[0].visible_url + '</a></td>' + 
											'<td style="text-align: right;">Ads by Google</td>' + 
										'</tr>' +
									'</table>'; 
									**/
							}; // if ( google_ad.image_url.indexOf('googleadplayer.swf') == -1 )
					} // try

				catch (e)
					{
					}; // catch (e)

				return xhtml;
			} // else if ( google_ads[0].type == "flash" ) 

		// Return a leaderboard advertisement.
		else if ( google_ad_unit == "leaderboard" ) 
			{	var xhtml = 
					'<table width="728" height="90" cellpadding="0" cellspacing="0" border="0"><tr><td valign="middle" align="center">' +
					 '<a href="' + google_ads[0].url + '" ' +
                     'onmouseout="window.status=\'\'" ' +
                     'onmouseover="window.status=\'go to ' +
                     google_ads[0].visible_url + '\'" ' +
                     'style="text-decoration:none">' +
                     '<span style="text-decoration:underline;font-size:14pt">' +
                     '<b>' + google_ads[0].line1 + '</b><br></span>' +
                     '<span style="color:#000000;font-size:11pt">' +
                     google_ads[0].line2 + '&nbsp;' +
                     google_ads[0].line3 + '<br></span>' +
                     '<span style="text-decoration:underline;color:#000099;font-size:12pt">' +
                     google_ads[0].visible_url + '</span></a><br>' +
					 '</td></tr><tr><td bgcolor="336699" align="right">' +
	  				'<span style="font-size:10px;color:FFFFFF;font-family: verdana, tahoma, helvetica, sans-serif;">Ads by Google&nbsp;</span>'	+
					'</td></tr></table>';

				return xhtml;
			} // else if ( google_ad_unit == "leaderboard" ) 

		// Return text link advertisements.
		else 
			{	var xhtml = '<div class="header">Ads by Google</div>';

				// Append each text ad.
				for ( var i=0; i < google_ads.length; ++i ) 
					{	xhtml += 
							'<br />' + 
							'<a href="' + google_ads[i].url + '" ' + 
								'onmouseout="window.status=\'\'" ' +
								'onmouseover="window.status=\'go to ' + google_ads[i].visible_url + '\'" ' +
								'style="text-decoration:none">' +
								'<span style="text-decoration:underline;"><strong>' + google_ads[i].line1 + '</strong><br /></span>' +
								'<span style="color: #000000;" class="descr">' + google_ads[i].line2 + '&nbsp;' + google_ads[i].line3 + '<br /></span>' + 
								'<span style="text-decoration:underline;">' + google_ads[i].visible_url + '</span>' +
							'</a><br />';
					}; // for ( var i=0; i < google_ads.length; ++i ) 

				return xhtml;
			}; // else
	}; // function google_write_ad (google_ads)

