autoplay.js 3.9KB

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