var VideoAds = function() { var videoAdEnd; var videoContent = document.getElementById('simplegameVideoContentElement'); var adsManager; var adsLoader; var adDisplayContainer; this.initVideoAdArea = function(p) { var w, h; if (p == 6) { w = 640; h = 360; } else { w = screen.availWidth; h = screen.availHeight; } parent.$('#simplegameContent').hide(); if (parent.$('#mainPageSet')[0]) { parent.$('#mainPageSet').width(w + 'px').height(h + 'px'); } parent.$('#centerADBox').width(w + 'px').height(h + 'px'); parent.$('#simplegameCenterAd').width(w + 'px').height(h + 'px'); parent.$('#centerADBox').css('background', '#000'); parent.$('#centerADBox').show(); } this.init = function(adEndProc) { videoAdEnd = adEndProc; // Create the ad display container. createAdDisplayContainer(); // Create ads loader. adsLoader = new google.ima.AdsLoader(adDisplayContainer); // Listen and respond to ads loaded and error events. adsLoader.addEventListener(google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED, onAdsManagerLoaded, false); adsLoader.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, onAdError, false); // Request video ads. var adsRequest = new google.ima.AdsRequest(); adsRequest.adTagUrl = ''; // Specify the linear and nonlinear slot sizes. This helps the SDK to // select the correct creative if multiple are returned. var w, h; w = 640; h = 360; adsRequest.linearAdSlotWidth = w; adsRequest.linearAdSlotHeight = h; adsRequest.nonLinearAdSlotWidth = w; adsRequest.nonLinearAdSlotHeight = h; adsLoader.requestAds(adsRequest); } var createAdDisplayContainer = function() { // We assume the adContainer is the DOM id of the element that will house // the ads. adDisplayContainer = new google.ima.AdDisplayContainer(document.getElementById('simplegameVideoAdContainer'), videoContent); } var playAds = function() { // Initialize the container. Must be done via a user action on mobile devices. videoContent.load(); adDisplayContainer.initialize(); try { var w, h; w = 640; h = 360; // Initialize the ads manager. Ad rules playlist will start at this time. adsManager.init(w, h, google.ima.ViewMode.NORMAL); // Call play to start showing the ad. Single video and overlay ads will // start at this time; the call will be ignored for ad rules. adsManager.start(); } catch (adError) { // An error may be thrown if there was a problem with the VAST response. videoContent.play(); } } var onAdsManagerLoaded = function(adsManagerLoadedEvent) { // Get the ads manager. var adsRenderingSettings = new google.ima.AdsRenderingSettings(); adsRenderingSettings.restoreCustomPlaybackStateOnAdBreakComplete = true; // videoContent should be set to the content video element. adsManager = adsManagerLoadedEvent.getAdsManager(videoContent, adsRenderingSettings); // Add listeners to the required events. adsManager.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, onAdError); adsManager.addEventListener(google.ima.AdEvent.Type.ALL_ADS_COMPLETED, onAdEvent); // Listen to any additional events, if necessary. adsManager.addEventListener(google.ima.AdEvent.Type.LOADED, onAdEvent); adsManager.addEventListener(google.ima.AdEvent.Type.STARTED, onAdEvent); adsManager.addEventListener(google.ima.AdEvent.Type.COMPLETE, onAdEvent); adsManager.addEventListener(google.ima.AdEvent.Type.SKIPPED, onAdEvent); adsManager.addEventListener(google.ima.AdEvent.Type.USER_CLOSE, onAdEvent); playAds(); } var onAdEvent = function(adEvent) { // Retrieve the ad from the event. Some events (e.g. ALL_ADS_COMPLETED) // don't have ad object associated. var ad = adEvent.getAd(); switch (adEvent.type) { case google.ima.AdEvent.Type.LOADED: // This is the first event sent for an ad - it is possible to // determine whether the ad is a video ad or an overlay. if (!ad.isLinear()) { // Position AdDisplayContainer correctly for overlay. // Use ad.width and ad.height. videoContent.play(); } break; case google.ima.AdEvent.Type.COMPLETE: case google.ima.AdEvent.Type.SKIPPED: case google.ima.AdEvent.Type.USER_CLOSE: videoAdEnd(); break; } } var onAdError = function(adErrorEvent) { videoAdEnd(); // Handle the error logging. console.log(adErrorEvent.getError()); if (typeof(adsManager) != "undefined") { adsManager.destroy(); } } }