var hiddenAds = new Array();
var ord = "";
var sub = "";

function refreshAdInfo()
{
    var tileCount = 0;

    ord = Math.random()*10000000000000000;
    sub = "free";
    if ( wwd && wwd.user ) {
        sub = wwd.user.getSubscriptionLevel();
    }
    sub = sub == "free" ? "" : sub;

    // set the ord in a div, for others to use
    var el = document.getElementById('doubleclickOrdHolder');
    if ( el ) el.innerHTML = ord;
}

function addHiddenAd(type, ad) {
    var ads = hiddenAds[type];
    if (ads == null) {
        ads = new Array();
    }
    ads.push(ad);
    hiddenAds[type] = ads;
}

/**
 * showHiddenAds
 *
 * on page load, each 'hidden' ad registers an object with its important @see addHiddenAd()
 * information in a global variable.
 * this function runs through each of those ads, by 'type' and assigns the
 * src and href properties to the <img> and <a> respectively, essentially turning the ad on
 *
 * @param type - string representing a group name for a hidden ad
 * @param overrides - an object containing any name/value pairs that need
 *                    to replace any existing query string params
 *
 */
function showHiddenAds( type, overrides )
{
    var ads = hiddenAds[type];
    if ( !ads || ads.length == 0 ) {
        return;
    }

    /**
     * lil' inner function to quickly swap out/add name value params to the query string
     */
    var replaceParam = function( query, name, value ) {
        var found = false;
        if ( query.lastIndexOf( ";" ) == query.length - 1 ) {
            query = query.substr( 0,query.length-1 );
        }
        splits = query.split(";");
        for ( var i=0;i<splits.length;i++ ) {
            if ( splits[i].indexOf("=") > -1 && name == splits[i].substr(0,splits[i].indexOf("=") ) ) {
                splits[i] = name + "=" + value;
                found = true;
            }
        }
        unsplit = "" + splits;
        unsplit = unsplit.replace( /,/g , ";" );
        if ( !found ) {
            unsplit += ";"+name+"="+value;
        }
        return unsplit;
    }

    //    try {
        for ( var i=0;i<ads.length;i++ ) {
            for ( var o in overrides ) {
                ads[i].params = replaceParam( ads[i].params, o, overrides[o] );
            }
            ads[i].img.src = ads[i].base + ads[i].src + ads[i].params + "sub=" + sub + ";ord=" + ord + "?";
            ads[i].link.href = ads[i].base + ads[i].href + ads[i].params + "sub=" + sub + ";ord=" + ord + "?";
        }
        //    } catch(e) {
        //        // ad's info wasnt properly set?
        //    }
}


var popupAdFunctions = new Array();

function setPopupAdFunction(name, func) {
  popupAdFunctions[name] = func;
}

function getPopupAdFunction(name) {
  return popupAdFunctions[name];
}

function adReload(iframe)
{
//alert (iframe.contentWindow.location);
//alert (iframe.src);
    if (iframe.contentWindow.location) {
           iframe.contentWindow.location.replace(iframe.src + "ptype%3dsubcategory%3b");
          //iframe.contentWindow.location.reload(false);
         // iframe.src = (iframe.src + "ptype%3dsubcategory%3b");
    }
      else {
        iframe.src = iframe.src;
    }
}

function adReloadAll()
{
  refreshAdInfo();
  var iframes = document.getElementsByTagName('iframe');
  for(var i = 0 ; i < iframes.length ; ++i) {
    if (iframes[i].className == 'WWD2DartAdUnitLocal') {
      adReload(iframes[i]);
    }
  }
}

function getIFrameDocument(iframe)
{
  var doc = (iframe.contentDocument // For NS6
      || iframe.contentWindow.document // For IE5.5 and IE6
      || iframe.document); // For IE5 et al
  return doc;
}

function adAutoResizeHeight(id){
    var iframe = document.getElementById(id);
    var iframeDocument = getIFrameDocument(iframe);
    var newheight = 0;
    try {
        newheight = iframeDocument.getElementById('bottom_div').offsetTop;
    }
    catch(e){ }
    iframe.height = (newheight) + "px";
}
