var ci_cap_ismobiledevice = false; var adFormat = ci_cap_format.split('x'); var ci_iframe_height = 610; // default size: house ad for ad unit var ci_iframe_width = 128; var ci_iframe_exists = false; var ci_loadCPUrl = '//' + 'bc.coupons.com'; var ci_parentProtocol = window.location.protocol; var ci_parentDomain = ci_parentProtocol + '//' + window.location.host; var ci_parentHref = window.location.href.split('#')[0]; var ci_screen_h = typeof(window.screen) !== 'undefined' ? window.screen.height : 0; var ci_screen_w = typeof(window.screen) !== 'undefined' ? window.screen.width : 0; var ci_viewport_w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0); var ci_viewport_h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0); var ci_configurations = { Retailer: false }; // default setting if (ci_loadcpurl_array === null || typeof(ci_loadcpurl_array) === "undefined") { var ci_loadcpurl_array = new Array(); } function ciInsertJavaScriptFile(src, id, text) { var script = document.createElement("script"); script.type = "text/javascript"; script.src = src; if (typeof id !== "undefined") { script.id = id; } if (typeof text !== "undefined") { script.text = text; } document.getElementsByTagName('head').item(0).appendChild(script); } ci_gallery_tool = { retry : 0, script : null, //Function to get widget configuration getWidgetConfig: function(scriptRawData, urlArrayIndex) { script = eval('('+ scriptRawData +')'); local_banner_format = script._bannerFormat; local_script_id = script._scriptId; local_banner_type = script._bannerType; adFormat = local_banner_format.split('x'); ci_iframe_width = (adFormat[0] * 1); ci_iframe_height = (adFormat[1] * 1); // ci_loadCPUrl += "&parent=" + encodeURIComponent(ci_parentHref) + "&iheight=" + ci_iframe_height + "&iwidth=" + ci_iframe_width; // ci_loadCPUrl += "&format=" + ci_cap_format; var url = (urlArrayIndex >= 0) ? ci_loadcpurl_array[urlArrayIndex] : ci_loadCPUrl; //Appending parent and format to the url url += "&parent=" + encodeURIComponent(ci_parentHref) + "&iheight=" + ci_iframe_height + "&iwidth=" + ci_iframe_width; url += "&format=" + local_banner_format; ci_gallery_tool.showiFrame(url, ci_iframe_height, ci_iframe_width, false, ci_cap_ismobiledevice, local_script_id); }, //Function to Display Widget displayWidget: function(ci_cap_bid, ci_cap_scriptid, urlArrayIndex) { var widgetName = 'ci_widget' + ci_cap_scriptid; var scriptId = document.getElementById("scriptId_" + ci_cap_format + "_" + ci_cap_scriptid); var widgetDiv = document.getElementById('ci_widget'); // don't use iframe name to check as it takes time to load the frame document.write("<" + "div id='" + widgetName + "'>"); var request = "//bc.coupons.com/dataapi/widgetscriptdetail?bid=" + ci_cap_bid + "&scriptid=" + ci_cap_scriptid + "&urlindex=" + urlArrayIndex; ciInsertJavaScriptFile(request); }, getOffset: function(element){ var rect = element.getBoundingClientRect(); var elementLeft, elementTop; //x and y var scrollTop = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop; var scrollLeft = document.documentElement.scrollLeft? document.documentElement.scrollLeft:document.body.scrollLeft; elementTop = rect.top+scrollTop; elementLeft = rect.left+scrollLeft; return { top: elementTop, left: elementLeft } }, showiFrame : function(url, height, width, isGallery, isMobile, scriptParam) { //update url with mobile controller if we determine it's a mobile browser if(typeof isMobile !== 'undefined' && (isMobile || navigator.userAgent.match(/iP([ao]d|hone)|Silk|Android|Linux|IEMobile/)) && isGallery) { url = url.replace('loadcoupons', 'mobile/gallery'); //use this method to find out where in the page the gallery is loaded, we will use this to scroll when paginating var offset = this.getOffset(document.getElementById('ci_gallery')); url = url + "&yOffset=" + offset.top; } var widgetName = 'ci_widget' + scriptParam; var adunitFrameObj = document.getElementsByName(widgetName); var galleryFrameObj = document.getElementsByName('ci_CouponsClickParentIframe_gallery'); if (isGallery) { var width = width + 'px'; var height = height + 'px'; var minHeight = ''; if(isMobile){ width = '100%'; height = '800px'; minHeight = "min-height: 800px;"; } if (typeof(galleryFrameObj) === "undefined" || galleryFrameObj === null || galleryFrameObj.length === 0) { var div = document.getElementById('ci_gallery'); url = url + (adunitFrameObj.length>0 ? "&exist=1" : ""); div.innerHTML = "<" + "iframe class='ci_ccss' id='ci_CouponsClickParentIframe' frameborder=0 height='" + height + "' width='" + width + "' name='ci_CouponsClickParentIframe' style='" + minHeight + "overflow: hidden; margin: 0px; padding: 0px; border:none; scroll:no; width:" + width + "' src='" + url + "'>"; } }else { url = url + (galleryFrameObj.length>0 ? "&exist=1" : ""); var scriptId = "scriptId_" + ci_cap_format + "_" + scriptParam; var scriptTag = this.getScriptTag(scriptId); if(scriptTag !== null){ var widgetName = 'ci_widget' + scriptParam; var iframeDiv = document.createElement("iframe"); iframeDiv.id = "ci_CouponsClickParentIframe"; iframeDiv.name = widgetName; iframeDiv.src = url + (adunitFrameObj.length > 0 ? "&fseq=" + adunitFrameObj.length : ""); iframeDiv.frameBorder = 0; iframeDiv.style.width = width + "px"; iframeDiv.style.height = height + "px"; iframeDiv.style.margin = "0px"; iframeDiv.style.border = "none"; //TODO: //iframeDiv.style.overflow = "hidden"; iframeDiv.style.scroll = "no"; var parentNode = scriptTag.parentNode; parentNode.insertBefore(iframeDiv, scriptTag); }else{ var widgetName = 'ci_widget' + scriptParam; var div = document.getElementById(widgetName); url = url + (adunitFrameObj.length>0 ? "&exist=1" : ""); if(div !== null) { div.innerHTML = "<" + "iframe class='ci_ccss' id='ci_CouponsClickParentIframe' name='" + widgetName + "' frameborder=0 style='height:" + height + "px;width:" + width + "px;overflow: hidden; margin: 0px; padding: 0px; border:none; scroll:no;' src='" + url + (adunitFrameObj.length > 0? "&fseq=" + adunitFrameObj.length : "") + "'>"; } else { document.write("<" + "iframe class='ci_ccss' id='ci_CouponsClickParentIframe' name='" + widgetName + "' frameborder=0 style='height:" + height + "px;width:" + width + "px;overflow: hidden; margin: 0px; padding: 0px; border:none; scroll:no;' src='" + url + (adunitFrameObj.length > 0? "&fseq=" + adunitFrameObj.length : "") + "'>"); } } } }, getScriptTag: function(id){ //if the script tag is used multiple times with the same class name, we will find the last occurence of the script tag to inject the content to. Since it's script tags, it will be executed in the order it appears on the page. var scripts = document.getElementsByTagName("script"); var occurence = -1; for(var i = 0; i < scripts.length; i++){ if(scripts[i].className === id){ occurence = i; } } if(occurence === -1){ return null; } else{ return scripts[occurence]; } }, // to handle cross domain ajax call, use dynamic script tag displayGallery : function(bid, sid, urlArrayIndex) { var scriptId = document.getElementById("scriptId_" + ci_cap_format + "_" + ci_cap_scriptId); var galleryDiv = document.getElementById('ci_gallery'); // don't use iframe name to check as it takes time to load the frame // check for multiple gallery if (typeof(galleryDiv) === "undefined" || galleryDiv === null) { if(scriptId !== null){ //for newer script tags, find the id and insert the div as a sibling var ciGalleryDiv = document.createElement("div"); ciGalleryDiv.id = "ci_gallery"; var parentNode = scriptId.parentNode; parentNode.insertBefore(ciGalleryDiv, scriptId); } else{ document.write("<" + "div id='ci_gallery'>"); } // get script detail var request = "//bc.coupons.com/dataapi/galleryscriptdetail?bid=" + bid + "&scriptid=" + sid + "&urlindex=" + urlArrayIndex; ciInsertJavaScriptFile(request); } else { var dupDiv = document.getElementById("ci_duplicate"); if (typeof(dupDiv) === "undefined" || dupDiv === null) { document.write(""); document.write("<" + "div id='ci_duplicate' class='ci_css_duplicate'> "); } } }, getGalleryConfig : function(scriptRawData, urlArrayIndex) { var url = (urlArrayIndex >= 0) ? ci_loadcpurl_array[urlArrayIndex] : ci_loadCPUrl; script = eval('('+ scriptRawData +')'); var isMobile = script._isMobile; var dimension = [750, 1440] ; // (x, y) if (typeof(script) !== 'undefined' && script !== null && !script._isMobile) { // display side bar if one of the 3 items, Category, CouponCarrier, or Brand, is showing. //var showSideBar = script._showCategory + script._showCouponCarrier + script._showBrand; if(!script._showThreeColumn){ if(!script._showCategory && !script._showBrand){ dimension[1] = dimension[1] - 100; } if(!script._showCouponCarrier){ dimension[1] = dimension[1] - 20; } if(!script._showAdBlob){ dimension[1] = dimension[1] - 110; dimension[0] = dimension[0] - 120; } } else{ dimension = [960, 1410]; if(!script._showCategory && !script._showBrand){ dimension[1] = dimension[1] - 80; } if(!script._showAdBlob){ dimension[1] = dimension[1] - 110; } } } url = url + "&iheight=" + dimension[1] + "&iwidth=" + dimension[0]; this.showiFrame(url, dimension[1], dimension[0], true, isMobile); // this.initIframeLoadComplete(); }, addBrightTagTracking : function(data) { var config = eval('('+ data +')'); if (!config.btTrackingEnabled) { return; } ciInsertJavaScriptFile(config.btTrackingScriptFile, config.btTrackingScriptID, '{ site: "' + config.btTrackingKey + '" }'); }, initIframeLoadComplete : function() { if (this.retry > 100) { return; } var iframeObj = document.getElementById("ci_CouponsClickParentIframe"); if (iframeObj === null || typeof(iframeObj) === "undefined" ) { this.retry ++; setTimeout("ci_gallery_tool.initIframeLoadComplete()", 300); } else { if (script._showThreeColumn) { // attach the event to collapse category when user moves scroll the window if (iframeObj.contentWindow.ci_Widget === null || typeof(iframeObj.contentWindow.ci_Widget) === "undefined") { setTimeout("ci_gallery_tool.initIframeLoadComplete()", 300); } else { window.onscroll = function(e) { iframeObj.contentWindow.ci_Widget.PageStructure.CollapseCategoriesAndBrands(); } } } } } } // the following code is for resizing the iframe var ci_loc_last = window.location.href; var ci_ntry = 0; ci_iframe_tool = { isPostMessageAvailable : function() { if(typeof parent.postMessage !== 'function' && typeof parent.postMessage !== 'object') { return false; } return true; }, AdjustiFrame : function(param) { var dim = param.split(':'); var height = parseInt(dim[0], 10); var width = parseInt(dim[1], 10); if (height > 0 && width > 0) { document.getElementById("ci_CouponsClickParentIframe").height = height + 10 + 'px'; } }, // check resizing msg thru hash CheckResizingMsg : function() { if (ci_ntry > 100) return; // only wait up for 30 sec if(ci_loc_last === window.location.href) { ci_ntry++; setTimeout('ci_iframe_tool.CheckResizingMsg();', 300); } else { ci_loc_last = window.location.href; var size = window.location.hash.substring(1, window.location.hash.length); this.AdjustiFrame(size); window.location.hash = ''; } } } // starting point var frameObj = document.getElementById('ci_CouponsClickParentIframe'); if (typeof(frameObj) !== "undefined" || frameObj !== null) { ci_iframe_exists = true; } // validate the variables and determine the size if (!(typeof(ci_cap_format) === "undefined" || ci_cap_format === null) && !(typeof(ci_cap_scriptId) === "undefined" || ci_cap_scriptId === null) && !(typeof(ci_cap_bid) === "undefined" || ci_cap_bid === null) && adFormat.length === 2 && !isNaN(adFormat[0]) && !isNaN(adFormat[1])) { width = adFormat[0]; height = adFormat[1]; isMobile = ci_cap_ismobiledevice; if(typeof isMobile !== 'undefined' && isMobile) { if(width == 500 && (height == 410 || height == 540 || height == 670 || height == 275)) { width = 300; height = 540; ci_cap_format = '300x540'; } else if(width == 500 && height == 295) { width = 300; height = 440; ci_cap_format = '300x440'; } adFormat = ci_cap_format.split('x'); } ci_loadCPUrl = ci_loadCPUrl + "/loadcoupons?scriptid=" + ci_cap_scriptId + "&bid=" + ci_cap_bid + "&scrh=" + ci_screen_h + "&scrw=" + ci_screen_w + "&vw=" + ci_viewport_w + "&vh=" + ci_viewport_h; if (window.location.search.substring(1).length > 0) { // attach add'l parameters, cid, catid, nocombo... var paramArr = window.location.search.substring(1).split("&"); var paramStr = ""; var index; for (index=0; index < paramArr.length; index++) { if (paramArr[index].toUpperCase().indexOf("BID=") != 0) { paramStr += '&' + paramArr[index]; } } if (paramStr.length > 0) { ci_loadCPUrl += paramStr; } } if (!(typeof(ci_cap_channel) === "undefined" || ci_cap_channel === null)) { ci_loadCPUrl = ci_loadCPUrl + "&channel=" + encodeURIComponent(ci_cap_channel); } if (!(typeof(ci_cap_categoryId) === "undefined" || ci_cap_categoryId === null)) { ci_loadCPUrl = ci_loadCPUrl + "&categoryId=" + ci_cap_categoryId; //if there is catId, also look for preclip attribute, if there isn't a catid query, ignore this attribute if (!(typeof(ci_cap_preclip) === "undefined" || ci_cap_preclip === null)) { ci_loadCPUrl = ci_loadCPUrl + "&preclip=" + ci_cap_preclip.toString().toLowerCase(); } else{ ci_loadCPUrl = ci_loadCPUrl + "&preclip=false"; } } if (ci_cap_format == '718x940') { ci_loadCPUrl = ci_loadCPUrl + "&bannertype=3" + "&format=" + ci_cap_format; } else { ci_loadCPUrl = ci_loadCPUrl + "&bannertype="; // don't default bannertype. let it be determined on server } // ci_cap_bannerType might be assigned because of existing gallery if (!(typeof(ci_cap_bannerType) === "undefined" || ci_cap_bannerType === null)) { if (ci_cap_bannerType == 3 && document.getElementById('ci_gallery') === null && (ci_cap_format == '728x90' || ci_cap_format == '468x60' || ci_cap_format == '120x240' || ci_cap_format == '180x150' || ci_cap_format == '120x600' || ci_cap_format == '160x600' || ci_cap_format == '300x250')) { // Invalid banner type code. Log a message in server. var logMessage = "Change Banner Type from 3 to 1 on " + document.URL + " page."; var request = "//bc.coupons.com/dataapi/logmessage?msg=" + encodeURIComponent(logMessage); ciInsertJavaScriptFile(request); } } /*if (!(typeof(ci_cap_bannerType) === "undefined" || ci_cap_bannerType === null)) { if (ci_cap_bannerType == 7 && (ci_cap_format == '728x90' || ci_cap_format == '468x60' || ci_cap_format == '120x240' || ci_cap_format == '180x150' || ci_cap_format == '120x600' || ci_cap_format == '160x600' || ci_cap_format == '300x250')) { // Invalid banner type code. Log a message in server. var logMessage = "Change Banner Type from 3 to 1 on " + document.URL + " page."; var request = "//bc.coupons.com/dataapi/logmessage?msg=" + encodeURIComponent(logMessage); ciInsertJavaScriptFile(request); } } */ /* parameters are valid */ ci_loadCPUrl += '&parenturl=' + encodeURIComponent(window.location.href); if (ci_cap_format == '718x940') { /* gallery */ // attache cid and catid if available in querystring if (ci_iframe_tool.isPostMessageAvailable()) { ci_loadCPUrl += '&parent=' + encodeURIComponent(ci_parentDomain); } else { ci_loadCPUrl += '&parent=' + encodeURIComponent(ci_parentHref); } // prevent url from getting overwritten when there are multiple // banners with different loading sequence (speed). ci_loadcpurl_array.push(ci_loadCPUrl); var dimension = ci_gallery_tool.displayGallery(ci_cap_bid, ci_cap_scriptId, ci_loadcpurl_array.length - 1); } else { /* ad unit */ ci_loadcpurl_array.push(ci_loadCPUrl); ci_gallery_tool.displayWidget(ci_cap_bid, ci_cap_scriptId, ci_loadcpurl_array.length - 1); } } else { ci_loadCPUrl += "/loadcoupons?scriptid=" + 0 + "&bid=" + 0 + "&format=" + 0; ci_gallery_tool.showiFrame(ci_loadCPUrl, ci_iframe_height, ci_iframe_width, false); } /* * checking postback message. if postMessage is not available, check the hash in url */ if (!ci_iframe_tool.isPostMessageAvailable()) { ci_iframe_tool.CheckResizingMsg(); } else { var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent"; var eventer = window[eventMethod]; var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message"; // Listen to message from child window, and filter out the message not from us eventer(messageEvent,function(e) { checkuri = ci_parentProtocol + '//' + 'bc.coupons.com'; if (e.origin === checkuri) { if (typeof e.data !== 'undefined') { if (typeof e.data.type === 'undefined') { ci_iframe_tool.AdjustiFrame(e.data); } else { if (typeof e.data.position !== 'undefined') { window.scrollTo(0, e.data.position); } } } } },false); }