shaz.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. $(document).ready(function() {
  2. var shaz = new Shazntr();
  3. shaz.load();
  4. });
  5. function Shazntr()
  6. {
  7. var clipCount = null;
  8. var clipsReady = null;
  9. var isPlayingBack = false;
  10. this.load = function()
  11. {
  12. this.clipsReady = 0;
  13. this.clipCount = $("#clip_count").val();
  14. // Tell all audio elements to report when they are ready
  15. var me = this;
  16. $("audio").bind("canplaythrough", function() {
  17. me.clipReady();
  18. });
  19. this.setupUI();
  20. };
  21. this.setupUI = function()
  22. {
  23. $("#create_button").on("click", this.create_button_onClick);
  24. $("#cancel_button").on("click", this.cancel_create_button_onClick);
  25. $("#play_button").on("click", this.play_button_onClick);
  26. $("#random_button").on("click", this.random_button_onClick);
  27. $("#home_button").on("click", this.home_button_onClick);
  28. $("#replay_button").on("click", {me: this}, this.replay_button_onClick);
  29. $("#help_button").on("click", this.help_button_onClick);
  30. $("#info_dialog_close_button").on("click", this.info_dialog_close_button_onClick);
  31. $(".info-nav li a").on("click", {me: this}, this.info_nav_onClick);
  32. $("#create_text_input").on("keydown", {me: this}, this.create_text_input_onKeyDown);
  33. this.createTips();
  34. };
  35. /**
  36. * Create the tool tips for the upper right links.
  37. */
  38. this.createTips = function()
  39. {
  40. var sharedTip = {
  41. position: {
  42. my: 'top middle',
  43. at: 'bottom middle'
  44. },
  45. style: {
  46. tip: true,
  47. classes: 'ui-tooltip-dark ui-tooltip-rounded'
  48. }
  49. };
  50. $("#tribes_button").qtip($.extend({}, sharedTip, {
  51. content: "Tribes."
  52. }));
  53. $("#twitter_button").qtip($.extend({}, sharedTip, {
  54. content: "Contact."
  55. }));
  56. $("#octocat_button").qtip($.extend({}, sharedTip, {
  57. content: "Github."
  58. }));
  59. };
  60. /**
  61. * Called for each audio element when it is ready for playback.
  62. */
  63. this.clipReady = function()
  64. {
  65. if (this.clipsReady == this.clipCount)
  66. return;
  67. this.clipsReady += 1;
  68. // Start playing audio after all clips are ready
  69. if (this.clipsReady == this.clipCount) {
  70. var me = this;
  71. setTimeout(function() {
  72. $("div.loading-modal-wrapper").hide();
  73. me.playClip(0, false);
  74. me.isPlayingBack = true;
  75. }, 500);
  76. }
  77. };
  78. /**
  79. * Play a clip and set the timeout for the next clip to be played.
  80. */
  81. this.playClip = function(number, isReplay)
  82. {
  83. if (number == this.clipCount) {
  84. // Show the action buttons
  85. if (!isReplay)
  86. $("#main_buttons").fadeIn();
  87. this.isPlayingBack = false;
  88. return;
  89. }
  90. // Fade / Highlight this clip in before playing it...
  91. $("#clip_wrapper_" + number).fadeIn();
  92. var clip = $("#clip_" + number).get(0);
  93. var duration = $("source", clip).attr("duration");
  94. clip.play();
  95. var me = this;
  96. setTimeout(function() {
  97. me.playClip(number + 1, isReplay);
  98. }, (duration * .85));
  99. };
  100. /**
  101. * Handle the create_button click event.
  102. * -hide #home_buttons and show #create_interface
  103. */
  104. this.create_button_onClick = function()
  105. {
  106. $("#create_text_input").val("");
  107. $("#home_buttons").animate({left: -960}, 300, "linear");
  108. $("#create_interface").animate({left: 960}, 300, "linear", function() {
  109. $("#create_text_input").focus();
  110. });
  111. };
  112. /**
  113. * Handle the cancel_create_button click event.
  114. * - hide #create_interface and show #home_buttons
  115. */
  116. this.cancel_create_button_onClick = function()
  117. {
  118. $("#create_text_input").val("");
  119. $("#create_interface").animate({left: 1920}, 300, "linear");
  120. $("#home_buttons").animate({left: 960}, 300, "linear");
  121. };
  122. /**
  123. * Play a new shazinator-thingy.
  124. */
  125. this.play_button_onClick = function()
  126. {
  127. var phrase = $("#create_text_input").val();
  128. if (phrase != null && phrase.length > 2)
  129. window.location = "/" + phrase;
  130. };
  131. /**
  132. * Play a new shazinator-thingy - via Enter Key Down
  133. */
  134. this.create_text_input_onKeyDown = function(event)
  135. {
  136. if (event.keyCode == 13)
  137. event.data.me.play_button_onClick();
  138. };
  139. /**
  140. * Replay the previous shazinator-thingy.
  141. */
  142. this.replay_button_onClick = function(event)
  143. {
  144. if (event.data.me.isPlayingBack != true) {
  145. $(".clip-wrapper").fadeOut(function() {
  146. event.data.me.playClip(0, true);
  147. event.data.me.isPlayingBack = true;
  148. });
  149. }
  150. };
  151. /**
  152. * Request a random shazinator-thingy.
  153. */
  154. this.random_button_onClick = function()
  155. {
  156. window.location = "/random/";
  157. };
  158. /**
  159. * Go Home...
  160. */
  161. this.home_button_onClick = function()
  162. {
  163. window.location = "/";
  164. };
  165. /**
  166. * Show the Help / Info Modal Dialog
  167. */
  168. this.help_button_onClick = function()
  169. {
  170. $("#info_modal_wrapper").fadeIn(400, function() {
  171. $("#info_dialog").fadeIn("slow");
  172. });
  173. };
  174. /**
  175. * Close the Help / Info Modal Dialog
  176. */
  177. this.info_dialog_close_button_onClick = function()
  178. {
  179. $("#info_dialog").fadeOut("slow", function() {
  180. $("#info_modal_wrapper").fadeOut();
  181. });
  182. };
  183. /**
  184. * Info Dialog Navigation Logic
  185. */
  186. this.info_nav_onClick = function(event)
  187. {
  188. var e = event.target.id; // what was clicked
  189. var s = $("ul.info-nav a.selected").get(0); // what is selected;
  190. var sid = $(s).attr("id");
  191. if (sid == e)
  192. return;
  193. $(s).removeClass("selected");
  194. $(s).next("span").removeClass("selected");
  195. $("#" + e).addClass("selected");
  196. $("#" + e).next("span").addClass("selected");
  197. // Fade In callback
  198. var fadeInCallback = function() {
  199. if (e == "info_nav_about")
  200. $("#info_about").fadeIn(400);
  201. else if (e == "info_nav_usage")
  202. $("#info_usage").fadeIn(400);
  203. else
  204. $("#info_commands").fadeIn(400);
  205. };
  206. // Fade out what is currently selected
  207. if (sid == "info_nav_about")
  208. $("#info_about").fadeOut(400, fadeInCallback);
  209. else if (sid == "info_nav_usage")
  210. $("#info_usage").fadeOut(400, fadeInCallback);
  211. else
  212. $("#info_commands").fadeOut(400, fadeInCallback);
  213. };
  214. };