Guest User

MTNL Ad Injection - Script for Loading Ads

a guest
Mar 21st, 2017
1,376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. URL of the Script:
  2. http://mtnl.phozeca.com:3000/getjs?lib=false&nadipdata=%7B%22url%22%3A%22%2Fscripts%2Ftimespoints-main.js%3Fversion%3D1%22%2C%22referer%22%3A%22http%3A%2F%2Ftimesofindia.indiatimes.com%2Fworld%2Fus%2Flaptops-ipads-banned-on-us-bound-flights-from-8-countries%2Farticleshow%2F57745820.cms%22%2C%22subscriberId%22%3A%22XXXXXXXX%40mtnl%22%2C%22host%22%3A%22image.timespoints.iimg.in%22%2C%22categories%22%3A%5B1%5D%2C%22reputations%22%3A%5B1%5D%7D&screenheight=XXX&screenwidth=XXXX
  3. Script:
  4. var ns_ip = 'mtnl.phozeca.com';var ns_port = '3000';var subscriberId = 'XXXXXXXX@mtnl';var subscriberIP = 'XXX.XX.XXX.XX';var nadipdata = {"url":"/scripts/timespoints-main.js?version=1","referer":"http://timesofindia.indiatimes.com/world/us/laptops-ipads-banned-on-us-bound-flights-from-8-countries/articleshow/57745820.cms","subscriberId":"XXXXXXXX@mtnl","host":"image.timespoints.iimg.in","categories":[1],"reputations":[1]};var userAgent = "Mozilla/5.0%20(Windows%20NT%2010.0;%20Win64;%20x64)%20AppleWebKit/537.36%20(KHTML,%20like%20Gecko)%20Chrome/57.0.2987.110%20Safari/537.36";
  5.  
  6.  
  7.  
  8.  
  9.  
  10. var AD_ID = "adMain";
  11. var AD_BACKGROUND_ID = "adBackground";
  12. var Z_INDEX_MAIN = 2147483647;
  13. var Z_INDEX_BACKGROUND = Z_INDEX_MAIN - 1;
  14. var MAIN_DISPLAY = "block"; // Para activar (none) o desactivar (block) fade in
  15. var injected_uuid = null;
  16.  
  17. function loadAdsNADIP() {
  18.     jQuery(document).ready(loadFirstAd());
  19. }
  20.  
  21. function loadAd() {
  22.     try {
  23.         // console.log("jQuery('#'" + AD_ID + ").length: " + jQuery('#' + AD_ID).length + ". sarazasaraza: " + sarazasaraza);
  24.         // Se fija que no exista un div con ese id
  25.         if (jQuery('#' + AD_ID).length === 0 && matchedView !== undefined && matchedView !== null) {
  26.             var adType = matchedView.type;
  27.             console.log("Ad Type: " + adType);
  28.             switch (adType) {
  29.             case "BANNER":
  30.             case "OVERSLIDE":
  31.                 loadVerticalAd(adType);
  32.                 break;
  33.             case "TOP_AD":
  34.             case "FLOORAD":
  35.                 loadHorizontalAd(adType);
  36.                 break;
  37.             case "FIXED":
  38.                 loadFixed();
  39.                 break;
  40.             case "repeat":
  41.                 loadRepeat();
  42.                 break;
  43.             case "INTERSTITIAL":
  44.             case "INTERSTITIAL_FLASH":
  45.                 loadInstertitialAd(adType);
  46.                 break;
  47.             case "CUSTOMIZED":
  48.             case "CUSTOMIZED_DYNAMIC":
  49.                 loadCustomizedAd(adType);
  50.                 break;
  51.             case "IFRAME":
  52.                 loadIframeAd();
  53.                 loadListener();
  54.                 break;
  55.             case "VAST":
  56.                 loadVastAd();
  57.                 loadListener();
  58.                 break;
  59.             case "IMA":
  60.                 loadImaAd();
  61.                 loadListener();
  62.                 break;
  63.             case "KOMLI_BANNER":
  64.                 loadKomliBannerAd();
  65.                 loadListener();
  66.                 break;
  67.             case "INMOBI":
  68.                 loadInmobiAd();
  69.                 loadListener();
  70.                 break;
  71.             case "MOBVISTA":
  72.                 loadMobvistaAd();
  73.                 loadListener();
  74.                 break;
  75.             case "ADATHA":
  76.                 loadAdathaAd();
  77.                 loadListener();
  78.                 break;
  79.             case "NONE":
  80.                 setTimeout(loadNextAdOnFallback, 0);
  81.                 break;
  82.             default:
  83.                 console.log("Default type no configurado");
  84.                 break;
  85.             }
  86.         } else {
  87.             logError("Existe div con id" + AD_ID, "loadAd");
  88.         }
  89.     } catch (err) {
  90.         logError(err, "loadAd");
  91.     }
  92. }
  93.  
  94. // INICIO IFRAME AD ////////////////////////////////////////////////////////////
  95. function loadIframeAd() {
  96.     try {
  97.         // Arma la url del Ad
  98.         var propSrc = matchedView.srcurl;
  99.         if (propSrc.indexOf("?") >= 0) {
  100.             propSrc += "&subscriberId=" + subscriberId;
  101.             propSrc += "&subscriberIP=" + subscriberIP;
  102.             propSrc += "&domainName=" + window.location.hostname;
  103.             propSrc += "&adId=" + nadipad.adid;
  104.             propSrc += "&viewKey=" + JSON.stringify(matchedView.viewKey);
  105.         }
  106.         // Obtiene las propiedades del Ad
  107.         var propPosition = matchedView.position;
  108.         var propWidth = matchedView.adwidth;
  109.         var propHeight = matchedView.adheight;
  110.         var removeScrollBody = (matchedView.removescrollbody === 'true');
  111.         var fondoOscuro = (matchedView.backgroundblack === 'true');
  112.  
  113.         // Crea iframe y setea valores generales
  114.         var ifrm = document.createElement("IFRAME");
  115.         ifrm.setAttribute("src", propSrc);
  116.         ifrm.setAttribute("id", AD_ID);
  117.         ifrm.setAttribute("allowFullScreen", '');
  118.         ifrm.style.width = getValue2(propWidth);
  119.         ifrm.style.height = getValue2(propHeight);
  120.         ifrm.style.position = 'fixed';
  121.         ifrm.style.border = 'none';
  122.         ifrm.style.overflow = 'hidden';
  123.         ifrm.style.margin = '0 auto';
  124.         ifrm.style.zIndex = Z_INDEX_MAIN;
  125.         ifrm.style.display = 'block';
  126.         ifrm.style.padding = '0px';
  127.  
  128.         if (propPosition === "FULLSCREEN") {
  129.             ifrm.style.top = 0 + "px";
  130.             ifrm.style.left = 0 + "px";
  131.             ifrm.style.width = 100 + "%";
  132.             ifrm.style.height = 100 + "%";
  133.         } else {
  134.             // Calcula y setea centrado horizontal
  135.             var windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  136.             if (propWidth.indexOf("%") >= 0) {
  137.                 propWidth = windowWidth * (parseInt(propWidth) / 100);
  138.             }
  139.             var calculatedLeft = windowWidth / 2 - propWidth / 2;
  140.             ifrm.style.left = calculatedLeft + "px";
  141.             // Calcula y setea el centrado vertical de acuerdo a lo seleccionado
  142.             if (propPosition === "TOP") {
  143.                 ifrm.style.top = 0 + "px";
  144.             } else if (propPosition === "BOTTOM") {
  145.                 ifrm.style.bottom = 0 + "px";
  146.             } else if (propPosition === "CENTERED") {
  147.                 // Calcula el centrado vertical
  148.                 var windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
  149.                 if (propHeight.indexOf("%") >= 0) {
  150.                     propHeight = windowHeight * (parseInt(propHeight) / 100);
  151.                 }
  152.                 var calculatedTop = windowHeight / 2 - propHeight / 2;
  153.                 ifrm.style.top = calculatedTop + "px";
  154.             }
  155.         }
  156.  
  157.         if (document.body !== null && document.body !== undefined) {
  158.             // Inserta el iframe en el documento
  159.             if (document.body.firstChild) {
  160.                 document.body.insertBefore(ifrm, document.body.firstChild);
  161.             } else {
  162.                 document.body.appendChild(ifrm);
  163.             }
  164.             // Oculta el scroll segun corresponda
  165.             if (removeScrollBody) {
  166.                 document.getElementsByTagName("body")[0].style.overflow = "hidden";
  167.             }
  168.             // Oscurece fondo o no segun corresponda
  169.             if (fondoOscuro) {
  170.                 var divfondooscuro = document.createElement("div");
  171.                 divfondooscuro.id = AD_BACKGROUND_ID;
  172.                 document.body.appendChild(divfondooscuro);
  173.                 loadCssPropertiesInstertitialBackground();
  174.             }
  175.             // Setea accion final del Ad
  176.             setFinalAction();
  177.         } else {
  178.             logError("(src=" + propSrc + "): document.body is " + document.body, "loadIframeAd");
  179.         }
  180.     } catch (err) {
  181.         logError(err, "loadIframeAd");
  182.     }
  183. }
  184. // FIN IFRAME AD ///////////////////////////////////////////////////////////////
  185.  
  186. // INICIO VAST AD //////////////////////////////////////////////////////////////
  187. function loadVastAd() {
  188.     try {
  189.         var propPosition = matchedView.position;
  190.         var propTop = parseInt(matchedView.top);
  191.         var removeScrollBody = (matchedView.removescrollbody === 'true');
  192.         var fondoOscuro = (matchedView.backgroundblack === 'true');
  193.         var propWidth;
  194.         var propHeight;
  195.         var propLeft;
  196.         if (propPosition === "FULLSCREEN") {
  197.             propWidth = "100%";
  198.             propHeight = "100%";
  199.             propLeft = 0;
  200.         } else {
  201.             propWidth = matchedView.adwidth;
  202.             propHeight = matchedView.adheight;
  203.             propLeft = parseInt(matchedView.left);
  204.         }
  205.  
  206.         if (propPosition === "CENTERED") {
  207.             var windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  208.             var windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
  209.             var popupHeight = propHeight;
  210.             var popupWidth = propWidth;
  211.             var elemTop = windowHeight / 2 - popupHeight / 2;
  212.             var elemLeft = windowWidth / 2 - popupWidth / 2;
  213.             propTop = elemTop;
  214.             propLeft = elemLeft;
  215.         }
  216.  
  217.         var adSizeWidth = calculateAdSize(matchedView.adwidth, window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth);
  218.         var adSizeHeight = calculateAdSize(matchedView.adheight, window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight);
  219.  
  220.         var ifrm = document.createElement("IFRAME");
  221.         var propsrc = "http://" + ns_ip + ":" + ns_port + "/nadip/iframevast?";
  222.         propsrc += "subscriberId=" + subscriberId;
  223.         propsrc += "&subscriberIP=" + subscriberIP;
  224.         propsrc += "&adId=" + nadipad.adid;
  225.         propsrc += "&viewKey=" + JSON.stringify(matchedView.viewKey);
  226.         propsrc += "&refererURL=" + window.location.href;
  227.         propsrc += "&domain=" + window.location.hostname.replace(/^www\./, "");
  228.         propsrc += "&adSize=" + adSizeWidth + "x" + adSizeHeight;
  229.         propsrc += "&userAgent=" + userAgent;
  230.         ifrm.setAttribute("src", propsrc);
  231.         ifrm.setAttribute("id", AD_ID);
  232.         ifrm.setAttribute("allowFullScreen", '');
  233.         ifrm.style.height = getValue2(propHeight);
  234.         ifrm.style.position = 'fixed';
  235.         ifrm.style.border = 'none';
  236.         ifrm.style.overflow = 'hidden';
  237.         ifrm.style.margin = '0 auto';
  238.         ifrm.style.zIndex = Z_INDEX_MAIN;
  239.         ifrm.style.display = 'block';
  240.         ifrm.style.padding = '0px';
  241.         ifrm.style.top = "auto";
  242.         ifrm.style.left = "0px";
  243.         if (propPosition === "CENTERED") {
  244.             ifrm.style.top = propTop + "px";
  245.             ifrm.style.left = propLeft + "px";
  246.         } else if (propPosition === "TOP") {
  247.             ifrm.style.top = "0px";
  248.         }
  249.  
  250.         if (propPosition === "FULLSCREEN") {
  251.             ifrm.style.width = "100%";
  252.             ifrm.style.top = "0px";
  253.         } else {
  254.             ifrm.style.width = getValue2(propWidth);
  255.         }
  256.  
  257.         if (document.body !== null && document.body !== undefined) {
  258.             if (document.body.firstChild) {
  259.                 document.body.insertBefore(ifrm, document.body.firstChild);
  260.             } else {
  261.                 document.body.appendChild(ifrm);
  262.             }
  263.  
  264.             if (propPosition === "BOTTOM") {
  265.                 var div = document.getElementById(AD_ID);
  266.                 div.style.bottom = "0";
  267.                 if (div.getBoundingClientRect) {
  268.                     var rect = div.getBoundingClientRect();
  269.                     var cHeight = document.documentElement.clientHeight;
  270.                     var bottomOffset = (rect.bottom - cHeight);
  271.                     if (bottomOffset > 0) {
  272.                         div.style.bottom = bottomOffset + "px";
  273.                     }
  274.                 }
  275.             }
  276.             if (removeScrollBody) {
  277.                 document.getElementsByTagName("body")[0].style.overflow = "hidden";
  278.             }
  279.             if (fondoOscuro) {
  280.                 var divfondooscuro = document.createElement("div");
  281.                 divfondooscuro.id = AD_BACKGROUND_ID;
  282.                 document.body.appendChild(divfondooscuro);
  283.                 loadCssPropertiesInstertitialBackground();
  284.             }
  285.  
  286.             setFinalAction();
  287.         } else {
  288.             logError("(src=" + propsrc + "): document.body is " + document.body, "loadVastAd");
  289.         }
  290.     } catch (err) {
  291.         logError(err, "loadVastAd");
  292.     }
  293. }
  294.  
  295. function calculateAdSize(configuredSize, windowSize) {
  296.     var adSize = configuredSize;
  297.     if (configuredSize !== undefined && configuredSize.indexOf("%") > 0) {
  298.         adSize = windowSize * (parseInt(configuredSize.replace("%", "")) / 100);
  299.     }
  300.     return adSize;
  301. }
  302. // FIN VAST AD /////////////////////////////////////////////////////////////////
  303.  
  304. // INICIO IMA AD //////////////////////////////////////////////////////////////
  305. function loadImaAd() {
  306.     try {
  307.         // Obtiene las propiedades del Ad
  308.         var propPosition = matchedView.position;
  309.         var propHorizontalPosition = matchedView.horizontalposition;
  310.         var removeScrollBody = (matchedView.removescrollbody === 'true');
  311.         var fondoOscuro = (matchedView.backgroundblack === 'true');
  312.  
  313.         var adSizeWidth = calculateAdSize(matchedView.adwidth, window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth);
  314.         var adSizeHeight = calculateAdSize(matchedView.adheight, window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight);
  315.  
  316.         var ifrm = document.createElement("IFRAME");
  317.         var propSrc = "http://" + ns_ip + ":" + ns_port + "/nadip/iframeimasdk?";
  318.         propSrc += "subscriberId=" + subscriberId;
  319.         propSrc += "&subscriberIP=" + subscriberIP;
  320.         propSrc += "&adId=" + nadipad.adid;
  321.         propSrc += "&viewKey=" + JSON.stringify(matchedView.viewKey);
  322.         propSrc += "&refererURL=" + window.location.href;
  323.         propSrc += "&domain=" + window.location.hostname.replace(/^www\./, "");
  324.         propSrc += "&adSize=" + adSizeWidth + "x" + adSizeHeight;
  325.         propSrc += "&userAgent=" + userAgent;
  326.         ifrm.setAttribute("src", propSrc);
  327.         ifrm.setAttribute("id", AD_ID);
  328.         ifrm.setAttribute("allowFullScreen", '');
  329.         ifrm.style.width = adSizeWidth + "px";
  330.         ifrm.style.height = adSizeHeight + "px";
  331.         ifrm.style.position = 'fixed';
  332.         ifrm.style.border = 'none';
  333.         ifrm.style.overflow = 'hidden';
  334.         ifrm.style.margin = '0 auto';
  335.         ifrm.style.zIndex = Z_INDEX_MAIN;
  336.         ifrm.style.display = 'block';
  337.         ifrm.style.padding = '0px';
  338.  
  339.         if (propPosition === "FULLSCREEN") {
  340.             ifrm.style.top = 0 + "px";
  341.             ifrm.style.left = 0 + "px";
  342.             ifrm.style.width = 100 + "%";
  343.             ifrm.style.height = 100 + "%";
  344.         } else {
  345.             // Calcula y setea centrado horizontal
  346.             if (propHorizontalPosition === undefined || propHorizontalPosition === null || propHorizontalPosition === "CENTERED") {
  347.                 // Calcula el centrado horizontal
  348.                 var windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  349.                 var calculatedLeft = windowWidth / 2 - adSizeWidth / 2;
  350.                 ifrm.style.left = calculatedLeft + "px";
  351.             } else if (propHorizontalPosition === "LEFT") {
  352.                 ifrm.style.left = 0 + "px";
  353.             } else if (propHorizontalPosition === "RIGHT") {
  354.                 ifrm.style.right = 0 + "px";
  355.             }
  356.  
  357.             // Calcula y setea el centrado vertical de acuerdo a lo seleccionado
  358.             if (propPosition === undefined || propPosition === null || propPosition === "CENTERED") {
  359.                 // Calcula el centrado vertical
  360.                 var windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
  361.                 var calculatedTop = windowHeight / 2 - adSizeHeight / 2;
  362.                 ifrm.style.top = calculatedTop + "px";
  363.             } else if (propPosition === "TOP") {
  364.                 ifrm.style.top = 0 + "px";
  365.             } else if (propPosition === "BOTTOM") {
  366.                 ifrm.style.bottom = 0 + "px";
  367.             }
  368.         }
  369.  
  370.         if (document.body !== null && document.body !== undefined) {
  371.             // Inserta el iframe en el documento
  372.             if (document.body.firstChild) {
  373.                 document.body.insertBefore(ifrm, document.body.firstChild);
  374.             } else {
  375.                 document.body.appendChild(ifrm);
  376.             }
  377.             // Oculta el scroll segun corresponda
  378.             if (removeScrollBody) {
  379.                 document.getElementsByTagName("body")[0].style.overflow = "hidden";
  380.             }
  381.             // Oscurece fondo o no segun corresponda
  382.             if (fondoOscuro) {
  383.                 var divfondooscuro = document.createElement("div");
  384.                 divfondooscuro.id = AD_BACKGROUND_ID;
  385.                 document.body.appendChild(divfondooscuro);
  386.                 loadCssPropertiesInstertitialBackground();
  387.             }
  388.             // Setea accion final del Ad
  389.             setFinalAction();
  390.         } else {
  391.             logError("(src=" + propSrc + "): document.body is " + document.body, "loadImaAd");
  392.         }
  393.  
  394.     } catch (err) {
  395.         logError(err, "loadImaAd");
  396.     }
  397. }
  398. // FIN IMA AD //////////////////////////////////////////////////////////////////
  399.  
  400. // INICIO KOMLI BANNER AD //////////////////////////////////////////////////////
  401. function loadKomliBannerAd() {
  402.     try {
  403.         var propsrc = "http://" + ns_ip + ":" + ns_port + "/nadip/iframekomlibanner?";
  404.         propsrc += "subscriberId=" + subscriberId;
  405.         propsrc += "&subscriberIP=" + subscriberIP;
  406.         propsrc += "&domainName=" + window.location.hostname;
  407.         propsrc += "&adId=" + nadipad.adid;
  408.         propsrc += "&viewKey=" + JSON.stringify(matchedView.viewKey);
  409.         buildAdNetworkBanner(propsrc);
  410.     } catch (err) {
  411.         logError(err, "loadKomliBannerAd");
  412.     }
  413. }
  414. // FIN KOMLI BANNER AD /////////////////////////////////////////////////////////
  415.  
  416. // INICIO INMOBI AD ////////////////////////////////////////////////////////////
  417. function loadInmobiAd() {
  418.     try {
  419.         var propsrc = "http://" + ns_ip + ":" + ns_port + "/nadip/iframeinmobi?";
  420.         propsrc += "subscriberId=" + subscriberId;
  421.         propsrc += "&subscriberIP=" + subscriberIP;
  422.         propsrc += "&adId=" + nadipad.adid;
  423.         propsrc += "&viewKey=" + JSON.stringify(matchedView.viewKey);
  424.         buildAdNetworkBanner(propsrc);
  425.     } catch (err) {
  426.         logError(err, "loadInmobiAd");
  427.     }
  428. }
  429. // FIN INMOBI AD ///////////////////////////////////////////////////////////////
  430.  
  431. // INICIO MOBVISTA AD //////////////////////////////////////////////////////////
  432. function loadMobvistaAd() {
  433.     try {
  434.         var propsrc = "http://" + ns_ip + ":" + ns_port + "/nadip/iframemobvista?";
  435.         propsrc += "subscriberId=" + subscriberId;
  436.         propsrc += "&subscriberIP=" + subscriberIP;
  437.         propsrc += "&adId=" + nadipad.adid;
  438.         propsrc += "&viewKey=" + JSON.stringify(matchedView.viewKey);
  439.         buildAdNetworkBanner(propsrc);
  440.     } catch (err) {
  441.         logError(err, "loadMobvistaAd");
  442.     }
  443. }
  444. // FIN MOBVISTA AD /////////////////////////////////////////////////////////////
  445.  
  446. // INICIO ADATHA AD ////////////////////////////////////////////////////////////
  447. function loadAdathaAd() {
  448.     try {
  449.         var propsrc = "http://" + ns_ip + ":" + ns_port + "/nadip/iframeadatha?";
  450.         propsrc += "subscriberId=" + subscriberId;
  451.         propsrc += "&subscriberIP=" + subscriberIP;
  452.         propsrc += "&adId=" + nadipad.adid;
  453.         propsrc += "&viewKey=" + JSON.stringify(matchedView.viewKey);
  454.         buildAdNetworkBanner(propsrc);
  455.     } catch (err) {
  456.         logError(err, "loadInmobiAd");
  457.     }
  458. }
  459. // FIN INMOBI AD ///////////////////////////////////////////////////////////////
  460.  
  461. // METODO AUXILIAR DE CREADOR DE AD NETWORK BANNER /////////////////////////////
  462. function buildAdNetworkBanner(adNetworkSource) {
  463.     // Obtiene las propiedades del Ad
  464.     var propPosition = matchedView.position;
  465.     var propHorizontalPosition = matchedView.horizontalposition;
  466.     var propWidth = matchedView.adwidth;
  467.     var propHeight = matchedView.adheight;
  468.     var removeScrollBody = (matchedView.removescrollbody === 'true');
  469.     var fondoOscuro = (matchedView.backgroundblack === 'true');
  470.  
  471.     // Crea iframe y setea valores generales
  472.     var ifrm = document.createElement("IFRAME");
  473.     ifrm.setAttribute("src", adNetworkSource);
  474.     ifrm.setAttribute("id", AD_ID);
  475.     ifrm.setAttribute("allowFullScreen", '');
  476.     ifrm.style.width = getValue2(propWidth);
  477.     ifrm.style.height = getValue2(propHeight);
  478.     ifrm.style.position = 'fixed';
  479.     ifrm.style.border = 'none';
  480.     ifrm.style.overflow = 'hidden';
  481.     ifrm.style.margin = '0 auto';
  482.     ifrm.style.zIndex = Z_INDEX_MAIN;
  483.     ifrm.style.display = 'block';
  484.     ifrm.style.padding = '0px';
  485.  
  486.     // Calcula y setea centrado horizontal
  487.     if (propHorizontalPosition === undefined || propHorizontalPosition === null || propHorizontalPosition === "CENTERED") {
  488.         // Calcula el centrado horizontal
  489.         var windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  490.         var calculatedLeft = windowWidth / 2 - calculatePropValue(propWidth, windowWidth) / 2;
  491.         ifrm.style.left = calculatedLeft + "px";
  492.     } else if (propHorizontalPosition === "LEFT") {
  493.         ifrm.style.left = 0 + "px";
  494.     } else if (propHorizontalPosition === "RIGHT") {
  495.         ifrm.style.right = 0 + "px";
  496.     }
  497.  
  498.     // Calcula y setea el centrado vertical de acuerdo a lo seleccionado
  499.     if (propPosition === undefined || propPosition === null || propPosition === "CENTERED") {
  500.         // Calcula el centrado vertical
  501.         var windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
  502.         var calculatedTop = windowHeight / 2 - calculatePropValue(propHeight, windowHeight) / 2;
  503.         ifrm.style.top = calculatedTop + "px";
  504.     } else if (propPosition === "TOP") {
  505.         ifrm.style.top = 0 + "px";
  506.     } else if (propPosition === "BOTTOM") {
  507.         ifrm.style.bottom = 0 + "px";
  508.     }
  509.  
  510.     if (document.body !== null && document.body !== undefined) {
  511.         // Inserta el iframe en el documento
  512.         if (document.body.firstChild) {
  513.             document.body.insertBefore(ifrm, document.body.firstChild);
  514.         } else {
  515.             document.body.appendChild(ifrm);
  516.         }
  517.         // Oculta el scroll segun corresponda
  518.         if (removeScrollBody) {
  519.             document.getElementsByTagName("body")[0].style.overflow = "hidden";
  520.         }
  521.         // Oscurece fondo o no segun corresponda
  522.         if (fondoOscuro) {
  523.             var divfondooscuro = document.createElement("div");
  524.             divfondooscuro.id = AD_BACKGROUND_ID;
  525.             document.body.appendChild(divfondooscuro);
  526.             loadCssPropertiesInstertitialBackground();
  527.         }
  528.         // Setea accion final del Ad
  529.         setFinalAction();
  530.     } else {
  531.         logError("(src=" + adNetworkSource + "): document.body is " + document.body, "buildAdNetworkBanner");
  532.     }
  533. }
  534.  
  535. function calculatePropValue(propWidth, windowWidth) {
  536.     if (propWidth.indexOf("%") < 0) {
  537.         try {
  538.             return parseInt(propWidth);
  539.         } catch (err) {
  540.             return propWidth;
  541.         }
  542.     } else {
  543.         return (parseInt(propWidth.replace("%", "")) * windowWidth) / 100;
  544.     }
  545. }
  546.  
  547. // INICIO VERTICAL AD //////////////////////////////////////////////////////////
  548. function loadVerticalAd(adType) {
  549.     // console.log("loadAdsNADIP (" + adType + " - adid: " + nadipad.adid + ")");
  550.  
  551.     // Arma div
  552.     var htmlDiv = "<div id='" + AD_ID + "' onclick='clickOnAdNADIPWithLink(this)'><a id='adButtonClose' onclick='closeAdNADIP()'>x</a></div>";
  553.     // Agrega div al body segun corresponda
  554.     switch (adType) {
  555.     case "BANNER":
  556.         jQuery("body").append(htmlDiv);
  557.         break;
  558.     case "OVERSLIDE":
  559.         jQuery("body").prepend(htmlDiv);
  560.         break;
  561.     }
  562.     // Carga propiedades de Main
  563.     loadCssPropertiesVerticalMain();
  564.     // Ubica horizontalmente segun corresponda
  565.     switch (adType) {
  566.     case "BANNER":
  567.         var mainLeft = getValue(matchedView.left, "0px");
  568.         jQuery("#" + AD_ID).css("left", mainLeft);
  569.         break;
  570.     case "OVERSLIDE":
  571.         var mainRight = getValue(matchedView.right, "0px");
  572.         jQuery("#" + AD_ID).css("right", mainRight);
  573.         break;
  574.     }
  575.     // Carga propiedades de Boton X
  576.     loadCssPropertiesButtonClose();
  577.     // Indica si sirvio el Ad
  578.     servedAdNADIP();
  579.     // Setea la accion final a realizar
  580.     setFinalAction();
  581. }
  582.  
  583. function loadCssPropertiesVerticalMain() {
  584.     // Prepara propiedades de div principal
  585.     var mainZIndex = Z_INDEX_MAIN;
  586.     var mainDisplay = MAIN_DISPLAY;
  587.     var mainPosition = "fixed";
  588.         var mainVerticalPosition = matchedView.position;
  589.     var mainWidth = getValue(matchedView.adwidth, "200px");
  590.     var mainHeight = getValue(matchedView.adheight, "600px");
  591.     var mainBackgroundImage = matchedView.imgurl;
  592.     var mainBackgroundRepeat = "round";        
  593.     var mainBackgroundSize = "cover";        
  594.     // Setea propiedades del div principal
  595.     jQuery("#" + AD_ID).css("z-index", mainZIndex);
  596.     jQuery("#" + AD_ID).css("display", mainDisplay);
  597.     jQuery("#" + AD_ID).css("position", mainPosition);
  598.     jQuery("#" + AD_ID).css("width", mainWidth);
  599.     jQuery("#" + AD_ID).css("height", mainHeight);
  600.     jQuery("#" + AD_ID).css("background-image", "url(" + mainBackgroundImage + ")");
  601.     jQuery("#" + AD_ID).css("cursor", "pointer");        
  602.     jQuery("#" + AD_ID).fadeIn("slow");
  603.     jQuery("#" + AD_ID).css("background-repeat", mainBackgroundRepeat);
  604.     jQuery("#" + AD_ID).css("background-size", mainBackgroundSize);      
  605.        
  606.         // Calcula y setea el centrado vertical de acuerdo a lo seleccionado
  607.         if (mainVerticalPosition === undefined || mainVerticalPosition === null || mainVerticalPosition === "CENTERED") {
  608.             var windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
  609.             if (mainHeight.indexOf("%") >= 0) {
  610.                 mainHeight = windowHeight * (parseInt(mainHeight) / 100);
  611.             }            
  612.             var calculatedTop = windowHeight / 2 - mainHeight / 2;
  613.             jQuery("#" + AD_ID).css("top", calculatedTop + "px");
  614.         } else if (mainVerticalPosition === "TOP") {
  615.             jQuery("#" + AD_ID).css("top", 0 + "px");
  616.         } else if (mainVerticalPosition === "BOTTOM") {
  617.             jQuery("#" + AD_ID).css("bottom", 0 + "px");
  618.         }
  619. }
  620. // FIN VERTICAL AD /////////////////////////////////////////////////////////////
  621.  
  622. // INICIO HORIZONTAL AD ////////////////////////////////////////////////////////
  623. function loadHorizontalAd(adType) {
  624.     // console.log("loadAdsNADIP (" + adType + " - adid: " + nadipad.adid + ")");
  625.  
  626.     // Arma div
  627.     var htmlDiv = "<div id='" + AD_ID + "' onclick='clickOnAdNADIPWithLink(this)'><a id='adButtonClose' onclick='closeAdNADIP()'>x</a></div>";
  628.     // Agrega div al body segun corresponda
  629.     switch (adType) {
  630.     case "TOP_AD":
  631.         jQuery("body").append(htmlDiv);
  632.         break;
  633.     case "FLOORAD":
  634.         jQuery("body").prepend(htmlDiv);
  635.         break;
  636.     }
  637.     // Carga propiedades de Main
  638.     loadCssPropertiesHorizontalMain();
  639.     // Ubica horizontalmente segun corresponda
  640.     switch (adType) {
  641.     case "TOP_AD":
  642.         var mainTop = getValue(matchedView.top, "0px");
  643.         jQuery("#" + AD_ID).css("top", mainTop);
  644.         break;
  645.     case "FLOORAD":
  646.         var mainBottom = getValue(matchedView.bottom, "0px");
  647.         jQuery("#" + AD_ID).css("bottom", mainBottom);
  648.         break;
  649.     }
  650.     // Carga propiedades de Boton X
  651.     loadCssPropertiesButtonClose();
  652.     // Indica si sirvio el Ad
  653.     servedAdNADIP();
  654.     // Setea la accion final a realizar
  655.     setFinalAction();
  656. }
  657.  
  658. function loadCssPropertiesHorizontalMain() {
  659.     // Prepara propiedades de div principal
  660.     var mainZIndex = Z_INDEX_MAIN;
  661.     var mainDisplay = MAIN_DISPLAY;
  662.     var mainPosition = "fixed";
  663.         var mainHorizontalPosition = matchedView.horizontalposition;        
  664.         var mainWidth = getValue(matchedView.adwidth, "100%");
  665.     var mainHeight = getValue(matchedView.adheight, "10%");
  666.     var mainBackgroundImage = matchedView.imgurl;
  667.     var mainBackgroundRepeat = "round";
  668.     // var mainBackgroundPosition = "center center";
  669.     var mainBackgroundSize = "cover";
  670.     // Setea propiedades del div principal
  671.     jQuery("#" + AD_ID).css("z-index", mainZIndex);
  672.     jQuery("#" + AD_ID).css("display", mainDisplay);
  673.     jQuery("#" + AD_ID).css("position", mainPosition);
  674.     jQuery("#" + AD_ID).css("width", mainWidth);
  675.     jQuery("#" + AD_ID).css("height", mainHeight);
  676.     jQuery("#" + AD_ID).css("background-image", 'url(' + mainBackgroundImage + ')');
  677.     // jQuery("#" + AD_ID).css("background-position", mainBackgroundPosition);
  678.     jQuery("#" + AD_ID).css("background-repeat", mainBackgroundRepeat);
  679.     jQuery("#" + AD_ID).css("background-size", mainBackgroundSize);
  680.     jQuery("#" + AD_ID).css("cursor", "pointer");        
  681.     jQuery("#" + AD_ID).fadeIn("slow");
  682.        
  683.         // Calcula y setea centrado horizontal
  684.         if (mainHorizontalPosition === undefined || mainHorizontalPosition === null || mainHorizontalPosition === "CENTERED") {
  685.             var windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  686.             if (mainWidth.indexOf("%") >= 0) {
  687.                 mainWidth = windowWidth * (parseInt(mainWidth) / 100);
  688.             }
  689.             var calculatedLeft = windowWidth / 2 - mainWidth / 2;
  690.             jQuery("#" + AD_ID).css("left", calculatedLeft + "px");            
  691.         } else if (mainHorizontalPosition === "LEFT") {
  692.             jQuery("#" + AD_ID).css("left", 0 + "px");            
  693.         } else if (mainHorizontalPosition === "RIGHT") {
  694.             jQuery("#" + AD_ID).css("right", 0 + "px");
  695.         }        
  696. }
  697. // FIN HORIZONTAL AD ///////////////////////////////////////////////////////////
  698.  
  699. // INICIO FIXED ////////////////////////////////////////////////////////////////
  700. function loadFixed() {
  701.     // console.log("loadAdsNADIP (FIXED - adid: " + nadipad.adid + ")");
  702.  
  703.     // Arma div
  704.     var htmlDiv = "<div id='" + AD_ID + "' onclick='clickOnAdNADIPWithLink(this)'><a id='adButtonClose' onclick='closeAdNADIP()'>x</a></div>";
  705.     // Agrega div al body segun corresponda
  706.     jQuery("body").append(htmlDiv);
  707.     // Carga propiedades de Main
  708.     loadCssPropertiesFixedMain();
  709.     // Carga propiedades de Boton X
  710.     loadCssPropertiesButtonClose();
  711.     // Indica si sirvio el Ad
  712.     servedAdNADIP();
  713.     // Setea la accion final a realizar
  714.     setFinalAction();
  715. }
  716.  
  717. function loadCssPropertiesFixedMain() {
  718.     // Prepara propiedades de div principal
  719.     var mainZIndex = Z_INDEX_MAIN;
  720.     var mainDisplay = MAIN_DISPLAY;
  721.     var mainPosition = "fixed";
  722.     var mainTop = getValue(matchedView.top, "0px");
  723.     var mainLeft = getValue(matchedView.left, "0px");
  724.     var mainWidth = getValue(matchedView.adwidth, "200px");
  725.     var mainHeight = getValue(matchedView.adheight, "200px");
  726.     var mainBackgroundImage = matchedView.imgurl;
  727.     var mainBackgroundRepeat = "round";
  728.     var mainBackgroundSize = "cover";        
  729.     // Setea propiedades del div principal
  730.     jQuery("#" + AD_ID).css("z-index", mainZIndex);
  731.     jQuery("#" + AD_ID).css("display", mainDisplay);
  732.     jQuery("#" + AD_ID).css("position", mainPosition);
  733.     jQuery("#" + AD_ID).css("top", mainTop);
  734.     jQuery("#" + AD_ID).css("left", mainLeft);
  735.     jQuery("#" + AD_ID).css("width", mainWidth);
  736.     jQuery("#" + AD_ID).css("height", mainHeight);
  737.     jQuery("#" + AD_ID).css("background-image", 'url(' + mainBackgroundImage + ')');
  738.     jQuery("#" + AD_ID).css("background-repeat", mainBackgroundRepeat);
  739.     jQuery("#" + AD_ID).css("background-size", mainBackgroundSize);
  740.     jQuery("#" + AD_ID).css("cursor", "pointer");        
  741.     jQuery("#" + AD_ID).fadeIn("slow");
  742. }
  743. // FIN FIXED ///////////////////////////////////////////////////////////////////
  744.  
  745. // INICIO REPEAT ///////////////////////////////////////////////////////////////
  746. function loadRepeat() {
  747.     // console.log("loadAdsNADIP (REPEAT - adid: " + nadipad.adid + ")");
  748.  
  749.     // Arma div
  750.     var htmlDiv = "<div id='" + AD_ID + "' onclick='clickOnAdNADIP(this)'><a id='adButtonClose' onclick='closeAdNADIP()'>x</a></div>";
  751.     // Agrega div al body
  752.     jQuery("body").append(htmlDiv);
  753.     // Carga propiedades de Main
  754.     loadCssPropertiesRepeatMain();
  755.     // Carga propiedades de Boton X
  756.     loadCssPropertiesButtonClose();
  757.     // Indica si sirvio el Ad
  758.     servedAdNADIP();
  759.     // Setea la accion final a realizar
  760.     setFinalAction();
  761. }
  762.  
  763. function loadCssPropertiesRepeatMain() {
  764.     // Prepara propiedades del div principal
  765.     var mainZIndex = Z_INDEX_MAIN;
  766.     var mainDisplay = MAIN_DISPLAY;
  767.     var mainPosition = "fixed";
  768.     var mainTop = getValue(matchedView.top, "0px");
  769.     var mainLeft = getValue(matchedView.left, "0px");
  770.     var mainWidth = "100%";
  771.     var mainHeight = getValue(matchedView.adheight, "200px");
  772.     var mainBackgroundImage = matchedView.imgurl;
  773.     // Setea propiedades del div principal
  774.     jQuery("#" + AD_ID).css("z-index", mainZIndex);
  775.     jQuery("#" + AD_ID).css("display", mainDisplay);
  776.     jQuery("#" + AD_ID).css("position", mainPosition);
  777.     jQuery("#" + AD_ID).css("top", mainTop);
  778.     jQuery("#" + AD_ID).css("left", mainLeft);
  779.     jQuery("#" + AD_ID).css("width", mainWidth);
  780.     jQuery("#" + AD_ID).css("height", mainHeight);
  781.     jQuery("#" + AD_ID).css("background-image", 'url(' + mainBackgroundImage + ')');
  782.     jQuery("#" + AD_ID).fadeIn("slow");
  783. }
  784. // FIN REPEAT //////////////////////////////////////////////////////////////////
  785.  
  786. // INICIO INTERSTITIAL | FLASH /////////////////////////////////////////////////
  787. function loadInstertitialAd(adType) {
  788.     // console.log("loadAdsNADIP (" + adType + " - adid: " + nadipad.adid + ")");
  789.  
  790.     // Arma htmlDiv segun corresponda
  791.     var htmlDiv;
  792.     switch (adType) {
  793.     case "INTERSTITIAL":
  794.         htmlDiv = "<div id='" + AD_ID + "' onclick='clickOnAdNADIPWithLink(this)'><a id='adButtonClose' onclick='closeAdNADIP()'>x</a></div>";
  795.         htmlDiv += "<div id='adBackground'></div>";
  796.         break;
  797.     case "INTERSTITIAL_FLASH":
  798.         // Arma div
  799.         htmlDiv = "<div id='" + AD_ID + "' onclick='clickOnAdNADIP(this)'><a id='adButtonClose' onclick='closeAdNADIP()'>x</a>";
  800.         htmlDiv += "<embed id='flashElement' src='" + matchedView.imgurl + "'</embed>";
  801.         htmlDiv += "</div><div id='adBackground'></div>";
  802.         break;
  803.     }
  804.     // Agrega div al body segun corresponda
  805.     jQuery("body").prepend(htmlDiv);
  806.     // Carga propiedades de Main
  807.     loadCssPropertiesInstertitialMain();
  808.     // Carga propiedades de Boton X
  809.     loadCssPropertiesButtonClose();
  810.     // Carga propiedades de Background
  811.     loadCssPropertiesInstertitialBackground();
  812.     // Deshabilita el scroll de la pantalla
  813.     enableBodyScroll(false);
  814.     // Indica si sirvio el Ad
  815.     servedAdNADIP();
  816.     // Setea la accion final a realizar
  817.     setFinalAction();
  818. }
  819.  
  820. function loadCssPropertiesInstertitialMain() {
  821.     // Prepara propiedades de div principal
  822.     var mainZIndex = Z_INDEX_MAIN;
  823.     var mainDisplay = "none";
  824.     var mainPosition = "fixed";
  825.     var main_position = "absolute"; /* hack for internet explorer 6 */
  826.     var mainTop = "50%";
  827.     var mainLeft = "50%";
  828.     var mainWidth = getValue(matchedView.adwidth, "200px");
  829.     var mainHeight = getValue(matchedView.adheight, "200px");
  830.     var mainBorder = "1px solid #CCCCCC";
  831.     var mainBackground = "#FFFFFF";
  832.     var mainBackgroundSize = "cover";
  833.     var mainBackgroundRepeat = "round";
  834.     var mainBackgroundImage = matchedView.imgurl;
  835.     // Setea propiedades del div principal
  836.     jQuery("#" + AD_ID).css("z-index", mainZIndex);
  837.     jQuery("#" + AD_ID).css("display", mainDisplay);
  838.     jQuery("#" + AD_ID).css("position", mainPosition);
  839.     jQuery("#" + AD_ID).css("_position", main_position);
  840.     jQuery("#" + AD_ID).css("top", mainTop);
  841.     jQuery("#" + AD_ID).css("left", mainLeft);
  842.     jQuery("#" + AD_ID).css("width", mainWidth);
  843.     jQuery("#" + AD_ID).css("height", mainHeight);
  844.     jQuery("#" + AD_ID).css("border", mainBorder);
  845.     jQuery("#" + AD_ID).css("background", mainBackground);
  846.     jQuery("#" + AD_ID).css("background-size", mainBackgroundSize);
  847.     jQuery("#" + AD_ID).css("background-repeat", mainBackgroundRepeat);
  848.     jQuery("#" + AD_ID).css("background-image", 'url(' + mainBackgroundImage + ')');
  849.     jQuery("#" + AD_ID).css("transform", "translate(-50%, -50%)");
  850.     jQuery("#" + AD_ID).css("cursor", "pointer");
  851.     jQuery("#" + AD_ID).fadeIn("slow");
  852.     // FLASH
  853.     if (document.getElementById("flashElement") !== null) {
  854.         jQuery("#flashElement").attr('width', mainWidth);
  855.         jQuery("#flashElement").attr('height', mainHeight);
  856.     }
  857. }
  858.  
  859. function loadCssPropertiesInstertitialBackground() {
  860.     // Prepara propiedades de background
  861.     var backgroundZIndex = Z_INDEX_BACKGROUND;
  862.     var backgroundDisplay = "none";
  863.     var backgroundPosition = "fixed";
  864.     var background_position = "absolute"; /* hack for internet explorer 6 */
  865.     var backgroundTop = "0px";
  866.     var backgroundLeft = "0px";
  867.     var backgroundWidth = "100%";
  868.     var backgroundHeight = "100%";
  869.     var backgroundBackground = "#000000";
  870.     // Setea propiedades de background
  871.     jQuery("#adBackground").css("z-index", backgroundZIndex);
  872.     jQuery("#adBackground").css("display", backgroundDisplay);
  873.     jQuery("#adBackground").css("position", backgroundPosition);
  874.     jQuery("#adBackground").css("_position", background_position);
  875.     jQuery("#adBackground").css("top", backgroundTop);
  876.     jQuery("#adBackground").css("left", backgroundLeft);
  877.     jQuery("#adBackground").css("width", backgroundWidth);
  878.     jQuery("#adBackground").css("height", backgroundHeight);
  879.     jQuery("#adBackground").css("background", backgroundBackground);
  880.     jQuery("#adBackground").css({
  881.         "opacity" : "0.7"
  882.     });
  883.     jQuery("#adBackground").fadeIn("fast");
  884. }
  885. // FIN INTERSTITIAL | FLASH ////////////////////////////////////////////////////
  886.  
  887. // INICIO CUSTOMIZED ///////////////////////////////////////////////////////////
  888. function loadCustomizedAd(adType) {
  889.     // console.log("loadAdsNADIP (" + adType + " - adid: " + nadipad.adid + ")");
  890.  
  891.     // Obtiene div custom
  892.     var divCustomized = matchedView.divCustomized;
  893.     // Agrega div al body
  894.     jQuery("body").append(divCustomized);
  895.     // Indica si sirvio el Ad
  896.     servedAdNADIP();
  897.     // Setea la accion final a realizar
  898.     setFinalAction();
  899. }
  900. // FIN CUSTOMIZED //////////////////////////////////////////////////////////////
  901.  
  902. // FUNCIONES AUXILIARES ////////////////////////////////////////////////////////
  903. function getValue(value, defaultValue) {
  904.     return (value !== undefined && value !== null && value.length !== 0) ? value : defaultValue;
  905. }
  906.  
  907. function getValue2(value) {
  908.     if (value.indexOf("%") < 0) {
  909.         try {
  910.             return parseInt(value) + "px";
  911.         } catch (err) {
  912.             return value;
  913.         }
  914.     } else {
  915.         return value;
  916.     }
  917. }
  918.  
  919. function loadCssPropertiesButtonClose() {
  920.     // Prepara propiedades del div Close
  921.     var closeDisplay = "block";
  922.     var closeZIndex = Z_INDEX_MAIN;
  923.     var closePosition = "absolute";
  924.     var closeWidth = "10px";
  925.     var closeHeight = "15px";
  926.     var closeRight = "0px";
  927.     var closeTop = "0px";
  928.     var closeMarginTop = "-5px";
  929.     var closeOverflow = "visible";
  930.     var closeCursor = "pointer";
  931.     var closeColor = "#CCCCCC";
  932.     var closeFontFamily = "verdana";
  933.     var closeFontSize = "14px";
  934.     var closeFontWeight = "bold";
  935.     // var backgroundColor = "black";
  936.     // Setea propiedades del div de Close
  937.     jQuery("#adButtonClose").css("float", "right");
  938.     jQuery("#adButtonClose").css("display", closeDisplay);
  939.     jQuery("#adButtonClose").css("z-index", closeZIndex);
  940.     jQuery("#adButtonClose").css("position", closePosition);
  941.     jQuery("#adButtonClose").css("width", closeWidth);
  942.     jQuery("#adButtonClose").css("height", closeHeight);
  943.     jQuery("#adButtonClose").css("right", closeRight);
  944.     jQuery("#adButtonClose").css("top", closeTop);
  945.     jQuery("#adButtonClose").css("margin-top", closeMarginTop);
  946.     jQuery("#adButtonClose").css("overflow", closeOverflow);
  947.     jQuery("#adButtonClose").css("cursor", closeCursor);
  948.     jQuery("#adButtonClose").css("color", closeColor);
  949.     jQuery("#adButtonClose").css("font-family", closeFontFamily);
  950.     jQuery("#adButtonClose").css("font-size", closeFontSize);
  951.     jQuery("#adButtonClose").css("font-weight", closeFontWeight);
  952.     // jQuery("#adButtonClose").css("background-color", "black");
  953. }
  954.  
  955. function enableBodyScroll(enabled) {
  956.     if (enabled) {
  957.         jQuery("body").css("overflow", "auto");
  958.     } else {
  959.         jQuery("body").css("overflow", "hidden");
  960.     }
  961. }
  962.  
  963. var autocloseTimeout;
  964. function setFinalAction() {
  965.     try {
  966.         var autocloseTime = parseInt(getValue(matchedView.autoClose, 0));
  967.         if (autocloseTime > 0) {
  968.             autocloseTimeout = setTimeout(function() {
  969.                 closeAdNADIP(true, false);
  970.             }, autocloseTime * 1000);
  971.         } else {
  972.             // console.log("Autoclose disabled");
  973.             var cycleTime = parseInt(getValue(matchedView.cycle, 0));
  974.             if (cycleTime > 0) {
  975.                 setTimeout(function() {
  976.                     closeAdNADIP(true, false);
  977.                     loadNextAd();
  978.                 }, cycleTime * 1000);
  979.             } else {
  980.                 // console.log("Cycle disabled");
  981.             }
  982.         }
  983.     } catch (err) {
  984.         logError(err, "setFinalAction");
  985.     }
  986. }
  987.  
  988. function setSleepTime() {
  989.     try {
  990.         var cycleTime = parseInt(getValue(matchedView.cycle, 0));
  991.         if (cycleTime === 0) {
  992.             var sleepTime = parseInt(getValue(matchedView.sleep, 0));
  993.             if (sleepTime > 0) {
  994.                 setTimeout(function() {
  995.                     loadNextAd();
  996.                 }, sleepTime * 1000);
  997.             } else {
  998.                 // console.log("Sleep disabled");
  999.             }
  1000.         } else {
  1001.             // console.log("Cannot sleep because Cycle is enabled");
  1002.         }
  1003.     } catch (err) {
  1004.         logError(err, "setSleepTime");
  1005.     }
  1006. }
  1007.  
  1008. function nadipNextAdOnAdError() {
  1009.     closeAdNADIP(false, true);
  1010.     loadNextAdOnFallback();
  1011. }
  1012.  
  1013. function loadNextAdOnFallback() {
  1014.     if (firstad !== undefined && firstad !== null && firstad.adid !== undefined && firstad.adid !== null) {
  1015.         jQuery.ajax({
  1016.             type : "GET",
  1017.             url : "http://" + ns_ip + ":" + ns_port + "/api/getad",
  1018.             crossDomain : true,
  1019.             dataType : 'json',
  1020.             data : {
  1021.                 "subscriberId" : subscriberId,
  1022.                 "subscriberIP" : subscriberIP,
  1023.                 "nadipdata" : JSON.stringify(nadipdata),
  1024.                 "screenheight" : screen.height,
  1025.                 "screenwidth" : screen.width,
  1026.                 "firstad" : firstad.adid,
  1027.                 "cycle" : false
  1028.             },
  1029.             cache : false,
  1030.             success : function(data) {
  1031.                 console.log("loadNextAdOnFallback data: " + JSON.stringify(data));
  1032.                 // Carga valores
  1033.                 try {
  1034.                     if (data !== null && data !== undefined && data.error !== null && data.error !== undefined && !data.error) {
  1035.                         var oldnadipad = nadipad;
  1036.                         nadipad = JSON.parse(data.nadipad);
  1037.                         if (nadipad !== undefined && nadipad !== null && nadipad.adid !== undefined && nadipad.adid != null && nadipad.adid != firstad.adid) {
  1038.                             matchedView = JSON.parse(data.matchedView);
  1039.                             if (data.hasOwnProperty('nadipcategories') && data.nadipcategories) {
  1040.                                 nadipcategories = JSON.parse(data.nadipcategories);
  1041.                             }
  1042.                             if (data.hasOwnProperty('nadipadgroups')) {
  1043.                                 nadipadgroups = JSON.parse(data.nadipadgroups);
  1044.                             }
  1045.                             if (data.hasOwnProperty('nadipadcampaignid')) {
  1046.                                 nadipadcampaignid = JSON.parse(data.nadipadcampaignid);
  1047.                             }
  1048.                             injected_uuid = JSON.parse(data.injected_uuid);
  1049.                             ns_ip = data.ns_ip;
  1050.                             ns_port = data.ns_port;
  1051.  
  1052.                             // Remueve el Ad existente
  1053.                             jQuery('#' + AD_ID).remove();
  1054.                             // Carga nuevo Ad
  1055.                             loadAd();
  1056.                         } else {
  1057.                             nadipad = oldnadipad;
  1058.                             logError("No view in session", "loadNextAdOnFallback: nadipdata: " + JSON.stringify(data));
  1059.                         }
  1060.                     } else {
  1061.                         logError("No data in getad", "loadNextAdOnFallback: nadipdata: " + JSON.stringify(data));
  1062.                     }
  1063.                 } catch (err) {
  1064.                     logError(err, "loadNextAdOnFallback: nadipdata: " + JSON.stringify(data));
  1065.                 }
  1066.             }
  1067.         });
  1068.     } else {
  1069.         logError(err, "Firstad undefined or null on loadNextAdOnFallback()");
  1070.     }
  1071.  
  1072. }
  1073.  
  1074. function loadNextAd() {
  1075.     try {
  1076.         jQuery.ajax({
  1077.             type : "GET",
  1078.             url : "http://" + ns_ip + ":" + ns_port + "/api/getad",
  1079.             crossDomain : true,
  1080.             dataType : 'json',
  1081.             data : {
  1082.                 "subscriberId" : subscriberId,
  1083.                 "subscriberIP" : subscriberIP,
  1084.                 "nadipdata" : JSON.stringify(nadipdata),
  1085.                 "screenheight" : screen.height,
  1086.                 "screenwidth" : screen.width,
  1087.                 "preadid" : nadipad.adid,
  1088.                 "cycle" : true
  1089.             },
  1090.             cache : false,
  1091.             success : function(data) {
  1092.                 // console.log("getAdByCycle data: " + JSON.stringify(data));
  1093.                 // Carga valores
  1094.                 matchedView = JSON.parse(data.matchedView);
  1095.                 nadipcategories = JSON.parse(data.nadipcategories);
  1096.                 nadipad = JSON.parse(data.nadipad);
  1097.                 if (data.hasOwnProperty('nadipadgroups')) {
  1098.                     nadipadgroups = JSON.parse(data.nadipadgroups);
  1099.                 }
  1100.                 if (data.hasOwnProperty('nadipadcampaignid')) {
  1101.                     nadipadcampaignid = JSON.parse(data.nadipadcampaignid);
  1102.                 }
  1103.                 ns_ip = data.ns_ip;
  1104.                 ns_port = data.ns_port;
  1105.                 // Remueve el Ad existente
  1106.                 jQuery('#' + AD_ID).remove();
  1107.                 // Carga nuevo Ad
  1108.                 loadAd();
  1109.             }
  1110.         });
  1111.     } catch (err) {
  1112.         logError(err, "loadNextAd");
  1113.     }
  1114. }
  1115.  
  1116. function loadFirstAd() {
  1117.     console.log(typeof nadipdata.adid);
  1118.     jQuery.ajax({
  1119.         type : "GET",
  1120.         url : "http://" + ns_ip + ":" + ns_port + "/api/getad",
  1121.         crossDomain : true,
  1122.         dataType : 'json',
  1123.         data : {
  1124.             "subscriberId" : subscriberId,
  1125.             "subscriberIP" : subscriberIP,
  1126.             "nadipdata" : JSON.stringify(nadipdata),
  1127.             "screenheight" : screen.height,
  1128.             "screenwidth" : screen.width,
  1129.             "preadid" : -1,
  1130.             "cycle" : false
  1131.         },
  1132.         cache : false,
  1133.         success : function(data) {
  1134.             console.log("loadFirstAd data: " + JSON.stringify(data));
  1135.             // Carga valores
  1136.             try {
  1137.                 if (data !== null && data !== undefined && data.error !== null && data.error !== undefined && !data.error) {
  1138.                     matchedView = JSON.parse(data.matchedView);
  1139.                     if (data.hasOwnProperty('nadipcategories') && data.nadipcategories) {
  1140.                         nadipcategories = JSON.parse(data.nadipcategories);
  1141.                     }
  1142.                     nadipad = JSON.parse(data.nadipad);
  1143.                     firstad = nadipad;
  1144.                     if (data.hasOwnProperty('nadipadgroups')) {
  1145.                         nadipadgroups = JSON.parse(data.nadipadgroups);
  1146.                     }
  1147.                     if (data.hasOwnProperty('nadipadcampaignid')) {
  1148.                         nadipadcampaignid = JSON.parse(data.nadipadcampaignid);
  1149.                     }
  1150.                     injected_uuid = JSON.parse(data.injected_uuid);
  1151.                     ns_ip = data.ns_ip;
  1152.                     ns_port = data.ns_port;
  1153.                     // Remueve el Ad existente
  1154.                     jQuery('#' + AD_ID).remove();
  1155.                     // Carga nuevo Ad
  1156.                     loadAd();
  1157.                 } else {
  1158.                     logError("No data in getad", "loadFirstAd: nadipdata: " + JSON.stringify(data));
  1159.                 }
  1160.             } catch (err) {
  1161.                 logError(err, "loadFirstAd : nadipdata: " + JSON.stringify(data));
  1162.             }
  1163.         }
  1164.     });
  1165. }
  1166.  
  1167. function clickOnAdNADIPWithLink() {
  1168.     try {
  1169.         saveStatsNADIP("clicked");
  1170.         window.open(matchedView.link, '_blank');
  1171.         closeAdNADIP(false, true);
  1172.     } catch (err) {
  1173.         logError(err, "clickOnAdNADIPWithLink");
  1174.     }
  1175. }
  1176.  
  1177. function clickOnAdNADIP(element) {
  1178.     try {
  1179.         // console.log('clicked!');
  1180.         saveStatsNADIP("clicked");
  1181.         // window.open(matchedView.link, '_blank');
  1182.     } catch (err) {
  1183.         logError(err, "clickOnAdNADIP");
  1184.     }
  1185. }
  1186.  
  1187. function closeAdNADIP(isAutoclose, noStats, e) {
  1188.     try {
  1189.         if (!e) {
  1190.             var e = window.event;
  1191.         }
  1192.         if (typeof e !== 'undefined') {
  1193.             e.cancelBubble = true;
  1194.             if (e.stopPropagation)
  1195.                 e.stopPropagation();
  1196.         }
  1197.         if (jQuery('#' + AD_ID).length !== 0) {
  1198.             var closeType;
  1199.             if (getValue(isAutoclose, false)) {
  1200.                 closeType = "autoclosed";
  1201.             } else {
  1202.                 closeType = "closed";
  1203.                 clearTimeout(autocloseTimeout);
  1204.             }
  1205.             // Console.log('' + closeType + '!');
  1206.             if (!getValue(noStats, false)) {
  1207.                 saveStatsNADIP(closeType);
  1208.             }
  1209.  
  1210.             jQuery("#" + AD_ID).remove();
  1211.             if (document.getElementById(AD_BACKGROUND_ID) !== null) {
  1212.                 jQuery("#" + AD_BACKGROUND_ID).fadeOut("slow");
  1213.             }
  1214.         }
  1215.         // Activa Scroll nuevamente
  1216.         enableBodyScroll(true);
  1217.         // Sleep Time
  1218.         setSleepTime();
  1219.     } catch (err) {
  1220.         logError(err, "closeAdNADIP");
  1221.     }
  1222. }
  1223.  
  1224. function clearProperties() {
  1225.     matchedView = null;
  1226.     nadipcategories = null;
  1227.     nadipad = null;
  1228.     nadipadgroups = null;
  1229.     nadipadcampaignid = null;
  1230.     ns_ip = null;
  1231.     ns_port = null;
  1232.     mapaResult = null;
  1233. }
  1234.  
  1235. function servedAdNADIP(e) {
  1236.     try {
  1237.         if (!e) {
  1238.             var e = window.event;
  1239.         }
  1240.         if (typeof e !== 'undefined') {
  1241.             e.cancelBubble = true;
  1242.             if (e.stopPropagation)
  1243.                 e.stopPropagation();
  1244.         }
  1245.         if (jQuery('#' + AD_ID).length !== 0) {
  1246.             // console.log('served!');
  1247.             saveStatsNADIP("served");
  1248.         }
  1249.     } catch (err) {
  1250.         logError(err, "servedAdNADIP");
  1251.     }
  1252. }
  1253.  
  1254. function requestedAdNADIP(e) {
  1255.     try {
  1256.         if (!e) {
  1257.             var e = window.event;
  1258.         }
  1259.         if (typeof e !== 'undefined') {
  1260.             e.cancelBubble = true;
  1261.             if (e.stopPropagation)
  1262.                 e.stopPropagation();
  1263.         }
  1264.         if (jQuery('#' + AD_ID).length !== 0) {
  1265.             console.log('requested!');
  1266.             saveStatsNADIP("requested");
  1267.         }
  1268.     } catch (err) {
  1269.         logError(err, "requestedAdNADIP");
  1270.     }
  1271. }
  1272.  
  1273. function loadListener() {
  1274.     try {
  1275.         if (window.addEventListener) {
  1276.             addEventListener("message", listenerFormSubmission, false);
  1277.         } else {
  1278.             attachEvent("onmessage", listenerFormSubmission);
  1279.         }
  1280.     } catch (err) {
  1281.         logError(err, "loadListener");
  1282.     }
  1283. }
  1284.  
  1285. function listenerFormSubmission(event) {
  1286.     try {
  1287.         console.log("llego listener. " + event.origin + " " + event.data);
  1288.         if (event.origin === "http://" + ns_ip + ":" + ns_port) {
  1289.             if (event.data.indexOf("nadipformsubmitted") === 0) {
  1290.                 var dataParams = event.data.substring(18, event.data.length);
  1291.                 var params = JSON.parse(dataParams);
  1292.                 formSubmittedAdNADIP(params);
  1293.             } else if (event.data === "nadipclose") {
  1294.                 closeAdNADIP(false, false);
  1295.             } else if (event.data === "nadipautoclose") {
  1296.                 closeAdNADIP(true, false);
  1297.             } else if (event.data === "nadipclosenostats") {
  1298.                 closeAdNADIP(true, true);
  1299.             } else if (event.data === "nadipclick") {
  1300.                 clickOnAdNADIP();
  1301.             } else if (event.data === "nadipserve") {
  1302.                 servedAdNADIP();
  1303.             } else if (event.data === "nadipblock") {
  1304.                 logError("Error por block de  ad", "adBlock");
  1305.             } else if (event.data === "nadiprequestad") {
  1306.                 requestedAdNADIP();
  1307.             } else if (event.data === "nadipfallbackonerror") {
  1308.                 nadipNextAdOnAdError();
  1309.             } else if (event.data.indexOf("nadiperror") === 0) {
  1310.                 var message = event.data.substring(10, event.data.length);
  1311.                 logError(message, "adError");
  1312.             }
  1313.         }
  1314.     } catch (err) {
  1315.         logError(err, "listenerFormSubmission");
  1316.     }
  1317. }
  1318.  
  1319. function formSubmittedAdNADIP(params, e) {
  1320.     try {
  1321.         if (!e) {
  1322.             var e = window.event;
  1323.         }
  1324.         if (typeof e !== 'undefined') {
  1325.             e.cancelBubble = true;
  1326.             if (e.stopPropagation)
  1327.                 e.stopPropagation();
  1328.         }
  1329.         if (jQuery('#' + AD_ID).length !== 0) {
  1330.             // console.log('formsubmitted!');
  1331.             saveStatsNADIP("formsubmitted", params);
  1332.             jQuery("#" + AD_ID).remove();
  1333.             if (document.getElementById(AD_BACKGROUND_ID) !== null) {
  1334.                 jQuery("#adBackground").fadeOut("slow");
  1335.             }
  1336.         }
  1337.         // Activa Scroll nuevamente
  1338.         enableBodyScroll(true);
  1339.         // Sleep Time
  1340.         setSleepTime();
  1341.     } catch (err) {
  1342.         logError(err, "formSubmittedAdNADIP");
  1343.     }
  1344. }
  1345.  
  1346. /**
  1347.  * Este se usa en el formulario de tipo foto
  1348.  *
  1349.  * @param {type}
  1350.  *            params
  1351.  * @param {type}
  1352.  *            e
  1353.  * @returns {undefined}
  1354.  */
  1355. function formSubmissionAdNADIP(params, e) {
  1356.     try {
  1357.         if (!e) {
  1358.             var e = window.event;
  1359.         }
  1360.         if (typeof e !== 'undefined') {
  1361.             e.cancelBubble = true;
  1362.             if (e.stopPropagation)
  1363.                 e.stopPropagation();
  1364.         }
  1365.         if (jQuery('#' + AD_ID).length !== 0) {
  1366.             console.log("params1: " + JSON.stringify(params));
  1367.             saveStatsNADIP("formsubmited", params);
  1368.             jQuery("#" + AD_ID).remove();
  1369.             if (document.getElementById(AD_BACKGROUND_ID) !== null) {
  1370.                 jQuery("#adBackground").fadeOut("slow");
  1371.             }
  1372.         }
  1373.         // Activa Scroll nuevamente
  1374.         enableBodyScroll(true);
  1375.         // Sleep Time
  1376.         setSleepTime();
  1377.     } catch (err) {
  1378.         logError(err, "formSubmissionAdNADIP");
  1379.     }
  1380. }
  1381.  
  1382. function saveStatsNADIP(eventtype, params) {
  1383.     try {
  1384.         console.log("nadipadcampaignid: " + nadipadcampaignid + ", eventtype: " + eventtype);
  1385.         jQuery.ajax({
  1386.             type : "POST",
  1387.             url : "http://" + ns_ip + ":" + ns_port + "/api/ad/" + eventtype,
  1388.             crossDomain : true,
  1389.             dataType : 'json',
  1390.             data : {
  1391.                 "subscriberId" : subscriberId,
  1392.                 "subscriberIP" : subscriberIP,
  1393.                 "adid" : nadipad.adid,
  1394.                 "categories" : nadipcategories,
  1395.                 "adgroups" : nadipadgroups,
  1396.                 "adcampaignid" : nadipadcampaignid,
  1397.                 "params" : JSON.stringify(params),
  1398.                 "injected_uuid" : JSON.stringify(injected_uuid)
  1399.             },
  1400.             success : function(data) {
  1401.                 // console.log("Data Loaded: " + data);
  1402.             }
  1403.         });
  1404.     } catch (err) {
  1405.         logError(err, "saveStatsNADIP");
  1406.     }
  1407. }
  1408.  
  1409. function getBrowser() {
  1410.     return navigator.userAgent;
  1411. }
  1412.  
  1413. function isString(s) {
  1414.     return typeof (s) === 'string' || s instanceof String;
  1415. }
  1416.  
  1417. function logError(error, methodName) {
  1418.     try {
  1419.         var objNadipdata = null;
  1420.         var params = {};
  1421.         var errMessage = null;
  1422.         params["browser"] = getBrowser();
  1423.         if (subscriberIP !== undefined) {
  1424.             params["subscriberIP"] = subscriberIP;
  1425.         }
  1426.  
  1427.         if (nadipdata !== undefined) {
  1428.             if (isString(nadipdata)) {
  1429.                 try {
  1430.                     objNadipdata = JSON.parse(nadipdata);
  1431.                     params["adid"] = nadipad.adid;
  1432.                     params["host"] = objNadipdata.host;
  1433.                     params["subscriberId"] = objNadipdata.subscriberId;
  1434.                 } catch (e) {
  1435.                     console.log(e.message);
  1436.                 }
  1437.             } else {
  1438.                 params["host"] = nadipdata.host;
  1439.                 params["subscriberId"] = nadipdata.subscriberId;
  1440.             }
  1441.         }
  1442.  
  1443.         if (isString(error)) {
  1444.             errMessage = error;
  1445.         } else {
  1446.             errMessage = error.message;
  1447.         }
  1448.  
  1449.         var paramsStringify = JSON.stringify(params);
  1450.         console.log("ERROR " + error + " - methodName=" + methodName + ", params=" + paramsStringify);
  1451.         jQuery.ajax({
  1452.             type : "POST",
  1453.             url : "http://" + ns_ip + ":" + ns_port + "/api/logerror/",
  1454.             crossDomain : true,
  1455.             dataType : 'json',
  1456.             data : {
  1457.                 "error" : errMessage,
  1458.                 "methodName" : methodName,
  1459.                 "params" : paramsStringify
  1460.             },
  1461.             success : function(data) {
  1462.                 console.log("Log sended: " + data);
  1463.             }
  1464.         });
  1465.     } catch (err) {
  1466.         console.log("ERROR logError - " + err);
  1467.     }
  1468. }
  1469.  
  1470. (function() {
  1471.     loadAdsNADIP();
  1472. })();
Add Comment
Please, Sign In to add comment