
/**
 * Gets the ad from a div. Use openX to put an ad in a div and
 * this function will get the ad for you.
 *
 * @param divName this is the string of the name of the div.  The 'id' tag of the div with the ad code.
 *
 * @author Patrick Henderson path2727@gmail.com
 */
function getAd( divName ) {

   //mlog("div name:"+divName);

   // The hidden div.
   var l = document.getElementById( divName );

   // New div for ad.
   var ht = null;

   // The parts of the ad.
   var img = null;
   var text = null;


   if( l !== null && l !== undefined && l.hasChildNodes() ) {

      var imageFound = false;
      var textFound = false;


      //mlog("has child nodes");
  
      var c = l.childNodes;

      // We are looking for an anchor tag with an image inside.
      for( var k = 0; k < c.length; k++ ) {
         var theKid = c[k];
         //mlog("k:"+k);
         if( theKid.nodeType === 1 ) {
            //mlog("node type  1:"+theKid.tagName);
            // Out ad is an anchor, plus the anchor has an image.
            if( theKid.tagName.toLowerCase() == "a" ) {

               // See if the anchor tag has an image.
               if( theKid.hasChildNodes() ) {
                  var aKids = theKid.childNodes;
                  for( var n = 0; n < aKids.length; n ++ ) {
                  var tKid = aKids[n];
                     if( tKid.nodeType === 1 ) {
                        // If it has an image it is our ad.
                        if( tKid.tagName == "IMG") {
                           //mlog("target");
                           imageFound = true;
                           break;
                        } // if( tKid.tagName
                     } // if( tKid.nodeType
                  } // for( var n
               } // if( theKid.hasChild

               if( imageFound ) {
                  img = theKid;
               } else {
                  text = theKid;
                  textFound = true;
               }

               imageFound = false;

            } // if( theKid.tagName == "a"
         }
      }

      ht = document.createElement("li");

      var header = document.createElement("h3");
      header.setAttribute("class","arbustum");
      var ua = navigator.userAgent;
      if( ua.indexOf("MSIE") !== -1 ) {
         header.style.fontSize = "14pt";
      } else {
         header.style.fontSize = "14px";
      }
      header.style.fontWeight = "normal";
      var txtNode = document.createTextNode("Advertisement"); 
      header.appendChild(txtNode);// "Advertisement";

      if( img !== null || text !== null ) {
         //mlog("#### append header #### ");
         ht.appendChild( header );
      }

      if( img !== null ) {
         //mlog("#### append image #### ");
         ht.appendChild( img );
      }
   
      if( text !== null ) {
         //mlog("#### append tet #### ");
         ht.appendChild( text );
      }

   }


   return ht;

} // function insertAd


/**
 * Ads the ad found with getAd( "divName" ) to a ul in the specified position.
 *
 * <p>
 * This uses the name of the 'ul' tag.  So, that tag needs to have this name as its 'id' tag.
 * </p>
 *
 * @param ad - the HtmlElement for the ad (this is an object, not a string).
 * @param ulName - the string of the name of the ul in question.
 * @param pos - the position you wish to enter the ad into. (1 for first, 2 for second, 3 for third)
 *
 * @author Patrick Henderson path2727@gmail.com
 *
 */
function insertAd( adObj, ulName, pos ) {
   //mlog("insertAd");
   if( adObj !== null && adObj !== undefined && adObj.hasChildNodes() ) {
      var u = document.getElementById( ulName);
      if( u !== null && u !== undefined ) {
         if( u.hasChildNodes() ) {
            var newKids = [];
            var kids = u.childNodes;
            if( kids !== null && kids !== undefined && kids.length > 0 ) {
               //mlog("kids length:"+kids.length);
               var count = 0;
               for( var i = 0; i < kids.length; i ++ ) {
                  //mlog("i:"+i);
                  var kid = kids[i];
                  if (kid.nodeType === 1) {
                     var id = kid.id;
                     //mlog("id:"+id);
                     // If count is the position insert our ad.
                     if( count == (pos-1) ) {
                        //mlog("append ad");
                        // Append ad
                        newKids[ count ] = adObj;
                        count++;
                        // Append old # 2
                        newKids[ count] = kid;
                        count++;
    
                     } else {
                        //mlog("append normal");
                        // Append li
                        newKids[ count ] = kid;
                        count++;
                     }
                  } // if( kid.nodeType
               } // for( var i
            } // if( kids !== null
            // Wipe out ul
            //u.innerHTML = "";
            // Re-add all of the children (with the new ad li too)
            for( var j = 0; j < newKids.length; j++ ) {
               var newKid = newKids[j];
               //mlog("append:"+j);
               u.appendChild( newKid );
            }
         } // if( u.hasChild
      } // if( u !== null
   } // if( l !== null
} // insertAd


