PageSourceSearch

https://academy.mlcto.com/wp-content/plugins/haru-vidi/assets/js/haru-vidi.js

js mlcto.com collected 2026-10-09 16:53:53 UTC 269,290 bytes, 5,158 lines download raw bytes

1/**
2 * @package    HaruTheme
3 * @version    1.0.0
4 * @author     Administrator <[email protected]>
5 * @copyright  Copyright (c) 2017, HaruTheme
6 * @license    http://opensource.org/licenses/gpl-2.0.php GPL v2 or later
7 * @link       http://harutheme.com
8*/
9
10(function ($) {
11    "use strict";
12    var HaruVidi = {
13        init: function() {
14            HaruVidi.video.init();
15        }
16    };
17
18    HaruVidi.video = {
19        init: function() {
20            // Other variabl es
21            HaruVidi.video.videoID = '';
22            HaruVidi.video.video_server = '';
23            HaruVidi.video.autoplay = 'on';
24            HaruVidi.video.muted = false;
25            HaruVidi.video.auto_next = true;
26            HaruVidi.video.videos_data = [];
27            // Functions
28            HaruVidi.video.videoPlayer();
29            HaruVidi.video.turnOffLight();
30            HaruVidi.video.floatingPlayer();
31            HaruVidi.video.autoNextStatus();
32            HaruVidi.video.videoRating();
33            HaruVidi.video.videoScreenShots();
34            HaruVidi.video.watchLater();
35            HaruVidi.video.videoSocialShare();
36            HaruVidi.video.videoDownload();
37            HaruVidi.video.videoToggleContent();
38            HaruVidi.video.videoReport();
39            // Ads
40            HaruVidi.video.adsGetParams();
41            // 
42            HaruVidi.video.playlistScrollBar();
43            HaruVidi.video.playlistScrollTitle();
44            HaruVidi.video.layoutToggle();
45            // Archive Video
46            HaruVidi.video.archiveVideoSort();
47            HaruVidi.video.videoLayoutIsotope();
48            HaruVidi.video.videoLoadMore();
49            HaruVidi.video.videoInfiniteScroll();
50            // Channel
51            HaruVidi.video.channelSubscribe();
52            // Shortcode
53            HaruVidi.video.shortcodeSingle();
54            HaruVidi.video.shortcodeVideoFilter();
55            HaruVidi.video.shortcodeVideoFeatured();
56            HaruVidi.video.shortcodeVideoOrder();
57            HaruVidi.video.shortcodeVideoOrderSingle();
58            HaruVidi.video.shortcodeVideoCategory();
59            HaruVidi.video.shortcodeVideoCategorySingle();
60            HaruVidi.video.shortcodeVideoTop();
61            HaruVidi.video.shortcodeVideoSlideshow();
62            HaruVidi.video.shortcodeVideoSearch();
63            HaruVidi.video.shortcodeChannelCategory();
64            HaruVidi.video.shortcodeChannelTop();
65            HaruVidi.video.shortcodeSeriesCategory();
66            HaruVidi.video.shortcodeSeriesTop();
67            HaruVidi.video.shortcodePlaylistCategory();
68            HaruVidi.video.shortcodePlaylistTop();
69            HaruVidi.video.shortcodeSubmit();
70            // Hover
71            HaruVidi.video.thumbnailHoverSlideshow();
72            // BuddyPress & Author
73            HaruVidi.video.buddyPress();
74            HaruVidi.video.authorSingle();
75        },
76        adsGetParams: function() {
77            HaruVidi.video.ads_params = {
78                'video_server':         '',
79                'ads_id':               '',
80                'ads_on':               false,
81                'ads_type':             '',
82                'ads_time_skip':        5,
83                'ads_time_hide':        10,
84                'ads_images':           [],
85                'ads_videos':           [],
86                'ads_ima_url': [],
87                'ads_ima_url_mobile': [],
88                'ads_image_source':      '',
89                'ads_image_link':        '',
90                'ads_video_source':      '',
91                'ads_video_link':        '',
92                'ads_unskippable':        '',
93            };
94
95            HaruVidi.video.ad_displayed = [];
96
97            if ( $('.video-ads-data').length > 0 ) {
98                var $ads = $('.video-ads-data').attr('data-ads');
99                // Inject params for Ads
100                $ads = JSON.parse($ads);
101                HaruVidi.video.ads_params.video_server = $ads.video_server;
102                HaruVidi.video.ads_params.ads_id = $ads.ads_id;
103                HaruVidi.video.ads_params.ads_on = $ads.ads_on;
104                HaruVidi.video.ads_params.ads_type = $ads.ads_type;
105                HaruVidi.video.ads_params.ads_time_skip = parseFloat($ads.ads_t
105ime_skip);
106                HaruVidi.video.ads_params.ads_time_hide = parseFloat($ads.ads_time_hide);
107                HaruVidi.video.ads_params.ads_images = JSON.parse($ads.ads_images);
108                HaruVidi.video.ads_params.ads_videos = JSON.parse($ads.ads_videos);
109                HaruVidi.video.ads_params.ads_google_ima = JSON.parse($ads.ads_google_ima);
110                HaruVidi.video.ads_params.ads_google_ima_mobile = JSON.parse($ads.ads_google_ima_mobile);
111            }
112        },
113        adsLocal: function(params) {
114            var player                  = params.player,
115                playerWrap              = params.playerWrap;
116                
117            var ads_type                = params.ads_params.ads_type,
118                video_server            = params.ads_params.video_server;
119
120            $(document).on('haruAdsFinish' + params.ads_params.ads_id, function(){
121                HaruVidi.video.adsPlayerControls(player, video_server, 'play');
122            });
123
124            HaruVidi.video.ad_displayed[params.ads_params.ads_id] = true;
125            HaruVidi.video.adsPlayerControls(player, video_server, 'pause');
126
127            switch (ads_type) {
128                case 'image':
129                    var ads_images = Object.values(params.ads_params.ads_images);
130
131                    params.ads_params.ads_image_source = ads_images[0].image;
132                    params.ads_params.ads_image_link = ads_images[0].link;
133                    params.ads_params.ads_unskippable = ads_images[0].unskippable;
134
135                    HaruVidi.video.adsImage(params);
136
137                    break;
138                    
139                case 'html5_video':
140                    var ads_videos = Object.values(params.ads_params.ads_videos);
141
142                    params.ads_params.ads_video_source = ads_videos[0].video;
143                    params.ads_params.ads_video_link = ads_videos[0].link;
144                    params.ads_params.ads_unskippable = ads_videos[0].unskippable;
145
146                    HaruVidi.video.adsHtml5Video(params);
147
148                    break;        
149            }
150        },
151        adsImage: function(params) {
152            var playerWrap             = params.playerWrap,
153                video_server           = params.ads_params.video_server,
154                ads_params             = params.ads_params;
155            
156            var ads_image_source    = $.trim(ads_params.ads_image_source),
157                ads_time_skip       = parseFloat(ads_params.ads_time_skip),
158                ads_time_hide       = parseFloat(ads_params.ads_time_hide);
159            
160            // Append Ads Banner
161            var video_ads_wrap = '';
162            video_ads_wrap = video_ads_wrap + '<img src="' + ads_image_source + '" alt="">';
163            playerWrap.find('#ads-video-' + ads_params.ads_id ).append(video_ads_wrap);
164            
165            var videoAdsContainer   = $('#' + video_server + '-video-haruAdsWrap'),
166                videoAdsRemain      = playerWrap.find('.ad-hide-time'),
167                videoAdsSkip        = videoAdsContainer.find('.video-skip-ad'),
168                videoSkipTime       = videoAdsContainer.find('.skip-ad-time'),
169                videoAdsLoading     = playerWrap.find('.video-ads-loading');
170            
171            videoAdsContainer.removeClass('ads-hide');
172            videoAdsLoading.addClass('ads-loading-show');
173            
174            videoAdsContainer.find('.ad-type-image img').on('load', function() {
175                // Process Ads
176                videoAdsLoading.removeClass('ads-loading-show');
177                videoAdsContainer.addClass('ads-ready');
178
179                // Ads Remain Time
180                var ads_hide_remain = ads_time_hide;
181                videoAdsRemain.text(HaruVidi.video.adsConvertTime(ads_hide_remain));
182                var AdsHideRemain = setInterval(function() {                
183                    ads_hide_remain--;
184
185                    if ( ads_hide_remain == 0 ) {
186                        clearInterval(AdsHideRemain);
187                    }
188                    
189                    videoAdsRemain.text(HaruVidi.video.adsConvertTime(ads_hide_remain));               
190                }, 1000);
191
192                if ( ads_params.ads_unskippable == 'on' ) {
193                    setTimeout(function() {                                
194                        videoAdsSkip.addClass('skip-active');   
195                    }, ads_time_hide * 1000);
196                } else {
197                    // Ads Skip
198                    var currentSkipTime = ads_time_skip;
199                    var AdsSkipTimeOut = setInterval(function() {                
200                        currentSkipTime--;
201
202                        if ( currentSkipTime == 0) {
203                            clearInterval(AdsSkipTimeOut);
204                            videoAdsSkip.addClass('skip-active');
205                        }
206                        
207                        videoSkipTime.text(currentSkipTime);               
208                    }, 1000);
209                }
210
211                var AdsHideTimeOut = setTimeout(function() {             
212                    clearTimeout(AdsHideTimeOut);                                  
213                    videoAdsSkip.trigger('click');              
214                }, ads_time_hide * 1000 + 10);
215            });
216
217            // Skip Ads
218            videoAdsSkip.off().on('click', function () {
219                if ( videoAdsSkip.hasClass('skip-active') ) {
220                    console.log('Remove Ads');
221
222                    videoAdsSkip.removeClass('skip-active');
223                    videoAdsContainer.addClass('ads-hide');
224                    videoSkipTime.text(ads_time_skip);
225                    
226                    // 
227                    $(document).trigger('haruAdsFinish' + ads_params.ads_id);
228                }
229            });
230
231            // @TODO: Process Lightbox Ads
232        },
233        adsHtml5Video: function(params) {
234            var playerWrap             = params.playerWrap,
235                video_server           = params.ads_params.video_server,
236                ads_params             = params.ads_params;
237            
238            var ads_video_source         = ads_params.ads_video_source,
239                ads_video_link           = $.trim(ads_params.ads_video_link),
240                ads_time_skip            = parseFloat(ads_params.ads_time_skip);
241
242            var video_source             = '';
243            var video_ext = ads_video_source[0].split('.').pop();
244
245            video_source = '<source src="' + ads_video_source[0] + '" type="video/' + video_ext 
245+ '">';
246
247            if ( playerWrap.find('#' + video_server + '-video-haruAdsWrap').length > 0 ) {
248                playerWrap.find('#ads-video-' + ads_params.ads_id ).append(video_source);
249                // @TODO: Use Player JS libraries
250            }
251            
252            var videoAdsContainer   = $('#' + video_server + '-video-haruAdsWrap'),
253                videoAdsHtml5             = $('#ads-video-' + ads_params.ads_id)[0],
254                videoAdsLoading     = playerWrap.find('.video-ads-loading'),
255                videoAdsSkip        = videoAdsContainer.find('.video-skip-ad'),
256                videoSkipTime       = videoAdsContainer.find('.skip-ad-time'),
257                videoAdsCountdown   = videoAdsContainer.find('.video-ad-countdown'),
258                videoAdsMute        = videoAdsContainer.find('.video-ad-mute');
259
260            videoAdsContainer.removeClass('ads-hide');
261            videoAdsLoading.addClass('ads-loading-show');
262
263            // Check Mobile
264            if ( HaruVidi.video.isMobileOS() ) {
265                videoAdsHtml5.muted = true;
266                videoAdsHtml5.volume = 0;
267                videoAdsMute.addClass('active');
268            }
269            
270            videoAdsHtml5.onloadeddata = function() {
271                videoAdsLoading.removeClass('ads-loading-show');
272                videoAdsContainer.addClass('ads-ready');
273                
274                videoAdsHtml5.onerror = function() {
275                    videoAdsSkip.trigger('click');
276                }
277                
278                videoAdsHtml5.onended = function() {
279                    if ( ads_params.ads_unskippable == 'on' ) {
280                        videoAdsSkip.addClass('skip-active');
281                    }
282                    videoAdsSkip.trigger('click');
283                }
284                
285                videoAdsHtml5.addEventListener('playing', function() {             
286                    // Process Ads Mute
287                    videoAdsMute.off().on('click', function() {
288                        videoAdsMute.toggleClass('active');
289
290                        if ( videoAdsMute.hasClass('active') ) {
291                            videoAdsHtml5.volume = 0;
292                        } else {
293                            // Check Mobile
294                            if ( HaruVidi.video.isMobileOS() ) {
295                                videoAdsHtml5.muted = false;
296                            }
297                            videoAdsHtml5.volume = 1;
298                        }
299                    });  
300                });
301                
302                videoAdsCountdown.text(HaruVidi.video.adsConvertTime(videoAdsHtml5.duration));
303                videoAdsHtml5.ontimeupdate = function() {
304                    if ( ads_params.ads_unskippable == 'on' ) {
305                        videoAdsCountdown.text(HaruVidi.video.adsConvertTime( (videoAdsHtml5.duration - videoAdsHtml5.currentTime) ));
306                    } else {
307                        videoAdsCountdown.text(HaruVidi.video.adsConvertTime( (videoAdsHtml5.duration - videoAdsHtml5.currentTime) ));
308                                        
309                        if ( ads_time_skip <= videoAdsHtml5.currentTime ) {
310                            videoAdsSkip.addClass('skip-active');
311                        } else {
312                            videoSkipTime.text(Math.ceil(ads_time_skip - videoAdsHtml5.currentTime));
313                        }
314                    }                                           
315                }
316                
317                if ( !videoAdsContainer.hasClass('ads-hide') ) {
318                    videoAdsHtml5.play();
319                }
320                
321            }
322
323            if ( videoAdsContainer.hasClass('ads-ready') || HaruVidi.video.isMobileOS() ) {
324                videoAdsHtml5.load();
325            }
326            
327            // Skip Ads
328            videoAdsSkip.off().on('click', function(){
329                if ( videoAdsSkip.hasClass('skip-active') ) {
330                    console.log('Remove Ads');
331
332                    videoAdsSkip.removeClass('skip-active');
333                    videoAdsContainer.addClass('ads-hide');
334                    videoSkipTime.text(ads_time_skip);
335                    videoAdsCountdown.text('');
336                    videoAdsMute.removeClass('active');
337                    // Process Ads video
338                    videoAdsHtml5.pause();
339                    videoAdsHtml5.addEventListener('loadedmetadata', function() {
340                        this.currentTime = 0;
341                    }, false);          
342                    videoAdsHtml5.load();
343
344                    // 
345                    $(document).trigger('haruAdsFinish' + ads_params.ads_id);
346                }
347            }); 
348
349            // @TODO: Process Lightbox Ads
350        },
351        adsGoogle: function(params) {
352            var player = params.player,
353                playerWrap = params.playerWrap;
354                
355            var video_server = params.ads_params.video_server;
356
357            var ads_google_ima = Object.values(params.ads_params.ads_google_ima);
358            params.ads_params.ads_ima_url = ads_google_ima[0].google_ima;
359                    
360            HaruVidi.video.ad_displayed[params.ads_params.ads_id] = true;
361
362            // Check Mobile
363            if ( HaruVidi.video.isMobileOS() ) {
364                ads_google_ima = Object.values(params.ads_params.ads_google_ima_mobile);
365                params.ads_params.ads_ima_url = ads_google_ima[0].google_ima_mobile;
366            }
367
368            if ( typeof(google) !== 'undefined' && typeof(google.ima) !== 'undefined' ) {
369                HaruVidi.video.adsGoogleIMA({
370                    'player': player,
371                    'playerWrap': playerWrap,
372                    'adsVideoContent': HaruVidi.video.adsVideoContent({
373                                    'player': player,
374                                    'video_server': video_server,
375                                }),
376                    'ads_params': params.ads_params
377                });
378            } else {
379                $.when(
380                    $.getScript( 'https://imasdk.googleapis.com/js/sdkloader/ima3.js' ),
381                    $.Deferred(function( deferred ){
382                        $( deferred.resolve );
383                    })
384                ).done(function(){
385                    HaruVidi.video.adsGoogleIMA({
386                        'player': player,
387                        'playerWrap': playerWrap,
388                        'adsVideoContent': HaruVidi.video.adsVideoContent({
389                                        'player': player,
390                                        'video_server': video_server,
391                                    }),
392                        'ads_params': params.ads_params 
393                    });
394                });
395            }
396        },
397        adsGoogleIMA: function(params) {
398            var player          = params.player,
399                playerWrap      = params.playerWrap,
400                adsVideoContent       = params.adsVideoContent,
401                adDisplayContainerID  = 'ads-video-' + params.ads_params.ads_id,
402                adWidth         = playerWrap.width(), // For responsive
403                adHeight        = playerWrap.height(), // For responsive
404                videoAdsLoading = playerWrap.find('.video-ads-loading');
405
406            var ads_ima_url = params.ads_params.ads_ima_url;
407
408            // https://developers.google.com/interactive-media-ads/docs/sdks/html5/
409            var adDisplayContainer,
410                adsLoader,
411                adsManager;
412
413            var videoContent        = adsVideoContent;
414            var adContainer         = $('#' + adDisplayContainerID);
415            var videoAdsMute        = adContainer.find('.video-ad-mute');
416            var videoAdsSkip        = adContainer.find('.video-skip-ad');
417            var videoAdsCountdown   = playerWrap.find('.video-ad-countdown');
418
419            // Reference: https://codepen.io/imasdk/pen/QyBNrq
420            // 1. Create an ad display container
421            adDisplayContainer = 
422                new google.ima.AdDisplayContainer(
423                    document.getElementById(adDisplayContainerID)
424                );          
425
426            // 2. Getting the AdsManager and Display Ads
427            var onAdsManagerLoaded = function(adsManagerLoadedEvent){
428                // Get the ads manager. https://developers.google.com/interactive-media-ads/docs/sdks/html5/v3/apis
429                var adsRenderingSettings = new google.ima.AdsRenderingSettings();       
430                    adsRenderingSettings.restoreCustomPlaybackStateOnAdBreakComplete = true;
431                    adsRenderingSettings.useStyledNonLinearAds = true;
432                
433                // videoContent should be set to the content video element.
434                adsManager = adsManagerLoadedEvent.getAdsManager( 
435                    videoContent, 
436                    adsRenderingSettings 
437                );
438                    
439                // Add listeners to the required events.     
440                adsManager.addEventListener( 
441                    google.ima.AdErrorEvent.Type.AD_ERROR, 
442                    onAdError
443                );
444                adsManager.addEventListener( 
445                    google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED, 
446                    onContentPauseRequested 
447                );        
448                adsManager.addEventListener( 
449                    google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED, 
450                    onContentResumeRequested 
451                );      
452                adsManager.addEventListener( 
453                    google.ima.AdEvent.Type.ALL_ADS_COMPLETED, 
454                    onAdEvent
455                );
456
457                // Listen to any additional events, if necessary.
458                adsManager.addEventListener( 
459                    google.ima.AdEvent.Type.LOADED, 
460                    onAdEvent 
461                );
462                adsManager.addEventListener( 
463                    google.ima.AdEvent.Type.STARTED, 
464                    onAdEvent 
465                );
466                adsManager.addEventListener( 
467                    google.ima.AdEvent.Type.COMPLETE, 
468                    onAdEvent 
469                );
470
471                // For non-auto ad breaks, listen for ad break ready
472                adsManager.addEventListener( 
473                    google.ima.AdEvent.Type.AD_BREAK_READY, 
474                    adBreakReadyHandler 
475                );
476
477                // Other events
478                adsManager.addEventListener( 
479                    google.ima.AdEvent.Type.AD_METADATA, 
480                    onAdEvent 
481                );  
482                adsManager.addEventListener( 
483                    google.ima.AdEvent.Type.CLICK, 
484                    onAdEvent 
485                );    
486                adsManager.addEventListener( 
487                    google.ima.AdEvent.Type.DURATION_CHANGE, 
488                    onAdEvent 
489                );  
490                adsManager.addEventListener( 
491                    google.ima.AdEvent.Type.FIRST_QUARTILE, 
492                    onAdEvent 
493                );   
494                adsManager.addEventListener( 
495                    google.ima.AdEvent.Type.IMPRESSION, 
496                    onAdEvent 
497                );   
498                adsManager.addEventListener( 
499                    google.ima.AdEvent.Type.INTERACTION, 
500                    onAdEvent 
501                );  
502                adsManager.addEventListener( 
503                    google.ima.AdEvent.Type.LINEAR_CHANGED, 
504                    onAdEvent 
505                );
506                adsManager.addEventListener( 
507                    google.ima.AdEvent.Type.LOG, 
508                    onAdEvent 
509                );
510                adsManager.addEventListener( 
511                    google.ima.AdEvent.Type.MIDPOINT, 
512                    onAdEvent 
513                );
514                adsManager.addEventListener( 
515                    google.ima.AdEvent.Type.PAUSED, 
516                    onAdEvent 
517                );
518                adsManager.addEventListener( 
519                    google.ima.AdEvent.Type.RESUMED, 
520                    onAdEvent 
521                );
522                adsManager.addEventListener( 
523                    google.ima.AdEvent.Type.SKIPPABLE_STATE_CHANGED, 
524                    onAdEvent 
525                );
526                adsManager.addEventListener( 
527                    google.ima.AdEvent.Type.SKIPPED, 
528                    onAdEvent 
529                );  
530                adsManager.addEventListener( 
531                    google.ima.AdEvent.Type.THIRD_QUARTILE, 
532                    onAdEvent 
533                );
534                adsManager.addEventListener( 
535                    google.ima.AdEvent.Type.USER_CLOSE, 
536                    onAdEvent 
537                );
538                adsManager.addEventListener( 
539                    google.ima.AdEvent.Type.VOLUME_CHANGED, 
540                    onAdEvent 
541                );
542                adsManager.addEventListener( 
543                    google.ima.AdEvent.Type.VOLUME_MUTED, 
544                    onAdEvent 
545                );
546
547                // Do other task
548                // Ads resize
549                $(window).on('resize', function() {
550                    adWidth         = playerWrap.width(); // New Width
551                    adHeight        = playerWrap.height();
551 // New Height
552
553                    adsManager.resize(adWidth, adHeight, google.ima.ViewMode.NORMAL);
554                });
555
556                // Floating Player
557                $(document).on('floatingVideoEnable floatingVideoDisable', function() {
558                    adWidth         = playerWrap.width(); // New Width
559                    adHeight        = playerWrap.height(); // New Height
560
561                    adsManager.resize(adWidth, adHeight, google.ima.ViewMode.NORMAL);
562                });
563
564                // Ads Mobile
565                if ( HaruVidi.video.isMobileOS() ) {
566                    adsManager.setVolume(0);
567                    videoAdsMute.addClass('active');
568                }
569
570                videoAdsMute.off().on('click', function() {
571                    videoAdsMute.toggleClass('active');
572
573                    if ( videoAdsMute.hasClass('active') ) {
574                        adsManager.setVolume(0);
575                    } else {
576                        adsManager.setVolume(1);
577                    }
578                });
579                
580                // https://developers.google.com/interactive-media-ads/docs/sdks/html5/autoplay
581                videoAdsLoading.addClass('ads-loading-show');
582
583                adDisplayContainer.initialize();
584
585                try {
586                    // Initialize the ads manager. Ad rules playlist will start at this time.
587                    adsManager.init(adWidth, adHeight, google.ima.ViewMode.NORMAL);
588                    // Do something like volume,...
589                    adsManager.start();                                 
590                } catch (adError) {
591                    // An error may be thrown if there was a problem with the VAST response.
592                    console.log('adsManagerInit: ' + adError);
593                                            
594                    // processVideoContentPlayer
595                    playerWrap.removeClass('ads-active');
596                    adContainer.addClass('ads-hide');  
597                    videoAdsLoading.removeClass('ads-loading-show');
598                    videoAdsMute.removeClass('active'); 
599                    videoContent.play();
600                }
601            }
602
603            var onAdError = function(adErrorEvent) {
604                // Handle the error logging.
605                console.log('adsManager: ' + adErrorEvent.getError());
606                adsManager.destroy();
607
608                // processVideoContentPlayer
609                playerWrap.removeClass('ads-active');
610                adContainer.addClass('ads-hide');  
611                videoAdsLoading.removeClass('ads-loading-show');
612                videoAdsMute.removeClass('active'); 
613                videoContent.play();
614            }
615
616            var onAdEvent = function(adEvent) {
617                // Retrieve the ad from the event. Some events (e.g. ALL_ADS_COMPLETED) don't have ad object associated.
618                var ad = adEvent.getAd();
619
620                switch (adEvent.type) {
621                    case google.ima.AdEvent.Type.LOADED:
622                        // This is the first event sent for an ad - it is possible to determine whether the ad is a video ad or an overlay.
623                        // https://developers.google.com/interactive-media-ads/docs/sdks/html5/v3/apis
624                        if ( !ad.isLinear() ) {                                                           
625                            playerWrap.addClass('ads-non-linear');                               
626                            videoContent.play();
627                            // Position AdDisplayContainer correctly for overlay. Use ad.width and ad.height.
628                            // Use this to show close ad button: useStyledNonLinearAds (VpaidMode not have skip button)
629                        } else {
630                            // @TODO: add auto skip + show skip button time to display (for skipable ads)
631                            playerWrap.removeClass('ads-non-linear');
632                        }
633                        
634                        videoAdsLoading.removeClass('ads-loading-show');
635                                            
636                        break;
637                    case google.ima.AdEvent.Type.STARTED:
638                        playerWrap.addClass('ads-active');
639                        adContainer.removeClass('ads-hide');
640                        videoAdsLoading.removeClass('ads-loading-show');
641                        
642                        if (ad.isLinear()) {
643                            // Countdown time video: https://developers.google.com/interactive-media-ads/docs/sdks/html5/countdown-timer
644                            var remainingTime = adsManager.getRemainingTime();
645                            videoAdsCountdown.text(HaruVidi.video.adsConvertTime(remainingTime));
646
647                            var remainingTimer = setInterval(function() {
648                                var remainingTime = adsManager.getRemainingTime();
649                                if ( remainingTime < 0 ) {
650                                    clearInterval(remainingTimer);
651                                }
652                                videoAdsCountdown.text(HaruVidi.video.adsConvertTime(remainingTime));
653                            }, 1000);
654                        }
655
656                        if ( adsManager.getVolume() == 0 ) {
657                            videoAdsMute.addClass('active');
658                        }
659                        
660                        break;
661
662                    case google.ima.AdEvent.Type.COMPLETE:
663                        playerWrap.removeClass('ads-active');
664                        adContainer.addClass('ads-hide');
665
666                        break;
667
668                    case google.ima.AdEvent.Type.SKIPPED:
669                        playerWrap.removeClass('ads-active');
670                        adContainer.addClass('ads-hide');
671
672                        break;
673
674                    case google.ima.AdEvent.Type.USER_CLOSE:
675                        playerWrap.removeClass('ads-active');
676                        adContainer.addClass('ads-hide');
677
678                        break;  
679
680                    case google.ima.AdEvent.Type.VOLUME_MUTED:
681                        videoAdsMute.addClass('active');
682
683                        break;  
684                }
685            }
686
687            // https://developers.google.com/interactive-media-ads/docs/sdks/html5/ad-rules
688            var contentEndedListener = function() {
689                console.log('video ended');
690                adsLoader.contentComplete();
691            };
692
693            // This function is where you should setup UI for showing ads (e.g. display ad timer countdown, disable seeking, etc.)
694            var onContentPauseRequested = function() {
695                videoContent.removeEventListener('ended', contentEndedListener);
696                videoContent.pause();
697            }
698
699            // This function is where you should ensure that your UI is ready to play content.
700            var onContentResumeRequested = function() {
701                videoContent.addEventListener('ended', contentEndedListener);
702                videoContent.play();
703            }
704
705            var adBreakReadyHandler = function adBreakReadyHandler(adEvent) {
706                // Here we can get any necessary data about the ad.
707                console.log(adEvent.getAdData());
708
709                // Once we're ready to play ads. To skip this ad break, simply return from this handler without calling adsManager.start().
710                adsManager.start();
711            }
712
713            // Create ads loader
714            adsLoader = new google.ima.AdsLoader(adDisplayContainer);
715            adsLoader.getSettings().setAutoPlayAdBreaks(false);
716
717            // Set VPAID Mode to Enabled
718            adsLoader.getSettings().setVpaidMode(google.ima.ImaSdkSettings.VpaidMode.ENABLED);
719            adsLoader.getSettings().setVpaidMode(google.ima.ImaSdkSettings.VpaidMode.INSECURE);
720
721            // Mobile Ads Skippable: https://developers.google.com/interactive-media-ads/docs/sdks/html5/skippable-ads
722            adsLoader.getSettings().setDisableCustomPlaybackForIOS10Plus(true);
723
724            // Request video ads.
725            var adsRequest = new google.ima.AdsRequest();
726                adsRequest.adTagUrl = ads_ima_url;
727
728            // Specify the linear and nonlinear slot sizes. This helps the SDK to select the correct creative if multiple are returned.
729            adsRequest.linearAdSlotWidth = adWidth;
730            adsRequest.linearAdSlotHeight = adHeight;
731            adsRequest.nonLinearAdSlotWidth = adWidth;
732            adsRequest.nonLinearAdSlotHeight = adHeight;
733            // Mobile Ads: https://developers.google.com/interactive-media-ads/docs/sdks/html5/autoplay
734            
735            adsLoader.requestAds(adsRequest);
736
737            // Add event listeners. Listen and respond to ads loaded and error events.
738            adsLoader.addEventListener(
739                google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED, 
740                onAdsManagerLoaded, 
741                false
742            );
743            adsLoader.addEventListener(
744                google.ima.AdErrorEvent.Type.AD_ERROR, 
745                onAdError,
746                false
747            );
748
749            // An event listener to tell the SDK that our content video is completed so the SDK can play any post-roll ads.
750            videoContent.addEventListener('ended', contentEndedListener); // @TODO: player.on('ended', contentEndedListener);
751        },
752        adsVideoContent: function(params) {
753            var player          = params.player,
754                video_server    = params.video_server;
755
756            if ( video_server == 'selfhost' || video_server == 'google' ) {
757                return player;
758            } else {
759                var videoContent = document.createElement('video');
760            
761                videoContent.play = function() {
762                    HaruVidi.video.adsPlayerControls(player, video_server, 'play');
763                }           
764                videoContent.pause =  function() {
765                    HaruVidi.video.adsPlayerControls(player, video_server, 'pause');
766                }
767                
768                return videoContent;
769            }
770        },
771        adsPlayerControls: function(player, video_server, control) {
772            // Video Server Process
773            if (video_server == 'youtube') {
774                if ( control == 'pause' ) {
775                    player.pauseVideo();
776                } else if ( control == 'play' ) {
777                    player.playVideo();
778                }
779            } else {
780                if ( control == 'pause' ) {
781                    player.pause();
782                } else if ( control == 'play' ) {
783                    player.play();
784                }
785            }
786            // @TODO: JS Player Process
787        },
788        adsConvertTime: function(seconds) {
789            // https://stackoverflow.com/questions/1322732/convert-seconds-to-hh-mm-ss-with-javascript
790            if ( !isNaN(seconds) ) {
791                var time;
792                time = new Date(seconds * 1000).toISOString().substr(11, 8);
793
794                var time_arr = time.split(':');
795
796                if ( time_arr.length > 2 && time_arr[0] == '00' ) {
797                    time = time_arr[1] + ':' + time_arr[2];
798                }
799
800                return time;
801            }
802        },
803        playlistScrollTitle: function() {
804            // Need add .audio-list__title span in HTML
805            $('.single-video-playlist-title-scroll').each(function(){
806                var el = $(this);
807
808                $(this).mouseenter(function(){
809                    if ( $(this).find('span').width() > $(this).width() ) {
810                        var scroll_text = setInterval(function () {
811                            scrollText();
812                        }, 30);
813                    } else {
814                        return;
815                    }
816
817                    $(this).mouseleave(function(){
818                        clearInterval(scroll_text);
819
820                        el.css({
821                            left: 0
822                        });
823                    });
824                });
825
826                var scrollText = function () {
827                    var width = el.find('span').width(),
828                        left = el.position().left - 1;
829
830                    left = -left >
830 width ? el.width() : left;
831                    el.css({
832                        left: left
833                    });
834                };
835            });
836        },
837        videoReport: function() {
838            $('.video-report-button').off().on('click', function() {
839                var $this           = $(this);
840                var video_id        = $(this).attr('data-video_id');
841                var reportWrap      = $(this).parents('.video-report-form');
842                var report_content  = reportWrap.find('.video-report-content').val();
843
844                $.ajax({
845                    type : 'POST',
846                    timeout : 30000,
847                    url : haru_vidi_ajax_url,
848                    data : {
849                        action: 'haru_video_report',
850                        video_id: video_id,
851                        report_content: report_content
852                    },
853                    error: function(xhr,err) {
854                        console.log('Have something wrong! Please try again!');
855                    },
856                    success: function(response) {
857                        console.log(response);
858                        if ( response ) {
859                            if ( response.status == 'success' ) {
860                                reportWrap.find('.video-report-notice').addClass('success').text(response.message);
861                                reportWrap.find('.video-report-reason').hide();
862                                reportWrap.find('.video-report-submit').hide();
863                            } else {
864                                reportWrap.find('.video-report-notice').addClass('failed').text(response.message);
865                            }
866                        }
867                    }
868                });
869            });
870        },
871        channelSubscribe: function() {
872            $('.channel-subscribe').each(function(){
873                var is_clicked = false;
874
875                $('.channel-subscribe').off().on('click', function() {
876                    var $this           = $(this);
877                    var channel_id      = parseInt($(this).attr('data-channel_id'));
878                    var text_subscribe  = $(this).attr('data-text_subscribe');
879                    var text_subscribed = $(this).attr('data-text_subscribed');
880                    var login           = $(this).attr('data-login');
881                    var login_url       = $(this).attr('data-login_url');
882
883                    if ( typeof(login) === 'undefined' || login === 'no' ) {
884                        window.location.href = login_url; // Go to sign up link
885
886                        return false;
887                    }
888
889                    // Process click multitimes before ajax process
890                    if ( is_clicked === true ) {
891                        return;
892                    }
893
894                    is_clicked = true;   
895
896                    $.ajax({
897                        type : 'POST',
898                        timeout : 30000,
899                        url : haru_vidi_ajax_url,
900                        data : {
901                            action: 'haru_channel_subscribe',
902                            channel_id: channel_id,
903                            login: login
904                        },
905                        error: function(xhr,err) {
906                            console.log('Have something wrong! Please try again!');
907                        },
908                        success: function(response) {
909                            if ( response ) {
910                                if ( response.channel_subscribed == 1 ) {
911                                    $this.find('.text-subscribe').text(text_subscribed);
912                                    $this.find('.count-subscribed').text(response.channel_subscribed_count);
913                                    $this.parents('.channel-item').find('.count-subscribed').text(response.channel_subscribed_count);
914                                    $this.parents('.single-channel__meta-info').find('.count-subscribed').text(response.channel_subscribed_count);
915                                    $this.addClass('subscribed');
916                                } else {
917                                    $this.find('.text-subscribe').text(text_subscribe);
918                                    $this.find('.count-subscribed').text(response.channel_subscribed_count);
919                                    $this.parents('.channel-item').find('.count-subscribed').text(response.channel_subscribed_count);
920                                    $this.parents('.single-channel__meta-info').find('.count-subscribed').text(response.channel_subscribed_count);
921                                    $this.removeClass('subscribed');
922                                }
923                            }
924
925                            is_clicked = false;
926                        }
927                    });
928                });
929            });
930        },
931        thumbnailHoverSlideshow: function() {
932            $('.video-thumbnail.slideshow, .playlist-thumbnail.slideshow, .series-thumbnail.slideshow').each(function(){
933                var $self = $(this);
934                var speed = $(this).data('speed');
935                var currentIndex = 0,
936                    items = $('img', $self),
937                    itemAmt = items.length;
938
939                if ( itemAmt == 1 ) {
940                    return;
941                }
942
943                function cycleItems(currentIndex) {
944                    var item = $('img', $self).eq(currentIndex);
945                    items.hide();
946                    item.css('display','block'); // @TODO: item.fadeIn();
947                }
948
949                $self.mouseenter(function(){
950                    var autoSlide = setInterval(function() {
951                        currentIndex += 1;
952                        if ( currentIndex > itemAmt - 1 ) {
953                            currentIndex = 0;
954                            // Go to first & stop
955                            clearInterval(autoSlide);
956                        }
957                        cycleItems(currentIndex);
958                    }, speed);
959
960                    $self.mouseleave(function(){
961                        clearInterval(autoSlide);
962                        // Show first thumb
963                        $.each(items, function( index, item ) {
964                            if ( item == items[0] ) {
965                                $(item).css('display','block');
966                            } else {
967                                $(item).hide();
968                            }
969                        });
970                    });
971                });
972            });
973        },
974        buddyPress: function() {
975            $('.bp-video-load-more').off().on('click', function() {
976                var $this           = $(this).addClass('loading');
977                var contentWrap     = '.layout-wrap';
978                var element         = '.layout-wrap article.grid-item';
979                var current_page    = parseInt($(this).attr('data-current_page'));
980                var max_page        = parseInt($(this).attr('data-max_page'));
981                var posts_per_page  = parseInt($(this).attr('data-posts_per_page'));
982
983                $this.addClass('loading');
984
985                $.ajax({
986                    type : 'POST',
987                    timeout : 30000,
988                    url : haru_vidi_ajax_url,
989                    data : {
990                        action: 'haru_bp_get_video', 
991                        current_page: current_page, 
992                        max_page: max_page,
993                        posts_per_page: posts_per_page
994                    },
995                    error: function(xhr,err) {
996                        console.log('Have something wrong! Please try again!');
997                    },
998                    success: function(response) {
999                        if ( response ) {
1000                            var $newElems = $(response).find('.video-item').css({ opacity: 0 });
1001
1002                            $(contentWrap).append($newElems);
1003
1004                            $newElems.imagesLoaded(function () {
1005                                $newElems.animate({ opacity: 1 });
1006
1007                                $(contentWrap).isotope('appended', $newElems);
1008
1009                                setTimeout(function() {
1010                                    $(contentWrap).isotope('layout');
1011                                }, 300);
1012
1013                                // Re Init functions
1014                                HaruVidi.video.init();
1015                            });
1016
1017                            if ( current_page + 1 >= max_page ) {
1018                                $this.parent().hide(); // @TODO: cause height issue
1019                            } else {
1020                                $this.removeClass('loading');
1021                                $this.attr('data-current_page', current_page + 1);
1022                            }
1023                        }
1024                    }
1025                });
1026            });
1027
1028            // Playlist
1029            $('.bp-playlist-load-more').off().on('click', function() {
1030                var $this           = $(this).addClass('loading');
1031                var contentWrap     = '.layout-wrap';
1032                var element         = '.layout-wrap article.grid-item';
1033                var current_page    = parseInt($(this).attr('data-current_page'));
1034                var max_page        = parseInt($(this).attr('data-max_page'));
1035                var posts_per_page  = parseInt($(this).attr('data-posts_per_page'));
1036
1037                $this.addClass('loading');
1038
1039                $.ajax({
1040                    type : 'POST',
1041                    timeout : 30000,
1042                    url : haru_vidi_ajax_url,
1043                    data : {
1044                        action: 'haru_bp_get_playlist', 
1045                        current_page: current_page, 
1046                        max_page: max_page,
1047                        posts_per_page: posts_per_page
1048                    },
1049                    error: function(xhr,err) {
1050                        console.log('Have something wrong! Please try again!');
1051                    },
1052                    success: function(response) {
1053                        if ( response ) {
1054                            var $newElems = $(response).find('.playlist-item').css({ opacity: 0 });
1055
1056                            $(contentWrap).append($newElems);
1057
1058                            $newElems.imagesLoaded(function () {
1059                                $newElems.animate({ opacity: 1 });
1060
1061                                $(contentWrap).isotope('appended', $newElems);
1062
1063                                setTimeout(function() {
1064                                    $(contentWrap).isotope('layout');
1065                                }, 300);
1066
1067                                // Re Init functions
1068                                HaruVidi.video.init();
1069                            });
1070
1071                            if ( current_page + 1 >= max_page ) {
1072                                $this.parent().hide(); // @TODO: cause height issue
1073                            } else {
1074                                $this.removeClass('loading');
1075                                $this.attr('data-current_page', current_page + 1);
1076                            }
1077                        }
1078                    }
1079                });
1080            });
1081
1082            // Series
1083            $('.bp-series-load-more').off().on('click', function() {
1084                var $this           = $(this).addClass('loading');
1085                var contentWrap     = '.layout-wrap';
1086                var element         = '.layout-wrap article.grid-item';
1087                var current_page    = parseInt($(this).attr('data-current_page'));
1088                var max_page        = parseInt($(this).attr('data-max_page'));
1089                var posts_per_page  = parseInt($(this).attr('data-posts_per_page'));
1090
1091                $this.addClass('loading');
1092
1093                $.ajax({
1094                    type : 'POST',
1095                    timeout : 30000,
1096                    url : haru_vidi_ajax_url,
1097                    data : {
1098                        action: 'haru_bp_get_series', 
1099                        current_page: current_page, 
1100                        max_page: max_page,
1101                        posts_per_page: posts_per_page
1102                    },
1103                    error: function(xhr,err) {
1104                        console.log('Have something wrong! Please try again!');
1105                    },
1106                    success: function(response) {
1107                        if ( response ) {
1108                            var $newElems = $(response).find('.series-item').css({ opacity: 0 });
1109
1110                            $(contentWrap).append($newElems);
1111
1112                            $newElems.imagesLoaded(function () {
1113                                $newElems.animate({ opacity: 1 });
1114
1115                                $(contentWrap).isotope('appended', $newElems);
1116
1117                                setTimeout(function() {
1118                                    $(contentWrap).isotope('layout');
1119                                }, 300);
1120
1121                                // Re Init functions
1122                                HaruVidi.video.init();
1123                            });
1124
1125                            if ( current_page + 1 >= max_page ) {
1126                                $this.parent().hide(); // @TODO: cause height issue
1127                            } else {
1128                                $this.removeClass('loading');
1129                                $this.attr('data-current_page', current_page + 1);
1130                            }
1131                        }
1132                    }
1133                });
1134            });
1135
1136            // Channel
1137            $('.bp-channel-load-more').off().on('click', function() {
1138                var $this           = $(this).addClass('loading');
1139                var contentWrap     = '.layout-wrap';
1140                var element         = '.layout-wrap article.grid-item';
1141                var current_page    = parseInt($(this).attr('data-current_page'));
1142                var max_page        = parseInt($(this).attr('data-max_page'));
1143                var posts_per_page  = parseInt($(this).attr('data-posts_per_page'));
1144
1145                $this.addClass('loading');
1146
1147                $.ajax({
1148                    type : 'POST',
1149                    timeout : 30000,
1150                    url : haru_vidi_ajax_url,
1151                    data : {
1152                        action: 'haru_bp_get_channel', 
1153                        current_page: current_page, 
1154                        max_page: max_page,
1155                        posts_per_page: posts_per_page
1156                    },
1157                    error: function(xhr,err) {
1158                        console.log('Have something wrong! Please try again!');
1159                    },
1160                    success: function(response) {
1161                        if ( response ) {
1162                            var $newElems = $(response).find('.channel-item').css({ opacity: 0 });
1163
1164                            $(contentWrap).append($newElems);
1165
1166                            $newElems.imagesLoaded(function () {
1167                                $newElems.animate({ opacity: 1 });
1168
1169                                $(contentWrap).isotope('appended', $newElems);
1170
1171                                setTimeout(function() {
1172                                    $(contentWrap).isotope('layout');
1173                                }, 300);
1174
1175                                // Re Init functions
1176                                HaruVidi.video.init();
1177                            });
1178
1179                            if ( current_page + 1 >= max_page ) {
1180                                $this.parent().hide(); // @TODO: cause height issue
1181                            } else {
1182                                $this.removeClass('loading');
1183                                $this.attr('data-current_page', current_page + 1);
1184                            }
1185                        }
1186                    }
1187                });
1188            });
1189        },
1190        authorSingle: function() {
1191            $('.author-video-load-more').off().on('click', function() {
1192                var $this           = $(this).addClass('loading');
1193                var contentWrap     = '.layout-wrap';
1194                var element         = '.layout-wrap article.grid-item';
1195                var current_page    = parseInt($(this).attr('data-current_page'));
1196                var max_page        = parseInt($(this).attr('data-max_page'));
1197                var posts_per_page  = parseInt($(this).attr('data-posts_per_page'));
1198
1199                $this.addClass('loading');
1200
1201                $.ajax({
1202                    type : 'POST',
1203                    timeout : 30000,
1204                    url : haru_vidi_ajax_url,
1205                    data : {
1206                        action: 'haru_author_get_video', 
1207                        current_page: current_page, 
1208                        max_page: max_page,
1209                        posts_per_page: posts_per_page
1210                    },
1211                    error: function(xhr,err) {
1212                        console.log('Have something wrong! Please try again!');
1213                    },
1214                    success: function(response) {
1215                        if ( response ) {
1216                            var $newElems = $(response).find('.video-item').css({ opacity: 0 });
1217
1218                            $(contentWrap).append($newElems);
1219
1220                            $newElems.imagesLoaded(function () {
1221                                $newElems.animate({ opacity: 1 });
1222
1223                                $(contentWrap).isotope('appended', $newElems);
1224
1225                                setTimeout(function() {
1226                                    $(contentWrap).isotope('layout');
1227                                }, 300);
1228
1229                                // Re Init functions
1230                                HaruVidi.video.init();
1231                            });
1232
1233                            if ( current_page + 1 >= max_page ) {
1234                                $this.parent().hide(); // @TODO: cause height issue
1235                            } else {
1236                                $this.removeClass('loading');
1237                                $this.attr('data-current_page', current_page + 1);
1238                            }
1239                        }
1240                    }
1241                });
1242            });
1243
1244            $('.author-playlist-load-more').off().on('click', function() {
1245                var $this           = $(this).addClass('loading');
1246                var contentWrap     = '.layout-wrap';
1247                var element         = '.layout-wrap article.grid-item';
1248                var current_page    = parseInt($(this).attr('data-current_page'));
1249                var max_page        = parseInt($(this).attr('data-max_page'));
1250                var posts_per_page  = parseInt($(this).attr('data-posts_per_page'));
1251
1252                $this.addClass('loading');
1253
1254                $.ajax({
1255                    type : 'POST',
1256                    timeout : 30000,
1257                    url : haru_vidi_ajax_url,
1258                    data : {
1259                        action: 'haru_author_get_playlist', 
1260                        current_page: current_page, 
1261                        max_page: max_page,
1262                        posts_per_page: posts_per_page
1263                    },
1264                    error: function(xhr,err) {
1265                        console.log('Have something wrong! Please try again!');
1266                    },
1267                    success: function(response) {
1268                        if ( response ) {
1269                            var $newElems = $(response).find('.playlist-item').css({ opacity: 0 });
1270
1271                            $(contentWrap).append($newElems);
1272
1273                            $newElems.imagesLoaded(function () {
1274                                $newElems.animate({ opacity: 1 });
1275
1276                                $(contentWrap).isotope('appended', $newElems);
1277
1278                                setTimeout(function() {
1279                                    $(contentWrap).isotope('layout');
1280                                }, 300);
1281
1282                                // Re Init functions
1283                                HaruVidi.video.init();
1284                            });
1285
1286                            if ( current_page + 1 >= max_page ) {
1287                                $this.parent().hide(); // @TODO: cause height issue
1288                            } else {
1289                                $this.removeClass('loading');
1290                                $this.attr('data-current_page', current_page + 1);
1291                            }
1292                        }
1293                    }
1294                });
1295            });
1296
1297            $('.author-series-load-more').off().on('click', function() {
1298                var $this           = $(this).addClass('loading');
1299                var contentWrap     = '.layout-wrap';
1300                var element         = '.layout-wrap article.grid-item';
1301                var current_page    = parseInt($(this).attr('data-current_page'));
1302                var max_page        = parseInt($(this).attr('data-max_page
1302'));
1303                var posts_per_page  = parseInt($(this).attr('data-posts_per_page'));
1304
1305                $this.addClass('loading');
1306
1307                $.ajax({
1308                    type : 'POST',
1309                    timeout : 30000,
1310                    url : haru_vidi_ajax_url,
1311                    data : {
1312                        action: 'haru_author_get_series', 
1313                        current_page: current_page, 
1314                        max_page: max_page,
1315                        posts_per_page: posts_per_page
1316                    },
1317                    error: function(xhr,err) {
1318                        console.log('Have something wrong! Please try again!');
1319                    },
1320                    success: function(response) {
1321                        if ( response ) {
1322                            var $newElems = $(response).find('.series-item').css({ opacity: 0 });
1323
1324                            $(contentWrap).append($newElems);
1325
1326                            $newElems.imagesLoaded(function () {
1327                                $newElems.animate({ opacity: 1 });
1328
1329                                $(contentWrap).isotope('appended', $newElems);
1330
1331                                setTimeout(function() {
1332                                    $(contentWrap).isotope('layout');
1333                                }, 300);
1334
1335                                // Re Init functions
1336                                HaruVidi.video.init();
1337                            });
1338
1339                            if ( current_page + 1 >= max_page ) {
1340                                $this.parent().hide(); // @TODO: cause height issue
1341                            } else {
1342                                $this.removeClass('loading');
1343                                $this.attr('data-current_page', current_page + 1);
1344                            }
1345                        }
1346                    }
1347                });
1348            });
1349
1350            $('.author-channel-load-more').off().on('click', function() {
1351                var $this           = $(this).addClass('loading');
1352                var contentWrap     = '.layout-wrap';
1353                var element         = '.layout-wrap article.grid-item';
1354                var current_page    = parseInt($(this).attr('data-current_page'));
1355                var max_page        = parseInt($(this).attr('data-max_page'));
1356                var posts_per_page  = parseInt($(this).attr('data-posts_per_page'));
1357
1358                $this.addClass('loading');
1359
1360                $.ajax({
1361                    type : 'POST',
1362                    timeout : 30000,
1363                    url : haru_vidi_ajax_url,
1364                    data : {
1365                        action: 'haru_author_get_channel', 
1366                        current_page: current_page, 
1367                        max_page: max_page,
1368                        posts_per_page: posts_per_page
1369                    },
1370                    error: function(xhr,err) {
1371                        console.log('Have something wrong! Please try again!');
1372                    },
1373                    success: function(response) {
1374                        if ( response ) {
1375                            var $newElems = $(response).find('.channel-item').css({ opacity: 0 });
1376
1377                            $(contentWrap).append($newElems);
1378
1379                            $newElems.imagesLoaded(function () {
1380                                $newElems.animate({ opacity: 1 });
1381
1382                                $(contentWrap).isotope('appended', $newElems);
1383
1384                                setTimeout(function() {
1385                                    $(contentWrap).isotope('layout');
1386                                }, 300);
1387
1388                                // Re Init functions
1389                                HaruVidi.video.init();
1390                            });
1391
1392                            if ( current_page + 1 >= max_page ) {
1393                                $this.parent().hide(); // @TODO: cause height issue
1394                            } else {
1395                                $this.removeClass('loading');
1396                                $this.attr('data-current_page', current_page + 1);
1397                            }
1398                        }
1399                    }
1400                });
1401            });
1402        },
1403        shortcodeSubmit: function() {
1404            // Do something
1405        },
1406        videoPlayer: function() {
1407            // Process for unmute video button
1408            if ( HaruVidi.video.isiOS() || HaruVidi.video.isMobileOS() ) {
1409                $('body').addClass('haru-ios');
1410            }
1411
1412            HaruVidi.video.displayVideoPlayerDirect();
1413            HaruVidi.video.displayVideoPlayerPopup();
1414        },
1415        processAutoPlay: function() {
1416            // Check on each server serverAPI
1417            var serverAPI = null;
1418            var autoplay = null;
1419
1420            if ( $('.video-player-data').length > 0 ) {
1421                serverAPI = $('.video-player-data').attr('data-server');
1422                autoplay = ($('.video-player-data').attr('data-autoplay')) == 'true' ? true : false;
1423
1424                // Check on iOS and Safari
1425                if ( HaruVidi.video.isiOS() ) {
1426                    autoplay == true;
1427
1428                    switch ( serverAPI ) {
1429                        case 'vimeo':
1430                            HaruVidi.video.muted = true;
1431
1432                            break;
1433
1434                        case 'youtube':
1435                            HaruVidi.video.muted = true;
1436
1437                            break;
1438
1439                        case 'twitch':
1440                            HaruVidi.video.muted = true;
1441
1442                            break;
1443
1444                        case 'dailymotion':
1445                            HaruVidi.video.muted = true;
1446
1447                            break;
1448
1449                        case 'cloudflare':
1450                            HaruVidi.video.muted = true;
1451
1452                            break;
1453
1454                        case 'facebook':
1455                            // @TODO: doesn't work safari & iOS
1456                            HaruVidi.video.muted = false;
1457
1458                            break;
1459                            
1460                        case 'selfhost':
1461                            // @TODO: check video player js library
1462                            HaruVidi.video.muted = true;
1463
1464                            break;
1465
1466                        case 'google':
1467                            // @TODO: check video player js library
1468                            HaruVidi.video.muted = true;
1469
1470                            break;                              
1471                    }
1472                } else {
1473                    if ( autoplay == true ) {
1474                        switch ( serverAPI ) {
1475                            case 'vimeo':
1476                                if ( HaruVidi.video.isSafari() ) {
1477                                    HaruVidi.video.muted = true;
1478                                }
1479
1480                                break;
1481
1482                            case 'youtube':
1483                                if ( HaruVidi.video.isSafari() ) {
1484                                    HaruVidi.video.muted = true;
1485                                }
1486
1487                                break;
1488
1489                            case 'twitch':
1490                                if ( HaruVidi.video.isSafari() ) {
1491                                    HaruVidi.video.muted = true;
1492                                }
1493
1494                                break;
1495
1496                            case 'dailymotion':
1497                                if ( HaruVidi.video.isSafari() ) {
1498                                    HaruVidi.video.muted = true;
1499                                }
1500
1501                                break;
1502
1503                            case 'cloudflare':
1504                                HaruVidi.video.muted = true;
1505
1506                                break;
1507
1508                            case 'facebook':
1509                                // @TODO: doesn't work safari & iOS
1510                                HaruVidi.video.muted = true;
1511
1512                                break;
1513                                
1514                            case 'selfhost':
1515                                // @TODO: check video player js library
1516                                HaruVidi.video.muted = true;
1517
1518                                break;
1519
1520                            case 'google':
1521                                // @TODO: check video player js library
1522                                HaruVidi.video.muted = true;
1523
1524                                break;                              
1525                        }
1526                    }
1527                }
1528
1529                if ( autoplay == true ) {
1530                    setTimeout(function() {
1531                        $('.video-player-direct').trigger('click');
1532                    }, 10);
1533                }
1534            }
1535        },
1536        beforeAjax: function() {
1537            $('.haru-ajax-overflow').addClass('active');
1538        },
1539        afterAjax: function() {
1540            $('.haru-ajax-overflow').removeClass('active');
1541        },
1542        displayVideoPlayerPopup: function() {
1543            $('.video-player-popup').off().on('click', function(e) {
1544                e.preventDefault();
1545                var video_id = $(this).data('id');
1546                var playlist_id = $(this).data('playlist');
1547                var playerID = 'youtube-video'; // @TODO: need change here
1548
1549                if ( HaruVidi.video.videos_data[video_id] != undefined ) {
1550                    $('body').find('.haru-lightbox-content').remove();
1551                    $('body').find('.haru-lightbox').append( HaruVidi.video.videos_data[video_id] );
1552                    $('body').find('.haru-lightbox-overlay').addClass('show-lightbox');
1553                    $('body').find('.haru-lightbox').addClass('show-lightbox');
1554
1555                    HaruVidi.video.displayVideoPlayerDirect();
1556                    HaruVidi.video.playlistScrollBar();
1557                    HaruVidi.video.getVideoPlayerPopupAjax();
1558
1559                    return;
1560                }
1561
1562                $.ajax({
1563                    url: haru_vidi_ajax_url,
1564                    type: "POST",
1565                    data: {
1566                        action: 'haru_video_player_popup_content',
1567                        video_id: video_id,
1568                        playlist_id: playlist_id
1569                    },
1570                    dataType: "html",
1571                    beforeSend: function() {
1572                        HaruVidi.video.beforeAjax();
1573                    }
1574                }).success(function(result) {
1575                    HaruVidi.video.afterAjax();
1576
1577                    if ( result ) {
1578                        $('body').find('.haru-lightbox-content').remove();
1579                        $('body').find('.haru-lightbox').append(result);
1580                        $('body').find('.haru-lightbox-overlay').addClass('show-lightbox');
1581                        $('body').find('.haru-lightbox').addClass('show-lightbox');                       
1582
1583                        // Actions after ajax
1584                        HaruVidi.video.displayVideoPlayerDirect();
1585                        HaruVidi.video.playlistScrollBar();
1586                        HaruVidi.video.getVideoPlayerPopupAjax(); // @TODO: for Playlist
1587                    }
1588
1589                    $('.close-lightbox').off().on('click', function() {
1590                        $('body').find('.haru-lightbox-content').remove();
1591                        $('body').find('.haru-lightbox-overlay').removeClass('show-lightbox');
1592                        $('body').find('.haru-lightbox').removeClass('show-lightbox');
1593                    });
1594                });
1595            });
1596        },
1597        getVideoPlayerPopupAjax: function() {
1598            $('.popup-playlist .video-item').each(function() {
1599                var $self = $(this);
1600
1601                $(this).find('.video-player-popup-ajax').off().on('click', function(e) {
1602                    e.preventDefault();
1603                    var video_id = $(this).data('id');
1604
1605                    $.ajax({
1606                        url: haru_vidi_ajax_url,
1607                        type: "POST",
1608                        data: {
1609                            action: 'haru_get_video_player_popup_ajax',
1610                            video_id: video_id
1611                        },
1612                        dataType: "html",
1613                        beforeSend: function() {
1614                            // Process animate
1615                        }
1616                    }).success(function(result) {
1617                        $self.addClass('active').siblings('.popup-playlist .video-item').removeClass('active');
1618
1619                        $('body').find('.video-player-container').remove();
1620                        $('body').find('.single-video-top').prepend(result); // prepend
1621
1622                        // Actions after ajax
1623                        HaruVidi.video.displayVideoPlayerDirect();
1624                        // Process
1625                    });
1626                });
1627            });
1628        },
1629        displayVideoPlayerDirect: function() {
1630            // Autoplay
1631            HaruVidi.video.processAutoPlay();
1632
1633            // Check on iOS
1634            if ( HaruVidi.video.isiOS() ) {
1635                $('.video-toggle-fullscreen').hide();
1636            }
1637
1638            // @TODO: ajax Youtube playlist player.getDuration is not a function
1639            $('.video-player-direct').off().on('click', function(e) {
1640                e.preventDefault();
1641
1642                var $self = $(this);
1643                var video_id = $(this).attr('data-video-id');
1644                var video_server = $(this).attr('data-video-server');
1645                var player_js = $(this).attr('data-player');
1646
1647                HaruVidi.video.videoID = video_id;
1648                HaruVidi.video.video_server = video_server;
1649
1650                // Loading icon
1651                $self.parents('.video-image').addClass('loading');
1652
1653                // Do not use Player JS
1654                if( player_js == 'none' ) {
1655                    // Youtube
1656                    if ( video_server == 'youtube' ) {
1657                        // Do something
1658                        HaruVidi.video.youtubeAPIReady(video_id);
1659                    }
1660                    // Vimeo
1661                    if ( video_server == 'vimeo' ) {
1662                        // Do something
1663                        HaruVidi.video.vimeoAPIReady();
1664                    }
1665                    // Twitch
1666                    if ( video_server == 'twitch' ) {
1667                        // Do something
1668                        HaruVidi.video.twitchAPIReady();
1669                    }
1670                    // Dailymotion
1671                    if ( video_server == 'dailymotion' ) {
1672                        // Do something
1673                        HaruVidi.video.dailymotionAPIReady();
1674                    }
1675                    // Cloudflare
1676                    if ( video_server == 'cloudflare' ) {
1677                        // Do something
1678                        HaruVidi.video.cloudflareAPIReady();
1679                    }
1680                    // Facebook
1681                    if ( video_server == 'facebook' ) {
1682                        // Do something
1683                        HaruVidi.video.facebookAPIReady();
1684                    }
1685                    // Google or SelfHosted
1686                    if ( video_server == 'selfhost' || video_server == 'google' ) {
1687                        // Do something
1688                        HaruVidi.video.selfHostAPIReady();
1689                    }
1690                    // Embed
1691                    if ( video_server == 'embed' ) {
1692                        // Do something like autoplay
1693                        $self.parents('.video-image').addClass('played');
1694                        $self.parents('.video-image').removeClass('loading');
1695                    }
1696                    // Shortcode
1697                    if ( video_server == 'shortcode' ) {
1698                        // Do something like autoplay
1699                        $self.parents('.video-image').addClass('played');
1700                        $self.parents('.video-image').removeClass('loading');
1701                    }
1702                    // Other
1703                    if ( video_server == 'other' ) {
1704                        // Do something like autoplay
1705                        $self.parents('.video-image').addClass('played');
1706                        $self.parents('.video-image').removeClass('loading');
1707                    }
1708                }
1709                // Use JS Player
1710
1711            });
1712
1713            // Toolbar actions
1714            $('.toolbar-action.action-show').off().on('click', function(e) {
1715                var group_show = $(this).attr('data-group');
1716
1717                setTimeout(function(){
1718                    $('.haru-slick-more-videos').not('.slick-initialized').slick();
1719                }, 10);
1720
1721                if ( $('.toolbar-group.' + group_show).hasClass('show') ) {
1722                    $('.toolbar-group').removeClass('show');
1723                    $('.toolbar-action.action-show').removeClass('active');
1724                    $(this).removeClass('active');
1725                } else {
1726                    $('.toolbar-action.action-show').removeClass('active');
1727                    $(this).addClass('active');
1728                    $('.toolbar-group:not(.' + group_show + ')').removeClass('show');
1729                    $('.toolbar-group.' + group_show).addClass('show');
1730                }
1731            });
1732        },
1733        youtubePlayer: function(video_id) {
1734            // https://css-tricks.com/play-button-youtube-and-vimeo-api/
1735            // https://developers.google.com/youtube/iframe_api_reference
1736            // https://chromatichq.com/blog/working-youtube-player-api-iframe-embeds
1737            // Failed to execute 'postMessage' on 'DOMWindow': The target origin provided (not use HTTPS)             
1738            var youtubeScriptId = 'youtube-api';
1739            var youtubeScript = document.getElementById(youtubeScriptId);
1740
1741            // Load YouTube API script
1742            if ( youtubeScript === null ) {
1743                var tag = document.createElement('script');
1744                    tag.src = 'https://www.youtube.com/iframe_api';
1745                var firstScriptTag = document.getElementsByTagName('script')[0];
1746                    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
1747            }
1748
1749            window.onYouTubeIframeAPIReady = function() {
1750                $(document).trigger('youtubeAPIReady' + video_id);
1751            };
1752        },
1753        youtubeAPIReady: function(video_id) {
1754            $(document).on('youtubeAPIReady' + video_id, function(){
1755                var player;
1756                var playerWrap = $('#youtube-video').parents('.haru-video-player');
1757
1758                // Remove loading and add played video
1759                playerWrap.find('.video-image').removeClass('loading').addClass('played');
1760
1761                // https://developers.google.com/youtube/player_parameters
1762                var options = {
1763                        autoplay:       1,
1764                        enablejsapi:    1,
1765                        iv_load_policy: 1,
1766                        modestbranding: 1,
1767                        playsinline:    1,
1768                        rel:            0
1769                    }
1770
1771                player = new YT.Player('youtube-video', {
1772                    videoId: String(HaruVidi.video.videoID),
1773                    playerVars: options,
1774                    events: {
1775                        // Call this function when player is ready to use
1776                        'onReady': function() {
1777                            if ( HaruVidi.video.muted == true ) {
1778                                player.mute();
1779                            }
1780                            player.playVideo(); // Autoplay
1781
1782                            // Do something
1783                            $('.video-youtube-unmute').on('click', function() {
1784                                player.unMute();
1785
1786                                $(this).hide(300);
1787                            });
1788                        },
1789                        'onStateChange': function(e){
1790                            if ( HaruVidi.video.ads_params.ads_on ) {
1791                                if ( typeof(HaruVidi.video.ad_displayed[HaruVidi.video.ads_params.ads_id]) === 'undefined' ) {
1792                                    // Show ads only when video start
1793                                    if ( player.getCurrentTime() >= 0 && player.getDuration() > 0 ) {
1794                                        // Self Ads
1795                                        if ( e.data == YT.PlayerState.PLAYING && HaruVidi.video.ads_params.ads_type != 'google_ima' ) {
1796                                            HaruVidi.video.adsLocal({
1797                                                'player':               player,
1798                                                'playerWrap':           playerWrap,
1799                                                'ads_params':           HaruVidi.video.ads_params,
1800                                            });
1801                                        } else if ( HaruVidi.video.ads_params.ads_type == 'google_ima' ) {
1802                                             HaruVidi.video.adsGoogle({
1803                                                'player':               player,
1804                                                'playerWrap':           playerWrap,
1805                                                'ads_params':           HaruVidi.video.ads_params,
1806                                            });
1807                                        }
1808                                    }
1809                                }
1810                            } 
1811
1812                            if ( e.data === 0 ) {
1813                                console.log('Player Ended');
1814                                
1815                                if ( HaruVidi.video.autoNextStatus() == true ) {
1816                                    HaruVidi.video.autoNextProcess({
1817                                        'playerWrap': playerWrap
1818                                    });
1819                                }
1820                                
1821                                e.target.stopVideo();
1822                            }
1823                        }
1824                    }
1825                });
1826            });
1827
1828            HaruVidi.video.youtubePlayer(video_id);
1829        },
1830        vimeoAPIReady: function() {
1831            // https://developer.vimeo.com/player/sdk/reference#play-a-video
1832            $(document).on('vimeoAPIReady', function(){
1833                var player;
1834                var playerWrap = $('#vimeo-video').parents('.haru-video-player');
1835
1836               // Remove loading and add played video
1837                playerWrap.find('.video-image').removeClass('loading').addClass('played');
1838
1839                // https://vimeo.zendesk.com/hc/en-us/articles/360001494447-Using-Player-Parameters
1840                var options = {
1841                        id:             String(HaruVidi.video.videoID),
1842                        autoplay:       1,
1843                        muted:          HaruVidi.video.muted,
1844                        playsinline:    true,
1845                    }
1846
1847                player = new Vimeo.Player('vimeo-video',
1848                    options
1849                );
1850
1851                player.ready().then(function(){
1852                    // Mobile iOS @TODO: now use auto click play or must use muted
1853                    player.play().then(function() {
1854                        // The video is playing
1855                        }).catch(function(error) {
1856                            switch (error.name) {
1857                                case 'PasswordError':
1858                                    // The video is password-protected
1859                                    break;
1860
1861                                case 'PrivacyError':
1862                                    // The video is private
1863                                    break;
1864
1865                                default:
1866                                    // Some other error occurred
1867                                    break;
1868                        }
1869                    });
1870
1871                    if ( HaruVidi.video.ads_params.ads_on ) {
1872                        player.on('timeupdate', function(e) {
1873                            Promise.all([player.getDuration(), player.getCurrentTime()]).then(function(dimensions) {
1874                                if ( typeof(HaruVidi.video.ad_displayed[HaruVidi.video.ads_params.ads_id]) === 'undefined' ) {
1875                                    // Show ads only when video start
1876                                    if ( dimensions[1] >= 0 && dimensions[0] > 0 ) {
1877                                        // Self Ads
1878                                        if ( HaruVidi.video.ads_params.ads_type != 'google_ima' ) {
1879                                            HaruVidi.video.adsLocal({
1880                                                'player':               player,
1881                                                'playerWrap':           playerWrap,
1882                                                'ads_params':           HaruVidi.video.ads_params,
1883                                            });
1884                                        } else if ( HaruVidi.video.ads_params.ads_type == 'google_ima' ) {
1885                                            HaruVidi.video.adsGoogle({
1886                                                'player':               player,
1887                                                'playerWrap':           playerWrap,
1888                                                'ads_params':           HaruVidi.video.ads_params,
1889                                            });
1890                                        }
1891                                    }
1892                                }
1893                            });                                     
1894                        });
1895                    }
1896
1897                    player.on('play', function(e) {
1898                        // Do something
1899                    });
1900
1901                    player.on('ended', function(){
1902                        console.log('Video Ended');
1903
1904                        if ( HaruVidi.video.autoNextStatus() == true ) {
1905                            HaruVidi.video.autoNextProcess({
1906                                'playerWrap': playerWrap
1907                            });
1908                        }
1909                    });
1910                });
1911            });
1912
1913            HaruVidi.video.playerAPIReady('vimeo');
1914        },
1915        twitchAPIReady: function() {
1916            // https://dev.twitch.tv/docs/embed/everything
1917            $(document).on('twitchAPIReady', function(){
1918                var player;
1919                var playerWrap = $('#twitch-video').parents('.haru-video-player');
1920
1921                // Remove loading and add played video
1922                playerWrap.find('.video-image').removeClass('loading').addClass('played');
1923
1924                // https://dev.twitch.tv/docs/embed/video-and-clips#interactive-frames-for-live-streams-and-vods
1925                // https://discuss.dev.twitch.tv/t/twitch-embedded-player-updates-in-2020/23956
1926                var options = {
1927                        video:          String(HaruVidi.video.videoID),
1928                        autoplay:       true,
1929                        playsinline:    true,
1930                        muted:      HaruVidi.video.muted,
1931                        parent:     [window.location.hostname, 'www' + window.location.hostname], // require parent and https
1932                        controls: true
1933                    }
1934
1935                player = new Twitch.Embed('twitch-video',
1936                    options
1937                );
1938
1939                player.addEventListener(Twitch.Player.READY, function() {
1940                    player.play();
1941
1942                    player.addEventListener(Twitch.Player.PLAY, function() {
1943                        // Do something
1944                        if ( HaruVidi.video.ads_params.ads_on ) {
1945                            if ( typeof(HaruVidi.video.ad_displayed[HaruVidi.video.ads_params.ads_id]) === 'undefined' ) {
1946                                // Show ads only when video start
1947                                if ( player.getDuration() >= 0 && player.getCurrentTime() > 0 ) {
1948                                    // Self Ads
1949                                    if ( HaruVidi.video.ads_params.ads_type != 'google_ima' ) {
1950                                        HaruVidi.video.adsLocal({
1951                                            'player':               player,
1952                                            'playerWrap':           playerWrap,
1953                                            'ads_params':           HaruVidi.video.ads_params,
1954                                        });
1955                                    } else if ( HaruVidi.video.ads_params.ads_type == 'google_ima' ) {
1956                                        HaruVidi.video.adsGoogle({
1957                                            'player':               player,
1958                                            'playerWrap':           playerWrap,
1959                                            'ads_params':           HaruVidi.video.ads_params,
1960                                        });
1961                                    }
1962                                }
1963                            }
1964                        }
1965                    });
1966
1967                    player.addEventListener(Twitch.Player.ENDED, function() {
1968                        console.log('Video Ended');
1969
1970                        if ( HaruVidi.video.autoNextStatus() == true ) {
1971                            HaruVidi.video.autoNextProcess({
1972                                'playerWrap': playerWrap
1973                            });
1974                        }
1975                    });
1976                });
1977            });
1978
1979            HaruVidi.video.playerAPIReady('twitch');
1980        },
1981        dailymotionAPIReady: function() {
1982            // https://developer.dailymotion.com/player/
1983            $(document).on('dailymotionAPIReady', function(){
1984                var player;
1985                var playerWrap = $('#dailymotion-video').parents('.haru-video-player');
1986
1987                // Remove loading and add played video
1988                playerWrap.find('.video-image').removeClass('loading').addClass('played');
1989
1990                // https://developer.dailymotion.com/player/#player-oembed-par
1990ams
1991                var options = {
1992                        video: String(HaruVidi.video.videoID),
1993                        width: "100%", 
1994                        height: "100%",
1995                        params: { 
1996                            'autoplay': true,
1997                            'mute': HaruVidi.video.muted,
1998                            'queue-enable': false,
1999                            'sharing-enable': false,
2000                            'playsinline': true,
2001                            'webkit-playsinline': true,
2002                            'ui-logo': false
2003                        }
2004                    }
2005
2006                player = DM.player(document.getElementById( 'dailymotion-video' ), 
2007                    options
2008                );
2009
2010                // https://developer.dailymotion.com/player/#player-api-events-player
2011                player.addEventListener('apiready', function() {
2012                    player.addEventListener('playing', function() {
2013                        // Do something
2014                    });
2015
2016                    if ( HaruVidi.video.ads_params.ads_on ) {
2017                        player.addEventListener('timeupdate', function(e){
2018                            if ( typeof(HaruVidi.video.ad_displayed[HaruVidi.video.ads_params.ads_id]) === 'undefined' ) {
2019                                // Show ads only when video start
2020                                if ( player.duration >= 0 && player.currentTime > 0 ) {
2021                                    // Self Ads
2022                                    if ( HaruVidi.video.ads_params.ads_type != 'google_ima' ) {
2023                                        HaruVidi.video.adsLocal({
2024                                            'player':               player,
2025                                            'playerWrap':           playerWrap,
2026                                            'ads_params':           HaruVidi.video.ads_params,
2027                                        });
2028                                    } else if ( HaruVidi.video.ads_params.ads_type == 'google_ima' ) {
2029                                        HaruVidi.video.adsGoogle({
2030                                            'player':               player,
2031                                            'playerWrap':           playerWrap,
2032                                            'ads_params':           HaruVidi.video.ads_params,
2033                                        });
2034                                    }
2035                                }
2036                            }                            
2037                        });
2038                    }
2039
2040                    player.addEventListener('video_end', function() {
2041                        console.log('Video Ended');
2042
2043                        if ( HaruVidi.video.autoNextStatus() == true ) {
2044                            HaruVidi.video.autoNextProcess({
2045                                'playerWrap': playerWrap
2046                            });
2047                        }
2048                    });
2049                });
2050            });
2051
2052            HaruVidi.video.playerAPIReady('dailymotion');
2053        },
2054        cloudflareAPIReady: function() {
2055            // https://developers.cloudflare.com/stream/viewing-videos/using-the-player-api
2056            $(document).on('cloudflareAPIReady', function(){
2057                var player;
2058                var playerWrap = $('#cloudflare-video').parents('.haru-video-player');
2059
2060                // Remove loading and add played video
2061                playerWrap.find('.video-image').removeClass('loading').addClass('played');
2062
2063                player = Stream(document.getElementById('cloudflare-video'));
2064
2065                player.addEventListener('buffered', function() {
2066                    console.log('ok')
2067                });
2068
2069                // https://developers.cloudflare.com/stream/viewing-videos/using-the-player-api
2070                player.muted = HaruVidi.video.muted;
2071                player.play();
2072                player.addEventListener('play', function() {
2073                    // Do something
2074                    if ( HaruVidi.video.ads_params.ads_on ) {
2075                        if ( typeof(HaruVidi.video.ad_displayed[HaruVidi.video.ads_params.ads_id]) === 'undefined' ) {
2076                            // Show ads only when video start
2077                            // Self Ads
2078                            if ( HaruVidi.video.ads_params.ads_type != 'google_ima' ) {
2079                                HaruVidi.video.adsLocal({
2080                                    'player':               player,
2081                                    'playerWrap':           playerWrap,
2082                                    'ads_params':           HaruVidi.video.ads_params,
2083                                });
2084                            } else if ( HaruVidi.video.ads_params.ads_type == 'google_ima' ) {
2085                                HaruVidi.video.adsGoogle({
2086                                    'player':               player,
2087                                    'playerWrap':           playerWrap,
2088                                    'ads_params':           HaruVidi.video.ads_params,
2089                                });
2090                            }
2091                        }
2092                    }
2093                });
2094
2095                player.addEventListener('loadedmetadata', function() {
2096                    player.play().catch(() => {
2097                        console.log('playback failed, muting to try again');
2098                        player.muted = HaruVidi.video.muted;
2099                        player.muted = true;
2100                        player.play();
2101                    });
2102                });
2103
2104                player.addEventListener('ended', function() {
2105                    console.log('Video Ended');
2106
2107                    if ( HaruVidi.video.autoNextStatus() == true ) {
2108                        HaruVidi.video.autoNextProcess({
2109                            'playerWrap': playerWrap
2110                        });
2111                    }
2112                });
2113            });
2114
2115            HaruVidi.video.playerAPIReady('cloudflare');
2116        },
2117        facebookAPIReady: function() {
2118            // https://developers.facebook.com/docs/plugins/embedded-video-player/api/
2119            $(document).on('facebookAPIReady', function(){
2120                var player;
2121                var playerWrap = $('#facebook-video').parents('.haru-video-player');
2122
2123                // Remove loading and add played video
2124                playerWrap.find('.video-image').removeClass('loading').addClass('played');
2125
2126                // Set attribute for Facebook video player
2127                var options = {
2128                        'data-autoplay': 'true',
2129                    }
2130                $('#facebook-video').attr( options );
2131
2132                // Get Embedded Video Player API Instance
2133                FB.Event.subscribe('xfbml.ready', function(msg) {
2134                    if ( msg.type === 'video' && msg.id === 'facebook-video' ) {
2135                        player = msg.instance;
2136
2137                        if ( HaruVidi.video.muted ) {
2138                            player.mute();
2139                        }
2140                        player.play();
2141                    }
2142
2143                    var videoEventStarted = player.subscribe('startedPlaying', function(e) {
2144                        // Do something
2145                        if ( HaruVidi.video.ads_params.ads_on ) {
2146                            if ( typeof(HaruVidi.video.ad_displayed[HaruVidi.video.ads_params.ads_id]) === 'undefined' ) {
2147                                // Self Ads
2148                                if ( HaruVidi.video.ads_params.ads_type != 'google_ima' ) {
2149                                    HaruVidi.video.adsLocal({
2150                                        'player':               player,
2151                                        'playerWrap':           playerWrap,
2152                                        'ads_params':           HaruVidi.video.ads_params,
2153                                    });
2154                                } else if ( HaruVidi.video.ads_params.ads_type == 'google_ima' ) {
2155                                    HaruVidi.video.adsGoogle({
2156                                        'player':               player,
2157                                        'playerWrap':           playerWrap,
2158                                        'ads_params':           HaruVidi.video.ads_params,
2159                                    });
2160                                }
2161                            }
2162                        }
2163                    });
2164
2165                    var videoEventPause = player.subscribe('paused', function(e) {
2166                        // Do something
2167                    });
2168
2169                    var videoEventFinished = player.subscribe('finishedPlaying', function(e) {
2170                        console.log('Video Ended');
2171
2172                        if ( HaruVidi.video.autoNextStatus() == true ) {
2173                            HaruVidi.video.autoNextProcess({
2174                                'playerWrap': playerWrap
2175                            });
2176                        }
2177                    });
2178                });
2179            });
2180
2181            HaruVidi.video.playerAPIReady('facebook');
2182        },
2183        selfHostAPIReady: function() {
2184            $(document).on('selfHostAPIReady', function(){
2185                var player;
2186                var playerWrap = $('#video-player').parents('.haru-video-player');
2187
2188                // Remove loading and add played video
2189                playerWrap.find('.video-image').removeClass('loading').addClass('played');
2190
2191                if ( !player ) {
2192                    $('#video-player').mediaelementplayer({
2193                        stretching: 'fill', // responsive, none, fill
2194                        videoWidth: '100%',
2195                        videoHeight: '100%',
2196                        loop: false,
2197                        features: ['playpause', 'progress', 'current', 'duration', 'volume', 'fullscreen'],
2198                        // setDimensions: false,
2199                        // pluginPath: "/path/to/shims/",
2200                        // When using jQuery's `mediaelementplayer`, an `instance` argument
2201                        // is available in the `success` callback
2202                        success: function(mediaElement, originalNode, instance) {
2203                            player = mediaElement;
2204
2205                            // Do something
2206                            player.load();
2207
2208                            if ( HaruVidi.video.muted == true ) {
2209                                player.setMuted(true);
2210                            }
2211                            
2212                            player.play();
2213                            
2214                            player.addEventListener('loadedmetadata', function(){
2215                                if ( HaruVidi.video.ads_params.ads_on ) {
2216                                    player.addEventListener('timeupdate', function(){
2217                                        if ( typeof(HaruVidi.video.ad_displayed[HaruVidi.video.ads_params.ads_id]) === 'undefined' ) {
2218                                            // Show ads only when video start
2219                                            if ( player.currentTime >= 0 && player.duration > 0 ) {
2220                                                // Self Ads
2221                                                if ( HaruVidi.video.ads_params.ads_type != 'google_ima' ) {
2222                                                    HaruVidi.video.adsLocal({
2223                                                        'player':               player,
2224                                                        'playerWrap':           playerWrap,
2225                                                        'ads_params':           HaruVidi.video.ads_params,
2226                                                    });
2227                                                } else if ( HaruVidi.video.ads_params.ads_type == 'google_ima' ) {
2228                                                     HaruVidi.video.adsGoogle({
2229                                                        'player':               player,
2230                                                        'playerWrap':           playerWrap,
2231                                                        'ads_params':           HaruVidi.video.ads_params,
2232                                                    });
2233                                                }
2234                                            }
2235                                        }
2236                                    });
2237                                }
2238                                
2239                                player.addEventListener('playing', function(){
2240                                    // Do something
2241                                    $('.video-mediaelement-unmute').on('click', function() {
2242                                        player.setMuted(false);
2243
2244                                        $(this).hide(300);
2245                                    });
2246                                });
2247
2248                                player.addEventListener('ended', function(){
2249                                    console.log('Player Ended');
2250
2251                                    if ( HaruVidi.video.autoNextStatus() == true ) {
2252                                        HaruVidi.video.autoNextProcess({
2253                                            'playerWrap': playerWrap
2254                                        });
2255                                    }
2256                                });
2257                            });
2258                        }
2259                    });
2260                }
2261            });
2262
2263            HaruVidi.video.playerAPIReady('selfhost');
2264        },
2265        playerAPIReady: function(serverAPI) {
2266            // Load serverAPI Script if need
2267            switch ( serverAPI ) {
2268                case 'vimeo':
2269                    if ( typeof(Vimeo) !== 'undefined' && typeof(Vimeo.Player) !== 'undefined' ) {
2270                        $(document).trigger('vimeoAPIReady');
2271                    } else {
2272                        var script_url = 'https://player.vimeo.com/api/player.js';
2273
2274                        $.getScript( script_url, function() {
2275                            $(document).trigger('vimeoAPIReady');
2276                        });
2277                    }
2278                    
2279                    break;
2280
2281                case 'twitch':
2282                    if ( typeof(Twitch) !== 'undefined' && typeof(Twitch.Player) !== 'undefined' ) {
2283                        $(document).trigger('twitchAPIReady');
2284                    } else {
2285                        var script_url = 'https://embed.twitch.tv/embed/v1.js';
2286
2287                        $.getScript( script_url, function() {
2288                            $(document).trigger('twitchAPIReady');
2289                        });
2290                    }
2291                    
2292                    break;
2293
2294                case 'dailymotion':
2295                    if ( typeof(DM) !== 'undefined' && typeof(DM.player) !== 'undefined' ) {
2296                        $(document).trigger('dailymotionAPIReady');
2297                    } else {
2298                        var script_url = 'https://api.dmcdn.net/all.js';
2299
2300                        $.getScript( script_url, function() {
2301                            $(document).trigger('dailymotionAPIReady');
2302                        });
2303                    }
2304                    
2305                    break;
2306
2307                case 'cloudflare':
2308                    if ( typeof(Stream) !== 'undefined' ) {
2309                        $(document).trigger('cloudflareAPIReady');
2310                    } else {
2311                        var script_url = 'https://embed.videodelivery.net/embed/sdk.latest.js';
2312
2313                        $.getScript( script_url, function() {
2314                            $(document).trigger('cloudflareAPIReady');
2315                        });
2316                    }
2317                    
2318                    break;
2319
2320                case 'facebook':
2321                    if ( typeof(FB) !== 'undefined' && typeof(FB.Event) !== 'undefined' ) {
2322                        $(document).trigger('facebookAPIReady');
2323                    } else {
2324                        var script_url = 'https://connect.facebook.net/en_US/sdk.js?xfbml=1&version=v3.2';
2325
2326                        $.getScript( script_url, function() {
2327                            $(document).trigger('facebookAPIReady');
2328                        });
2329                    }
2330                    
2331                    break;
2332                    
2333                case 'selfhost':
2334                    $(document).trigger('selfHostAPIReady');
2335
2336                    break;
2337
2338                case 'google':
2339                    $(document).trigger('selfHostAPIReady');
2340
2341                    break;                              
2342            }
2343        },
2344        autoNextStatus: function() {
2345            HaruVidi.video.videoAutoNext();
2346
2347            if ( typeof(Cookies.get('videoautonext')) !== 'undefined' ) {
2348                HaruVidi.video.auto_next = Cookies.get('videoautonext') == 'true' ? true : false;
2349            }  
2350
2351            if ( HaruVidi.video.auto_next == true ) {           
2352                $('.video-auto-next').addClass('active');
2353
2354                return true;      
2355            } else {
2356                return false;
2357            }
2358        },
2359        videoAutoNext: function() {
2360            $('.video-auto-next').off().on('click', function(e) {
2361                $(this).toggleClass('active');
2362
2363                if ( $(this).hasClass('active') ) {
2364                    Cookies.set('videoautonext', 'true', { 
2365                        expires: 1
2366                    });
2367                } else {
2368                    Cookies.set('videoautonext', 'false', { 
2369                        expires: 1
2370                    });
2371                }
2372            });
2373
2374            // Process Hide AutoNext
2375            var hideautonext = null;
2376
2377            if ( $('.video-player-data').length > 0 ) {
2378                hideautonext = ($('.video-player-data').attr('data-hide-autonext')) == 'true' ? true : false;
2379
2380                if ( hideautonext == true ) {
2381                    Cookies.set('videoautonext', 'false', { 
2382                        expires: 1
2383                    });
2384                }
2385            }
2386        },
2387        autoNextProcess: function( params ) {
2388            var playerWrap = params.playerWrap;
2389            var autoNextUrl = playerWrap.find('.video-auto-next-wrap').attr('data-next-url');
2390            var autoNextTimer = playerWrap.find('.progess-time-remain');
2391            var autoNextSeconds = 10;
2392
2393            // @TODO: check last video auto next
2394            if ( typeof( autoNextUrl ) === 'undefined' ) {
2395                return;
2396            }
2397
2398            playerWrap.find('.video-auto-next-wrap').addClass('active');
2399            autoNextTimer.text(autoNextSeconds);
2400
2401            playerWrap.find('.progess-time').addClass('active');
2402            var autoNextInterval = setInterval(function() {
2403                autoNextSeconds--;
2404                if ( autoNextSeconds === 0 ) {
2405                    playerWrap.find('.progess-time').removeClass('active');
2406                    clearInterval(autoNextInterval);
2407                }
2408                
2409                autoNextTimer.text(autoNextSeconds);
2410            }, 1000);
2411
2412            var autoNextCancel = null;
2413                
2414                autoNextCancel = setTimeout(function() {
2415                    if ( autoNextCancel !== null ) {
2416                        clearTimeout(autoNextCancel);
2417                    }
2418
2419                    window.location.href = autoNextUrl;
2420
2421                }, autoNextSeconds * 1000);
2422
2423            playerWrap.find('.video-next-cancel').off().on('click', function() {
2424                if ( autoNextCancel !== null ) {
2425                    clearTimeout(autoNextCancel);
2426                }
2427
2428                playerWrap.find('.progess-time').removeClass('active'); // @TODO: autoNextTimer.text(0); 
2429
2430                return false;
2431            });
2432        },
2433        isMobileOS: function() {
2434            var check = false;
2435
2436            (function(a){if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4))) check = true;})(navigator.userAgent||navigator.vendor||window.opera);
2437
2438            return check;
2439        },
2440        isiOS: function() {
2441            var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
2442
2443            return iOS;
2444        },
2445        isSafari: function() {
2446            // https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser/9851769
2447            // Safari 3.0+ "[object HTMLElementConstructor]" 
2448            var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || (typeof safari !== 'undefined' && safari.pushNotification));
2449
2450            return isSafari;
2451        },
2452        playlistScrollBar: function() {
2453            if ( $('.single-video-playlist .playlist-videos').length > 0 ) {
2454                // More options: http://manos.malihu.gr/jquery-custom-content-scroller/
2455                var active_index = $('.single-video-playlist .playlist-videos').find('li.video-item.active').index();
2456
2457                $('.single-video-playlist .playlist-videos').mCustomScrollbar({
2458                    axis: 'y', // vertical scrollbar
2459                    autoHideScrollbar: false,                
2460                    autoExpandScrollbar: false,
2461                    scrollButtons: {
2462                        enable: true,
2463                        scrollType: 'stepped'
2464                    },
2465                    keyboard: {
2466                        scrollType: 'stepped'
2467                    },
2468                    mouseWheel: {
2469                        scrollAmount: 188,
2470                        normalizeDelta: true
2471                    },
2472                    theme: 'minimal-dark',
2473                    callbacks: {
2474                        onInit:function(){
2475                            if ( active_index != -1 ) {
2476                                $('.single-video-playlist .playlist-videos').mCustomScrollbar('scrollTo',$('.single-video-playlist .playlist-videos').find('.mCSB_container').find('li.video-item:eq('+ (active_index) + ')'));
2477                            }
2478                        }
2479                    }
2480                });
2481            }
2482        },
2483        turnOffLight: function() {
2484            $('.video-turn-off-light').on('click', function() {
2485                $('body').toggleClass('video-light-off');
2486                $(this).toggleClass('active');
2487            });
2488        },
2489        floatingPlayer: function() {
2490            if ( $('.video-player-wrap').length ) {
2491                var playerWrap = $('.video-player-wrap');
2492                var playerOffset = playerWrap.offset().top + playerWrap.outerHeight(true);
2493
2494                $(window).on('scroll', function() {
2495                    if ( !$('body').hasClass('floating-video-disabled') ) {
2496                        if ( $(this).scrollTop() > playerOffset ) {
2497                            $('body').addClass('floating-video');
2498                            $('.video-player-container').addClass('floating-video');
2499                            $(document).trigger('floatingVideoEnable'); // Use this for responsive Google Ads
2500                        } else {
2501                            $('body').removeClass('floating-video');
2502                            $('.video-player-container').removeClass('floating-video');
2503                            $(document).trigger('floatingVideoDisable'); // Use this for responsive Google Ads
2504                        }
2505                    }
2506                });
2507
2508                $('.video-float__close').on('click', function() {
2509                    $('body').addClass('floating-video-disabled');
2510                    $('body').removeClass('floating-video');
2511                    $('.video-player-container').removeClass('floating-video');
2512                });
2513            }
2514        },
2515        videoRating: function() {
2516            $('.post-rating').each(function(){
2517                var $rating_element  = $(this);
2518                var is_clicked = false;
2519
2520                $rating_element.find('.action-rating').off().on('click', function(e) {
2521                    // Process click multitimes before ajax process
2522                    if ( is_clicked === true ) {
2523                        return;
2524                    }
2525
2526                    is_clicked = true;
2527
2528                    var post_id = $(this).data('id');
2529                    var post_action = $(this).attr('data-action');
2530                    var vote_status = $(this).attr('data-vote-status');
2531                    var login = $(this).attr('data-login');
2532                    var login_required = $(this).attr('data-login-required');
2533
2534                    if ( login_required == 'yes' && login == 'false' ) {
2535                        return;
2536                    }
2537
2538                    $(this).toggleClass('active').siblings('.action-rating').removeClass('active');
2539
2540                    if ( $(this).hasClass('active') ) {
2541                        vote_status = post_action;
2542                    } else {
2543                        vote_status = '';
2544                    }
2545
2546                    $.ajax({
2547                        url: haru_vidi_ajax_url,
2548                        type: "POST",
2549                        data: {
2550                            action: 'haru_ajax_voting',
2551                            post_id: post_id,
2552                            post_action: post_action,
2553                            vote_status: vote_status
2554                        },
2555                        dataType: "html",
2556                        beforeSend: function() {
2557                            // Do Somethings
2558                        }
2559                    }).success(function(result) {
2560                        // Do Somethings
2561                        result = JSON.parse(result);
2562
2563                        $rating_element.attr('data-vote-status', result.vote_status);
2564                        $rating_element.find('.post-like-count').text(result.like_count);
2565                        $rating_element.find('.post-dislike-count').text(result.dislike_count);
2566                        $rating_element.find('.like-tooltip').text(result.message_like);
2567                        $rating_element.find('.dislike-tooltip').text(result.message_dislike);
2568                        $rating_element.find('.post-rating-percentage').css('width', result.like_percentage);
2569                        // For single rating count display
2570                        $('.post-rating-bar').find('.post-rating-percentage').css('width', result.like_percentage);
2571                        $('.post-rating-count').find('.post-like-count').text(result.like_count);
2572                        $('.post-rating-count').find('.post-dislike-count').text(result.dislike_count);
2573
2574                        is_clicked = false;
2575                    });
2576                });
2577            });
2578        },
2579        videoScreenShots: function() {
2580            // https://codepen.io/ryanjbonnell/pen/dqsxI
2581            // https://codepen.io/dariodev/pen/PbwVxv
2582            $('.single-video-screenshot-btn').off().on('click', function(){
2583                $(this).next().magnificPopup('open');
2584            });
2585
2586            $('.single-video-screenshots .video-screenshots').each(function(){
2587                $(this).magnificPopup({
2588                    delegate: 'a.video-screenshot',
2589                    type: 'image',
2590                    mainClass: 'video-screenshots-popup',
2591                    gallery: {
2592                        enabled: true,
2593                        navigateByImgClick: true,
2594                        preload: [0,1] // Will preload 0 - before current, and 1 after the current image
2595                    },
2596                    image: {
2597                        titleSrc: function(item) {
2598                            var $gallery = $('.video-screenshots');
2599                            var $result = '';
2600                            if ( $gallery.find('.video-screenshot').length > 0 ) {
2601                                $result = '<div class="mfp-pager">' + 
2602                                    '<div class="arrow_prev">' +
2603                                        '<button type="button" class="prev arrow" onclick="javascript:jQuery(\'.video-screenshots\').magnificPopup(\'prev\');return false;">Previous</button>' +
2604                                    '</div>' + 
2605                                    '<div class="dots">' +
2606                                        '<ul class="dots" style="display: inline-block;">';
2607                                for ( var i = 0; i < $gallery.find('.video-screenshot').length; i++ ) {
2608                                    var $cl_active = '';
2609                                    if ( item.index == i ) $cl_active = ' class="active"'; else $cl_active = '';
2610                                    var $thumb = $gallery.find('.video-screenshot:eq('+ i +')').attr('href');
2611                                    $result += '<li' + $cl_active + '>' +
2612                                            '<button type="button" onclick="javascript:jQuery(\'.video-screenshots\').magnificPopup(\'goTo\', '+ i +');return false;"><img src="' + $thumb + '" width="100"></button>' +
2613                                        '</li>';
2614                                }
2615                                $result += '</ul>' +
2616                                    '</div>' +
2617                                    '<div class="arrow_next">' +
2618                                        '<button type="button" class="next arrow" onclick="javascript:jQuery(\'.video-screenshots\').magnificPopup(\'next\');return false;">Next</button>' +
2619                                    '</div>' +
2620                                '</div>';
2621                            }
2622
2623                            return $result;
2624                        }
2625                    }
2626                });
2627
2628            });
2629        },
2630        videoSocialShare: function() {
2631            $('.single-video-share').off().on('click', function() {
2632                $(this).toggleClass('active');
2633                $(this).find('.video-social-share').toggle();
2634            });
2635        },
2636        videoDownload: function() {
2637            $('.single-video-download').off().on('click', function() {
2638                $(this).toggleClass('active');
2639                $(this).find('.video-download').toggle();
2640            });
2641        },
2642        videoToggleContent: function() {
2643            $('.single-video-content-btn').off().on('click', function(){
2644                var show_more = $(this).attr('data-show-more');
2645                var show_less = $(this).attr('data-show-less');
2646
2647                $(this).toggleClass('active');
2648                $('.single-video-content').toggleClass('active');
2649
2650                if ( $(this).hasClass('active') ) {
2651                    $(this).text(show_less);
2652                } else {
2653                    $(this).text(show_more);
2654                }
2655            });
2656        },
2657        shortcodeVideoCategory: function() {
2658            var video_category_data = []; // Use this for video category shortcode
2659            var video_category_control = [];
2660
2661            $('.video-category-shortcode').each(function() {
2662                var element         = $(this);
2663                var element_id      = element.attr('id');
2664                var atts            = element.data('atts');
2665
2666
2667                if ( atts['filter'] != 'hide' ) {
2668                    var category        = element.find('.video-filter .filter-item:first').attr('data-category');
2669                    var max_pages       = parseInt(element.find('.video-control-item').attr('data-max_pages'));
2670                    var current_page    = parseInt(element.find('.video-control-item').attr('data-current_page'));
2671
2672                    // Save first load to cache
2673                    var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-1';
2674                    var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
2675
2676                    video_category_data[tab_data_index] = element.find('.video-category-content').clone(true);
2677                    video_category_control[tab_control_index] = [category, max_pages, current_page];
2678
2679                    element.find('.video-filter .filter-item').off().on('click', function() {
2680                        // Check if already click or loading
2681                        if ( $(this).hasClass('active') || element.find('.video-ajax-content').hasClass('loading') ) {
2682                            return;
2683                        }
2684
2685                        element.find('.video-filter .filter-item').removeClass('active');
2686                        $(this).addClass('active');
2687
2688                        // Check cache
2689                        var category            = $(this).data('category');
2690                        var tab_control_index   = element_id + '-' + category.toString().split(',').join('-');
2691
2692                        if ( video_category_control[tab_control_index] != undefined ) {
2693                            element.find('.video-category-content').remove();
2694
2695                            var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-' + video_category_control[tab_control_index][2].toString();
2696
2697                            element.find('.video-ajax-content').append( video_category_data[tab_data_index] );
2698                            element.find('.video-control .video-control-item').attr('data-category', video_category_control[tab_control_index][0]);
2699                            element.find('.video-control .video-control-item').attr('data-max_pages', video_category_control[tab_control_index][1]);
2700                            element.find('.video-control .video-control-item').attr('data-current_page', video_category_control[tab_control_index][2]);
2701                            // Check show/hide control
2702                            if ( video_category_control[tab_control_index][1] <= 1 ) {
2703                                element.find('.video-control').addClass('hide');
2704                            } else {
2705                                element.find('.video-control').removeClass('hide');
2706                            }
2707                            // Check last page, first page
2708                            if ( video_category_control[tab_control_index][2] == video_category_control[tab_control_index][1] ) {
2709                                element.find('.video-control .video-control-item[data-action="prev"]').removeClass('disable');
2710                                element.find('.video-control .video-control-item[data-action="next"]').addClass('disable');
2711                            } else if ( video_category_control[tab_control_index][2] == 1 ) {
2712                                element.find('.video-control .video-control-item[data-action="prev"]').addClass('disable');
2713                                element.find('.video-control .video-control-item[data-action="next"]').removeClass('disable');
2714                            } else {
2715                                element.find('.video-control .video-control-item[data-action="prev"]').removeClass('disable');
2716                                element.find('.video-control .video-control-item[data-action="next"]').removeClass('disable');
2717                            }
2718
2719                            return;
2720                        }
2721
2722                        // Load content via ajax
2723                        element.find('.video-ajax-content').addClass('loading');
2724
2725                        $.ajax({
2726                            type : "POST",
2727                            timeout : 30000,
2728                            url : haru_vidi_ajax_url,
2729                            data : {
2730                                action: 'haru_get_video_category', 
2731                                atts: atts, 
2732                                category: category
2733                            },
2734                            error: function(xhr,err) {
2735                                console.log('Have something wrong! Please try again!');
2736                            },
2737                            success: function(response) {
2738                                if ( response ) {
2739                                    element.find('.video-category-content').remove();
2740                                    element.find('.video-ajax-content').append( response );       
2741
2742                                    // Save cache
2743                                    var tab_data_index      = element_id + '-' + category.toString().split(',').join('-') + '-page-1';
2744                                    video_category_data[tab_data_index] = response;
2745
2746                                    // Do something
2747                                    var max_pages = parseInt(element.find('.video-category-content').attr('data-max_pages'));
2748                                    video_category_control[tab_control_index] = [category, max_pages, 1];
2749                                    // Check show/hide control
2750                                    if ( max_pages <= 1 ) {
2751                                        // Do something
2752                                        element.find('.video-control').addClass('hide');
2753                                    } else {
2754                                        element.find('.video-control').removeClass('hide');
2755                                    }
2756                                    // Set new value for control
2757                                    element.find('.video-control .video-control-item').attr('data-category', category);
2758                                    element.find('.video-control .video-control-item').attr('data-max_pages', max_pages);
2759                                    element.find('.video-control .video-control-item').attr('data-current_page', 1);
2760                                    // Check last page, first page
2761                                    element.find('.video-control .video-control-item[data-action="prev"]').addClass('disable');
2762                                    element.find('.video-control .video-control-item[data-action="next"]').removeClass('disable');
2763                                }
2764                                element.find('.video-ajax-content').removeClass('loading');
2765                            }
2766                        });
2767                    });
2768                } else {
2769                    var category        = element.find('.video-control-item').attr('data-category');
2770                    var max_pages       = parseInt(element.find('.video-control-item').attr('data-max_pages'));
2771                    var current_page    = parseInt(element.find('.video-control-item').attr('data-current_page'));
2772
2773                    // Save first load to cache
2774                    var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-1';
2775                    var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
2776
2777                    video_category_data[tab_data_index] = element.find('.video-category-content').clone(true);
2778                    video_category_control[tab_control_index] = [category, max_pages, current_page];
2779                }
2780
2781                // Next/Prev control
2782                element.find('.video-control .video-control-item').off().on('click', function() {
2783                    // Check if already click or loading
2784                    if ( $(this).hasClass('disable') || element.find('.video-ajax-content').hasClass('loading') ) {
2785                        return;
2786                    }
2787
2788                    var category        = $(this).attr('data-category');
2789                    var max_pages       = parseInt($(this).attr('data-max_pages'));
2790                    var action          = $(this).attr('data-action');
2791
2792                    if ( action == 'next' ) {
2793                        var current_page    = parseInt($(this).attr('data-current_page'));
2794
2795                        if ( (current_page + 1) <= max_pages ) {
2796                            // Check cache
2797                            var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-' + ( current_page + 1 );
2798                            var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
2799
2800                            if ( video_category_data[tab_data_index] != undefined ) {
2801                                element.find('.video-category-content').remove();
2802                                element.find('.video-ajax-content').append( video_category_data[tab_data_index] );
2803                                
2804                                // Do something
2805                                element.find('.video-control .video-control-item').attr('data-current_page', current_page + 1);
2806                                video_category_control[tab_control_index] = [category, max_pages, current_page + 1];
2807                                // Check last page
2808                                element.find('.video-control .video-control-item[data-action="prev"]').removeClass('disable');
2809                                if ( current_page + 1 >= max_pages ) {
2810                                    element.find('.video-control .video-control-item[data-action="next"]').addClass('disable');
2811                                }
2812
2813                                return;
2814                            }
2815                        
2816                            // Load content via ajax
2817                            element.find('.video-ajax-content').addClass('loading');
2818
2819                            $.ajax({
2820                                type : "POST",
2821                                timeout : 30000,
2822                                url : haru_vidi_ajax_url,
2823                                data : {
2824                                    action: 'haru_get_video_category_next', 
2825                                    atts: atts, 
2826                                    category: category,
2827                                    current_page: current_page
2828                                },
2829                                error: function(xhr,err) {
2830                                    console.log('Have something wrong! Please try again!');
2831                                },
2832                                success: function(response) {
2833                                    if ( response ) {
2834                                        element.find('.video-category-content').remove();
2835                                        element.find('.video-ajax-content').append( response );       
2836
2837                                        // Save cache
2838                                        video_category_data[tab_data_index] = response;
2839                                        video_category_control[tab_control_index] = [category, max_pages, current_page + 1];
2840                                    }
2841                                    // Do something
2842                                    element.find('.video-control .video-control-item').attr('data-current_page', current_page + 1);
2843                                    element.find('.video-ajax-content').removeClass('loading');
2844                                    // Check last page
2845                                    element.find('.video-control .video-control-item[data-action="prev"]').removeClass('disable');
2846                                    if ( current_page + 1 >= max_pages ) {
2847                                        element.find('.video-control .video-control-item[data-action="next"]').addClass('disable');
2848                                    }
2849                                }
2850                            });
2851                        } else {
2852                            return;
2853                        }
2854                    } else if ( action == 'prev' ) {
2855                        var current_page    = parseInt($(this).attr('data-current_page'));
2856
2857                        if ( current_page == 1 ) {
2858                            return;
2859                        }
2860
2861                        // Check cache
2862                        var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-' + ( current_page - 1 );
2863                        var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
2864
2865                        if ( video_category_data[tab_data_index] != undefined ) {
2866                            element.find('.video-category-content').remove();
2867                            element.find('.video-ajax-content').append( video_category_data[tab_data_index] );
2868                            
2869                            // Do something
2870                            element.find('.video-control .video-control-item').attr('data-current_page', current_page - 1);
2871                            video_category_control[tab_control_index] = [category, max_pages, current_page - 1];
2872                        }
2873
2874                        // Check first page
2875                        element.find('.video-control .video-control-item[data-action="next"]').removeClass('disable');
2876                        if ( current_page - 1 <= 1 ) {
2877                            element.find('.video-control .video-control-item[data-action="prev"]').addClass('disable');
2878                        }
2879                    }
2880                });
2881            });
2882        },
2883        shortcodeVideoCategorySingle: function() {
2884            var video_category_single_data = []; // Use this for video category shortcode
2885            var video_category_single_control = [];
2886
2887            $('.video-category-single-shortcode').each(function() {
2888                var element         = $(this);
2889                var element_id      = element.attr('id');
2890                var atts            = element.data('atts');
2891
2892                var category        = element.find('.video-filter .filter-item:first').attr('data-category');
2893                var max_pages       = parseInt(element.find('.video-control-item').attr('data-max_pages'));
2894                var current_page    = parseInt(element.find('.video-control-item').attr('data-current_page'));
2895
2896                // Save first load to cache
2897                var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-1';
2898                var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
2899
2900                video_category_single_data[tab_data_index] = element.find('.video-category-single-content').clone(true);
2901                video_category_single_control[tab_control_index] = [category, max_pages, current_page];
2902
2903                // Next/Prev control
2904                element.find('.video-control .video-control-item').off().on('click', function() {
2905                    // Check if already click or loading
2906                    if ( $(this).hasClass('disable') || element.find('.video-ajax-content').hasClass('loading') ) {
2907                        return;
2908                    }
2909
2910                    var category        = $(this).attr('data-category');
2911                    var max_pages       = parseInt($(this).attr('data-max_pages'));
2912                    var action          = $(this).attr('data-action');
2913
2914                    if ( action == 'next' ) {
2915                        var current_page    = parseInt($(this).attr('data-current_page'));
2916
2917                        if ( (current_page + 1) <= max_pages ) {
2918                            // Check cache
2919                            var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-' + ( current_page + 1 );
2920                            var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
2921
2922                            if ( video_category_single_data[tab_data_index] != undefined ) {
2923                                element.find('.video-category-single-content').remove();
2924                                element.find('.video-ajax-content').append( video_category_single_data[tab_data_index] );
2925                                
2926                                // Do something
2927                                element.find('.video-control .video-control-item').attr('data-current_page', current_page + 1);
2928                                video_category_single_control[tab_control_index] = [category, max_pages, current_page + 1];
2929                                // Check last page
2930                                element.find('.video-control .video-control-item[data-action="prev"]').removeClass('disable');
2931                                if ( current_page + 1 >= max_pages ) {
2932                                    element.find('.video-control .video-control-item[data-action="next"]').addClass('disable');
2933                                }
2934
2935                                return;
2936                            }
2937                        
2938                            // Load content via ajax
2939                            element.find('.video-ajax-content').addClass('loading');
2940
2941                            $.ajax({
2942                                type : "POST",
2943                                timeout : 30000,
2944                                url : haru_vidi_ajax_url,
2945                                data : {
2946                                    action: 'haru_get_video_category_single_next', 
2947                                    atts: atts, 
2948                                    category: category,
2949                                    current_page: current_page
2950                                },
2951                                error: function(xhr,err) {
2952                                    console.log('Have something wrong! Please try again!');
2953                                },
2954                                success: function(response) {
2955                                    if ( response ) {
2956                                        element.find('.video-category-single-content').remove();
2957                                        element.find('.video-ajax-content').append( response );       
2958
2959                                        // Save cache
2960                                        video_category_single_data[tab_data_index] = response;
2961                                        video_category_single_control[tab_control_index] = [category, max_pages
2961, current_page + 1];
2962                                    }
2963                                    // Do something
2964                                    element.find('.video-control .video-control-item').attr('data-current_page', current_page + 1);
2965                                    element.find('.video-ajax-content').removeClass('loading');
2966                                    // Check last page
2967                                    element.find('.video-control .video-control-item[data-action="prev"]').removeClass('disable');
2968                                    if ( current_page + 1 >= max_pages ) {
2969                                        element.find('.video-control .video-control-item[data-action="next"]').addClass('disable');
2970                                    }
2971                                }
2972                            });
2973                        } else {
2974                            return;
2975                        }
2976                    } else if ( action == 'prev' ) {
2977                        var current_page    = parseInt($(this).attr('data-current_page'));
2978
2979                        if ( current_page == 1 ) {
2980                            return;
2981                        }
2982
2983                        // Check cache
2984                        var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-' + ( current_page - 1 );
2985                        var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
2986
2987                        if ( video_category_single_data[tab_data_index] != undefined ) {
2988                            element.find('.video-category-single-content').remove();
2989                            element.find('.video-ajax-content').append( video_category_single_data[tab_data_index] );
2990                            
2991                            // Do something
2992                            element.find('.video-control .video-control-item').attr('data-current_page', current_page - 1);
2993                            video_category_single_control[tab_control_index] = [category, max_pages, current_page - 1];
2994                        }
2995
2996                        // Check first page
2997                        element.find('.video-control .video-control-item[data-action="next"]').removeClass('disable');
2998                        if ( current_page - 1 <= 1 ) {
2999                            element.find('.video-control .video-control-item[data-action="prev"]').addClass('disable');
3000                        }
3001                    }
3002                });
3003            });
3004        },
3005        shortcodeVideoFeatured: function() {
3006            var video_featured_data = []; // Use this for video featured shortcode
3007            var video_featured_control = [];
3008
3009            $('.video-featured-shortcode').each(function() {
3010                var element         = $(this);
3011                var element_id      = element.attr('id');
3012                var atts            = element.data('atts');
3013
3014
3015                if ( atts['filter'] != 'hide' ) {
3016                    var category        = element.find('.video-filter .filter-item:first').attr('data-category');
3017                    var max_pages       = parseInt(element.find('.video-control-item').attr('data-max_pages'));
3018                    var current_page    = parseInt(element.find('.video-control-item').attr('data-current_page'));
3019
3020                    // Save first load to cache
3021                    var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-1';
3022                    var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
3023
3024                    video_featured_data[tab_data_index] = element.find('.video-featured-content').clone(true);
3025                    video_featured_control[tab_control_index] = [category, max_pages, current_page];
3026
3027                    element.find('.video-filter .filter-item').off().on('click', function() {
3028                        // Check if already click or loading
3029                        if ( $(this).hasClass('active') || element.find('.video-ajax-content').hasClass('loading') ) {
3030                            return;
3031                        }
3032
3033                        element.find('.video-filter .filter-item').removeClass('active');
3034                        $(this).addClass('active');
3035
3036                        // Check cache
3037                        var category            = $(this).data('category');
3038                        var tab_control_index   = element_id + '-' + category.toString().split(',').join('-');
3039
3040                        if ( video_featured_control[tab_control_index] != undefined ) {
3041                            element.find('.video-featured-content').remove();
3042
3043                            var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-' + video_featured_control[tab_control_index][2].toString();
3044
3045                            element.find('.video-ajax-content').append( video_featured_data[tab_data_index] );
3046                            element.find('.video-control .video-control-item').attr('data-category', video_featured_control[tab_control_index][0]);
3047                            element.find('.video-control .video-control-item').attr('data-max_pages', video_featured_control[tab_control_index][1]);
3048                            element.find('.video-control .video-control-item').attr('data-current_page', video_featured_control[tab_control_index][2]);
3049                            // Check show/hide control
3050                            if ( video_featured_control[tab_control_index][1] <= 1 ) {
3051                                element.find('.video-control').addClass('hide');
3052                            } else {
3053                                element.find('.video-control').removeClass('hide');
3054                            }
3055                            // Check last page, first page
3056                            if ( video_featured_control[tab_control_index][2] == video_featured_control[tab_control_index][1] ) {
3057                                element.find('.video-control .video-control-item[data-action="prev"]').removeClass('disable');
3058                                element.find('.video-control .video-control-item[data-action="next"]').addClass('disable');
3059                            } else if ( video_featured_control[tab_control_index][2] == 1 ) {
3060                                element.find('.video-control .video-control-item[data-action="prev"]').addClass('disable');
3061                                element.find('.video-control .video-control-item[data-action="next"]').removeClass('disable');
3062                            } else {
3063                                element.find('.video-control .video-control-item[data-action="prev"]').removeClass('disable');
3064                                element.find('.video-control .video-control-item[data-action="next"]').removeClass('disable');
3065                            }
3066
3067                            return;
3068                        }
3069
3070                        // Load content via ajax
3071                        element.find('.video-ajax-content').addClass('loading');
3072
3073                        $.ajax({
3074                            type : "POST",
3075                            timeout : 30000,
3076                            url : haru_vidi_ajax_url,
3077                            data : {
3078                                action: 'haru_get_video_featured', 
3079                                atts: atts, 
3080                                category: category
3081                            },
3082                            error: function(xhr,err) {
3083                                console.log('Have something wrong! Please try again!');
3084                            },
3085                            success: function(response) {
3086                                if ( response ) {
3087                                    element.find('.video-featured-content').remove();
3088                                    element.find('.video-ajax-content').append( response );       
3089
3090                                    // Save cache
3091                                    var tab_data_index      = element_id + '-' + category.toString().split(',').join('-') + '-page-1';
3092                                    video_featured_data[tab_data_index] = response;
3093
3094                                    // Do something
3095                                    var max_pages = parseInt(element.find('.video-featured-content').attr('data-max_pages'));
3096                                    video_featured_control[tab_control_index] = [category, max_pages, 1];
3097                                    // Check show/hide control
3098                                    if ( max_pages <= 1 ) {
3099                                        // Do something
3100                                        element.find('.video-control').addClass('hide');
3101                                    } else {
3102                                        element.find('.video-control').removeClass('hide');
3103                                    }
3104                                    // Set new value for control
3105                                    element.find('.video-control .video-control-item').attr('data-category', category);
3106                                    element.find('.video-control .video-control-item').attr('data-max_pages', max_pages);
3107                                    element.find('.video-control .video-control-item').attr('data-current_page', 1);
3108                                    // Check last page, first page
3109                                    element.find('.video-control .video-control-item[data-action="prev"]').addClass('disable');
3110                                    element.find('.video-control .video-control-item[data-action="next"]').removeClass('disable');
3111                                }
3112                                element.find('.video-ajax-content').removeClass('loading');
3113                            }
3114                        });
3115                    });
3116                } else {
3117                    var category        = element.find('.video-control-item').attr('data-category');
3118                    var max_pages       = parseInt(element.find('.video-control-item').attr('data-max_pages'));
3119                    var current_page    = parseInt(element.find('.video-control-item').attr('data-current_page'));
3120
3121                    // Save first load to cache
3122                    var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-1';
3123                    var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
3124
3125                    video_featured_data[tab_data_index] = element.find('.video-featured-content').clone(true);
3126                    video_featured_control[tab_control_index] = [category, max_pages, current_page];
3127                }
3128
3129                // Next/Prev control
3130                element.find('.video-control .video-control-item').off().on('click', function() {
3131                    // Check if already click or loading
3132                    if ( $(this).hasClass('disable') || element.find('.video-ajax-content').hasClass('loading') ) {
3133                        return;
3134                    }
3135
3136                    var category        = $(this).attr('data-category');
3137                    var max_pages       = parseInt($(this).attr('data-max_pages'));
3138                    var action          = $(this).attr('data-action');
3139
3140                    if ( action == 'next' ) {
3141                        var current_page    = parseInt($(this).attr('data-current_page'));
3142
3143                        if ( (current_page + 1) <= max_pages ) {
3144                            // Check cache
3145                            var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-' + ( current_page + 1 );
3146                            var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
3147
3148                            if ( video_featured_data[tab_data_index] != undefined ) {
3149                                element.find('.video-featured-content').remove();
3150                                element.find('.video-ajax-content').append( video_featured_data[tab_data_index] );
3151                                
3152                                // Do something
3153                                element.find('.video-control .video-control-item').attr('data-current_page', current_page + 1);
3154                                video_featured_control[tab_control_index] = [category, max_pages, current_page + 1];
3155                                // Check last page
3156                                element.find('.video-control .video-control-item[data-action="prev"]').removeClass('disable');
3157                                if ( current_page + 1 >= max_pages ) {
3158                                    element.find('.video-control .video-control-item[data-action="next"]').addClass('disable');
3159                                }
3160
3161                                return;
3162                            }
3163                        
3164                            // Load content via ajax
3165                            element.find('.video-ajax-content').addClass('loading');
3166
3167                            $.ajax({
3168                                type : "POST",
3169                                timeout : 30000,
3170                                url : haru_vidi_ajax_url,
3171                                data : {
3172                                    action: 'haru_get_video_featured_next', 
3173                                    atts: atts, 
3174                                    category: category,
3175                                    current_page: current_page
3176                                },
3177                                error: function(xhr,err) {
3178                                    console.log('Have something wrong! Please try again!');
3179                                },
3180                                success: function(response) {
3181                                    if ( response ) {
3182                                        element.find('.video-featured-content').remove();
3183                                        element.find('.video-ajax-content').append( response );       
3184
3185                                        // Save cache
3186                                        video_featured_data[tab_data_index] = response;
3187                                        video_featured_control[tab_control_index] = [category, max_pages, current_page + 1];
3188                                    }
3189                                    // Do something
3190                                    element.find('.video-control .video-control-item').attr('data-current_page', current_page + 1);
3191                                    element.find('.video-ajax-content').removeClass('loading');
3192                                    // Check last page
3193                                    element.find('.video-control .video-control-item[data-action="prev"]').removeClass('disable');
3194                                    if ( current_page + 1 >= max_pages ) {
3195                                        element.find('.video-control .video-control-item[data-action="next"]').addClass('disable');
3196                                    }
3197                                }
3198                            });
3199                        } else {
3200                            return;
3201                        }
3202                    } else if ( action == 'prev' ) {
3203                        var current_page    = parseInt($(this).attr('data-current_page'));
3204
3205                        if ( current_page == 1 ) {
3206                            return;
3207                        }
3208
3209                        // Check cache
3210                        var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-' + ( current_page - 1 );
3211                        var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
3212
3213                        if ( video_featured_data[tab_data_index] != undefined ) {
3214                            element.find('.video-featured-content').remove();
3215                            element.find('.video-ajax-content').append( video_featured_data[tab_data_index] );
3216                            
3217                            // Do something
3218                            element.find('.video-control .video-control-item').attr('data-current_page', current_page - 1);
3219                            video_featured_control[tab_control_index] = [category, max_pages, current_page - 1];
3220                        }
3221
3222                        // Check first page
3223                        element.find('.video-control .video-control-item[data-action="next"]').removeClass('disable');
3224                        if ( current_page - 1 <= 1 ) {
3225                            element.find('.video-control .video-control-item[data-action="prev"]').addClass('disable');
3226                        }
3227                    }
3228                });
3229            });
3230        },
3231        shortcodeVideoOrder: function() {
3232            var video_order_data = []; // Use this for video order shortcode
3233            var video_order_control = [];
3234
3235            $('.video-order-shortcode').each(function() {
3236                var element         = $(this);
3237                var element_id      = element.attr('id');
3238                var atts            = element.data('atts');
3239
3240                // Have Video filter
3241                var video_order     = element.find('.video-filter .filter-item:first').attr('data-video_order');
3242                var max_pages       = parseInt(element.find('.video-control-item').attr('data-max_pages'));
3243                var current_page    = parseInt(element.find('.video-control-item').attr('data-current_page'));
3244
3245                // Save first load to cache
3246                var tab_data_index = element_id + '-' + video_order.toString().split(',').join('-') + '-page-1';
3247                var tab_control_index = element_id + '-' + video_order.toString().split(',').join('-');
3248
3249                video_order_data[tab_data_index] = element.find('.video-order-content').clone(true);
3250                video_order_control[tab_control_index] = [video_order, max_pages, current_page];
3251
3252                element.find('.video-filter .filter-item').off().on('click', function() {
3253                    // Check if already click or loading
3254                    if ( $(this).hasClass('active') || element.find('.video-ajax-content').hasClass('loading') ) {
3255                        return;
3256                    }
3257
3258                    element.find('.video-filter .filter-item').removeClass('active');
3259                    $(this).addClass('active');
3260
3261                    // Check cache
3262                    var video_order            = $(this).data('video_order');
3263                    var tab_control_index   = element_id + '-' + video_order.toString().split(',').join('-');
3264
3265                    if ( video_order_control[tab_control_index] != undefined ) {
3266                        element.find('.video-order-content').remove();
3267
3268                        var tab_data_index = element_id + '-' + video_order.toString().split(',').join('-') + '-page-' + video_order_control[tab_control_index][2].toString();
3269
3270                        element.find('.video-ajax-content').append( video_order_data[tab_data_index] );
3271                        element.find('.video-control .video-control-item').attr('data-video_order', video_order_control[tab_control_index][0]);
3272                        element.find('.video-control .video-control-item').attr('data-max_pages', video_order_control[tab_control_index][1]);
3273                        element.find('.video-control .video-control-item').attr('data-current_page', video_order_control[tab_control_index][2]);
3274                        // Check show/hide control
3275                        if ( video_order_control[tab_control_index][1] <= 1 ) {
3276                            element.find('.video-control').addClass('hide');
3277                        } else {
3278                            element.find('.video-control').removeClass('hide');
3279                        }
3280                        // Check last page, first page
3281                        if ( video_order_control[tab_control_index][2] == video_order_control[tab_control_index][1] ) {
3282                            element.find('.video-control .video-control-item[data-action="prev"]').removeClass('disable');
3283                            element.find('.video-control .video-control-item[data-action="next"]').addClass('disable');
3284                        } else if ( video_order_control[tab_control_index][2] == 1 ) {
3285                            element.find('.video-control .video-control-item[data-action="prev"]').addClass('disable');
3286                            element.find('.video-control .video-control-item[data-action="next"]').removeClass('disable');
3287                        } else {
3288                            element.find('.video-control .video-control-item[data-action="prev"]').removeClass('disable');
3289                            element.find('.video-control .video-control-item[data-action="next"]').removeClass('disable');
3290                        }
3291
3292                        return;
3293                    }
3294
3295                    // Load content via ajax
3296                    element.find('.video-ajax-content').addClass('loading');
3297
3298                    $.ajax({
3299                        type : "POST",
3300                        timeout : 30000,
3301                        url : haru_vidi_ajax_url,
3302                        data : {
3303                            action: 'haru_get_video_order', 
3304                            atts: atts, 
3305                            video_order: video_order
3306                        },
3307                        error: function(xhr,err) {
3308                            console.log('Have something wrong! Please try again!');
3309                        },
3310                        success: function(response) {
3311                            if ( response ) {
3312                                element.find('.video-order-content').remove();
3313                                element.find('.video-ajax-content').append( response );       
3314
3315                                // Save cache
3316                                var tab_data_index      = element_id + '-' + video_order.toString().split(',').join('-') + '-page-1';
3317                                video_order_data[tab_data_index] = response;
3318
3319                                // Do something
3320                                var max_pages = parseInt(element.find('.video-order-content').attr('data-max_pages'));
3321                                video_order_control[tab_control_index] = [video_order, max_pages, 1];
3322                                // Check show/hide control
3323                                if ( max_pages <= 1 ) {
3324                                    // Do something
3325                                    element.find('.video-control').addClass('hide');
3326                                } else {
3327                                    element.find('.video-control').removeClass('hide');
3328                                }
3329                                // Set new value for control
3330                                element.find('.video-control .video-control-item').attr('data-video_order', video_order);
3331                                element.find('.video-control .video-control-item').attr('data-max_pages', max_pages);
3332                                element.find('.video-control .video-control-item').attr('data-current_page', 1);
3333                                // Check last page, first page
3334                                element.find('.video-control .video-control-item[data-action="prev"]').addClass('disable');
3335                                element.find('.video-control .video-control-item[data-action="next"]').removeClass('disable');
3336                            }
3337                            element.find('.video-ajax-content').removeClass('loading');
3338                        }
3339                    });
3340                });
3341
3342                // Next/Prev control
3343                element.find('.video-control .video-control-item').off().on('click', function() {
3344                    // Check if already click or loading
3345                    if ( $(this).hasClass('disable') || element.find('.video-ajax-content').hasClass('loading') ) {
3346                        return;
3347                    }
3348
3349                    var video_order        = $(this).attr('data-video_order');
3350                    var max_pages       = parseInt($(this).attr('data-max_pages'));
3351                    var action          = $(this).attr('data-action');
3352
3353                    if ( action == 'next' ) {
3354                        var current_page    = parseInt($(this).attr('data-current_page'));
3355
3356                        if ( (current_page + 1) <= max_pages ) {
3357                            // Check cache
3358                            var tab_data_index = element_id + '-' + video_order.toString().split(',').join('-') + '-page-' + ( current_page + 1 );
3359                            var tab_control_index = element_id + '-' + video_order.toString().split(',').join('-');
3360
3361                            if ( video_order_data[tab_data_index] != undefined ) {
3362                                element.find('.video-order-content').remove();
3363                                element.find('.video-ajax-content').append( video_order_data[tab_data_index] );
3364                                
3365                                // Do something
3366                                element.find('.video-control .video-control-item').attr('data-current_page', current_page + 1);
3367                                video_order_control[tab_control_index] = [video_order, max_pages, current_page + 1];
3368                                // Check last page
3369                                element.find('.video-control .video-control-item[data-action="prev"]').removeClass('disable');
3370                                if ( current_page + 1 >= max_pages ) {
3371                                    element.find('.video-control .video-control-item[data-action="next"]').addClass('disable');
3372                                }
3373
3374                                return;
3375                            }
3376                        
3377                            // Load content via ajax
3378                            element.find('.video-ajax-content').addClass('loading');
3379
3380                            $.ajax({
3381                                type : "POST",
3382                                timeout : 30000,
3383                                url : haru_vidi_ajax_url,
3384                                data : {
3385                                    action: 'haru_get_video_order_next', 
3386                                    atts: atts, 
3387                                    video_order: video_order,
3388                                    current_page: current_page
3389                                },
3390                                error: function(xhr,err) {
3391                                    console.log('Have something wrong! Please try again!');
3392                                },
3393                                success: function(response) {
3394                                    if ( response ) {
3395                                        element.find('.video-order-content').remove();
3396                                        element.find('.video-ajax-content').append( response );       
3397
3398                                        // Save cache
3399                                        video_order_data[tab_data_index] = response;
3400                                        video_order_control[tab_control_index] = [video_order, max_pages, current_page + 1];
3401                                    }
3402                                    // Do something
3403                                    element.find('.video-control .video-control-item').attr('data-current_page', current_page + 1);
3404                                    element.find('.video-ajax-content').removeClass('loading');
3405                                    // Check last page
3406                                    element.find('.video-control .video-control-item[data-action="prev"]').removeClass('disable');
3407                                    if ( current_page + 1 >= max_pages ) {
3408                                        element.find('.video-control .video-control-item[data-action="next"]').addClass('disable');
3409                                    }
3410                                }
3411                            });
3412                        } else {
3413                            return;
3414                        }
3415                    } else if ( action == 'prev' ) {
3416                        var current_page    = parseInt($(this).attr('data-current_page'));
3417
3418                        if ( current_page == 1 ) {
3419                            return;
3420                        }
3421
3422                        // Check cache
3423                        var tab_data_index = element_id + '-' + video_order.toString().split(',').join('-') + '-page-' + ( current_page - 1 );
3424                        var tab_control_index = element_id + '-' + video_order.toString().split(',').join('-');
3425
3426                        if ( video_order_data[tab_data_index] != undefined ) {
3427                            element.find('.video-order-content').remove();
3428                            element.find('.video-ajax-content').append( video_order_data[tab_data_index] );
3429                            
3430                            // Do something
3431                            element.find('.video-control .video-control-item').attr('data-current_page', current_page - 1);
3432                            video_order_control[tab_control_index] = [video_order, max_pages, current_page - 1];
3433                        }
3434
3435                        // Check first page
3436                        element.find('.video-control .video-control-item[data-action="next"]').removeClass('disable');
3437                        if ( current_page - 1 <= 1 ) {
3438                            element.find('.video-control .video-control-item[data-action="prev"]').addClass('disable');
3439                        }
3440                    }
3441                });
3442            });
3443        },
3444        shortcodeVideoOrderSingle: function() {
3445            var video_order_single_data = []; // Use this for video order shortcode
3446            var video_order_single_control = [];
3447
3448            $('.video-order-single-shortcode').each(function() {
3449                var element         = $(this);
3450                var element_id      = element.attr('id');
3451                var atts            = element.data('atts');
3452
3453                // Have Video filter
3454                var video_order     = element.find('.video-filter .filter-item:first').attr('data-video_order');
3455                var max_pages       = parseInt(element.find('.video-control-item').attr('data-max_pages'));
3456                var current_page    = parseInt(element.find('.video-control-item').attr('data-current_page'));
3457
3458                // Save first load to cache
3459                var tab_data_index = element_id + '-' + video_order.toString().split(',').join('-') + '-page-1';
3460                var tab_control_index = element_id + '-' + video_order.toString().split(',').join('-');
3461
3462                video_order_single_data[tab_data_index] = element.find('.video-order-single-content').clone(true);
3463                video_order_single_control[tab_control_index] = [video_order, max_pages, current_page];
3464
3465                // Next/Prev control
3466                element.find('.video-control .video-control-item').off().on('click', function() {
3467                    // Check if already click or loading
3468                    if ( $(this).hasClass('disable') || element.find('.video-ajax-content').hasClass('loading') ) {
3469                        return;
3470                    }
3471
3472                    var video_order        = $(this).attr('data-video_order');
3473                    var max_pages       = parseInt($(this).attr('data-max_pages'));
3474                    var action          = $(this).attr('data-action');
3475
3476                    if ( action == 'next' ) {
3477                        var current_page    = parseInt($(this).attr('data-current_page'));
3478
3479                        if ( (current_page + 1) <= max_pages ) {
3480                            // Check cache
3481                            var tab_data_index = element_id + '-' + video_order.toString().split(',').join('-') + '-page-' + ( current_page + 1 );
3482                            var tab_control_index = element_id + '-' + video_order.toString().split(',').join('-');
3483
3484                            if ( video_order_single_data[tab_data_index] != undefined ) {
3485                                element.find('.video-order-single-content').remove();
3486                                element.find('.video-ajax-content').append( video_order_single_data[tab_data_index] );
3487                                
3488                                // Do something
3489                                element.find('.video-control .video-control-item').attr('data-current_page', current_page + 1);
3490                                video_order_single_control[tab_control_index] = [video_order, max_pages, current_page + 1];
3491                                // Check last page
3492                                element.find('.video-control .video-control-item[data-action="prev"]').removeClass('disable');
3493                                if ( current_page + 1 >= max_pages ) {
3494                                    element.find('.video-control .video-control-item[data-action="next"]').addClass('disable');
3495                                }
3496
3497                                return;
3498                            }
3499                        
3500                            // Load content via ajax
3501                            element.find('.video-ajax-content').addClass('loading');
3502
3503                            $.ajax({
3504                                type : "POST",
3505                                timeout : 30000,
3506                                url : haru_vidi_ajax_url,
3507                                data : {
3508                                    action: 'haru_get_video_order_single_next', 
3509                                    atts: atts, 
3510                                    video_order: video_order,
3511                                    current_page: current_page
3512                                },
3513                                error: function(xhr,err) {
3514                                    console.log('Have something wrong! Please try again!');
3515                                },
3516                                success: function(response) {
3517                                    if ( response ) {
3518                                        element.find('.video-order-single-content').remove();
3519                                        element.find('.video-ajax-content').append( response );       
3520
3521                                        // Save cache
3522                                        video_order_single_data[tab_data_index] = response;
3523                                        video_order_single_control[tab_control_index] = [video_order, max_pages, current_page + 1];
3524                                    }
3525                                    // Do something
3526                                    element.find('.video-control .video-control-item').attr('data-current_page', current_page + 1);
3527                                    element.find('.video-ajax-content').removeClass('loading');
3528                                    // Check last page
3529                                    element.find('.video-control .video-control-item[data-action="prev"]').removeClass('disable');
3530                                    if ( current_page + 1 >= max_pages ) {
3531                                        element.find('.video-control .video-control-item[data-action="next"]').addClass('disable');
3532                                    }
3533                                }
3534                            });
3535                        } else {
3536                            return;
3537                        }
3538                    } else if ( action == 'prev' ) {
3539                        var current_page    = parseInt($(this).attr('data-current_page'));
3540
3541           
3541             if ( current_page == 1 ) {
3542                            return;
3543                        }
3544
3545                        // Check cache
3546                        var tab_data_index = element_id + '-' + video_order.toString().split(',').join('-') + '-page-' + ( current_page - 1 );
3547                        var tab_control_index = element_id + '-' + video_order.toString().split(',').join('-');
3548
3549                        if ( video_order_single_data[tab_data_index] != undefined ) {
3550                            element.find('.video-order-single-content').remove();
3551                            element.find('.video-ajax-content').append( video_order_single_data[tab_data_index] );
3552                            
3553                            // Do something
3554                            element.find('.video-control .video-control-item').attr('data-current_page', current_page - 1);
3555                            video_order_single_control[tab_control_index] = [video_order, max_pages, current_page - 1];
3556                        }
3557
3558                        // Check first page
3559                        element.find('.video-control .video-control-item[data-action="next"]').removeClass('disable');
3560                        if ( current_page - 1 <= 1 ) {
3561                            element.find('.video-control .video-control-item[data-action="prev"]').addClass('disable');
3562                        }
3563                    }
3564                });
3565            });
3566        },
3567        shortcodeVideoTop: function() {
3568            var video_top_data = []; // Use this for video top shortcode
3569            var video_top_control = [];
3570
3571            $('.video-top-shortcode').each(function() {
3572                var element         = $(this);
3573                var element_id      = element.attr('id');
3574                var atts            = element.data('atts');
3575
3576                // Have Video filter
3577                var video_order_by  = element.attr('data-video_order_by'); // Different if have tab
3578                var max_pages       = parseInt(element.find('.video-control-item').attr('data-max_pages'));
3579                var current_page    = parseInt(element.find('.video-control-item').attr('data-current_page'));
3580
3581                // Save first load to cache
3582                var tab_data_index = element_id + '-' + video_order_by.toString().split(',').join('-') + '-page-1';
3583                var tab_control_index = element_id + '-' + video_order_by.toString().split(',').join('-');
3584
3585                video_top_data[tab_data_index] = element.find('.video-top-content').clone(true);
3586                video_top_control[tab_control_index] = [video_order_by, max_pages, current_page];
3587
3588                // Next/Prev control
3589                element.find('.video-control .video-control-item').off().on('click', function() {
3590                    // Check if already click or loading
3591                    if ( $(this).hasClass('disable') || element.find('.video-ajax-content').hasClass('loading') ) {
3592                        return;
3593                    }
3594
3595                    var video_order_by  = $(this).attr('data-video_order_by');
3596                    var max_pages       = parseInt($(this).attr('data-max_pages'));
3597                    var action          = $(this).attr('data-action');
3598
3599                    if ( action == 'next' ) {
3600                        var current_page    = parseInt($(this).attr('data-current_page'));
3601
3602                        if ( (current_page + 1) <= max_pages ) {
3603                            // Check cache
3604                            var tab_data_index = element_id + '-' + video_order_by.toString().split(',').join('-') + '-page-' + ( current_page + 1 );
3605                            var tab_control_index = element_id + '-' + video_order_by.toString().split(',').join('-');
3606
3607                            if ( video_top_data[tab_data_index] != undefined ) {
3608                                element.find('.video-top-content').remove();
3609                                element.find('.video-ajax-content').append( video_top_data[tab_data_index] );
3610                                
3611                                // Do something
3612                                element.find('.video-control .video-control-item').attr('data-current_page', current_page + 1);
3613                                video_top_control[tab_control_index] = [video_order_by, max_pages, current_page + 1];
3614                                // Check last page
3615                                element.find('.video-control .video-control-item[data-action="prev"]').removeClass('disable');
3616                                if ( current_page + 1 >= max_pages ) {
3617                                    element.find('.video-control .video-control-item[data-action="next"]').addClass('disable');
3618                                }
3619
3620                                return;
3621                            }
3622                        
3623                            // Load content via ajax
3624                            element.find('.video-ajax-content').addClass('loading');
3625
3626                            $.ajax({
3627                                type : "POST",
3628                                timeout : 30000,
3629                                url : haru_vidi_ajax_url,
3630                                data : {
3631                                    action: 'haru_get_video_top_next', 
3632                                    atts: atts, 
3633                                    video_order_by: video_order_by,
3634                                    current_page: current_page
3635                                },
3636                                error: function(xhr,err) {
3637                                    console.log('Have something wrong! Please try again!');
3638                                },
3639                                success: function(response) {
3640                                    if ( response ) {
3641                                        element.find('.video-top-content').remove();
3642                                        element.find('.video-ajax-content').append( response );       
3643
3644                                        // Save cache
3645                                        video_top_data[tab_data_index] = response;
3646                                        video_top_control[tab_control_index] = [video_order_by, max_pages, current_page + 1];
3647                                    }
3648                                    // Do something
3649                                    element.find('.video-control .video-control-item').attr('data-current_page', current_page + 1);
3650                                    element.find('.video-ajax-content').removeClass('loading');
3651                                    // Check last page
3652                                    element.find('.video-control .video-control-item[data-action="prev"]').removeClass('disable');
3653                                    if ( current_page + 1 >= max_pages ) {
3654                                        element.find('.video-control .video-control-item[data-action="next"]').addClass('disable');
3655                                    }
3656                                }
3657                            });
3658                        } else {
3659                            return;
3660                        }
3661                    } else if ( action == 'prev' ) {
3662                        var current_page    = parseInt($(this).attr('data-current_page'));
3663
3664                        if ( current_page == 1 ) {
3665                            return;
3666                        }
3667
3668                        // Check cache
3669                        var tab_data_index = element_id + '-' + video_order_by.toString().split(',').join('-') + '-page-' + ( current_page - 1 );
3670                        var tab_control_index = element_id + '-' + video_order_by.toString().split(',').join('-');
3671
3672                        if ( video_top_data[tab_data_index] != undefined ) {
3673                            element.find('.video-top-content').remove();
3674                            element.find('.video-ajax-content').append( video_top_data[tab_data_index] );
3675                            
3676                            // Do something
3677                            element.find('.video-control .video-control-item').attr('data-current_page', current_page - 1);
3678                            video_top_control[tab_control_index] = [video_order_by, max_pages, current_page - 1];
3679                        }
3680
3681                        // Check first page
3682                        element.find('.video-control .video-control-item[data-action="next"]').removeClass('disable');
3683                        if ( current_page - 1 <= 1 ) {
3684                            element.find('.video-control .video-control-item[data-action="prev"]').addClass('disable');
3685                        }
3686                    }
3687                });
3688            });
3689        },
3690        shortcodeVideoSlideshow: function() {
3691            $('.haru-slick').not('.slick-initialized').each(function(index, value){
3692                var $self          = $(this);
3693
3694                $self.slick();
3695                
3696                // Special
3697                if ( $self.parent().hasClass('info-featured') ) {
3698                    var stHeight = $self.find('.slick-track').height();
3699                    $self.find('.slick-slide').css('height',stHeight + 'px' );
3700                }
3701            });
3702        },
3703        shortcodeVideoSearch: function() {
3704            $('.video-search-shortcode').each(function() {
3705                var search_sc = $(this);
3706                
3707                $(this).find('.video-search-button').on('click', function() {
3708                    var popup_effect    = $(this).data('effect');
3709
3710                    $(this).magnificPopup({
3711                        items: {
3712                          src: search_sc.find('.video-search-popup'),
3713                          type: 'inline'
3714                        },
3715                        removalDelay: 500, //delay removal by X to allow out-animation
3716                        callbacks: {
3717                            beforeOpen: function() {
3718                                this.st.mainClass = popup_effect;
3719                            },
3720                            beforeClose: function() {
3721                            
3722                            },
3723                        }
3724                        // (optionally) other options
3725                    }).magnificPopup('open');
3726                });
3727            });
3728        },
3729        shortcodeChannelCategory: function() {
3730            var channel_category_data = []; // Use this for channel category shortcode
3731            var channel_category_control = [];
3732
3733            $('.channel-category-shortcode').each(function() {
3734                var element         = $(this);
3735                var element_id      = element.attr('id');
3736                var atts            = element.data('atts');
3737
3738
3739                if ( atts['filter'] != 'hide' ) {
3740                    var category        = element.find('.channel-filter .filter-item:first').attr('data-category');
3741                    var max_pages       = parseInt(element.find('.channel-control-item').attr('data-max_pages'));
3742                    var current_page    = parseInt(element.find('.channel-control-item').attr('data-current_page'));
3743
3744                    // Save first load to cache
3745                    var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-1';
3746                    var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
3747
3748                    channel_category_data[tab_data_index] = element.find('.channel-category-content').clone(true);
3749                    channel_category_control[tab_control_index] = [category, max_pages, current_page];
3750
3751                    element.find('.channel-filter .filter-item').off().on('click', function() {
3752                        // Check if already click or loading
3753                        if ( $(this).hasClass('active') || element.find('.channel-ajax-content').hasClass('loading') ) {
3754                            return;
3755                        }
3756
3757                        element.find('.channel-filter .filter-item').removeClass('active');
3758                        $(this).addClass('active');
3759
3760                        // Check cache
3761                        var category            = $(this).data('category');
3762                        var tab_control_index   = element_id + '-' + category.toString().split(',').join('-');
3763
3764                        if ( channel_category_control[tab_control_index] != undefined ) {
3765                            element.find('.channel-category-content').remove();
3766
3767                            var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-' + channel_category_control[tab_control_index][2].toString();
3768
3769                            element.find('.channel-ajax-content').append( channel_category_data[tab_data_index] );
3770                            element.find('.channel-control .channel-control-item').attr('data-category', channel_category_control[tab_control_index][0]);
3771                            element.find('.channel-control .channel-control-item').attr('data-max_pages', channel_category_control[tab_control_index][1]);
3772                            element.find('.channel-control .channel-control-item').attr('data-current_page', channel_category_control[tab_control_index][2]);
3773                            // Check show/hide control
3774                            if ( channel_category_control[tab_control_index][1] <= 1 ) {
3775                                element.find('.channel-control').addClass('hide');
3776                            } else {
3777                                element.find('.channel-control').removeClass('hide');
3778                            }
3779                            // Check last page, first page
3780                            if ( channel_category_control[tab_control_index][2] == channel_category_control[tab_control_index][1] ) {
3781                                element.find('.channel-control .channel-control-item[data-action="prev"]').removeClass('disable');
3782                                element.find('.channel-control .channel-control-item[data-action="next"]').addClass('disable');
3783                            } else if ( channel_category_control[tab_control_index][2] == 1 ) {
3784                                element.find('.channel-control .channel-control-item[data-action="prev"]').addClass('disable');
3785                                element.find('.channel-control .channel-control-item[data-action="next"]').removeClass('disable');
3786                            } else {
3787                                element.find('.channel-control .channel-control-item[data-action="prev"]').removeClass('disable');
3788                                element.find('.channel-control .channel-control-item[data-action="next"]').removeClass('disable');
3789                            }
3790
3791                            return;
3792                        }
3793
3794                        // Load content via ajax
3795                        element.find('.channel-ajax-content').addClass('loading');
3796
3797                        $.ajax({
3798                            type : "POST",
3799                            timeout : 30000,
3800                            url : haru_vidi_ajax_url,
3801                            data : {
3802                                action: 'haru_get_channel_category', 
3803                                atts: atts, 
3804                                category: category
3805                            },
3806                            error: function(xhr,err) {
3807                                console.log('Have something wrong! Please try again!');
3808                            },
3809                            success: function(response) {
3810                                if ( response ) {
3811                                    element.find('.channel-category-content').remove();
3812                                    element.find('.channel-ajax-content').append( response );       
3813
3814                                    // Save cache
3815                                    var tab_data_index      = element_id + '-' + category.toString().split(',').join('-') + '-page-1';
3816                                    channel_category_data[tab_data_index] = response;
3817
3818                                    // Do something
3819                                    var max_pages = parseInt(element.find('.channel-category-content').attr('data-max_pages'));
3820                                    channel_category_control[tab_control_index] = [category, max_pages, 1];
3821                                    // Check show/hide control
3822                                    if ( max_pages <= 1 ) {
3823                                        // Do something
3824                                        element.find('.channel-control').addClass('hide');
3825                                    } else {
3826                                        element.find('.channel-control').removeClass('hide');
3827                                    }
3828                                    // Set new value for control
3829                                    element.find('.channel-control .channel-control-item').attr('data-category', category);
3830                                    element.find('.channel-control .channel-control-item').attr('data-max_pages', max_pages);
3831                                    element.find('.channel-control .channel-control-item').attr('data-current_page', 1);
3832                                    // Check last page, first page
3833                                    element.find('.channel-control .channel-control-item[data-action="prev"]').addClass('disable');
3834                                    element.find('.channel-control .channel-control-item[data-action="next"]').removeClass('disable');
3835                                }
3836                                element.find('.channel-ajax-content').removeClass('loading');
3837                            }
3838                        });
3839                    });
3840                } else {
3841                    var category        = element.find('.channel-control-item').attr('data-category');
3842                    var max_pages       = parseInt(element.find('.channel-control-item').attr('data-max_pages'));
3843                    var current_page    = parseInt(element.find('.channel-control-item').attr('data-current_page'));
3844
3845                    // Save first load to cache
3846                    var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-1';
3847                    var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
3848
3849                    channel_category_data[tab_data_index] = element.find('.channel-category-content').clone(true);
3850                    channel_category_control[tab_control_index] = [category, max_pages, current_page];
3851                }
3852
3853                // Next/Prev control
3854                element.find('.channel-control .channel-control-item').off().on('click', function() {
3855                    // Check if already click or loading
3856                    if ( $(this).hasClass('disable') || element.find('.channel-ajax-content').hasClass('loading') ) {
3857                        return;
3858                    }
3859
3860                    var category        = $(this).attr('data-category');
3861                    var max_pages       = parseInt($(this).attr('data-max_pages'));
3862                    var action          = $(this).attr('data-action');
3863
3864                    if ( action == 'next' ) {
3865                        var current_page    = parseInt($(this).attr('data-current_page'));
3866
3867                        if ( (current_page + 1) <= max_pages ) {
3868                            // Check cache
3869                            var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-' + ( current_page + 1 );
3870                            var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
3871
3872                            if ( channel_category_data[tab_data_index] != undefined ) {
3873                                element.find('.channel-category-content').remove();
3874                                element.find('.channel-ajax-content').append( channel_category_data[tab_data_index] );
3875                                
3876                                // Do something
3877                                element.find('.channel-control .channel-control-item').attr('data-current_page', current_page + 1);
3878                                channel_category_control[tab_control_index] = [category, max_pages, current_page + 1];
3879                                // Check last page
3880                                element.find('.channel-control .channel-control-item[data-action="prev"]').removeClass('disable');
3881                                if ( current_page + 1 >= max_pages ) {
3882                                    element.find('.channel-control .channel-control-item[data-action="next"]').addClass('disable');
3883                                }
3884
3885                                return;
3886                            }
3887                        
3888                            // Load content via ajax
3889                            element.find('.channel-ajax-content').addClass('loading');
3890
3891                            $.ajax({
3892                                type : "POST",
3893                                timeout : 30000,
3894                                url : haru_vidi_ajax_url,
3895                                data : {
3896                                    action: 'haru_get_channel_category_next', 
3897                                    atts: atts, 
3898                                    category: category,
3899                                    current_page: current_page
3900                                },
3901                                error: function(xhr,err) {
3902                                    console.log('Have something wrong! Please try again!');
3903                                },
3904                                success: function(response) {
3905                                    if ( response ) {
3906                                        element.find('.channel-category-content').remove();
3907                                        element.find('.channel-ajax-content').append( response );       
3908
3909                                        // Save cache
3910                                        channel_category_data[tab_data_index] = response;
3911                                        channel_category_control[tab_control_index] = [category, max_pages, current_page + 1];
3912                                    }
3913                                    // Do something
3914                                    element.find('.channel-control .channel-control-item').attr('data-current_page', current_page + 1);
3915                                    element.find('.channel-ajax-content').removeClass('loading');
3916                                    // Check last page
3917                                    element.find('.channel-control .channel-control-item[data-action="prev"]').removeClass('disable');
3918                                    if ( current_page + 1 >= max_pages ) {
3919                                        element.find('.channel-control .channel-control-item[data-action="next"]').addClass('disable');
3920                                    }
3921                                }
3922                            });
3923                        } else {
3924                            return;
3925                        }
3926                    } else if ( action == 'prev' ) {
3927                        var current_page    = parseInt($(this).attr('data-current_page'));
3928
3929                        if ( current_page == 1 ) {
3930                            return;
3931                        }
3932
3933                        // Check cache
3934                        var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-' + ( current_page - 1 );
3935                        var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
3936
3937                        if ( channel_category_data[tab_data_index] != undefined ) {
3938                            element.find('.channel-category-content').remove();
3939                            element.find('.channel-ajax-content').append( channel_category_data[tab_data_index] );
3940                            
3941                            // Do something
3942                            element.find('.channel-control .channel-control-item').attr('data-current_page', current_page - 1);
3943                            channel_category_control[tab_control_index] = [category, max_pages, current_page - 1];
3944                        }
3945
3946                        // Check first page
3947                        element.find('.channel-control .channel-control-item[data-action="next"]').removeClass('disable');
3948                        if ( current_page - 1 <= 1 ) {
3949                            element.find('.channel-control .channel-control-item[data-action="prev"]').addClass('disable');
3950                        }
3951                    }
3952                });
3953            });
3954        },
3955        shortcodeChannelTop: function() {
3956            var channel_top_data = []; // Use this for channel top shortcode
3957            var channel_top_control = [];
3958
3959            $('.channel-top-shortcode').each(function() {
3960                var element         = $(this);
3961                var element_id      = element.attr('id');
3962                var atts            = element.data('atts');
3963
3964                // Have Video filter
3965                var channel_order_by  = element.attr('data-channel_order_by'); // Different if have tab
3966                var max_pages       = parseInt(element.find('.channel-control-item').attr('data-max_pages'));
3967                var current_page    = parseInt(element.find('.channel-control-item').attr('data-current_page'));
3968
3969                // Save first load to cache
3970                var tab_data_index = element_id + '-' + channel_order_by.toString().split(',').join('-') + '-page-1';
3971                var tab_control_index = element_id + '-' + channel_order_by.toString().split(',').join('-');
3972
3973                channel_top_data[tab_data_index] = element.find('.channel-top-content').clone(true);
3974                channel_top_control[tab_control_index] = [channel_order_by, max_pages, current_page];
3975
3976                // Next/Prev control
3977                element.find('.channel-control .channel-control-item').off().on('click', function() {
3978                    // Check if already click or loading
3979                    if ( $(this).hasClass('disable') || element.find('.channel-ajax-content').hasClass('loading') ) {
3980                        return;
3981                    }
3982
3983                    var channel_order_by  = $(this).attr('data-channel_order_by');
3984                    var max_pages       = parseInt($(this).attr('data-max_pages'));
3985                    var action          = $(this).attr('data-action');
3986
3987                    if ( action == 'next' ) {
3988                        var current_page    = parseInt($(this).attr('data-current_page'));
3989
3990                        if ( (current_page + 1) <= max_pages ) {
3991                            // Check cache
3992                            var tab_data_index = element_id + '-' + channel_order_by.toString().split(',').join('-') + '-page-' + ( current_page + 1 );
3993                            var tab_control_index = element_id + '-' + channel_order_by.toString().split(',').join('-');
3994
3995                            if ( channel_top_data[tab_data_index] != undefined ) {
3996                                element.find('.channel-top-content').remove();
3997                                element.find('.channel-ajax-content').append( channel_top_data[tab_data_index] );
3998                                
3999                                // Do something
4000                                element.find('.channel-control .channel-control-item').attr('data-current_page', current_page + 1);
4001                                channel_top_control[tab_control_index] = [channel_order_by, max_pages, current_page + 1];
4002                                // Check last page
4003                                element.find('.channel-control .channel-control-item[data-action="prev"]').removeClass('disable');
4004                                if ( current_page + 1 >= max_pages ) {
4005                                    element.find('.channel-control .channel-control-item[data-action="next"]').addClass('disable');
4006                                }
4007
4008                                return;
4009                            }
4010                        
4011                            // Load content via ajax
4012                            element.find('.channel-ajax-content').addClass('loading');
4013
4014                            $.ajax({
4015                                type : "POST",
4016                                timeout : 30000,
4017                                url : haru_vidi_ajax_url,
4018                                data : {
4019                                    action: 'haru_get_channel_top_next', 
4020                                    atts: atts, 
4021                                    channel_order_by: channel_order_by,
4022                                    current_page: current_page
4023                                },
4024                                error: function(xhr,err) {
4025                                    console.log('Have something wrong! Please try again!');
4026                                },
4027                                success: function(response) {
4028                                    if ( response ) {
4029                                        element.find('.channel-top-content').remove();
4030                                        element.find('.channel-ajax-content').append( response );       
4031
4032                                        // Save cache
4033                                        c
4033hannel_top_data[tab_data_index] = response;
4034                                        channel_top_control[tab_control_index] = [channel_order_by, max_pages, current_page + 1];
4035                                    }
4036                                    // Do something
4037                                    element.find('.channel-control .channel-control-item').attr('data-current_page', current_page + 1);
4038                                    element.find('.channel-ajax-content').removeClass('loading');
4039                                    // Check last page
4040                                    element.find('.channel-control .channel-control-item[data-action="prev"]').removeClass('disable');
4041                                    if ( current_page + 1 >= max_pages ) {
4042                                        element.find('.channel-control .channel-control-item[data-action="next"]').addClass('disable');
4043                                    }
4044                                }
4045                            });
4046                        } else {
4047                            return;
4048                        }
4049                    } else if ( action == 'prev' ) {
4050                        var current_page    = parseInt($(this).attr('data-current_page'));
4051
4052                        if ( current_page == 1 ) {
4053                            return;
4054                        }
4055
4056                        // Check cache
4057                        var tab_data_index = element_id + '-' + channel_order_by.toString().split(',').join('-') + '-page-' + ( current_page - 1 );
4058                        var tab_control_index = element_id + '-' + channel_order_by.toString().split(',').join('-');
4059
4060                        if ( channel_top_data[tab_data_index] != undefined ) {
4061                            element.find('.channel-top-content').remove();
4062                            element.find('.channel-ajax-content').append( channel_top_data[tab_data_index] );
4063                            
4064                            // Do something
4065                            element.find('.channel-control .channel-control-item').attr('data-current_page', current_page - 1);
4066                            channel_top_control[tab_control_index] = [channel_order_by, max_pages, current_page - 1];
4067                        }
4068
4069                        // Check first page
4070                        element.find('.channel-control .channel-control-item[data-action="next"]').removeClass('disable');
4071                        if ( current_page - 1 <= 1 ) {
4072                            element.find('.channel-control .channel-control-item[data-action="prev"]').addClass('disable');
4073                        }
4074                    }
4075                });
4076            });
4077        },
4078        shortcodeSeriesCategory: function() {
4079            var series_category_data = []; // Use this for series category shortcode
4080            var series_category_control = [];
4081
4082            $('.series-category-shortcode').each(function() {
4083                var element         = $(this);
4084                var element_id      = element.attr('id');
4085                var atts            = element.data('atts');
4086
4087
4088                if ( atts['filter'] != 'hide' ) {
4089                    var category        = element.find('.series-filter .filter-item:first').attr('data-category');
4090                    var max_pages       = parseInt(element.find('.series-control-item').attr('data-max_pages'));
4091                    var current_page    = parseInt(element.find('.series-control-item').attr('data-current_page'));
4092
4093                    // Save first load to cache
4094                    var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-1';
4095                    var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
4096
4097                    series_category_data[tab_data_index] = element.find('.series-category-content').clone(true);
4098                    series_category_control[tab_control_index] = [category, max_pages, current_page];
4099
4100                    element.find('.series-filter .filter-item').off().on('click', function() {
4101                        // Check if already click or loading
4102                        if ( $(this).hasClass('active') || element.find('.series-ajax-content').hasClass('loading') ) {
4103                            return;
4104                        }
4105
4106                        element.find('.series-filter .filter-item').removeClass('active');
4107                        $(this).addClass('active');
4108
4109                        // Check cache
4110                        var category            = $(this).data('category');
4111                        var tab_control_index   = element_id + '-' + category.toString().split(',').join('-');
4112
4113                        if ( series_category_control[tab_control_index] != undefined ) {
4114                            element.find('.series-category-content').remove();
4115
4116                            var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-' + series_category_control[tab_control_index][2].toString();
4117
4118                            element.find('.series-ajax-content').append( series_category_data[tab_data_index] );
4119                            element.find('.series-control .series-control-item').attr('data-category', series_category_control[tab_control_index][0]);
4120                            element.find('.series-control .series-control-item').attr('data-max_pages', series_category_control[tab_control_index][1]);
4121                            element.find('.series-control .series-control-item').attr('data-current_page', series_category_control[tab_control_index][2]);
4122                            // Check show/hide control
4123                            if ( series_category_control[tab_control_index][1] <= 1 ) {
4124                                element.find('.series-control').addClass('hide');
4125                            } else {
4126                                element.find('.series-control').removeClass('hide');
4127                            }
4128                            // Check last page, first page
4129                            if ( series_category_control[tab_control_index][2] == series_category_control[tab_control_index][1] ) {
4130                                element.find('.series-control .series-control-item[data-action="prev"]').removeClass('disable');
4131                                element.find('.series-control .series-control-item[data-action="next"]').addClass('disable');
4132                            } else if ( series_category_control[tab_control_index][2] == 1 ) {
4133                                element.find('.series-control .series-control-item[data-action="prev"]').addClass('disable');
4134                                element.find('.series-control .series-control-item[data-action="next"]').removeClass('disable');
4135                            } else {
4136                                element.find('.series-control .series-control-item[data-action="prev"]').removeClass('disable');
4137                                element.find('.series-control .series-control-item[data-action="next"]').removeClass('disable');
4138                            }
4139
4140                            return;
4141                        }
4142
4143                        // Load content via ajax
4144                        element.find('.series-ajax-content').addClass('loading');
4145
4146                        $.ajax({
4147                            type : "POST",
4148                            timeout : 30000,
4149                            url : haru_vidi_ajax_url,
4150                            data : {
4151                                action: 'haru_get_series_category', 
4152                                atts: atts, 
4153                                category: category
4154                            },
4155                            error: function(xhr,err) {
4156                                console.log('Have something wrong! Please try again!');
4157                            },
4158                            success: function(response) {
4159                                if ( response ) {
4160                                    element.find('.series-category-content').remove();
4161                                    element.find('.series-ajax-content').append( response );       
4162
4163                                    // Save cache
4164                                    var tab_data_index      = element_id + '-' + category.toString().split(',').join('-') + '-page-1';
4165                                    series_category_data[tab_data_index] = response;
4166
4167                                    // Do something
4168                                    var max_pages = parseInt(element.find('.series-category-content').attr('data-max_pages'));
4169                                    series_category_control[tab_control_index] = [category, max_pages, 1];
4170                                    // Check show/hide control
4171                                    if ( max_pages <= 1 ) {
4172                                        // Do something
4173                                        element.find('.series-control').addClass('hide');
4174                                    } else {
4175                                        element.find('.series-control').removeClass('hide');
4176                                    }
4177                                    // Set new value for control
4178                                    element.find('.series-control .series-control-item').attr('data-category', category);
4179                                    element.find('.series-control .series-control-item').attr('data-max_pages', max_pages);
4180                                    element.find('.series-control .series-control-item').attr('data-current_page', 1);
4181                                    // Check last page, first page
4182                                    element.find('.series-control .series-control-item[data-action="prev"]').addClass('disable');
4183                                    element.find('.series-control .series-control-item[data-action="next"]').removeClass('disable');
4184                                }
4185                                element.find('.series-ajax-content').removeClass('loading');
4186                            }
4187                        });
4188                    });
4189                } else {
4190                    var category        = element.find('.series-control-item').attr('data-category');
4191                    var max_pages       = parseInt(element.find('.series-control-item').attr('data-max_pages'));
4192                    var current_page    = parseInt(element.find('.series-control-item').attr('data-current_page'));
4193
4194                    // Save first load to cache
4195                    var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-1';
4196                    var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
4197
4198                    series_category_data[tab_data_index] = element.find('.series-category-content').clone(true);
4199                    series_category_control[tab_control_index] = [category, max_pages, current_page];
4200                }
4201
4202                // Next/Prev control
4203                element.find('.series-control .series-control-item').off().on('click', function() {
4204                    // Check if already click or loading
4205                    if ( $(this).hasClass('disable') || element.find('.series-ajax-content').hasClass('loading') ) {
4206                        return;
4207                    }
4208
4209                    var category        = $(this).attr('data-category');
4210                    var max_pages       = parseInt($(this).attr('data-max_pages'));
4211                    var action          = $(this).attr('data-action');
4212
4213                    if ( action == 'next' ) {
4214                        var current_page    = parseInt($(this).attr('data-current_page'));
4215
4216                        if ( (current_page + 1) <= max_pages ) {
4217                            // Check cache
4218                            var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-' + ( current_page + 1 );
4219                            var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
4220
4221                            if ( series_category_data[tab_data_index] != undefined ) {
4222                                element.find('.series-category-content').remove();
4223                                element.find('.series-ajax-content').append( series_category_data[tab_data_index] );
4224                                
4225                                // Do something
4226                                element.find('.series-control .series-control-item').attr('data-current_page', current_page + 1);
4227                                series_category_control[tab_control_index] = [category, max_pages, current_page + 1];
4228                                // Check last page
4229                                element.find('.series-control .series-control-item[data-action="prev"]').removeClass('disable');
4230                                if ( current_page + 1 >= max_pages ) {
4231                                    element.find('.series-control .series-control-item[data-action="next"]').addClass('disable');
4232                                }
4233
4234                                return;
4235                            }
4236                        
4237                            // Load content via ajax
4238                            element.find('.series-ajax-content').addClass('loading');
4239
4240                            $.ajax({
4241                                type : "POST",
4242                                timeout : 30000,
4243                                url : haru_vidi_ajax_url,
4244                                data : {
4245                                    action: 'haru_get_series_category_next', 
4246                                    atts: atts, 
4247                                    category: category,
4248                                    current_page: current_page
4249                                },
4250                                error: function(xhr,err) {
4251                                    console.log('Have something wrong! Please try again!');
4252                                },
4253                                success: function(response) {
4254                                    if ( response ) {
4255                                        element.find('.series-category-content').remove();
4256                                        element.find('.series-ajax-content').append( response );       
4257
4258                                        // Save cache
4259                                        series_category_data[tab_data_index] = response;
4260                                        series_category_control[tab_control_index] = [category, max_pages, current_page + 1];
4261                                    }
4262                                    // Do something
4263                                    element.find('.series-control .series-control-item').attr('data-current_page', current_page + 1);
4264                                    element.find('.series-ajax-content').removeClass('loading');
4265                                    // Check last page
4266                                    element.find('.series-control .series-control-item[data-action="prev"]').removeClass('disable');
4267                                    if ( current_page + 1 >= max_pages ) {
4268                                        element.find('.series-control .series-control-item[data-action="next"]').addClass('disable');
4269                                    }
4270                                }
4271                            });
4272                        } else {
4273                            return;
4274                        }
4275                    } else if ( action == 'prev' ) {
4276                        var current_page    = parseInt($(this).attr('data-current_page'));
4277
4278                        if ( current_page == 1 ) {
4279                            return;
4280                        }
4281
4282                        // Check cache
4283                        var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-' + ( current_page - 1 );
4284                        var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
4285
4286                        if ( series_category_data[tab_data_index] != undefined ) {
4287                            element.find('.series-category-content').remove();
4288                            element.find('.series-ajax-content').append( series_category_data[tab_data_index] );
4289                            
4290                            // Do something
4291                            element.find('.series-control .series-control-item').attr('data-current_page', current_page - 1);
4292                            series_category_control[tab_control_index] = [category, max_pages, current_page - 1];
4293                        }
4294
4295                        // Check first page
4296                        element.find('.series-control .series-control-item[data-action="next"]').removeClass('disable');
4297                        if ( current_page - 1 <= 1 ) {
4298                            element.find('.series-control .series-control-item[data-action="prev"]').addClass('disable');
4299                        }
4300                    }
4301                });
4302            });
4303        },
4304        shortcodeSeriesTop: function() {
4305            var series_top_data = []; // Use this for series top shortcode
4306            var series_top_control = [];
4307
4308            $('.series-top-shortcode').each(function() {
4309                var element         = $(this);
4310                var element_id      = element.attr('id');
4311                var atts            = element.data('atts');
4312
4313                // Have Video filter
4314                var series_order_by  = element.attr('data-series_order_by'); // Different if have tab
4315                var max_pages       = parseInt(element.find('.series-control-item').attr('data-max_pages'));
4316                var current_page    = parseInt(element.find('.series-control-item').attr('data-current_page'));
4317
4318                // Save first load to cache
4319                var tab_data_index = element_id + '-' + series_order_by.toString().split(',').join('-') + '-page-1';
4320                var tab_control_index = element_id + '-' + series_order_by.toString().split(',').join('-');
4321
4322                series_top_data[tab_data_index] = element.find('.series-top-content').clone(true);
4323                series_top_control[tab_control_index] = [series_order_by, max_pages, current_page];
4324
4325                // Next/Prev control
4326                element.find('.series-control .series-control-item').off().on('click', function() {
4327                    // Check if already click or loading
4328                    if ( $(this).hasClass('disable') || element.find('.series-ajax-content').hasClass('loading') ) {
4329                        return;
4330                    }
4331
4332                    var series_order_by  = $(this).attr('data-series_order_by');
4333                    var max_pages       = parseInt($(this).attr('data-max_pages'));
4334                    var action          = $(this).attr('data-action');
4335
4336                    if ( action == 'next' ) {
4337                        var current_page    = parseInt($(this).attr('data-current_page'));
4338
4339                        if ( (current_page + 1) <= max_pages ) {
4340                            // Check cache
4341                            var tab_data_index = element_id + '-' + series_order_by.toString().split(',').join('-') + '-page-' + ( current_page + 1 );
4342                            var tab_control_index = element_id + '-' + series_order_by.toString().split(',').join('-');
4343
4344                            if ( series_top_data[tab_data_index] != undefined ) {
4345                                element.find('.series-top-content').remove();
4346                                element.find('.series-ajax-content').append( series_top_data[tab_data_index] );
4347                                
4348                                // Do something
4349                                element.find('.series-control .series-control-item').attr('data-current_page', current_page + 1);
4350                                series_top_control[tab_control_index] = [series_order_by, max_pages, current_page + 1];
4351                                // Check last page
4352                                element.find('.series-control .series-control-item[data-action="prev"]').removeClass('disable');
4353                                if ( current_page + 1 >= max_pages ) {
4354                                    element.find('.series-control .series-control-item[data-action="next"]').addClass('disable');
4355                                }
4356
4357                                return;
4358                            }
4359                        
4360                            // Load content via ajax
4361                            element.find('.series-ajax-content').addClass('loading');
4362
4363                            $.ajax({
4364                                type : "POST",
4365                                timeout : 30000,
4366                                url : haru_vidi_ajax_url,
4367                                data : {
4368                                    action: 'haru_get_series_top_next', 
4369                                    atts: atts, 
4370                                    series_order_by: series_order_by,
4371                                    current_page: current_page
4372                                },
4373                                error: function(xhr,err) {
4374                                    console.log('Have something wrong! Please try again!');
4375                                },
4376                                success: function(response) {
4377                                    if ( response ) {
4378                                        element.find('.series-top-content').remove();
4379                                        element.find('.series-ajax-content').append( response );       
4380
4381                                        // Save cache
4382                                        series_top_data[tab_data_index] = response;
4383                                        series_top_control[tab_control_index] =
4383 [series_order_by, max_pages, current_page + 1];
4384                                    }
4385                                    // Do something
4386                                    element.find('.series-control .series-control-item').attr('data-current_page', current_page + 1);
4387                                    element.find('.series-ajax-content').removeClass('loading');
4388                                    // Check last page
4389                                    element.find('.series-control .series-control-item[data-action="prev"]').removeClass('disable');
4390                                    if ( current_page + 1 >= max_pages ) {
4391                                        element.find('.series-control .series-control-item[data-action="next"]').addClass('disable');
4392                                    }
4393                                }
4394                            });
4395                        } else {
4396                            return;
4397                        }
4398                    } else if ( action == 'prev' ) {
4399                        var current_page    = parseInt($(this).attr('data-current_page'));
4400
4401                        if ( current_page == 1 ) {
4402                            return;
4403                        }
4404
4405                        // Check cache
4406                        var tab_data_index = element_id + '-' + series_order_by.toString().split(',').join('-') + '-page-' + ( current_page - 1 );
4407                        var tab_control_index = element_id + '-' + series_order_by.toString().split(',').join('-');
4408
4409                        if ( series_top_data[tab_data_index] != undefined ) {
4410                            element.find('.series-top-content').remove();
4411                            element.find('.series-ajax-content').append( series_top_data[tab_data_index] );
4412                            
4413                            // Do something
4414                            element.find('.series-control .series-control-item').attr('data-current_page', current_page - 1);
4415                            series_top_control[tab_control_index] = [series_order_by, max_pages, current_page - 1];
4416                        }
4417
4418                        // Check first page
4419                        element.find('.series-control .series-control-item[data-action="next"]').removeClass('disable');
4420                        if ( current_page - 1 <= 1 ) {
4421                            element.find('.series-control .series-control-item[data-action="prev"]').addClass('disable');
4422                        }
4423                    }
4424                });
4425            });
4426        },
4427        shortcodePlaylistCategory: function() {
4428            var playlist_category_data = []; // Use this for playlist category shortcode
4429            var playlist_category_control = [];
4430
4431            $('.playlist-category-shortcode').each(function() {
4432                var element         = $(this);
4433                var element_id      = element.attr('id');
4434                var atts            = element.data('atts');
4435
4436
4437                if ( atts['filter'] != 'hide' ) {
4438                    var category        = element.find('.playlist-filter .filter-item:first').attr('data-category');
4439                    var max_pages       = parseInt(element.find('.playlist-control-item').attr('data-max_pages'));
4440                    var current_page    = parseInt(element.find('.playlist-control-item').attr('data-current_page'));
4441
4442                    // Save first load to cache
4443                    var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-1';
4444                    var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
4445
4446                    playlist_category_data[tab_data_index] = element.find('.playlist-category-content').clone(true);
4447                    playlist_category_control[tab_control_index] = [category, max_pages, current_page];
4448
4449                    element.find('.playlist-filter .filter-item').off().on('click', function() {
4450                        // Check if already click or loading
4451                        if ( $(this).hasClass('active') || element.find('.playlist-ajax-content').hasClass('loading') ) {
4452                            return;
4453                        }
4454
4455                        element.find('.playlist-filter .filter-item').removeClass('active');
4456                        $(this).addClass('active');
4457
4458                        // Check cache
4459                        var category            = $(this).data('category');
4460                        var tab_control_index   = element_id + '-' + category.toString().split(',').join('-');
4461
4462                        if ( playlist_category_control[tab_control_index] != undefined ) {
4463                            element.find('.playlist-category-content').remove();
4464
4465                            var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-' + playlist_category_control[tab_control_index][2].toString();
4466
4467                            element.find('.playlist-ajax-content').append( playlist_category_data[tab_data_index] );
4468                            element.find('.playlist-control .playlist-control-item').attr('data-category', playlist_category_control[tab_control_index][0]);
4469                            element.find('.playlist-control .playlist-control-item').attr('data-max_pages', playlist_category_control[tab_control_index][1]);
4470                            element.find('.playlist-control .playlist-control-item').attr('data-current_page', playlist_category_control[tab_control_index][2]);
4471                            // Check show/hide control
4472                            if ( playlist_category_control[tab_control_index][1] <= 1 ) {
4473                                element.find('.playlist-control').addClass('hide');
4474                            } else {
4475                                element.find('.playlist-control').removeClass('hide');
4476                            }
4477                            // Check last page, first page
4478                            if ( playlist_category_control[tab_control_index][2] == playlist_category_control[tab_control_index][1] ) {
4479                                element.find('.playlist-control .playlist-control-item[data-action="prev"]').removeClass('disable');
4480                                element.find('.playlist-control .playlist-control-item[data-action="next"]').addClass('disable');
4481                            } else if ( playlist_category_control[tab_control_index][2] == 1 ) {
4482                                element.find('.playlist-control .playlist-control-item[data-action="prev"]').addClass('disable');
4483                                element.find('.playlist-control .playlist-control-item[data-action="next"]').removeClass('disable');
4484                            } else {
4485                                element.find('.playlist-control .playlist-control-item[data-action="prev"]').removeClass('disable');
4486                                element.find('.playlist-control .playlist-control-item[data-action="next"]').removeClass('disable');
4487                            }
4488
4489                            return;
4490                        }
4491
4492                        // Load content via ajax
4493                        element.find('.playlist-ajax-content').addClass('loading');
4494
4495                        $.ajax({
4496                            type : "POST",
4497                            timeout : 30000,
4498                            url : haru_vidi_ajax_url,
4499                            data : {
4500                                action: 'haru_get_playlist_category', 
4501                                atts: atts, 
4502                                category: category
4503                            },
4504                            error: function(xhr,err) {
4505                                console.log('Have something wrong! Please try again!');
4506                            },
4507                            success: function(response) {
4508                                if ( response ) {
4509                                    element.find('.playlist-category-content').remove();
4510                                    element.find('.playlist-ajax-content').append( response );       
4511
4512                                    // Save cache
4513                                    var tab_data_index      = element_id + '-' + category.toString().split(',').join('-') + '-page-1';
4514                                    playlist_category_data[tab_data_index] = response;
4515
4516                                    // Do something
4517                                    var max_pages = parseInt(element.find('.playlist-category-content').attr('data-max_pages'));
4518                                    playlist_category_control[tab_control_index] = [category, max_pages, 1];
4519                                    // Check show/hide control
4520                                    if ( max_pages <= 1 ) {
4521                                        // Do something
4522                                        element.find('.playlist-control').addClass('hide');
4523                                    } else {
4524                                        element.find('.playlist-control').removeClass('hide');
4525                                    }
4526                                    // Set new value for control
4527                                    element.find('.playlist-control .playlist-control-item').attr('data-category', category);
4528                                    element.find('.playlist-control .playlist-control-item').attr('data-max_pages', max_pages);
4529                                    element.find('.playlist-control .playlist-control-item').attr('data-current_page', 1);
4530                                    // Check last page, first page
4531                                    element.find('.playlist-control .playlist-control-item[data-action="prev"]').addClass('disable');
4532                                    element.find('.playlist-control .playlist-control-item[data-action="next"]').removeClass('disable');
4533                                }
4534                                element.find('.playlist-ajax-content').removeClass('loading');
4535                            }
4536                        });
4537                    });
4538                } else {
4539                    var category        = element.find('.playlist-control-item').attr('data-category');
4540                    var max_pages       = parseInt(element.find('.playlist-control-item').attr('data-max_pages'));
4541                    var current_page    = parseInt(element.find('.playlist-control-item').attr('data-current_page'));
4542
4543                    // Save first load to cache
4544                    var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-1';
4545                    var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
4546
4547                    playlist_category_data[tab_data_index] = element.find('.playlist-category-content').clone(true);
4548                    playlist_category_control[tab_control_index] = [category, max_pages, current_page];
4549                }
4550
4551                // Next/Prev control
4552                element.find('.playlist-control .playlist-control-item').off().on('click', function() {
4553                    // Check if already click or loading
4554                    if ( $(this).hasClass('disable') || element.find('.playlist-ajax-content').hasClass('loading') ) {
4555                        return;
4556                    }
4557
4558                    var category        = $(this).attr('data-category');
4559                    var max_pages       = parseInt($(this).attr('data-max_pages'));
4560                    var action          = $(this).attr('data-action');
4561
4562                    if ( action == 'next' ) {
4563                        var current_page    = parseInt($(this).attr('data-current_page'));
4564
4565                        if ( (current_page + 1) <= max_pages ) {
4566                            // Check cache
4567                            var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-' + ( current_page + 1 );
4568                            var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
4569
4570                            if ( playlist_category_data[tab_data_index] != undefined ) {
4571                                element.find('.playlist-category-content').remove();
4572                                element.find('.playlist-ajax-content').append( playlist_category_data[tab_data_index] );
4573                                
4574                                // Do something
4575                                element.find('.playlist-control .playlist-control-item').attr('data-current_page', current_page + 1);
4576                                playlist_category_control[tab_control_index] = [category, max_pages, current_page + 1];
4577                                // Check last page
4578                                element.find('.playlist-control .playlist-control-item[data-action="prev"]').removeClass('disable');
4579                                if ( current_page + 1 >= max_pages ) {
4580                                    element.find('.playlist-control .playlist-control-item[data-action="next"]').addClass('disable');
4581                                }
4582
4583                                return;
4584                            }
4585                        
4586                            // Load content via ajax
4587                            element.find('.playlist-ajax-content').addClass('loading');
4588
4589                            $.ajax({
4590                                type : "POST",
4591                                timeout : 30000,
4592                                url : haru_vidi_ajax_url,
4593                                data : {
4594                                    action: 'haru_get_playlist_category_next', 
4595                                    atts: atts, 
4596                                    category: category,
4597                                    current_page: current_page
4598                                },
4599                                error: function(xhr,err) {
4600                                    console.log('Have something wrong! Please try again!');
4601                                },
4602                                success: function(response) {
4603                                    if ( response ) {
4604                                        element.find('.playlist-category-content').remove();
4605                                        element.find('.playlist-ajax-content').append( response );       
4606
4607                                        // Save cache
4608                                        playlist_category_data[tab_data_index] = response;
4609                                        playlist_category_control[tab_control_index] = [category, max_pages, current_page + 1];
4610                                    }
4611                                    // Do something
4612                                    element.find('.playlist-control .playlist-control-item').attr('data-current_page', current_page + 1);
4613                                    element.find('.playlist-ajax-content').removeClass('loading');
4614                                    // Check last page
4615                                    element.find('.playlist-control .playlist-control-item[data-action="prev"]').removeClass('disable');
4616                                    if ( current_page + 1 >= max_pages ) {
4617                                        element.find('.playlist-control .playlist-control-item[data-action="next"]').addClass('disable');
4618                                    }
4619                                }
4620                            });
4621                        } else {
4622                            return;
4623                        }
4624                    } else if ( action == 'prev' ) {
4625                        var current_page    = parseInt($(this).attr('data-current_page'));
4626
4627                        if ( current_page == 1 ) {
4628                            return;
4629                        }
4630
4631                        // Check cache
4632                        var tab_data_index = element_id + '-' + category.toString().split(',').join('-') + '-page-' + ( current_page - 1 );
4633                        var tab_control_index = element_id + '-' + category.toString().split(',').join('-');
4634
4635                        if ( playlist_category_data[tab_data_index] != undefined ) {
4636                            element.find('.playlist-category-content').remove();
4637                            element.find('.playlist-ajax-content').append( playlist_category_data[tab_data_index] );
4638                            
4639                            // Do something
4640                            element.find('.playlist-control .playlist-control-item').attr('data-current_page', current_page - 1);
4641                            playlist_category_control[tab_control_index] = [category, max_pages, current_page - 1];
4642                        }
4643
4644                        // Check first page
4645                        element.find('.playlist-control .playlist-control-item[data-action="next"]').removeClass('disable');
4646                        if ( current_page - 1 <= 1 ) {
4647                            element.find('.playlist-control .playlist-control-item[data-action="prev"]').addClass('disable');
4648                        }
4649                    }
4650                });
4651            });
4652        },
4653        shortcodePlaylistTop: function() {
4654            var playlist_top_data = []; // Use this for playlist top shortcode
4655            var playlist_top_control = [];
4656
4657            $('.playlist-top-shortcode').each(function() {
4658                var element         = $(this);
4659                var element_id      = element.attr('id');
4660                var atts            = element.data('atts');
4661
4662                // Have Video filter
4663                var playlist_order_by  = element.attr('data-playlist_order_by'); // Different if have tab
4664                var max_pages       = parseInt(element.find('.playlist-control-item').attr('data-max_pages'));
4665                var current_page    = parseInt(element.find('.playlist-control-item').attr('data-current_page'));
4666
4667                // Save first load to cache
4668                var tab_data_index = element_id + '-' + playlist_order_by.toString().split(',').join('-') + '-page-1';
4669                var tab_control_index = element_id + '-' + playlist_order_by.toString().split(',').join('-');
4670
4671                playlist_top_data[tab_data_index] = element.find('.playlist-top-content').clone(true);
4672                playlist_top_control[tab_control_index] = [playlist_order_by, max_pages, current_page];
4673
4674                // Next/Prev control
4675                element.find('.playlist-control .playlist-control-item').off().on('click', function() {
4676                    // Check if already click or loading
4677                    if ( $(this).hasClass('disable') || element.find('.playlist-ajax-content').hasClass('loading') ) {
4678                        return;
4679                    }
4680
4681                    var playlist_order_by  = $(this).attr('data-playlist_order_by');
4682                    var max_pages       = parseInt($(this).attr('data-max_pages'));
4683                    var action          = $(this).attr('data-action');
4684
4685                    if ( action == 'next' ) {
4686                        var current_page    = parseInt($(this).attr('data-current_page'));
4687
4688                        if ( (current_page + 1) <= max_pages ) {
4689                            // Check cache
4690                            var tab_data_index = element_id + '-' + playlist_order_by.toString().split(',').join('-') + '-page-' + ( current_page + 1 );
4691                            var tab_control_index = element_id + '-' + playlist_order_by.toString().split(',').join('-');
4692
4693                            if ( playlist_top_data[tab_data_index] != undefined ) {
4694                                element.find('.playlist-top-content').remove();
4695                                element.find('.playlist-ajax-content').append( playlist_top_data[tab_data_index] );
4696                                
4697                                // Do something
4698                                element.find('.playlist-control .playlist-control-item').attr('data-current_page', current_page + 1);
4699                                playlist_top_control[tab_control_index] = [playlist_order_by, max_pages, current_page + 1];
4700                                // Check last page
4701                                element.find('.playlist-control .playlist-control-item[data-action="prev"]').removeClass('disable');
4702                                if ( current_page + 1 >= max_pages ) {
4703                                    element.find('.playlist-control .playlist-control-item[data-action="next"]').addClass('disable');
4704                                }
4705
4706                                return;
4707                            }
4708                        
4709                            // Load content via ajax
4710                            element.find('.playlist-ajax-content').addClass('loading');
4711
4712                            $.ajax({
4713                                type : "POST",
4714                                timeout : 30000,
4715                                url : haru_vidi_ajax_url,
4716                                data : {
4717                                    action: 'haru_get_playlist_top_next', 
4718                                    atts: atts, 
4719                                    playlist_order_by: playlist_order_by,
4720                                    current_page: current_page
4721                                },
4722                                error: function(xhr,err) {
4723                                    console.log('Have something wrong! Please try again!');
4724                                },
4725                                success: function(response) {
4726                                    if ( response ) {
4727                                        element.find('.playlist-top-content').remove();
4728                                        element.find('.playlist-ajax-content').a
4728ppend( response );       
4729
4730                                        // Save cache
4731                                        playlist_top_data[tab_data_index] = response;
4732                                        playlist_top_control[tab_control_index] = [playlist_order_by, max_pages, current_page + 1];
4733                                    }
4734                                    // Do something
4735                                    element.find('.playlist-control .playlist-control-item').attr('data-current_page', current_page + 1);
4736                                    element.find('.playlist-ajax-content').removeClass('loading');
4737                                    // Check last page
4738                                    element.find('.playlist-control .playlist-control-item[data-action="prev"]').removeClass('disable');
4739                                    if ( current_page + 1 >= max_pages ) {
4740                                        element.find('.playlist-control .playlist-control-item[data-action="next"]').addClass('disable');
4741                                    }
4742                                }
4743                            });
4744                        } else {
4745                            return;
4746                        }
4747                    } else if ( action == 'prev' ) {
4748                        var current_page    = parseInt($(this).attr('data-current_page'));
4749
4750                        if ( current_page == 1 ) {
4751                            return;
4752                        }
4753
4754                        // Check cache
4755                        var tab_data_index = element_id + '-' + playlist_order_by.toString().split(',').join('-') + '-page-' + ( current_page - 1 );
4756                        var tab_control_index = element_id + '-' + playlist_order_by.toString().split(',').join('-');
4757
4758                        if ( playlist_top_data[tab_data_index] != undefined ) {
4759                            element.find('.playlist-top-content').remove();
4760                            element.find('.playlist-ajax-content').append( playlist_top_data[tab_data_index] );
4761                            
4762                            // Do something
4763                            element.find('.playlist-control .playlist-control-item').attr('data-current_page', current_page - 1);
4764                            playlist_top_control[tab_control_index] = [playlist_order_by, max_pages, current_page - 1];
4765                        }
4766
4767                        // Check first page
4768                        element.find('.playlist-control .playlist-control-item[data-action="next"]').removeClass('disable');
4769                        if ( current_page - 1 <= 1 ) {
4770                            element.find('.playlist-control .playlist-control-item[data-action="prev"]').addClass('disable');
4771                        }
4772                    }
4773                });
4774            });
4775        },
4776        archiveVideoSort: function() {
4777            if ( $('.order-item-current').length > 0 ) {
4778                $('.order-item-current').off().on('click', function(e) {
4779                    e.stopPropagation();
4780
4781                    $(this).toggleClass('active');
4782                    $(this).next('.order-items').toggleClass('active');
4783                });
4784
4785                $(document).on('click', function(e) {
4786                    if ( e.target.className !== 'order-item-current' ) {
4787                        $(document).find('.order-item-current').removeClass('active');
4788                        $(document).find('.order-items').removeClass('active');
4789                    }
4790                })
4791            }
4792        },
4793        layoutToggle: function() {
4794            // @TODO: Add to cookie archivelayoutcookie
4795            $('.toggle-layout').off().on('click', function() {
4796                if ( $(this).hasClass('active') ) {
4797                    return;
4798                } else {
4799                    $(this).addClass('active').siblings('.toggle-layout').removeClass('active');
4800
4801                    var active_layout = $(this).attr('data-layout');
4802
4803                    $('.layout-wrap').removeClass(function (index, css) {
4804                       return (css.match (/(^|\s)style-\S+/g) || []).join(' ');
4805                    }).addClass('style-' + active_layout);
4806
4807                    HaruVidi.video.videoLayoutIsotope();
4808                }
4809            });
4810        },
4811        shortcodeSingle: function() {
4812            $('.haru-shortcode-copy').on('click', function(){
4813              var share_id = $(this).data('id');
4814              var copyText = document.getElementsByClassName('haru-shortcode-core');
4815              
4816              copyText[0].select();
4817              copyText[0].setSelectionRange(0, 99999);
4818              document.execCommand('copy');
4819              console.log('Copied the text: ' + copyText[0].value);
4820            });
4821        },
4822        shortcodeVideoFilter: function() {
4823            var default_filter = [];
4824            var array_filter = []; // Push filter to an array to process when don't have filter
4825
4826            $('.video-list-shortcode').each(function(index, value) {
4827                // Process filter each shortcode
4828                $(this).find('.video-filter li').first().find('a').addClass('selected');
4829                default_filter[index] = $(this).find('.video-filter li').first().find('a').attr('data-option-value');
4830
4831                var self = $(this);
4832                var $layout_wrap = $(this).find('.layout-wrap'); // parent element of .item
4833                var $filter = $(this).find('.video-filter a');
4834                var masonry_options = {
4835                    'gutter': 0
4836                };
4837
4838                array_filter[index] = $filter;
4839
4840                // Add to process products layout style
4841                var layoutMode = 'fitRows';
4842                if (($(this).hasClass('masonry'))) {
4843                    var layoutMode = 'masonry';
4844                }
4845
4846                for (var i = 0; i < array_filter.length; i++) {
4847                    if (array_filter[i].length == 0) {
4848                        default_filter = '';
4849                    }
4850                    $layout_wrap.isotope({
4851                        itemSelector: 'article', // .item
4852                        transitionDuration: '0.4s',
4853                        masonry: masonry_options,
4854                        layoutMode: layoutMode,
4855                        filter: default_filter[i]
4856                    });
4857                }
4858
4859                imagesLoaded(self, function() {
4860                    setTimeout(function() {
4861                        $layout_wrap.isotope('layout');
4862                    }, 300);
4863                });
4864
4865                $(window).resize(function() {
4866                    $layout_wrap.isotope('layout');
4867                });
4868
4869                $filter.click(function(e) {
4870                    e.stopPropagation();
4871                    e.preventDefault();
4872
4873                    var $this = $(this);
4874                    // Don't proceed if already selected
4875                    if ($this.hasClass('selected')) {
4876                        return false;
4877                    }
4878                    var filters = $this.closest('ul');
4879                    filters.find('.selected').removeClass('selected');
4880                    $this.addClass('selected');
4881
4882                    var options = {
4883                            layoutMode: layoutMode,
4884                            transitionDuration: '0.4s',
4885                            packery: {
4886                                horizontal: true
4887                            },
4888                            masonry: masonry_options
4889                        },
4890                        key = filters.attr('data-option-key'),
4891                        value = $this.attr('data-option-value');
4892                        value = value === 'false' ? false : value;
4893                        options[key] = value;
4894
4895                    $layout_wrap.isotope(options);
4896                });
4897            });
4898        },
4899        videoLayoutIsotope: function() {
4900            var $layout_wrap = $('.layout-wrap');
4901
4902            if ( $layout_wrap.length > 0 ) {
4903                $layout_wrap.isotope({
4904                    itemSelector: 'article',
4905                    layoutMode: 'fitRows'
4906                });
4907                $layout_wrap.imagesLoaded( function() {
4908                    setTimeout(function() {
4909                        $layout_wrap.isotope('layout');
4910                    }, 300);  
4911                    $layout_wrap.addClass('isotope-loaded');
4912                });
4913            }
4914        },
4915        videoLoadMore: function() {
4916            $('.video-load-more').off().on('click', function() {
4917                var $this          = $(this).addClass('loading');
4918                var link           = $(this).attr('data-href');
4919                var contentWrap    = '.layout-wrap';
4920                var element        = '.layout-wrap article.grid-item';
4921
4922                $.get(link, function (data) {
4923                    var next_href = $('.video-load-more', data).attr('data-href');
4924                    var $newElems = $(element, data).css({ opacity: 0 });
4925
4926                    $(contentWrap).append($newElems);
4927
4928                    $newElems.imagesLoaded(function () {
4929                        // Re Init functions (@TODO: Make order archive video and playlist doesn't work)
4930                        HaruVidi.video.init();
4931
4932                        $newElems.animate({ opacity: 1 });
4933
4934                        $(contentWrap).isotope('appended', $newElems);
4935
4936                        setTimeout(function() {
4937                            $(contentWrap).isotope('layout');
4938                        }, 300);
4939                    });
4940
4941                    if ( typeof(next_href) == 'undefined' ) {
4942                        $this.parent().remove(); // @TODO: cause height issue
4943                    } else {
4944                        $this.removeClass('loading');
4945                        $this.attr('data-href', next_href);
4946                    }
4947                });
4948            });
4949        },
4950        videoInfiniteScroll: function() {
4951            var contentWrap = '.layout-wrap';
4952            var maxPag = $('#infinite_scroll_button').attr('data-max-page');
4953            var msgText = $('#infinite_scroll_button').attr('data-msgText');
4954            var finishedMsg = $('#infinite_scroll_button').attr('data-finishedMsg');
4955
4956            if ( $(contentWrap).length ) {
4957                $(contentWrap).infinitescroll({
4958                    navSelector: '#infinite_scroll_button',
4959                    nextSelector: '#infinite_scroll_button a',
4960                    itemSelector: '.layout-wrap article.grid-item',
4961                    loading: {
4962                        'selector': '#infinite_scroll_loading',
4963                        'img': haru_vidi_plugin_url + '/assets/images/ajax-loader.gif',
4964                        'msgText': msgText,
4965                        'finishedMsg': finishedMsg
4966                    },
4967                    maxPage: maxPag
4968                }, function (newElements, data, url) {
4969                    var $newElems = $(newElements).css({ opacity: 0 });
4970
4971                    $newElems.imagesLoaded(function () {
4972                        // Re Init functions
4973                        HaruVidi.video.init();
4974
4975                        $newElems.animate({ opacity: 1 });
4976
4977                        $(contentWrap).isotope('appended', $newElems);
4978
4979                        setTimeout(function() {
4980                            $(contentWrap).isotope('layout');
4981                        }, 300);
4982                    });
4983                });
4984            }
4985        },
4986        watchLater: function() {
4987            var watch_later_cookie = 'haruwatchlatervideos';
4988
4989            $('.video-watch-later').off().on('click', function(e) {
4990                e.preventDefault();
4991
4992                var $later = $(this);
4993
4994                if ( $later.hasClass('active') ) {
4995                    HaruVidi.video.processWatchLaterDeleteVideo($later, watch_later_cookie);
4996                } else {
4997                    HaruVidi.video.processWatchLaterAddVideo($later, watch_later_cookie);
4998                }
4999            });
5000
5001            $('.video-watch-later-delete').off().on('click', function() {
5002                var $later = $(this);
5003                    
5004                HaruVidi.video.processWatchLaterDeleteVideo($later, watch_later_cookie);
5005            });
5006        },
5007        processWatchLaterAddVideo: function( $later, watch_later_cookie ) {
5008            var id = Number($later.attr('data-id'));
5009
5010            // https://github.com/js-cookie/js-cookie
5011            if ( typeof(Cookies.get(watch_later_cookie)) !== 'undefined' ) {
5012                var watch_later_video_ids = Cookies.get(watch_later_cookie);
5013                var new_watch_later_video_ids = [];
5014                
5015                watch_later_video_ids = JSON.parse("[" + watch_later_video_ids + "]");
5016                
5017                var i;
5018                for ( i = 0; i < watch_later_video_ids.length; i++ ) {
5019                    new_watch_later_video_ids[i] = watch_later_video_ids[i];
5020                }
5021
5022                if ( new_watch_later_video_ids.indexOf(id) === -1 ) {
5023                    new_watch_later_video_ids.push( id );
5024                    var new_watch_later_cookie = new_watch_later_video_ids.toString();
5025
5026                    Cookies.set(
5027                        watch_later_cookie, 
5028                        new_watch_later_cookie, 
5029                        {
5030                            expires: 365
5031                        }
5032                    );
5033
5034                    console.log( new_watch_later_video_ids );
5035                }
5036
5037                HaruVidi.video.processWatchLaterAddVideoToList( $later, watch_later_cookie );
5038            } else {
5039                Cookies.set(
5040                    watch_later_cookie,
5041                    id,
5042                    {
5043                        expires: 365
5044                    }
5045                );
5046
5047                HaruVidi.video.processWatchLaterAddVideoToList( $later, watch_later_cookie );
5048            }
5049
5050            HaruVidi.video.watchLater(); // To make delete video work
5051
5052            $(document).find('.watch-later-status').addClass('has-videos');
5053
5054            $later.addClass('active');
5055        },
5056        processWatchLaterAddVideoToList: function( $later, watch_later_cookie ) {
5057            var id = Number($later.attr('data-id'));
5058
5059            var title = $later.attr('data-title'),
5060                permalink = $later.attr('data-permalink'),
5061                thumb = $later.attr('data-thumb');
5062
5063            var watch_later_item = '<div class="video-watch-later-item video-item video-' + id + '" id="video-watch-later-' + id + '">' +
5064                                        '<div class="video-content">' +
5065                                            '<div class="video-thumb">' +
5066                                                '<img class="video-img" src="' + thumb + '" alt="' + title + '">' +
5067                                            '</div>' +
5068                                            '<h6 class="video-title">' +
5069                                                '<a href="' + permalink + '" title="' + title + '">' + title + '</a>' +
5070                                            '</h6>' +
5071                                        '</div>' +
5072                                        '<div class="video-watch-later-delete" data-id="' + id + '"><i class="haru-icon haru-times"></i></div>' +
5073                                    '</div>';
5074
5075            $(document).find('.haru-watch-later-videos').prepend(watch_later_item);
5076            $(document).find('.haru-watch-later-videos').removeClass('empty-video');
5077        },
5078        processWatchLaterDeleteVideo: function( $later, watch_later_cookie ) {
5079            var id = Number($later.attr('data-id'));
5080
5081            if ( typeof( Cookies.get(watch_later_cookie) ) !== 'undefined' ) {
5082                var watch_later_video_ids = Cookies.get(watch_later_cookie);
5083                var new_watch_later_video_ids = [];
5084                
5085                watch_later_video_ids = JSON.parse("[" + watch_later_video_ids + "]");
5086                
5087                var i;
5088                for ( i = 0; i < watch_later_video_ids.length; i++ ) {
5089                    new_watch_later_video_ids[i] = watch_later_video_ids[i];
5090                }
5091
5092                if ( new_watch_later_video_ids.indexOf(id) !== -1 ) {
5093                    new_watch_later_video_ids.pop( id );
5094                    var new_watch_later_cookie = new_watch_later_video_ids.toString();
5095
5096                    Cookies.set(
5097                        watch_later_cookie, 
5098                        new_watch_later_cookie, 
5099                        {
5100                            expires: 365
5101                        }
5102                    );
5103
5104                    console.log( new_watch_later_video_ids );
5105
5106                    if ( new_watch_later_video_ids === 'undefined' || new_watch_later_video_ids.length == 0 ) {
5107                        $(document).find('.watch-later-status').removeClass('has-videos');
5108                    }
5109                }
5110
5111                if ( $later.hasClass('active') ) {
5112                    $later.removeClass('active');
5113                }
5114
5115                $(document).find('.video-watch-later[data-id="' + id + '"]').removeClass('active');                   
5116                
5117                // Process in archive watch later page
5118                var $watch_later_archive_parent = $later.parents('.haru-archive-watch-later');
5119                if ( $watch_later_archive_parent.length > 0 ) {
5120                    $('.video-' + id, $watch_later_archive_parent).hide(50, function(){ 
5121                        $(this).remove();
5122
5123                        // Process layout
5124                        var contentWrap    = '.layout-wrap';
5125                            $(contentWrap).isotope('layout');
5126
5127                        if ( $watch_later_archive_parent.find('.video-item').length === 0 ) {
5128                            $watch_later_archive_parent.find('.haru-watch-later-videos').addClass('empty-video');
5129                            $watch_later_archive_parent.find('.haru-watch-later-videos').empty();
5130
5131                            $(document).find('.watch-later-status').removeClass('has-videos');
5132                        }
5133                    });
5134                }
5135                
5136                // Process in watch later element
5137                var $watch_later_element_parent = $(document).find('.haru-watch-later-videos');
5138                $('.video-' + id, $watch_later_element_parent).hide(50, function(){ 
5139                    $(this).remove();
5140
5141                    // Process layout
5142                    var contentWrap    = '.layout-wrap';
5143                        $(contentWrap).isotope('layout');
5144
5145                    if ( $watch_later_element_parent.find('.video-item').length === 0 ) {
5146                        $watch_later_element_parent.addClass('empty-video');
5147
5148                        $(document).find('.watch-later-status').removeClass('has-videos');
5149                    }
5150                });
5151            }
5152        }
5153    }
5154
5155    $(document).ready(function() {
5156        HaruVidi.init();
5157    });
5158})(jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.