
TNCMS_Used_Ad=[];TNCMS_Ad={init:function(oCfg){if(!oCfg.ads){this.oAds=[];return this;}
this.oAds=oCfg.ads;this.sDomain=oCfg.domain;this.sPosition=oCfg.position;if(oCfg.position_settings){this.oPosSettings=oCfg.position_settings;}else{this.oPosSettings={};}
this.oActiveAd={};this.sSection='';this.nWidth='';var oDate=new Date();this.nTodayTimestamp=Math.ceil(oDate.getTime()*.001);this.aDay_Limits=!(aTNCMS_Day_Limit)?new Array():aTNCMS_Day_Limit;this.aTotal_Limits=!(aTNCMS_Total_Limit)?new Array():aTNCMS_Total_Limit;return this;},show:function(sSection,nWidth){sURL=document.location.href;aMatch=sURL.match(/http:\/\/(.+townnews-staging.com)\/{1,1}/,"");if(aMatch){this.stagingDomain=aMatch[1];}
if(!(sSection)){var docPath=document.location.href;docPath=docPath.replace(/\?.*/,'');docPath=docPath.replace(/#\w+/,'');oPath=this._getPathInfo(docPath);if(oPath.path=="/"){this.sSection='frontpage';}else{this.sSection=oPath.path.replace(/^\//g,'');this.sSection=this.sSection.replace(/\/$/g,'');}}else{this.sSection=sSection;}
for(nAdId in this.oAds){if(TNCMS_Used_Ad[nAdId]==1){delete this.oAds[nAdId];}}
for(nAdId in this.oAds){var oToday=new Date();var oStartDate=new Date();var oEndDate=new Date();var aDateParts=this.oAds[nAdId].startdate.split('-');oStartDate.setFullYear(aDateParts[0],(aDateParts[1]-1),aDateParts[2]);aDateParts=this.oAds[nAdId].enddate.split('-');oEndDate.setFullYear(aDateParts[0],(aDateParts[1]-1),aDateParts[2]);if((oStartDate>oToday)||(oEndDate<oToday)){delete this.oAds[nAdId];continue;}
if(this.nWidth&&this.oAds[nAdId].width){if(this.oAds[nAdId].width>this.nWidth){delete this.oAds[nAdId];continue;}}
if(this.aDay_Limits[nAdId]){if(this.nTodayTimestamp>this.aDay_Limits[nAdId]){delete this.oAds[nAdId];continue;}}
if(this.aTotal_Limits[nAdId]){if(this.nTodayTimestamp>this.aTotal_Limits[nAdId]){delete this.oAds[nAdId];}}}
this._checkFreqLimits();oValidAds=this._getAdsBySection(this.sSection,this.oAds);if(oValidAds.length>0){this.oActiveAd=this._getAdUsingWeight(oValidAds);}else{this.oActiveAd={};}
if(!this.oActiveAd.asseturl){this._noAd();return true;}
if(this.stagingDomain){this.oActiveAd.asseturl=this.oActiveAd.asseturl.replace(this.sDomain,this.stagingDomain);}
TNCMS_Used_Ad[this.oActiveAd.adid]=1;this._updateFreqCount(this.oActiveAd);var nRand;var tempPath;var assetUrl;if(this.oActiveAd.clickuri){if(this.oActiveAd.type=='flash'){if(this.oActiveAd.clickuri){var aUrls=this.oActiveAd.clickuri.split(",");for(var i=0;i<aUrls.length;i++){aUrls[i]=aUrls[i].replace(/^\s*/,"").replace(/\s*$/,"");if(!(aUrls[i].match(/^http(s)?:\/\//i))){aUrls[i]="http://"+aUrls[i];}
nRand=Math.floor(Math.random()*100000000);tmpPath="c"+nRand+"/"+this.sSection;assetUrl=this.oActiveAd.asseturl.replace("[random_number]",tmpPath);aUrls[i]=assetUrl+"?r="+aUrls[i];}
this.oActiveAd.clickuri=aUrls;}}else{nRand=Math.floor(Math.random()*100000000);tmpPath="c"+nRand+"/"+this.sSection;assetUrl=this.oActiveAd.asseturl.replace("[random_number]",tmpPath);this.oActiveAd.clickuri=assetUrl+"?r="+this.oActiveAd.clickuri;}}
nRand=Math.floor(Math.random()*100000000);tmpPath=nRand+"/"+this.sSection;this.oActiveAd.asseturl=this.oActiveAd.asseturl.replace("[random_number]",tmpPath)+"?_dc="+this.oActiveAd.file_modified;switch(this.oActiveAd.type){case'image':this._imageAd();break;case'html':this._htmlAd();break;case'flash':this._flashAd();break;case'video':this._flvAd();break;}
return true;},setWidth:function(nWidth){this.nWidth=nWidth;},_noAd:function(){var sHTML="<!-- There are no ads assigned to position -->";document.write(sHTML);},_imageAd:function(){var sHTML="<img src='"+this.oActiveAd.asseturl+"' alt='*' border='0'/>";if(this.oActiveAd.clickuri){var sHTML="<a rel='nofollow' href='"+this.oActiveAd.clickuri+"' target='blank'>"+sHTML+"</a>";}
document.write(sHTML);},_htmlAd:function(){var strippedUrl=this.oActiveAd.asseturl.replace(/\?_dc=[0-9]+/,"");strippedUrl=strippedUrl.replace(/\?dc=[0-9]+/,"");if(strippedUrl.match(/-revisions/)){var sVarName=strippedUrl.replace(/\/(\w|\-)+\.html\.js/,"")+".html.js";sVarName=sVarName.replace('-revisions',"");var aPath=this._getPathInfo(sVarName);}else{var aPath=this._getPathInfo(strippedUrl);}
sVarName=aPath['file'].replace(".html.js","");sVarName=sVarName.replace(/-/g,"");var sHTML="<scr"+"ipt language='javascript'>\n";sHTML+="var pos_"+sVarName+" = '"+this.sPosition+"';\n";sHTML+="var sec_"+sVarName+" = '"+this.sSection+"';\n";sHTML+="</scr"+"ipt>\n";sHTML+="<scr"+"ipt type='text/javascript' src='"+this.oActiveAd.asseturl+"'></scr"+"ipt>";document.write(sHTML);},_flashAd:function(){var sHTML="<div id='blox-ad-"+this.oActiveAd.adid+"'></div>\n";if(typeof tncms_flash_ad=='function'){sHTML+="<script language='javascript'>\n";sHTML+="tncms_flash_ad("+this.oActiveAd.adid+",'"+this.oActiveAd.asseturl+"',";sHTML+="['"+this.oActiveAd.clickuri.join("','")+"'],";sHTML+="{ width: "+this.oActiveAd.width+", height: "+this.oActiveAd.height+",";sHTML+=" version: "+this.oActiveAd.version+", transparent: "+this.oActiveAd.transparent+"});";sHTML+="</script>\n";}else{sHTML+="<script language='javascript'>";sHTML+="var oSWF = new SWFObject('"+this.oActiveAd.asseturl+"','blox-flash-ad-"+this.oActiveAd.adid+"',"+this.oActiveAd.width+","+this.oActiveAd.height+",'"+this.oActiveAd.version+"', '#000');\n";if(this.oActiveAd.transparent){sHTML+="oSWF.addParam('wmode', 'transparent');\n";}else{sHTML+="oSWF.addParam('wmode', 'opaque');\n";}
if(this.oActiveAd.clickuri.length>0){for(var i=0;i<this.oActiveAd.clickuri.length;i++){if(i==0){sHTML+="oSWF.addVariable('clickTAG', '"+this.oActiveAd.clickuri[i]+"');\n";}else{sHTML+="oSWF.addVariable('clickTAG"+(i+1)+"', '"+this.oActiveAd.clickuri[i]+"');\n";}}}
sHTML+="oSWF.write('blox-ad-"+this.oActiveAd.adid+"');\n";sHTML+="</script>\n";}
document.write(sHTML);},_flvAd:function(){var sHTML="<div id='blox-ad-"+this.oActiveAd.adid+"'></div>\n";if(typeof tncms_video_ad=='function'){sHTML+="<script language='javascript'>\n";sHTML+="tncms_video_ad("+this.oActiveAd.adid+",'"+this.oActiveAd.asseturl+"',";sHTML+="'"+this.oActiveAd.clickuri+"', { width: "+this.oActiveAd.width+", height: "+this.oActiveAd.height+",";sHTML+=" rolltype: '"+this.oPosSettings[this.sSection].video_rolltype+"',";sHTML+=" autoplay: "+parseInt(this.oPosSettings[this.sSection].video_autoplay,10)+", ";sHTML+=" embed: "+parseInt(this.oPosSettings[this.sSection].video_embed,10)+", ";sHTML+=" volume: "+parseInt(this.oPosSettings[this.sSection].video_volume,10)+"});</script>\n";}else{if(parseInt(this.oPosSettings[this.sSection].video_embed)==0){document.write('<scr'+'ipt src="/shared-content/flowplayer/flashembed.min.js"></scr'+'ipt>');sHTML+='<script language="javascript">\n';sHTML+='flashembed("blox-ad-'+this.oActiveAd.adid+'", {\n';sHTML+='src: "/shared-content/flowplayer/FlowPlayer.swf", ';sHTML+='width: '+this.oActiveAd.width+', height: '+this.oActiveAd.height+'},\n';sHTML+='{config: {\n';if(this.oPosSettings[this.sSection].video_autoplay==1){sHTML+='autoPlay: true,';}else{sHTML+='autoPlay: false,';}
sHTML+='initialVolumePercentage: '+parseInt(this.oPosSettings[this.sSection].video_volume,10)+',';sHTML+='autoBuffering: false, loop: false, \nplayList: [';sHTML+='{ url: "'+this.oActiveAd.asseturl+'", type: "flv", linkUrl: "'+this.oActiveAd.clickuri+'"}';sHTML+=']\n}}\n);';sHTML+='</script>';}else{sHTML+="<!-- "+this.sPosition+" was set to embed but the function tncms_video ad was not defined. -->";}}
document.write(sHTML);},_writeError:function(sMsg){sErrorMsg="<!-- TNCMS-AD-MANAGER Error: "+sMsg+" -->";document.write(sErrorMsg);},_getAdsBySection:function(sSection,oAds){oValidAds=[];for(nAdId in oAds){var adSections=oAds[nAdId].sections;for(var i=0;i<adSections.length;i++){if(adSections[i]==sSection){oValidAds.push(oAds[nAdId]);}}}
if(oValidAds.length>0){this.sSection=sSection;return oValidAds;}
sMatch=sSection.match(/(\/\w+)$/g);if(!(sMatch)){if(sSection!='ros'){return this._getAdsBySection('ros',oAds);}else{return oValidAds;}}
sSection=sSection.replace(sMatch,'');return this._getAdsBySection(sSection,oAds);},_getAdUsingWeight:function(oAds){var nIterator=0;var nLockedTotalPerc=0;var nUnlockedTotalPerc=0;var nUnlockedAds=0;for(i=0;i<oAds.length;i++){var adWeightData=this._getAdWeightData(oAds[i]);if(adWeightData.locked==1){nLockedTotalPerc+=parseFloat(adWeightData.percent);oAds[i].minRange=nIterator;oAds[i].maxRange=nIterator+(10000*parseFloat(adWeightData.percent));nIterator=oAds[i].maxRange;}
else{nUnlockedTotalPerc+=parseFloat(adWeightData.percent);nUnlockedAds++;}}
if(nUnlockedAds>0){if((nUnlockedTotalPerc+nLockedTotalPerc)<100){var nRemainder=100-nLockedTotalPerc;var nAdjustedPerc=nRemainder/nUnlockedAds;}}
for(i=0;i<oAds.length;i++){var adWeightData=this._getAdWeightData(oAds[i]);if(adWeightData.locked!=1){oAds[i].minRange=nIterator;if(nAdjustedPerc){oAds[i].maxRange=nIterator+(10000*parseFloat(nAdjustedPerc));nIterator=oAds[i].maxRange;}else{oAds[i].maxRange=nIterator+(10000*parseFloat(adWeightData.percent));nIterator=oAds[i].maxRange;}}}
var randomWeight=Math.floor(Math.random()*nIterator);for(i=0;i<oAds.length;i++){if(oAds[i].minRange<=randomWeight&&oAds[i].maxRange>randomWeight){return oAds[i];}}},_getAdWeightData:function(oAd){for(var j=0;j<oAd.weights.length;j++){if(oAd.weights[j].section==this.sSection){return oAd.weights[j];}}
return 0;},_checkFreqLimits:function(){for(nAdId in this.oAds){if(this.oAds[nAdId].frequency_limit){sValueName='tncms_ad_'+nAdId;sValue=this._readCookie(sValueName);if(sValue){aData=sValue.split('&');nAdCount=parseInt(aData[0],10);if(nAdCount>this.oAds[nAdId].frequency_limit){delete this.oAds[nAdId];}}}}},_updateFreqCount:function(oAd){if(!oAd.frequency_limit){return false;}
sValueName='tncms_ad_'+oAd.adid;if(!this._readCookie(sValueName)){if(oAd.frequency_number&&oAd.frequency_type){var oDate=new Date();switch(oAd.frequency_type){case'minutes':oDate.setTime(oDate.getTime()+(oAd.frequency_number*60*1000));break;case'hours':oDate.setTime(oDate.getTime()+(oAd.frequency_number*60*60*1000));break;case'days':oDate.setTime(oDate.getTime()+(oAd.frequency_number*24*60*60*1000));break;}
var cookieExp="; expires="+oDate.toGMTString();document.cookie=sValueName+"=1&"+escape(oDate.toGMTString())+cookieExp+"; path=/";}}else{sCookieValue=this._readCookie(sValueName);aData=sCookieValue.split('&');nAdCount=parseInt(aData[0],10)+1;document.cookie=sValueName+"="+nAdCount+"&"+aData[1]+"; expires="+unescape(aData[1]);}},_readCookie:function(name){var nameEQ=name+"=";var ca=document.cookie.split(';');for(var i=0;i<ca.length;i++){var c=ca[i];while(c.charAt(0)==' ')c=c.substring(1,c.length);if(c.indexOf(nameEQ)==0)return c.substring(nameEQ.length,c.length);}
return null;},_getPathInfo:function(data){var e=/^((http|ftp|https):\/)?\/?([^:\/\s]+)((\/[\s\w\-\.]+)*\/)([\s\w\-\.]+\.[^#?\s]+)(#[\w\-]+)?$/;if(data.match(e)){return{url:RegExp['$&'],path:RegExp.$4,file:RegExp.$6};}
e=/^((http|ftp|https):\/)?\/?([^:\/\s]+)((\/[\s\w\-\.]+)*\/)?$/;if(data.match(e)){return{url:RegExp['$&'],path:RegExp.$4,file:""};}
return{url:"",path:"",file:""};}};