autoplay.js 3.8KB

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