summaryrefslogtreecommitdiff
path: root/contrib/site/js/player.js
blob: 2ecf9f7d2df20ba7f46d393721097ffd59980e07 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict';

function Player() {
  this.init = function(video) {
    if (video) {
      this.$media = $('#video');
    } else {
      this.$media = $('#audio');
    }
    this.media = this.$media.get()[0];
  };

  this.play = function() {
    $('#video-overlay').hide();
    $('#player-state').css('background-position', '-25px -125px');
    this.media.load();
    this.media.play();
  };

  this.stop = function() {
    $('#video-overlay').show();
    $('#player-state').css('background-position', '0px -125px');
    this.media.pause();
  };

  this.playstop = function() {
    if(this.media.paused == true) {
      this.play();
    } else {
      this.stop();
    }
  };

  this.repaintVolumeControls = function() {
    if(this.media.muted) {
      $('#player-mute').css('background-position', '0px -150px');
      return;
    }

    if(this.media.volume <= 0) {
      $('#player-mute').css('background-position', '-25px -150px');
      return;
    }

    if(this.media.volume < 0.5) {
      $('#player-mute').css('background-position', '-50px -150px');
      return;
    }

    $('#player-mute').css('background-position', '-75px -150px');
  };

  this.updatevolume = function() {
    this.media.volume = $('#player-volume').val() / 100;
    this.repaintVolumeControls();
  };

  this.togglemute = function() {
    this.media.muted = !this.audio.muted;
    $('#player-volume').prop('disabled', this.media.muted);
    this.repaintVolumeControls();
  };

  this.fullscreen = function() {
    if (this.media.requestFullscreen) {
      this.media.requestFullscreen();
    } else if (this.media.msRequestFullscreen) {
      this.media.msRequestFullscreen();
    } else if (this.media.mozRequestFullScreen) {
      this.media.mozRequestFullScreen();
    } else if (this.media.webkitRequestFullscreen) {
      this.media.webkitRequestFullscreen();
    }
  };
}

var player = new Player();

function player_init(video) {
  player.init(video);
  $('#video-overlay').on('click', player_playstop);
  $('#player-state').on('click', player_playstop);
  player.playstop();
  if(navigator.userAgent.match(/(\(iPod|\(iPhone|\(iPad)/)) {
    $('#player-volume').prop('disabled', true);
    this.repaintVolumeControls();
  } else {
    $('#player-volume').on('change input', player_updatevolume);
    $('#player-mute').on('click', player_togglemute);
  }
  $('#fullscreen').on('click', function(event) {
    event.preventDefault();
    player.fullscreen();
  });

  $(document).on('keypress', function(e) {
    if(e.which == 32) {
      player.playstop();
    }
  });
}

function player_playstop() {
  player.playstop();
}

function player_updatevolume() {
  player.updatevolume();
}

function player_togglemute() {
  player.togglemute();
}