autoplay.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. function Autoplay()
  2. {
  3. var _playlist = new Array();
  4. var _player = null;
  5. var _current_index = 0;
  6. var _link = null;
  7. var _full_screen = false;
  8. this.start = function(link)
  9. {
  10. _link = link;
  11. open_popin_dialog('autoplay');
  12. initializePlaylist(this.play);
  13. }
  14. var initializePlaylist = function(callback)
  15. {
  16. JQueryJson(_link.attr('href'), {}, function(response){
  17. if (response.status == 'success')
  18. {
  19. if (response.data.length)
  20. {
  21. _playlist = response.data;
  22. }
  23. }
  24. callback(0);
  25. });
  26. }
  27. this.play = function(index_to_play, timed)
  28. {
  29. window.autoplay.stopAndClearAllPlayers();
  30. if (array_key_exists(index_to_play, _playlist))
  31. {
  32. if (!timed)
  33. {
  34. _current_index = index_to_play;
  35. $('#autoplay_element_loader').show();
  36. window.setTimeout(function(){
  37. window.autoplay.play(index_to_play, true);
  38. });
  39. }
  40. else if (_current_index == index_to_play)
  41. {
  42. loadAndDisplayInfos(_playlist[index_to_play].element_id);
  43. if (!createPlayer(_playlist[index_to_play], window.autoplay.playNext))
  44. {
  45. this.play(index_to_play+1);
  46. }
  47. else
  48. {
  49. _current_index = index_to_play;
  50. }
  51. }
  52. }
  53. else
  54. {
  55. window.autoplay.nothingToPlay();
  56. }
  57. }
  58. this.stopAndClearAllPlayers = function()
  59. {
  60. players = window.players_manager.getAll();
  61. for (var i in players)
  62. {
  63. if (i in players)
  64. {
  65. players[i].stopAndDestroy();
  66. window.players_manager.remove(i);
  67. }
  68. }
  69. }
  70. var loadAndDisplayInfos = function(element_id)
  71. {
  72. $('#autoplay_element_loader').show();
  73. JQueryJson(url_element_dom_get_one_autoplay+'/'+element_id, {}, function(response){
  74. $('#autoplay_element_loader').hide();
  75. if (response.status == 'success')
  76. {
  77. $('li#autoplay_element_container').html(response.data);
  78. refresh_social_buttons(true);
  79. }
  80. else
  81. {
  82. $('li#autoplay_element_container').html('');
  83. }
  84. });
  85. }
  86. var createPlayer = function(play_data, finish_callback)
  87. {
  88. $('#autoplay_loader').show();
  89. if ((_player = window.dynamic_player.play(
  90. $('#autoplay_player'),
  91. play_data.element_type,
  92. play_data.element_ref_id,
  93. play_data.element_id,
  94. true,
  95. finish_callback,
  96. function(player) {
  97. if (!_player || !_full_screen) {
  98. return;
  99. }
  100. player.enableFullScreen();
  101. }
  102. )))
  103. {
  104. $('#autoplay_loader').hide();
  105. window.players_manager.add(_player, 'autoplay_'+play_data.element_id);
  106. return true;
  107. }
  108. else
  109. {
  110. return false;
  111. }
  112. }
  113. this.playNext = function()
  114. {
  115. window.autoplay.play(_current_index+1);
  116. }
  117. this.playPrevious = function()
  118. {
  119. if (array_key_exists(_current_index-1, _playlist))
  120. {
  121. window.autoplay.play(_current_index-1);
  122. }
  123. return false;
  124. }
  125. this.nothingToPlay = function()
  126. {
  127. this.stopAndClearAllPlayers();
  128. $('#autoplay_noelements_text').show();
  129. $('div#autoplay_player_container').html('<div id="autoplay_player"></div>');
  130. $('li#autoplay_element_container').html('');
  131. $('#autoplay iframe').hide();
  132. $('#autoplay img[alt="loader"]').hide();
  133. }
  134. this.setFullScreen = function(status) {
  135. _full_screen = status;
  136. if (status) {
  137. _player.enableFullScreen();
  138. }
  139. }
  140. }
  141. $(document).ready(function() {
  142. window.autoplay = new Autoplay();
  143. $('a.autoplay_link').live('click', function(){
  144. window.autoplay.start($(this));
  145. $('html, body').animate({ scrollTop: 0 }, 'fast');
  146. return false;
  147. });
  148. $('a.autoplay_playlist, a.autoplay_favshow').live('click', function(){
  149. window.autoplay.start($(this));
  150. $('html, body').animate({ scrollTop: 0 }, 'fast');
  151. return false;
  152. });
  153. $('a#autoplay_previous').click(function(){window.autoplay.playPrevious()});
  154. $('a#autoplay_next').click(function(){window.autoplay.playNext()});
  155. $('a#autoplay_close').click(function(){
  156. // Fond gris
  157. $('#fade').fadeOut(1000, function(){$('#fade').remove();});
  158. // On cache le lecteur
  159. $('#autoplay').hide();
  160. window.autoplay.stopAndClearAllPlayers();
  161. });
  162. $('#full_screen_toggle input[name="full_screen_toggle"]').on('change', function(){
  163. window.autoplay.setFullScreen($(this).is(':checked'));
  164. });
  165. });