2cc8a3355def28c2d43c6ab69bc32034b5083794
[snf-cloudcms] / cloudcms / static / cloudcms / js / youtube.js
1 function  YouTube(options) {
2   this.options = options;
3   this.wrapper = options.wrapper;
4   this.link = this.wrapper.find('a');
5   this.image = this.wrapper.find('img');
6   this.videoEl = this.wrapper.find('.player-placeholder');
7   this.currwidth = this.wrapper.width();
8   this.currheight = parseInt(458* (this.currwidth/816));
9   var self = this;
10   
11   this.player =  new YT.Player(this.videoEl[0], {
12     height: self.currheight,
13     width: self.currwidth,
14     playerVars: { 'modestbranding':1},
15     videoId: options.youtube_id,
16     suggestedQuality: 'hd1080',
17     events: {
18       'onStateChange': function(event) {
19         if ( event.data == YT.PlayerState.ENDED ) {
20           self.endEvents();
21         }
22       }
23     },
24   })
25   console.log(this.player);
26  // this.player.setPlaybackQuality('hd1080');
27
28   this.initEvents();
29
30 }
31
32
33 YouTube.prototype.initEvents = function() {
34   var self = this;
35   this.link.click(function(e){
36       e.preventDefault();
37       self.image.fadeOut({duration:2500,easing:'easeOutCirc'}); 
38       $(self.player.a).hide().fadeIn({duration:3500,easing:'easeOutCirc'}); 
39       self.wrapper.animate({
40         height: self.currheight
41       },1000, function(){
42         self.link.hide();
43   self.player.setPlaybackQuality('hd1080');
44         self.player.playVideo(); 
45       })
46   });
47   $('.stop-youtube').click(function(e){
48     e.preventDefault();
49     self.endEvents();
50   })
51 }
52
53 YouTube.prototype.endEvents = function() {
54   var self = this;
55
56   self.image.fadeIn({duration:1500,easing:'easeOutCirc'}); 
57     $(self.player.a).show().fadeIn({duration:1500,easing:'easeOutCirc'}); 
58     self.wrapper.animate({
59       height:'282'
60     },1000, function(){
61       self.link.show();
62   })
63 }