










var gatherUrlBase = 'http://ads.gather.com/serve';
var gatherUrl = gatherUrlBase;
var tmpDate = new Date();
var ipStr = '38.107.191.95';
var gatherPageId = null; //Only generate this once per page


function pageIDGen() 
{
	if (gatherPageId == null)
		gatherPageId = tmpDate.getTime() + ipStr + (((1+Math.random())*0x10000)|0).toString(16);
    return gatherPageId;
}

function gather_append_param( name, value ) {
    if (value != null ) {
       if (gatherUrl.indexOf('?') != -1) {
		   window.gatherUrl = gatherUrl + '&' + name + '=' + escape(value);
	   } else {
	       window.gatherUrl = gatherUrl + '?' + name + '=' + escape(value);
	   }
	}
}

function gather_get_meta_keywords() {
   var metaKeywords = '';
   var metaNodes = document.getElementsByTagName('meta');
   if ( metaNodes != null ) {
	   for (var i=0; i<metaNodes.length; i++)  {
	      var metaName = metaNodes[i].getAttribute('name');
	      var metaContent = metaNodes[i].getAttribute('content');
	      if ( metaName == 'keywords' ) {
	         metaKeywords = metaContent;         
	      }   
	   }
   }
   return metaKeywords;
}


// !! PERFORMANCE TESTING !!
// USE BELOW DUMMY FUNCTION AND RENAME THE REAL show_gather_ad() FUNCTION
function _show_gather_ad() {
// dummy function; do nothing
}


function show_gather_ad() {

   gather_append_param('gatherPageId', pageIDGen());
   gather_append_param('gatherPublisherId', window.gatherPublisherId);
   gather_append_param('gatherAdFormat', window.gatherAdFormat);
   gather_append_param('gatherIsAdult', window.gatherIsAdult);
   //gather_append_param('gatherMetaKeywords', gather_get_meta_keywords() );
   
   if (window.gatherAdType != null) {
	   gather_append_param('gatherAdType', window.gatherAdType);
   } else {
       gather_append_param('gatherAdType', 'TEXT');
   }
   
   if (window.gatherResponseType != null) {
       gather_append_param('gatherResponseType', window.gatherResponseType);
   } else {
       gather_append_param('gatherResponseType', 'html');
   }
   
   if (window.gatherGroupNamespace != null) {
       gather_append_param('gatherGroupNamespace', window.gatherGroupNamespace);
   }
   
   if (window.gatherAdPosition != null) {
       gather_append_param('gatherAdPosition', window.gatherAdPosition);
   } else {
       gather_append_param('gatherAdPosition', '1');
   }
   
   // append the full URL of the requesting page
   gather_append_param('gatherPageUrl', document.location );

	if (window.gatherAdType == null || window.gatherAdType == 'TEXT') {
		gather_append_param('gatherTableBorderColor', window.gatherTableBorderColor);
   		gather_append_param('gatherTableBackgroundColor', window.gatherTableBackgroundColor);
   		gather_append_param('gatherFontColor', window.gatherFontColor);
   		gather_append_param('gatherLinkColor', window.gatherLinkColor);
   		gather_append_param('gatherDisplayLinkColor', window.gatherDisplayLinkColor);
   		gather_append_param('gatherTrailerBackground', window.gatherTrailerBackground );
   		gather_append_param('gatherTrailerFontColor', window.gatherTrailerFontColor);
   		gather_append_param('gatherRestrictCategory', window.gatherRestrictCategory);
    }
    
   // Append a random number to the request URL to ensure the full URL
   // is unique.  This will deter proxy caching.
   gather_append_param('gatherRandom', new Date().getTime() ); 

   if ( window.gatherKeywordTags != null ) {
      gatherKeywordTags = gatherKeywordTags.substring(0,254);
      gather_append_param('gatherKeywordTags', gatherKeywordTags);
   }

   // It is likely there are many more data points to append to the
   // URL.  These can include browser variables, timezone, etc.  These
   // additional user attributes may be useful for analysis and
   // possible further optimizations.  These may be added later.
   
   var numImpressions = 0;
   	if (window.gatherAdType == null || window.gatherAdType == 'TEXT') {
   		// Default the number of impressions to 4, if not specified.
   		if (window.gatherNumImpressions == 0 || window.gatherNumImpressions == null ) {
   			if (window.gatherPublisherId == null || window.gatherPublisherId == 0) {
   				numImpressions = 8;
   			} else {
      			numImpressions = 4;
      		}
   		} else {
	  		numImpressions = window.gatherNumImpressions;
   		}
   	}
 
   // Size of the iframe
   
   // Defaults... (variable-skyscraper)
   var gatherAdHeight =  numImpressions * 150;   
   var gatherAdWidth  =  120; 

   if ( window.gatherAdFormat == 'variable-skyscraper-wide' ) {
	   if (window.gatherNumImpressions == 0 || window.gatherNumImpressions == null ) {
	      	if (window.gatherPublisherId == null || window.gatherPublisherId == 0) {
   				numImpressions = 8;
   			} else {
    			numImpressions = 5;
    		}
   	   } else {
	  	  numImpressions = window.gatherNumImpressions;
   	   }
       gatherAdHeight =  numImpressions * 120;
       gatherAdWidth = 160;
   } else if (window.gatherAdFormat == 'gather-www') {
		if (window.gatherNumImpressions == 0 || window.gatherNumImpressions == null ) {
			numImpressions = 8;
		}
		gatherAdHeight = numImpressions * 96 + 10;
		gatherAdWidth = 210;   
   } else if (window.gatherAdFormat == 'banner' || window.gatherAdFormat == 'full-banner') {
   	  gatherAdWidth = 468;
   	  gatherAdHeight = 60;
   	  numImpressions = 2;
   } else if (window.gatherAdFormat == 'leaderboard' ) {
   	  gatherAdWidth = 728;
   	  gatherAdHeight = 90;     
   	  numImpressions = 4;
   } else if (window.gatherAdFormat == 'medium-rectangle' ) {
   	  gatherAdWidth = 300;
   	  gatherAdHeight = 250;
   	  numImpressions = 4;
   } else if (window.gatherAdFormat == 'skyscraper' ) {
   	  gatherAdWidth = 120;
   	  gatherAdHeight = 600;
   	  numImpressions = 4;
   } else if (window.gatherAdFormat == 'wide-skyscraper' ) {
   	  gatherAdWidth = 160;
   	  gatherAdHeight = 600;
   	  numImpressions = 4;
   } else if (window.gatherAdFormat == 'button-1' ) {
   	  gatherAdWidth = 120;
   	  gatherAdHeight = 90;
   	  numImpressions = 4;
   } else if (window.gatherAdFormat == 'button-2' ) {
   	  gatherAdWidth = 120;
   	  gatherAdHeight = 60;
   	  numImpressions = 4;
   } else if (window.gatherAdFormat == 'merch-home-1' ) {
   	  gatherAdWidth = 225;
   	  gatherAdHeight = 140;
   	  numImpressions = 1;
   } else if (window.gatherAdFormat == 'merch-home-2' ) {
   	  gatherAdWidth = 225;
   	  gatherAdHeight = 140;
   	  numImpressions = 1;
   } else if (window.gatherAdFormat == 'merch-my-1' ) {
   	  gatherAdWidth = 300;
   	  gatherAdHeight = 145;
   	  numImpressions = 1;
   } else if (window.gatherAdFormat == 'merch-my-2' ) {
   	  gatherAdWidth = 300;
   	  gatherAdHeight = 145;
   	  numImpressions = 1;
   } else if (window.gatherAdFormat == 'merch-my-3' ) {
   	  gatherAdWidth = 180;
   	  gatherAdHeight = 150;
   	  numImpressions = 1;
   } else if (window.gatherAdFormat == 'merch-my-col-a-1' ) {
      gatherAdWidth = 225;
      gatherAdHeight = 140;
      numImpressions = 1;
   } else if (window.gatherAdFormat == 'merch-my-col-a-2' ) {
      gatherAdWidth = 225;
      gatherAdHeight = 140;
      numImpressions = 1;
   } else if (window.gatherAdFormat == 'square-button' ) {
      gatherAdWidth = 125;
      gatherAdHeight = 125;
      numImpressions = 1;
   } else if (window.gatherAdFormat == 'medium-rectangle-upload' ) {
      gatherAdWidth = 300;
      gatherAdHeight = 250;
      numImpressions = 1;
   }
   
	if (window.gatherAdType == 'BANNER') {
		numImpressions = 1;
	}
	
   gather_append_param('gatherNumImpressions', numImpressions);
   
   retVal = null;
   
   if (window.gatherResponseType == 'javascript') {
   	    var jscrpt = '<script src="' + gatherUrl + '" ></script>';
   	    document.writeln(jscrpt);
   	    //alert('returning script tags');
   	    //retVal = jscrpt;
   	    gatherUrl = gatherUrlBase;   	    
   } else {

            var iframe = '<if' + 'rame'
            + ' width=' + gatherAdWidth 
            + ' height=' + gatherAdHeight 
            + ' frameborder=0 '
            + ' scrolling=no ' 
            + ' marginheight=0 ' 
            + ' marginwidth=0 '
            + ' leftmargin=0 '
            + ' topmargin=0 '    
            + '  src="' + gatherUrl + '" ></if' + 'rame>';

            // Executing document.write after the page has finish loading overwrites the document
            // So use gatherAdReturn = 'true' to return the iframe html. So the calling jsp can set
            // the return value as the innerHTML for the appropriate div element.
            // See viewArticle.jsp for an example.
            if (window.gatherAdReturn == null || window.gatherAdReturn != 'true') {
                document.writeln(iframe);
            } else {
                retVal = iframe;
            }

            gatherUrl = gatherUrlBase;
	}
	
   // All parameters must be reset to null to avoid possible confusion
   // when multiple ads per page are placed.
   window.gatherAdWidth        = null;
   window.gatherAdHeight       = null;
   window.gatherPublisherId    = null;
   window.gatherAdFormat       = null;
   window.gatherResponseType   = null;
   window.gatherAdType         = null;
   window.gatherGroupNamespace = null;
   window.gatherAdPosition     = null;
   window.gatherKeywordTags    = null;
   window.gatherNumImpressions = null;   
   window.gatherFontColor      = null;
   window.gatherTableBorderColor = null;
   window.gatherTableBackgroundColor = null;
   window.gatherDisplayLinkColor  = null;
   window.gatherLinkColor = null;
   window.gatherTrailerBackground = null;
   window.gatherTrailerFontColor = null;
   window.gatherRestrictCategory = null;
   window.gatherIsAdult = null;
   
   if (window.gatherAdReturn == 'true') {
      window.gatherAdReturn = null;
      return retVal;
   }
   //alert('retVal: '+retVal);
   //return retVal;
}