autoplay.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. players[i].stopAndDestroy();
  63. window.players_manager.remove(i);
  64. }
  65. }
  66. var loadAndDisplayInfos = function(element_id)
  67. {
  68. $('#autoplay_element_loader').show();
  69. JQueryJson(url_element_dom_get_one_autoplay+'/'+element_id, {}, function(response){
  70. $('#autoplay_element_loader').hide();
  71. if (response.status == 'success')
  72. {
  73. $('li#autoplay_element_container').html(response.data);
  74. }
  75. else
  76. {
  77. $('li#autoplay_element_container').html('');
  78. }
  79. });
  80. }
  81. var createPlayer = function(play_data, finish_callback)
  82. {
  83. $('#autoplay_loader').show();
  84. if ((player = window.dynamic_player.play(
  85. $('#autoplay_player'),
  86. play_data.element_type,
  87. play_data.element_ref_id,
  88. play_data.element_id,
  89. true,
  90. finish_callback
  91. )))
  92. {
  93. $('#autoplay_loader').hide();
  94. window.players_manager.add(player, 'autoplay_'+play_data.element_id);
  95. return true;
  96. }
  97. else
  98. {
  99. return false;
  100. }
  101. }
  102. this.playNext = function()
  103. {
  104. window.autoplay.play(_current_index+1);
  105. }
  106. this.playPrevious = function()
  107. {
  108. if (array_key_exists(_current_index-1, _playlist))
  109. {
  110. window.autoplay.play(_current_index-1);
  111. }
  112. return false;
  113. }
  114. this.nothingToPlay = function()
  115. {
  116. this.stopAndClearAllPlayers();
  117. $('#autoplay_noelements_text').show();
  118. $('div#autoplay_player_container').html('<div id="autoplay_player"></div>');
  119. $('li#autoplay_element_container').html('');
  120. $('#autoplay iframe').hide();
  121. $('#autoplay img[alt="loader"]').hide();
  122. }
  123. }
  124. $(document).ready(function() {
  125. window.autoplay = new Autoplay();
  126. $('a.autoplay_link').live('click', function(){
  127. window.autoplay.start($(this));
  128. $('html, body').animate({ scrollTop: 0 }, 'fast');
  129. return false;
  130. });
  131. $('a.autoplay_playlist').live('click', function(){
  132. window.autoplay.start($(this));
  133. $('html, body').animate({ scrollTop: 0 }, 'fast');
  134. return false;
  135. });
  136. $('a#autoplay_previous').click(function(){window.autoplay.playPrevious()});
  137. $('a#autoplay_next').click(function(){window.autoplay.playNext()});
  138. $('a#autoplay_close').click(function(){
  139. // Fond gris
  140. $('#fade').fadeOut(1000, function(){$('#fade').remove();});
  141. // On cache le lecteur
  142. $('#autoplay').hide();
  143. window.autoplay.stopAndClearAllPlayers();
  144. });
  145. });