
function track_adsense(url, adunit) {

  var vid = VID;
  var uid = UID;
  var page_key = PAGE_KEY;
  url = url.replace(/&amp;/g,'&');
  url = url.replace(/%/g,'%25');
  url = url.replace(/&/g,'%26');
  url = url.replace(/\?/g,'%3f');
  url = url.replace(/=/g,'%3d'); //
  return ADSENSE_REDIR_BASE + url + '&adunit=' + adunit;

}

function display_side_adsense( ads, div, begin, max, alignment, adunit ) {
  if ( ! ads || ads.length <= begin )
    return;

  div = document.getElementById(div);
  if ( ! div ){
    return;
  }

  var html = ""
  html += "<td height='1'><span style='font-size: 12px; font-weight: bold;' class='adwords_header' ><a href='http://google.com/' >"+ADS_BY_GOOGLE_TEXT+"</a></span></td>";

  var horiz = 'horizontal';
  var vert = 'vertical';

  if( alignment == horiz ) {
    html += "<tr>";
  }

  for ( var i = begin; i < ads.length && i-begin < max ; ++i ) {
    var clickURL = track_adsense( ads[i].url, adunit );

    if( alignment == vert) {
      html += '<tr><td width="200">';
    } else {
      html += '<td width="200">';
    }
    
    html += "<a href='" + clickURL + "' class='adwords_title'>" + ads[i].line1 + "</a><br>";

    html += "<span class='adwords_desc' >";

    if ( ads[i].line2 && ads[i].line2.length > 0 )
      html += "<a class='adwords_text_link' href='" + clickURL + "'>" + ads[i].line2 + "</a>";

    if ( ads[i].line3 && ads[i].line3.length > 0 )
      html += " <br><a class='adwords_text_link' href='" + clickURL + "'>" + ads[i].line3 + "</a>";

    var vu = ads[i].visible_url;
    var temp = vu.indexOf("/");
    if ( temp > 0 ){
      vu = vu.substring( 0 , temp );
    }

    html += "</span><br><a class='adwords_link' href='" + clickURL + "'>" + vu + "</a><br><br>";

    html += "</td>";
    if( alignment == vert) {
      html += '</tr>';
    }
  }

  if( alignment == horiz ) {
    html += "</tr>"
  }

  div.innerHTML = "<table>" + html + "</table>";

}

function create(tag, attrs) {
  var ret = document.createElement(tag);
  for ( key in attrs ) {
    if(!attrs.hasOwnProperty(key))
      continue;
    if(key == '_class')
      ret.setAttribute('class',attrs[key]);
    else
      ret[key] = attrs[key];
  }
  return ret;
}

function text(t) {
  return document.createTextNode(t);
}

function append(parent,child) {
  parent.appendChild(child);
}

function display_sponsored_listings( ads, div, adunit ) {
  div = document.getElementById(div);
  if ( ! div ){
    return;
  }

  if ( ! ads )
  {
    YAHOO.util.Dom.setStyle(div.id, 'visibility', 'hidden');
    return;
  }

  var res = []
  html = "";
  html += '<a href="http://google.com/" class="ads_by_google">' + ADS_BY_GOOGLE_TEXT + '</a>';

  for ( key in ads ) {
    ad = ads[key];
    if( !ad.url ) {
      continue;
    }
    
    html += "<div>"
    
    var clickURL = track_adsense( ad.url, adunit );

    // make link
    html += '<div><a href="'+clickURL+'" class="sponsored_listing_title">' + ad.line1 + '</a></div>';

    // make description
    html += '<div class="sponsored_listing_desc">'

    if ( ad.line2 )
        html += ad.line2
        
    if ( ad.line3 )
        html += ' ' + ad.line3
    
    html += '</div>';
    
    html += '</div>'
    
  }

  // html += "</span>&nbsp;&nbsp;<a target='ad_"+Math.random()+"' class='adwords_link' href='" + clickURL + "'>" + vu + "</a>";

  // div.innerHTML = "<table>" + html + "</table>";
  
    div.innerHTML += html;

}



function google_ad_request_done(google_ads){
  if (PAGE_TYPE.indexOf('wiki') != -1) {
    display_side_adsense(google_ads, "side_adsense", 0, 7, 'vertical', 'side_adsense');
  } else {
    display_side_adsense(google_ads, "side_adsense", 0, 10,'vertical', 'side_adsense');
    
    if (DETAIL_ADS_ABOVE_FALLBACK_SEARCH) {
        // Only 2 ads for the "Detail Ads Above Fallback Search" bucket, see FB 3764
        display_sponsored_listings(google_ads.slice(14,16), "detail_ads_above_fallback_search", 'detail_ads_above_fallback_search');
    } else {
        // 3 ads normally
        display_sponsored_listings(google_ads.slice(14,17), "sponsored_listings_below", 'sponsored_listings_below');
    }

    display_side_adsense(google_ads, 'sponsored_listings_above', 17, 20, 'horizontal', 'sponsored_listings_above');
  }
}
