/*
 * aamBundledAds.js
 * 
 * Library for use with Advanced Bundled Ad Requests
 * Copyright 2007 Atlas Solutions, LLC
 * 
 */     

function aamRenderAd(divName, aambTag)
{

   /* Name for this invisible object */
   var invObj;
   
   invObj = 'aamInv' + divName;

   /* Test to make sure the AAMBxxxx object exists */
   try { 
      var adCode = eval(aambTag); 
   } catch(aamErr) { }

   if (typeof(adCode) != 'undefined') {

      if (navigator.userAgent.indexOf('MSIE') > -1) {

         /* If this is IE, use an IFRAME as a rendering engine for the ad */
         document.getElementById(divName).innerHTML += 
            '<div id="TMP' + divName + '" style="display:none">' + 
            escape('<body><div id="adDiv">' + adCode + '</div>') + 
            '</div>' +
            '<iframe name="' + invObj + '" width="0" height="0" frameborder="0" ' +
            'onload="javascript:try { document.getElementById(\'' + divName + '\').' +
            'insertAdjacentElement(\'beforeEnd\', window.frames[\'' + invObj + '\'].' +
            'document.getElementById(\'adDiv\')) } catch(aamErr) { }"></iframe>';

         /* Workaround for eliminating cross-zone warnings from Adobe Flash player */
         window.frames[invObj].document.location = 
            'javascript:unescape(parent.document.' +
            'getElementById(\'TMP' + divName + '\').innerHTML)';
      
      } else 
         
         /* Otherwise just use an invisible DIV to render the ad */
         document.writeln(
            '<div id="' + invObj + '" style="display:none">' + 
            adCode + 
            '<script language="javascript">' +
            'document.getElementById(\'' + divName + '\').innerHTML = ' +
            'document.getElementById(\'' + invObj + '\').innerHTML; ' +
            'document.getElementById(\'' + invObj + '\').innerHTML = \'\'; ' +
            '</scr' + 'ipt>' +
            '</div>'
         );

   }
}

function aamRenderAllAds()
{

   var allDivs,
       divName,
       aambTag;
       
   /* Get all the divs in the DOM */
   allDivs = document.getElementsByTagName('div');
   
   
   /* Find which divs have an ID that match the approved 
      naming convention, and pass its name along with 
      the parsed AAMB name to aamRenderAd() */
         
   for (a = 0; a < allDivs.length; a++)
      if (allDivs[a].id && allDivs[a].id.search(/iacasAAMB/) == 0) {
         divName = allDivs[a].id;
         aambTag = 'AAMB' + divName.substring(9);
         aamRenderAd(divName, aambTag);
      }

}
