"use strict";angular.module("com.2fdevs.videogular.plugins.imaads",[]).directive("vgImaAds",["$window","VG_STATES",function(a,b){return{restrict:"E",require:"^videogular",scope:{vgNetwork:"=",vgUnitPath:"=",vgCompanion:"=",vgCompanionSize:"=",vgAdTagUrl:"=",vgSkipButton:"="},link:function(c,d,e,f){function g(a){a&&(f.mediaElement[0].addEventListener("ended",A),u=f.videogularElement[0].offsetWidth,v=f.videogularElement[0].offsetHeight,x.addEventListener(google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED,j,!1,this),x.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR,p,!1,this),c.vgCompanion&&googletag.cmd.push(function(){googletag.defineSlot("/"+c.vgNetwork+"/"+c.vgUnitPath,c.vgCompanionSize,c.vgCompanion).addService(googletag.companionAds()).addService(googletag.pubads()),googletag.companionAds().setRefreshUnfilledSlots(!0),googletag.pubads().enableVideoAds(),googletag.enableServices()}))}function h(a){switch(a){case b.PLAY:z||(f.pause(),w.initialize(),i(c.vgAdTagUrl),z=!0);break;case b.STOP:x.contentComplete()}}function i(b){s();var c=new google.ima.AdsRequest,e=a.getComputedStyle(d[0]);c.adTagUrl=b,c.linearAdSlotWidth=parseInt(e.width,10),c.linearAdSlotHeight=parseInt(e.height,10),c.nonLinearAdSlotWidth=parseInt(e.width,10),c.nonLinearAdSlotHeight=parseInt(e.height,10),x.requestAds(c)}function j(a){s(),y=a.getAdsManager(f.mediaElement[0]),k(y)}function k(a){a.addEventListener(google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED,n,!1,this),a.addEventListener(google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED,o,!1,this),a.addEventListener(google.ima.AdEvent.Type.SKIPPABLE_STATE_CHANGED,l,!1,this),a.addEventListener(google.ima.AdEvent.Type.ALL_ADS_COMPLETED,q,!1,this),a.addEventListener(google.ima.AdEvent.Type.COMPLETE,r,!1,this),a.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR,p,!1,this),a.init(u,v,google.ima.ViewMode.NORMAL),a.start()}function l(){var a=y.getAdSkippableState();a?C.css("display","block"):C.css("display","none")}function m(){y.skip()}function n(){s(),f.mediaElement[0].removeEventListener("ended",A),f.pause()}function o(){f.mediaElement[0].addEventListener("ended",A),f.play(),t()}function p(){y&&y.destroy(),t(),f.play()}function q(){t()}function r(){B++}function s(){d.css("display","block")}function t(){d.css("display","none")}var u,v,w=new google.ima.AdDisplayContainer(d[0]),x=new google.ima.AdsLoader(w),y=null,z=!1,A=function(){x.contentComplete()},B=0,C=angular.element(c.vgSkipButton);C.bind("click",m),d.prepend(C),angular.element(a).bind("resize",function(){u=f.videogularElement[0].offsetWidth,v=f.videogularElement[0].offsetHeight,y&&(f.isFullScreen?y.resize(u,v,google.ima.ViewMode.FULLSCREEN):y.resize(u,v,google.ima.ViewMode.NORMAL))}),c.$watch(function(){return f.isReady},function(a,b){a!=b&&g(a)}),c.$watch(function(){return f.currentState},function(a,b){a!=b&&h(a)})}}}]);