var ord=Math.random()*10000000000000000;var refreshAds=function(){if(typeof pageviewTracking=="function"){try{pageviewTracking();}catch(e){$h.console.error("[OMNITURE pageviewTracking] ERROR "+e);}}else{$h.console.error("[refreshAds.js] pageviewTracking not found");}
if(typeof _vrtrack=='function'){console.log('VRTrack exists.. calling _vrtrack()..');_vrtrack();}
if((!!!_ad_refresh_interval)||(_ad_refresh_interval==0)){return false;}
this.debug;this.counter;this.forceRefresh=false;var individualPosition=null;if(this.debug==null)this.debug=false;var refreshdebug=this.debug;if(!!!this.counter)this.counter=0;if(!!arguments[0]){switch(arguments[0].option){case"ignorecount":this.counter--;$h.console.warn("adrefresh count ignored[forceRefresh:"+this.forceRefresh+"]");break;case"norefresh":$h.console.warn("adrefresh bypassed[forceRefresh:"+this.forceRefresh+"]");return false;break;case"alwaysrefresh":this.forceRefresh=true;$h.console.warn("adrefresh always on[forceRefresh:"+this.forceRefresh+"]");break;case"resetcount":$h.console.warn("adrefresh count reset[forceRefresh:"+this.forceRefresh+"]");this.counter=0;break;case"debug":$h.console.warn("debug mode toggle set["+!this.debug+"]");this.debug=!this.debug;break;default:$h.console.warn("adrefresh default[forceRefresh:"+this.forceRefresh+"]");break;}
individualPosition=(!!arguments[0].positions)?arguments[0].positions:false;}
if((++this.counter>=_ad_refresh_interval)||(this.forceRefresh)){var allpositions=$h.util.getParameter("position_list",_ghearst_vars["ams_ads_script_src"]).split("%2C");var mypositions="";for(i in allpositions){var flipbookreg=/flipbook$/g;var epicreg=/epic$/g;if(flipbookreg.test(allpositions[i])||epicreg.test(allpositions[i])){}else if(document.getElementById(allpositions[i])){if(mypositions==""){mypositions+=allpositions[i];}else{mypositions+=","+allpositions[i];}}}
if(!!individualPosition){mypositions=individualPosition;}
if(this.forceRefresh){--this.counter;}else{this.counter=0;}
pageAdsParams["position_list"]=mypositions;$.ajax({transport:"xhr",type:"GET",url:"/ams/page-ads.js",dataType:"json",data:pageAdsParams,error:function(){$h.console.error("refreshAds[ajax Error]");},success:function(ads){ord=Math.random()*10000000000000000;loopAds:for(adposition in ads){var passArray=["google","Sponsored Links","ntwrkURL","hpto","subscribe.hearstmags.com","app.hearstmags.com","1x5 ad","adsyndication.msn.com","No creatives found for pos_name","IGNOREREFRESH"];for(items in passArray){var theReggie=new RegExp(passArray[items],"i");if(theReggie.test(ads[adposition])){continue loopAds;}}
var adDom=document.getElementById(adposition);var initialadWidth=parseInt(adDom.offsetWidth);var adWidth=adDom.getAttribute("adWidth");var adHeight=adDom.getAttribute("adHeight");if(!(!!adWidth&&!!adHeight)){adDom.style.display="block";var tHeight;var tWidth;if(initialadWidth!=0){adDom.style.width=initialadWidth;tWidth=initialadWidth;}else{adDom.style.width="auto";tWidth=adDom.offsetWidth;}
adDom.style.height="auto";tHeight=adDom.offsetHeight;if((tHeight+tWidth)==0){var telement=document.createElement("div");telement.setAttribute("id","temporaryAdElement");telement.setAttribute("style","float: left;visibility: hidden;position: absolute; bottom: 0px; left: 0px;");document.body.appendChild(telement);telement.innerHTML=adDom.innerHTML;tHeight=telement.offsetHeight;tWidth=telement.offsetWidth;telement.style.display="none";document.body.removeChild(document.getElementById("temporaryAdElement"))}
adDom.setAttribute("adWidth",tWidth);adDom.setAttribute("adHeight",tHeight);adDom.style.width=tWidth;adDom.style.height=tHeight;adDom.style.overflow="hidden";adWidth=tWidth;adHeight=tHeight;}
var regtest=/<script/i.test(ads[adposition]);if(/<script/i.test(ads[adposition])){if(refreshdebug){if(ads[adposition].match(/\<\!\-\- begin (.*) ad tag \-\-\>/i)){$h.console.error("[ROGUE AD CONTENT:"+adposition+"] "+ads[adposition]);}else{$h.console.error("[BAD AD CONTENT:"+adposition+"] "+ads[adposition]);}}
var ad=ads[adposition];ad=ad.replace(/[\n\r\t]/g,"");var reg=/\'\s\+\s\w*\s\+\s\'/g;var mylist=ad.match(reg);if(!!mylist){for(var i=0;i<mylist.length;i++){var simplestring=mylist[i];var simplereg=/\w*/g;var thevar=mylist[i].match(simplereg)[4];ad=ad.replace(mylist[i],window[thevar]);}}
var cssLink=document.createElement("link");cssLink.href="/cm/shared/styles/iframe_proxy.css";cssLink.rel="stylesheet";cssLink.type="text/css";var adPositionDOM=document.getElementById(adposition);adPositionDOM.innerHTML="";var myframe=document.createElement("iframe");myframe.setAttribute("width",adWidth);myframe.setAttribute("height",adHeight);myframe.setAttribute("frameborder",0);myframe.setAttribute("scrolling","no");myframe.setAttribute("name",adposition+"-frame");myframe.setAttribute("id",adposition+"-frame");myframe.setAttribute("mytype","directfile");myframe.setAttribute("allowtransparency","true");myframe.setAttribute("style","border: 0; overflow: hidden: margin: 0px; padding: 0px;");adPositionDOM.appendChild(myframe);var treg=/.doubleclick.net/i;if(treg.test(ad)){myframe.src=ad.match(/src\=\"\S*\"/i)[0].replace(/src\=\"/i,"").replace("\"","").replace(/adj/gi,"adi");}else{function iDocWrite(destinationDoc,contents){destinationDoc.open();destinationDoc.write(contents);destinationDoc.close();if(!!destinationDoc.body){destinationDoc.body.style.margin=0;destinationDoc.body.style.border=0;}
destinationDoc.getElementsByTagName("head")[0].appendChild(cssLink);}
if(myframe.contentDocument){iDocWrite(myframe.contentDocument,ad)}else if(myframe.contentWindow){iDocWrite(myframe.contentWindow.document,ad)}else if(myframe.document){iDocWrite(myframe.document,ad)}else{throw"Document not found, append the parent element to the DOM before creating the IFrame";}}}else{if(document.getElementById(adposition)){document.getElementById(adposition).innerHTML=ads[adposition];}}}}});}}
