/** * Consumer Reports - Donation Alert Banner for August * * Version 1.0.0 */ (function (_win, $) { 'use strict'; /*global window: false */ var _logging = false, _isDebug = false, _logg = function (msg, func) {return true; }, utils = {}; utils.config = {}; utils.constants = {}; utils.constants.APP_NAME = 'crDonationAlert'; utils.constants.OMNITURE_O_VALUE = 'Donation Alert', utils.constants.DONATION_LINK_DELAY = 1000, utils.constants.LOGGER_PREFIX = utils.constants.APP_NAME + ' -> ', utils.constants.DEBUG_HASH_NAME = '#' + utils.constants.APP_NAME + 'Debug'; utils.constants.NO_RUN_HASH_NAME = '#' + utils.constants.APP_NAME + 'NoRun'; utils.constants.INJECT_BANNER_TARGET_SELECTOR = '.global-header-container'; utils.constants.CALENDAR_ICON_IMAGE_URL = '//cdn.oas-c17.adnxs.com/RealMedia/ads/Creatives/Consumer/Donate_higlight_x97_May31/2016-Calendar-August-31.png/1423159657'; utils.constants.CLOSE_BUTTON_IMAGE_URL = '//cdn.oas-c17.adnxs.com/RealMedia/ads/Creatives/Consumer/Donate_higlight_x97_May31/close-button-x-grey-background.png/1423159657'; utils.constants.BANNER_CLOSED_COOKIE_NAME = utils.constants.APP_NAME + 'bannerClosed'; utils.constants.BANNER_CLOSED_COOKIE_DURATION = 5; utils.constants.DONATION_LINK_SELECTOR_BASE = '#crDonationAlertContainer #bbDonationsContainer a'; utils.constants.DONATION_URL_BASE = 'http://oascentral.consumerreports.org/RealMedia/ads/click_lx.ads/cu/cro/L12/{oasId}/{oasId2}/Consumer/Donate_higlight_x97_May31/{amount}.gif/?https://donateconsumers.org/ea-action/action?ea.client.id=1926&ea.campaign.id=54311&sourcecode=', /* Ask Amounts – $35 | $50 | $100 | Other Links: $35 = https://donateconsumers.org/ea-action/action?ea.client.id=1926&ea.campaign.id=54311&sourcecode=7023808121&ask=1000|500|100|50|35&ask_selected=35&en_txn6=7023808121 $50 = https://donateconsumers.org/ea-action/action?ea.client.id=1926&ea.campaign.id=54311&sourcecode=7023808122&ask=1000|500|100|50|35&ask_selected=50&en_txn6=7023808122 $100 = https://donateconsumers.org/ea-action/action?ea.client.id=1926&ea.campaign.id=54311&sourcecode=7023808123&ask=1000|500|100|50|35&ask_selected=100&en_txn6=7023808123 Other = https://donateconsumers.org/ea-action/action?ea.client.id=1926&ea.campaign.id=54311&sourcecode=7023808124&ask=1000|500|100|50|35&ask_selected=other&en_txn6=7023808124 */ //These are the parameters that will differ from the logged-out / logged-in versions: utils.constants.STANDARD_DONATION_ASK_AMOUNTS = '1000|500|100|50|35'; utils.constants.SELECTED_DONATION_LINK_INDEX = 1; utils.config.donation_1 = {copy: '$35', amount: '35', id: '7023808121', evar: 'donation10', url: '', oasId: '1561785601', oasId2: 'x07'}; utils.config.donation_2 = {copy: '$50', amount: '50', id: '7023808122', evar: 'donation10', url: '', oasId: '1561785601', oasId2: 'x08'}; utils.config.donation_3 = {copy: '$100', amount: '100', id: '7023808123', evar: 'donation10', url: '', oasId: '1561785601', oasId2: 'x09'}; utils.config.donation_4 = {copy: 'Other', amount: 'other', id: '7023808124', evar: 'donation10', url: '', oasId: '1561785601', oasId2: 'x10'}; //initialize the applcation utils.init = function () { var bannerClosedCookie = utils.readCookie(utils.constants.BANNER_CLOSED_COOKIE_NAME); //set the logging function _logg = utils.logg; //for local dev if (utils.isNoRunHash()) { utils.startLogging(); _logg('utils.init -> NO_RUN hash present: EXITING'); //exit return; } //set logging if indicated in the hash if (utils.isDebugHash()) {_isDebug = true; utils.startLogging(); } //if if (bannerClosedCookie && bannerClosedCookie === 'true') { _logg('banner already closed: EXITING'); //exit return; } //in case you are working locally $('#crDonationAlertContainer').remove(); $('.crDonationAlertContainer_style').remove(); //inject the custom CSS utils.addCustomCss(utils.getCssText()); //update the donation urls utils.config.donation_1.url = utils.buildDonationUrl(utils.config.donation_1); utils.config.donation_2.url = utils.buildDonationUrl(utils.config.donation_2); utils.config.donation_3.url = utils.buildDonationUrl(utils.config.donation_3); utils.config.donation_4.url = utils.buildDonationUrl(utils.config.donation_4); //start the application utils.start(); _logg('utils.init'); }; /** * Starts the application * * @return undefined */ utils.start = function () { //inject the banner utils.injectBanner(); //bind the close button utils.bindCloseButton(); //set the selected donation amount utils.setSelectedDonationAmount(utils.constants.SELECTED_DONATION_LINK_INDEX); //override the donation links so that omniture data is sent setTimeout(utils.overrideDonationLinksClickEvents, 500); _logg('utils.start'); }; /** * Logs debug messages * * @return undefined */ utils.logg = function (msg, func) { if (!_logging) {return; } if (msg && window.console && window.console.log) { window.console.log(utils.constants.LOGGER_PREFIX + msg); } if (func && func instanceof Function && window.console && window.console.dir) { func(); } }; /** * Starts logging * * @return undefined */ utils.startLogging = function () { _logging = true; }; /** * Determines if the debug hash is in the URL * * @return undefined */ utils.isDebugHash = function () { return (_win.location.hash && _win.location.hash === utils.constants.DEBUG_HASH_NAME) ? true : false; }; /** * Determines if the no run hash is in the URL * * @return undefined */ utils.isNoRunHash = function () { return (_win.location.hash && _win.location.hash === utils.constants.NO_RUN_HASH_NAME) ? true : false; }; /** * Starts the application * * @return undefined */ utils.buildDonationUrl = function (donationObject) { var retVal = '', _constants = utils.constants, donationId = '', donationAmount = '', standardDonationAmounts = '', oasId = '', oasId2 = ''; donationObject = donationObject || {}; if (!donationObject.amount || !donationObject.id) {_logg('utils.buildDonationUrl -> problem with donationObject. EXITING. ' + retVal); return; } donationId = donationObject.id; donationAmount = donationObject.amount; standardDonationAmounts = _constants.STANDARD_DONATION_ASK_AMOUNTS; oasId = donationObject.oasId; oasId2 = donationObject.oasId2; //build the URL retVal = _constants.DONATION_URL_BASE + donationId + '&ask=' + standardDonationAmounts + '&ask_selected=' + donationAmount + '&en_txn6=' + donationId; retVal = retVal .replace('{oasId}', oasId) .replace('{oasId2}', oasId2) .replace('{amount}', donationAmount); _logg('utils.buildDonationUrl -> ' + retVal); return retVal; }; /** * Overrides the default click behaviour for all donation links * * @return undefined */ utils.overrideDonationLinksClickEvents = function () { var $links = $(utils.constants.DONATION_LINK_SELECTOR_BASE); utils.overrideDonationLinkClickEvent($links.eq(0), utils.config.donation_1); utils.overrideDonationLinkClickEvent($links.eq(1), utils.config.donation_2); utils.overrideDonationLinkClickEvent($links.eq(2), utils.config.donation_3); utils.overrideDonationLinkClickEvent($links.eq(3), utils.config.donation_4); _logg('utils.overrideDonationLinksClickEvents'); }; /** * Overrides the default click behaviour for a given donation link * * @return undefined */ utils.overrideDonationLinkClickEvent = function ($donationLink, donationObject) { donationObject = donationObject || {}; donationObject.evar = donationObject.evar || ''; if (!$donationLink || !$donationLink.length){_logg('utils.overrideDonationLinkClickEvent - element not provided or incorrect. EXITING.'); return; } $donationLink.click(function (e) { var donationUrl = $(this).attr('href'); e.preventDefault(); e.stopPropagation(); //send the omniture report utils.sendOmnitureReport(donationObject.evar); //wait for the omniture call to fire setTimeout(function () { //and then go to the donation url window.location.href = donationUrl; }, utils.constants.DONATION_LINK_DELAY); }); _logg('utils.overrideDonationLinkClickEvent -> ' + donationObject.copy); }; /** * Sets the selected class for the specified donation link * * @return undefined */ utils.setSelectedDonationAmount = function (elementIndex) { elementIndex = elementIndex || 0; $(utils.constants.DONATION_LINK_SELECTOR_BASE).eq(elementIndex).addClass('selected'); _logg('utils.setSelectedDonationAmount -> ' + elementIndex); }; /** * Adds custom CSS to the page * * @param cssText - string - css to add to an embedded stylesheet * * @return undefined */ utils.addCustomCss = function (cssText) { var $styleSheet = $(''); if(!cssText){return; } $styleSheet.append(cssText); $('body').append($styleSheet); _logg('utils.getCssText'); }; /** * Returns custom CSS text * * @return string */ utils.getCssText = function () { _logg('utils.getCssText'); return [ '#crDonationAlertContainer {position: relative; width: 100%; text-align: center;border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(153, 153, 153); background-color: rgb(211, 211, 211);}', '#crDonationAlertContainer #bbBanner{display: inline-block;margin: 0px auto;min-height: 161px;}', '#crDonationAlertContainer #calendarDec31 {float: left; margin: 21px 50px 32px 4px;}', '#crDonationAlertContainer #bbBannerText {float: left; text-align: left; margin: 23px 0px 10px; width: 560px; font-family: "Neue Helvetica W01", Helvetica, Arial, sans-serif; font-weight: 300;}', '#crDonationAlertContainer #bbBannerText h3 {display: block; font-weight: 500; font-size: 21px; line-height: 28px;margin: 0; padding: 0 3px 0;}', '#crDonationAlertContainer #bbBannerText p{font-size: 16px; font-family: "Neue Helvetica W01", Helvetica, Arial, sans-serif; font-weight: 300;line-height: 1.4;margin: 5px 0 0; padding: 0 3px 0;}', '#crDonationAlertContainer #bbBannerText p.highlight-donation{background: #ffea00; padding: 0 3px 2px; margin: 3px 0 0;}', '#crDonationAlertContainer #bbBannerText sup{font-size: 12px; vertical-align: initial;}', '#crDonationAlertContainer #bbDonationsContainer {float: left;margin: 62px 0px 0px 50px; overflow:auto;}', '#crDonationAlertContainer #bbDonationsContainer a{display:inline-block; margin-right: 16px; padding-top: 10px; width: 72px; height: 45px; text-align: center; color: rgb(235, 28, 36); font-weight: 500; font-size: 18px; background: rgb(255, 255, 255);}', '#crDonationAlertContainer #closeButtonWrapper{height: 23px; width: 23px; position: absolute; top: 15px; right: 15px; cursor: pointer; z-index: 10;}', '#crDonationAlertContainer #bbDonationsContainer a:last-child{margin-right: 0;}', '#crDonationAlertContainer .closeButtonWrapper{height: 23px; width: 23px; position: absolute; top: 15px; right: 15px; cursor: pointer; z-index: 10;}', '#crDonationAlertContainer #bbDonationsContainer a.selected{color: rgb(255, 255, 255);background-color: rgb(235, 28, 36);}', //responsive sytles '@media all and (max-width: 1100px) {', '#crDonationAlertContainer #bbBannerText{margin-bottom:30px;}', '#crDonationAlertContainer #bbDonationsContainer {float:none;margin: 0;clear:both;margin: 30px 0;}', '}', '@media all and (max-width: 768px) {', '#crDonationAlertContainer #calendarDec31{display:none;}', '#crDonationAlertContainer #bbBannerText{float:none;width:80%;margin:20px auto;}', '}', '@media all and (max-width: 767px) {', '#crDonationAlertContainer #bbDonationsContainer a{width:20%;}', '}' ].join(''); }; /** * Returns HTML for the banner component * * @return string */ utils.getBannerHtml = function () { _logg('utils.getBannerHtml'); return [ '
', '
', '
', '

20-Day Challenge for Consumer Safety - $125,000 Goal

', '

We're working to make the world safer and healthier for consumers by providing reliable information on products and services. But, we need your help.

', '

Donate today and your gift will be matched.

', '
', '', '
', '
', '', '
', '
' ].join(''); }; /** * Injects the banner into the page * * @return undefined */ utils.injectBanner = function () { _logg('utils.injectBanner'); $(utils.getBannerHtml()).insertBefore(utils.constants.INJECT_BANNER_TARGET_SELECTOR); }; /** * Returns banner container * * @return jQuery object */ utils.getBannerContainer = function () { _logg('utils.injectBanner'); return $('#crDonationAlertContainer'); }; utils.readCookie = function(name) { var i = 0, c = '', nameEQ = name + '=', ca = document.cookie.split(';'); _logg('utils.readCookie'); for (;i < ca.length;i++) { 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; }; utils.createCookie = function(name, value, days) { var expires = '', date = null; _logg('utils.createCookie'); if (days) { date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); expires = '; expires=' + date.toGMTString(); } else { expires = ''; } var cookie = name + '=' + value + expires + '; path=/; domain=' + utils.getCookieDomain(); document.cookie = cookie; return true; }; utils.getCookieDomain = function(){ if( document.location.host.indexOf('consumer.org') > -1 ){ return 'consumer.org'; }else{ return 'consumerreports.org'; } } /** * Binds the click event of the close button * * @return undefined */ utils.bindCloseButton = function () { $('#crDonationAlertContainer .closeButtonWrapper').click(function () { //slide the banner up utils.getBannerContainer().slideUp('slow'); //if in debug mode, do not set the closed cookie if(_isDebug){return; } //set the closed cookie utils.setBannerClosedCookie(); }); _logg('utils.bindCloseButton'); }; /** * Sets the cookie that indicates the banner was closed * * @return boolean */ utils.setBannerClosedCookie = function () { _logg('utils.setBannerClosedCookie'); utils.createCookie(utils.constants.BANNER_CLOSED_COOKIE_NAME, 'true', utils.constants.BANNER_CLOSED_COOKIE_DURATION); }; /** * Sends an omniture report * * @return undefined */ utils.sendOmnitureReport = function (evarValue) { var _s = null; if (!(s instanceof Object) || !(s.tl instanceof Function)) {_logg('utils.sendOmnitureReport -> problem with s or s.tl. EXITING.'); return; } _s = s_gi("cuconsumer,cuglobal"); evarValue = evarValue || ''; _s.linkTrackVars = 'eVar48,prop48'; _s.prop48 = _s.pageName + ' | ' + evarValue; _s.eVar48 = _s.pageName + ' | ' + evarValue; _s.tl(this, 'o', utils.constants.OMNITURE_O_VALUE); _logg('utils.sendOmnitureReport -> ' + evarValue); } //when the document is ready jQuery(document).ready(function () { //initialize the applcation utils.init(); }); })(window, window.jQuery); jQuery.getScript('http://oascentral.consumerreports.org/RealMedia/ads/adstream.cap/1775805488?c=fourbuttons&dv=1&e=10d&s=1')